<s> import os <EOL> from itertools import chain <EOL> import shutil <EOL> import commands <EOL> from robotide . controller . dataloader import ExcludedDirectory , TestData <EOL> from robotide . publish import ( RideDataFileRemoved , RideInitFileRemoved , <EOL> RideDataChangedToDirty , RideDataDirtyCleared , RideSuiteAdded , <EOL> RideItemSettingsChanged ) <EOL> from robotide . publish . messages import RideDataFileSet , RideOpenResource <EOL> from robotide . robotapi import TestDataDirectory , TestCaseFile , ResourceFile <EOL> from robotide import utils <EOL> from . basecontroller import WithUndoRedoStacks , _BaseController , WithNamespace , ControllerWithParent <EOL> from . macrocontrollers import UserKeywordController <EOL> from . robotdata import NewTestCaseFile , NewTestDataDirectory <EOL> from robotide . utils import overrides <EOL> from . settingcontrollers import ( DocumentationController , FixtureController , <EOL> TimeoutController , TemplateController , DefaultTagsController , <EOL> ForceTagsController ) <EOL> from . tablecontrollers import ( VariableTableController , TestCaseTableController , <EOL> KeywordTableController , ImportSettingsController , <EOL> MetadataListController , TestCaseController ) <EOL> def _get_controller ( project , data , parent ) : <EOL> if isinstance ( data , TestCaseFile ) : <EOL> return TestCaseFileController ( data , project , parent ) <EOL> if isinstance ( data , ExcludedDirectory ) : <EOL> return ExcludedDirectoryController ( data , project , parent ) <EOL> return TestDataDirectoryController ( data , project , parent ) <EOL> def DataController ( data , project , parent = None ) : <EOL> return _get_controller ( project , data , parent ) <EOL> class _FileSystemElement ( object ) : <EOL> def __init__ ( self , filename , directory ) : <EOL> self . filename = filename <EOL> self . directory = directory <EOL> self . _stat = self . _get_stat ( filename ) <EOL> def _get_stat ( self , path ) : <EOL> if path and os . path . isfile ( path ) : <EOL> stat = os . stat ( path ) <EOL> return stat . st_mtime , stat . st_size <EOL> return <NUM_LIT:0> , <NUM_LIT:0> <EOL> def refresh_stat ( self ) : <EOL> self . _stat = self . _get_stat ( self . filename ) <EOL> def has_been_modified_on_disk ( self ) : <EOL> return self . _get_stat ( self . filename ) != self . _stat <EOL> def has_been_removed_from_disk ( self ) : <EOL> return self . _stat != ( <NUM_LIT:0> , <NUM_LIT:0> ) and not self . exists ( ) <EOL> def relative_path_to ( self , other ) : <EOL> other_path = os . path . join ( other . directory , other . filename ) <EOL> return os . path . relpath ( other_path , start = self . directory ) . replace ( '<STR_LIT:\\>' , '<STR_LIT:/>' ) <EOL> def is_readonly ( self ) : <EOL> return not os . access ( self . filename , os . W_OK ) <EOL> def exists ( self ) : <EOL> return self . filename and os . path . isfile ( self . filename ) <EOL> @ property <EOL> def basename ( self ) : <EOL> return os . path . splitext ( os . path . basename ( self . filename ) ) [ <NUM_LIT:0> ] <EOL> @ property <EOL> def source ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . filename or self . directory <EOL> class _DataController ( _BaseController , WithUndoRedoStacks , WithNamespace ) : <EOL> def __init__ ( self , data , project = None , parent = None ) : <EOL> self . _project = project <EOL> if project : <EOL> self . _set_namespace_from ( project ) <EOL> self . _resource_file_controller_factory = project . resource_file_controller_factory <EOL> else : <EOL> self . _resource_file_controller_factory = None <EOL> self . parent = parent <EOL> self . set_datafile ( data ) <EOL> self . dirty = False <EOL> self . children = self . _children ( data ) <EOL> def set_datafile ( self , datafile ) : <EOL> self . data = datafile <EOL> self . _variables_table_controller = None <EOL> self . _testcase_table_controller = None <EOL> self . _keywords_table_controller = None <EOL> self . _imports = None <EOL> RideDataFileSet ( item = self ) . publish ( ) <EOL> def _children ( self , data ) : <EOL> return [ ] <EOL> @ property <EOL> def name ( self ) : <EOL> return self . data . name <EOL> @ property <EOL> def settings ( self ) : <EOL> return self . _settings ( ) <EOL> def _settings ( self ) : <EOL> ss = self . data . setting_table <EOL> return [ DocumentationController ( self , ss . doc ) , <EOL> FixtureController ( self , ss . suite_setup ) , <EOL> FixtureController ( self , ss . suite_teardown ) , <EOL> FixtureController ( self , ss . test_setup ) , <EOL> FixtureController ( self , ss . test_teardown ) , <EOL> self . force_tags ] <EOL> @ property <EOL> def _setting_table ( self ) : <EOL> return self . data . setting_table <EOL> @ property <EOL> def force_tags ( self ) : <EOL> return ForceTagsController ( self , self . _setting_table . force_tags ) <EOL> @ property <EOL> def variables ( self ) : <EOL> if self . _variables_table_controller is None : <EOL> self . _variables_table_controller = VariableTableController ( self , self . data . variable_table ) <EOL> return self . _variables_table_controller <EOL> @ property <EOL> def tests ( self ) : <EOL> if self . _testcase_table_controller is None : <EOL> self . _testcase_table_controller = TestCaseTableController ( self , self . data . testcase_table ) <EOL> return self . _testcase_table_controller <EOL> @ property <EOL> def datafile ( self ) : <EOL> return self . data <EOL> @ property <EOL> def datafiles ( self ) : <EOL> return chain ( [ self ] , ( df for df in self . _project . datafiles <EOL> if df != self ) ) <EOL> @ property <EOL> def datafile_controller ( self ) : <EOL> return self <EOL> @ property <EOL> def keywords ( self ) : <EOL> if self . _keywords_table_controller is None : <EOL> self . _keywords_table_controller = KeywordTableController ( self , self . data . keyword_table ) <EOL> return self . _keywords_table_controller <EOL> @ property <EOL> def imports ( self ) : <EOL> if not self . _imports : <EOL> self . _imports = ImportSettingsController ( self , self . data . setting_table , <EOL> self . _resource_file_controller_factory ) <EOL> return self . _imports <EOL> @ property <EOL> def metadata ( self ) : <EOL> return MetadataListController ( self , self . data . setting_table ) <EOL> def is_user_keyword ( self , value ) : <EOL> return WithNamespace . is_user_keyword ( self , self . datafile , value ) <EOL> def is_library_keyword ( self , value ) : <EOL> return WithNamespace . is_library_keyword ( self , self . datafile , value ) <EOL> def keyword_info ( self , keyword_name ) : <EOL> return WithNamespace . keyword_info ( self , self . data , keyword_name ) <EOL> def mark_dirty ( self ) : <EOL> if not self . dirty : <EOL> self . dirty = True <EOL> RideDataChangedToDirty ( datafile = self ) . publish ( ) <EOL> def unmark_dirty ( self ) : <EOL> self . refresh_stat ( ) <EOL> if self . dirty : <EOL> self . dirty = False <EOL> RideDataDirtyCleared ( datafile = self ) . publish ( ) <EOL> def create_keyword ( self , name , argstr = '<STR_LIT>' ) : <EOL> return self . keywords . new ( name , argstr ) <EOL> def add_test_or_keyword ( self , item ) : <EOL> if isinstance ( item , TestCaseController ) : <EOL> self . tests . add ( item ) <EOL> item . set_parent ( self . tests ) <EOL> elif isinstance ( item , UserKeywordController ) : <EOL> self . keywords . add ( item ) <EOL> item . set_parent ( self . keywords ) <EOL> else : <EOL> self . variables . add_variable ( item . name , item . value , item . comment ) <EOL> def sort_keywords ( self ) : <EOL> if self . keywords : <EOL> index_difference = self . keywords . sort ( ) <EOL> self . mark_dirty ( ) <EOL> RideDataFileSet ( item = self ) . publish ( ) <EOL> return index_difference <EOL> return None <EOL> def restore_keyword_order ( self , index_difference ) : <EOL> if self . keywords and index_difference : <EOL> self . keywords . restore_keyword_order ( index_difference ) <EOL> self . mark_dirty ( ) <EOL> RideDataFileSet ( item = self ) . publish ( ) <EOL> def get_keyword_names ( self ) : <EOL> if self . keywords : <EOL> return [ kw . name for kw in self . keywords . _items ] <EOL> return None <EOL> def has_format ( self ) : <EOL> return True <EOL> def get_format ( self ) : <EOL> if not self . filename : <EOL> return None <EOL> return os . path . splitext ( self . filename ) [ <NUM_LIT:1> ] . replace ( '<STR_LIT:.>' , '<STR_LIT>' ) <EOL> def set_format ( self , format ) : <EOL> self . data . source = utils . replace_extension ( self . filename , format ) <EOL> self . filename = self . data . source <EOL> def is_same_format ( self , format ) : <EOL> if format and self . has_format ( ) : <EOL> return format . lower ( ) == self . get_format ( ) . lower ( ) <EOL> return False <EOL> def set_basename ( self , basename ) : <EOL> old_file = self . filename <EOL> self . data . source = os . path . join ( self . directory , '<STR_LIT>' % ( basename , self . get_format ( ) ) ) <EOL> self . filename = self . data . source <EOL> self . execute ( commands . SaveFile ( ) ) <EOL> if old_file != self . filename : <EOL> self . remove_from_filesystem ( old_file ) <EOL> def remove_from_filesystem ( self , path = None ) : <EOL> path = path or self . filename <EOL> if os . path . exists ( path ) : <EOL> os . remove ( path ) <EOL> def remove_folder_from_filesystem ( self , path = None ) : <EOL> shutil . rmtree ( path or self . data . directory ) <EOL> def save_with_new_format ( self , format ) : <EOL> self . _project . change_format ( self , format ) <EOL> def save_with_new_format_recursive ( self , format ) : <EOL> self . _project . change_format_recursive ( self , format ) <EOL> def validate_keyword_name ( self , name ) : <EOL> return self . keywords . validate_name ( name ) <EOL> def is_directory_suite ( self ) : <EOL> return False <EOL> def resource_import_modified ( self , path ) : <EOL> return self . _project . resource_import_modified ( path , self . directory ) <EOL> def notify_settings_changed ( self ) : <EOL> RideItemSettingsChanged ( item = self ) . publish ( ) <EOL> def notify_steps_changed ( self ) : <EOL> for test in self . tests : <EOL> test . notify_steps_changed ( ) <EOL> def iter_datafiles ( self ) : <EOL> yield self <EOL> for child in self . children : <EOL> for datafile in child . iter_datafiles ( ) : <EOL> yield datafile <EOL> def save ( self ) : <EOL> self . _project . save ( self ) <EOL> def get_local_variables ( self ) : <EOL> return { } <EOL> def is_inside_top_suite ( self , res ) : <EOL> return False <EOL> class TestDataDirectoryController ( _DataController , _FileSystemElement , _BaseController ) : <EOL> def __init__ ( self , data , project = None , parent = None ) : <EOL> dir_ = data . directory <EOL> dir_ = os . path . abspath ( dir_ ) if isinstance ( dir_ , basestring ) else dir_ <EOL> _FileSystemElement . __init__ ( self , self . _filename ( data ) , dir_ ) <EOL> _DataController . __init__ ( self , data , project , parent ) <EOL> self . _dir_controllers = { } <EOL> def _filename ( self , data ) : <EOL> return data . initfile <EOL> @ property <EOL> def default_dir ( self ) : <EOL> return self . data . source <EOL> @ property <EOL> def display_name ( self ) : <EOL> return self . data . name <EOL> @ property <EOL> def longname ( self ) : <EOL> if self . parent : <EOL> return self . parent . longname + '<STR_LIT:.>' + self . display_name <EOL> return self . display_name <EOL> @ property <EOL> def suites ( self ) : <EOL> return [ child for child in self . children if <EOL> isinstance ( child , TestDataDirectoryController ) or <EOL> isinstance ( child , TestCaseFileController ) ] <EOL> def add_child ( self , child ) : <EOL> self . children . append ( child ) <EOL> def contains_tests ( self ) : <EOL> for suite in self . suites : <EOL> if suite . contains_tests ( ) : <EOL> return True <EOL> return False <EOL> def find_controller_by_longname ( self , longname , testname = None ) : <EOL> return self . find_controller_by_names ( longname . split ( "<STR_LIT:.>" ) , testname ) <EOL> def find_controller_by_names ( self , names , testname ) : <EOL> namestring = '<STR_LIT:.>' . join ( names ) <EOL> if not namestring . startswith ( self . name ) : <EOL> return None <EOL> if namestring == self . name : <EOL> return self <EOL> for suite in self . suites : <EOL> res = suite . find_controller_by_names ( namestring [ len ( self . name ) + <NUM_LIT:1> : ] . split ( '<STR_LIT:.>' ) , testname ) <EOL> if res : <EOL> return res <EOL> def is_excluded ( self ) : <EOL> return self . _project . is_excluded ( self . source ) if self . _project else False <EOL> def _children ( self , data ) : <EOL> children = [ DataController ( child , self . _project , self ) for child in data . children ] <EOL> if self . _can_add_directory_children ( data ) : <EOL> self . _add_directory_children ( children , data . source , data . initfile ) <EOL> return children <EOL> def _can_add_directory_children ( self , data ) : <EOL> return data . source and os . path . isdir ( data . source ) and self . _namespace <EOL> def _add_directory_children ( self , children , path , initfile ) : <EOL> for filename in self . _get_unknown_files_in_directory ( children , path , initfile ) : <EOL> if not self . _is_robot_ignored_name ( filename ) : <EOL> self . _add_directory_child ( children , filename ) <EOL> def _is_robot_ignored_name ( self , filename ) : <EOL> base = os . path . basename ( filename ) <EOL> return any ( base . startswith ( c ) for c in '<STR_LIT>' ) <EOL> def _add_directory_child ( self , children , filename ) : <EOL> if os . path . isdir ( filename ) : <EOL> children . append ( self . _directory_controller ( filename ) ) <EOL> else : <EOL> r = self . _namespace . get_resource ( filename , report_status = False ) <EOL> if self . _is_valid_resource ( r ) : <EOL> children . append ( self . _resource_controller ( r ) ) <EOL> def _directory_controller ( self , path ) : <EOL> dc = TestDataDirectoryController ( TestDataDirectory ( source = path ) , <EOL> project = self . _project , <EOL> parent = self ) <EOL> self . _add_directory_children ( dc . children , dc . source , None ) <EOL> return dc <EOL> def _is_valid_resource ( self , resource ) : <EOL> return resource and ( resource . setting_table or resource . variable_table or resource . keyword_table or os . stat ( resource . source ) [ <NUM_LIT:6> ] == <NUM_LIT:0> ) <EOL> def _resource_controller ( self , resource ) : <EOL> resource_control = self . _resource_file_controller_factory . create ( resource ) <EOL> resource_control . parent = self <EOL> return resource_control <EOL> def _get_unknown_files_in_directory ( self , children , path , initfile ) : <EOL> already_in_use = [ c . filename for c in children ] + [ initfile ] <EOL> already_in_use += [ c . directory for c in children ] <EOL> return [ f for f in self . _get_filenames_in_directory ( path ) if f not in already_in_use ] <EOL> def _get_filenames_in_directory ( self , path ) : <EOL> return [ os . path . join ( path , f ) for f in os . listdir ( path ) ] <EOL> def add_child ( self , controller ) : <EOL> assert controller not in self . children <EOL> self . children . append ( controller ) <EOL> def has_format ( self ) : <EOL> return self . data . initfile is not None <EOL> def set_format ( self , format ) : <EOL> self . data . initfile = os . path . join ( self . data . source , '<STR_LIT>' <EOL> % format . lower ( ) ) <EOL> self . filename = self . data . initfile <EOL> def new_test_case_file ( self , path ) : <EOL> ctrl = self . _new_data_controller ( NewTestCaseFile ( path ) ) <EOL> ctrl . mark_dirty ( ) <EOL> return ctrl <EOL> def new_test_data_directory ( self , path ) : <EOL> return self . _new_data_controller ( NewTestDataDirectory ( path ) ) <EOL> def _new_data_controller ( self , datafile ) : <EOL> self . data . children . append ( datafile ) <EOL> datafile . parent = self . data <EOL> self . children . append ( DataController ( datafile , self . _project , self ) ) <EOL> return self . children [ - <NUM_LIT:1> ] <EOL> def notify_suite_added ( self , suite ) : <EOL> RideSuiteAdded ( parent = self , suite = suite ) . publish ( ) <EOL> def is_directory_suite ( self ) : <EOL> return True <EOL> def reload ( self ) : <EOL> self . __init__ ( TestDataDirectory ( source = self . directory , parent = self . data . parent ) . populate ( ) , <EOL> self . _project , parent = self . parent ) <EOL> def remove ( self ) : <EOL> path = self . filename <EOL> self . data . initfile = None <EOL> self . _stat = self . _get_stat ( None ) <EOL> self . reload ( ) <EOL> RideInitFileRemoved ( path = path , datafile = self ) . publish ( ) <EOL> def _remove_resources ( self ) : <EOL> for resource in self . _find_resources_recursively ( self ) : <EOL> self . _project . remove_resource ( resource ) <EOL> RideDataFileRemoved ( path = resource . filename , datafile = resource ) . publish ( ) <EOL> def remove_from_model ( self ) : <EOL> self . _project . remove_datafile ( self ) <EOL> self . _remove_resources ( ) <EOL> RideDataFileRemoved ( path = self . filename , datafile = self ) . publish ( ) <EOL> def remove_child ( self , controller ) : <EOL> if controller in self . children : <EOL> self . children . remove ( controller ) <EOL> else : <EOL> for child in self . children : <EOL> child . remove_child ( controller ) <EOL> def is_inside_top_suite ( self , ctrl ) : <EOL> return ctrl . filename . startswith ( self . directory ) <EOL> def _is_inside_test_data_directory ( self , directory ) : <EOL> return any ( True for s in [ self ] + self . children <EOL> if s . is_directory_suite ( ) and s . directory == directory ) <EOL> def remove_static_imports_to_this ( self ) : <EOL> for resource_import in self . get_where_used ( ) : <EOL> resource_import [ <NUM_LIT:1> ] . remove ( ) <EOL> def get_where_used ( self ) : <EOL> for imp in self . _get_recursive_imports ( ) : <EOL> yield imp <EOL> def _all_imports ( self ) : <EOL> for df in self . datafiles : <EOL> for imp in df . imports : <EOL> yield imp <EOL> def _get_recursive_imports ( self ) : <EOL> all_imports = self . _all_imports ( ) <EOL> ctrls = self . _find_controllers_recursively ( self ) <EOL> for res in self . _find_resources_recursively ( self ) : <EOL> for imp in all_imports : <EOL> if imp . get_imported_controller ( ) == res and imp . parent . parent not in ctrls : <EOL> yield res , imp <EOL> def _find_resources_recursively ( self , controller ) : <EOL> resources = [ ] <EOL> if controller . children : <EOL> for child in controller . children : <EOL> resources += self . _find_resources_recursively ( child ) <EOL> elif isinstance ( controller , ResourceFileController ) : <EOL> resources . append ( controller ) <EOL> return resources <EOL> def _find_controllers_recursively ( self , controller ) : <EOL> resources = [ ] <EOL> if controller . children : <EOL> for child in controller . children : <EOL> resources += self . _find_controllers_recursively ( child ) <EOL> resources . append ( controller ) <EOL> return resources <EOL> def insert_to_test_data_directory ( self , res ) : <EOL> res_dir = os . path . dirname ( res . filename ) <EOL> if res_dir in self . _dir_controllers : <EOL> self . _dir_controllers [ res_dir ] . add_child ( res ) <EOL> else : <EOL> target = self . _find_closest_directory ( res ) <EOL> if target is self : <EOL> self . _create_target_dir_controller ( res , res_dir , target ) <EOL> else : <EOL> target . insert_to_test_data_directory ( res ) <EOL> def _find_closest_directory ( self , res ) : <EOL> target = self <EOL> for s in self . iter_datafiles ( ) : <EOL> if not isinstance ( s , TestDataDirectoryController ) : <EOL> continue <EOL> if res . filename . startswith ( s . directory ) : <EOL> target = s <EOL> return target <EOL> def _create_target_dir_controller ( self , res , res_dir , target ) : <EOL> for dirname in res_dir [ len ( self . directory ) : ] . split ( os . sep ) : <EOL> if not dirname : <EOL> continue <EOL> target_dir = os . path . join ( target . directory , dirname ) <EOL> dir_ctrl = TestDataDirectoryController ( TestDataDirectory ( source = target_dir ) , self . _project , self ) <EOL> target . _dir_controllers [ target . directory ] = dir_ctrl <EOL> target . add_child ( dir_ctrl ) <EOL> if target_dir == res_dir : <EOL> dir_ctrl . add_child ( res ) <EOL> return <EOL> target = dir_ctrl <EOL> if res not in self . children : <EOL> self . add_child ( res ) <EOL> def new_resource ( self , path ) : <EOL> ctrl = self . _project . new_resource ( path , parent = self ) <EOL> ctrl . mark_dirty ( ) <EOL> return ctrl <EOL> def exclude ( self ) : <EOL> if self . _project . is_datafile_dirty ( self ) : <EOL> raise DirtyRobotDataException ( ) <EOL> self . _project . _settings . excludes . update_excludes ( [ self . directory ] ) <EOL> index = self . parent . children . index ( self ) <EOL> result = ExcludedDirectoryController ( self . data , self . _project , self . parent ) <EOL> self . parent . children [ index ] = result <EOL> return result <EOL> class DirtyRobotDataException ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class TestCaseFileController ( _FileSystemElement , _DataController ) : <EOL> def __init__ ( self , data , project = None , parent = None ) : <EOL> _FileSystemElement . __init__ ( self , data . source if data else None , data . directory ) <EOL> _DataController . __init__ ( self , data , project , parent ) <EOL> def _settings ( self ) : <EOL> ss = self . _setting_table <EOL> sett = _DataController . _settings ( self ) <EOL> sett . insert ( - <NUM_LIT:1> , TemplateController ( self , ss . test_template ) ) <EOL> sett . insert ( - <NUM_LIT:1> , TimeoutController ( self , ss . test_timeout ) ) <EOL> return sett + [ self . default_tags ] <EOL> @ property <EOL> def longname ( self ) : <EOL> if self . parent : <EOL> return self . parent . longname + '<STR_LIT:.>' + self . name <EOL> return self . name <EOL> @ property <EOL> def suites ( self ) : <EOL> return ( ) <EOL> def contains_tests ( self ) : <EOL> return bool ( self . tests ) <EOL> def find_controller_by_longname ( self , longname , node_testname = None ) : <EOL> return self . find_controller_by_names ( longname . split ( "<STR_LIT:.>" ) , node_testname ) <EOL> def find_controller_by_names ( self , names , node_testname = None ) : <EOL> names = '<STR_LIT:.>' . join ( names ) <EOL> if not names . startswith ( self . name ) : <EOL> return None <EOL> if len ( self . name ) < len ( names ) and not names . startswith ( self . name + '<STR_LIT:.>' ) : <EOL> return None <EOL> if len ( names ) == <NUM_LIT:1> : <EOL> return self <EOL> for test in self . tests : <EOL> if test . name == node_testname : <EOL> return test <EOL> return None <EOL> @ property <EOL> def default_tags ( self ) : <EOL> return DefaultTagsController ( self , self . _setting_table . default_tags ) <EOL> def is_modifiable ( self ) : <EOL> return not self . exists ( ) or not self . is_readonly ( ) <EOL> def create_test ( self , name ) : <EOL> return self . tests . new ( name ) <EOL> def validate_test_name ( self , name ) : <EOL> return self . tests . validate_name ( name ) <EOL> def remove_child ( self , controller ) : <EOL> if controller is self : <EOL> self . remove ( ) <EOL> def remove ( self ) : <EOL> self . _project . remove_datafile ( self ) <EOL> RideDataFileRemoved ( path = self . filename , datafile = self ) . publish ( ) <EOL> def reload ( self ) : <EOL> self . __init__ ( TestCaseFile ( parent = self . data . parent , source = self . filename ) . populate ( ) , <EOL> project = self . _project , <EOL> parent = self . parent ) <EOL> def get_template ( self ) : <EOL> return self . data . setting_table . test_template <EOL> class ResourceFileControllerFactory ( object ) : <EOL> def __init__ ( self , namespace , project ) : <EOL> self . _resources = [ ] <EOL> self . _namespace = namespace <EOL> self . _project = project <EOL> self . _all_resource_imports_resolved = False <EOL> @ property <EOL> def resources ( self ) : <EOL> return self . _resources <EOL> def find ( self , data ) : <EOL> return self . _find_with_source ( data . source ) <EOL> def _find_with_source ( self , source ) : <EOL> for other in self . resources : <EOL> if other . filename == source : <EOL> return other <EOL> return None <EOL> def find_with_import ( self , import_ ) : <EOL> resource_model = self . _namespace . find_resource_with_import ( import_ ) <EOL> if not resource_model : <EOL> return None <EOL> res = self . find ( resource_model ) <EOL> if not res : <EOL> res = self . create ( resource_model ) <EOL> self . _project . insert_into_suite_structure ( res ) <EOL> assert ( res is not None ) <EOL> return res <EOL> def create ( self , data , parent = None ) : <EOL> rfc = ResourceFileController ( data , self . _project , parent , self ) <EOL> self . resources . append ( rfc ) <EOL> self . set_all_resource_imports_unresolved ( ) <EOL> return rfc <EOL> def set_all_resource_imports_resolved ( self ) : <EOL> self . _all_resource_imports_resolved = True <EOL> def set_all_resource_imports_unresolved ( self ) : <EOL> self . _all_resource_imports_resolved = False <EOL> def is_all_resource_file_imports_resolved ( self ) : <EOL> return self . _all_resource_imports_resolved <EOL> def remove ( self , controller ) : <EOL> self . _resources . remove ( controller ) <EOL> self . set_all_resource_imports_unresolved ( ) <EOL> class ResourceFileController ( _FileSystemElement , _DataController ) : <EOL> def __init__ ( self , data , project = None , parent = None , resource_file_controller_factory = None ) : <EOL> self . _resource_file_controller_factory = resource_file_controller_factory <EOL> self . _known_imports = set ( ) <EOL> _FileSystemElement . __init__ ( self , data . source if data else None , data . directory ) <EOL> _DataController . __init__ ( self , data , project , <EOL> parent or self . _find_parent_for ( project , data . source ) ) <EOL> if self . parent and self not in self . parent . children : <EOL> self . parent . add_child ( self ) <EOL> self . _unresolve_all_if_none_existing ( ) <EOL> def _unresolve_all_if_none_existing ( self ) : <EOL> if not self . exists ( ) and self . _resource_file_controller_factory : <EOL> self . _resource_file_controller_factory . set_all_resource_imports_unresolved ( ) <EOL> def _find_parent_for ( self , project , source ) : <EOL> if not project : <EOL> return None <EOL> dir = os . path . dirname ( source ) <EOL> for ctrl in project . datafiles : <EOL> if ctrl . is_directory_suite ( ) and self . _to_os_style ( ctrl . directory ) == dir : <EOL> return ctrl <EOL> return None <EOL> def _to_os_style ( self , path ) : <EOL> return path . replace ( '<STR_LIT:/>' , os . sep ) <EOL> @ property <EOL> def display_name ( self ) : <EOL> _ , tail = os . path . split ( self . data . source ) <EOL> return tail <EOL> def is_modifiable ( self ) : <EOL> return not self . exists ( ) or not self . is_readonly ( ) <EOL> def set_format ( self , format ) : <EOL> self . _modify_file_name ( lambda : _DataController . set_format ( self , format ) , <EOL> lambda imp : imp . change_format ( format ) ) <EOL> def set_basename ( self , basename ) : <EOL> self . _modify_file_name ( lambda : _DataController . set_basename ( self , basename ) , <EOL> lambda imp : imp . unresolve ( ) ) <EOL> def set_basename_and_modify_imports ( self , basename ) : <EOL> old = self . filename <EOL> self . _modify_file_name ( lambda : _DataController . set_basename ( self , basename ) , <EOL> lambda imp : imp . change_name ( os . path . basename ( old ) , os . path . basename ( self . filename ) ) ) <EOL> def remove_static_imports_to_this ( self ) : <EOL> name = os . path . basename ( self . filename ) <EOL> imports = [ import_ for import_ in self . get_where_used ( ) ] <EOL> for resource_import in imports : <EOL> if resource_import . name . endswith ( name ) : <EOL> resource_import . remove ( ) <EOL> def _modify_file_name ( self , modification , notification ) : <EOL> old = self . filename <EOL> modification ( ) <EOL> resource_imports = [ resource_import_ for resource_import_ in self . get_where_used ( ) ] <EOL> for resource_import in resource_imports : <EOL> notification ( resource_import ) <EOL> self . _namespace . resource_filename_changed ( old , self . filename ) <EOL> def _settings ( self ) : <EOL> return [ DocumentationController ( self , self . data . setting_table . doc ) ] <EOL> def validate_name ( self , name ) : <EOL> for uk in self . data . keyword_table . keywords : <EOL> if uk != name and utils . eq ( uk . name , name ) : <EOL> return '<STR_LIT>' <EOL> return None <EOL> def reload ( self ) : <EOL> self . __init__ ( ResourceFile ( source = self . filename ) . populate ( ) , self . _project , parent = self . parent ) <EOL> def remove ( self ) : <EOL> self . _project . remove_resource ( self ) <EOL> RideDataFileRemoved ( path = self . filename , datafile = self ) . publish ( ) <EOL> def remove_known_import ( self , _import ) : <EOL> self . _known_imports . remove ( _import ) <EOL> def add_known_import ( self , _import ) : <EOL> self . _known_imports . add ( _import ) <EOL> def notify_opened ( self ) : <EOL> RideOpenResource ( path = self . filename , datafile = self ) . publish ( ) <EOL> for _import in [ imp for imp in self . imports if imp . is_resource ] : <EOL> _import . import_loaded_or_modified ( ) <EOL> def is_used ( self ) : <EOL> if self . _known_imports : <EOL> return True <EOL> if self . _resource_file_controller_factory . is_all_resource_file_imports_resolved ( ) : <EOL> return False <EOL> return any ( self . _resolve_known_imports ( ) ) <EOL> def get_where_used ( self ) : <EOL> if self . _resource_file_controller_factory . is_all_resource_file_imports_resolved ( ) : <EOL> source = self . _known_imports <EOL> else : <EOL> source = self . _resolve_known_imports ( ) <EOL> for usage in source : <EOL> yield usage <EOL> def _resolve_known_imports ( self ) : <EOL> for imp in self . _all_imports ( ) : <EOL> if imp . get_imported_controller ( ) is self : <EOL> yield imp <EOL> self . _resource_file_controller_factory . set_all_resource_imports_resolved ( ) <EOL> def _all_imports ( self ) : <EOL> for df in self . datafiles : <EOL> for imp in df . imports : <EOL> yield imp <EOL> def remove_child ( self , controller ) : <EOL> pass <EOL> class ExcludedDirectoryController ( _FileSystemElement , ControllerWithParent , WithNamespace ) : <EOL> def __init__ ( self , data , project , parent ) : <EOL> self . data = data <EOL> self . _project = project <EOL> if self . _project : <EOL> self . _set_namespace_from ( self . _project ) <EOL> self . _resource_file_controller_factory = self . _project . resource_file_controller_factory <EOL> else : <EOL> self . _resource_file_controller_factory = None <EOL> self . _parent = parent <EOL> self . children = [ ] <EOL> self . keywords = [ ] <EOL> self . variables = tuple ( ) <EOL> self . tests = tuple ( ) <EOL> self . imports = tuple ( ) <EOL> _FileSystemElement . __init__ ( self , '<STR_LIT>' , data . directory ) <EOL> @ property <EOL> def settings ( self ) : <EOL> return self . _settings ( ) <EOL> def _settings ( self ) : <EOL> ss = self . data . setting_table <EOL> return [ DocumentationController ( self , ss . doc ) , <EOL> FixtureController ( self , ss . suite_setup ) , <EOL> FixtureController ( self , ss . suite_teardown ) , <EOL> FixtureController ( self , ss . test_setup ) , <EOL> FixtureController ( self , ss . test_teardown ) , <EOL> self . force_tags ] <EOL> @ property <EOL> def _setting_table ( self ) : <EOL> return self . data . setting_table <EOL> @ property <EOL> def force_tags ( self ) : <EOL> return ForceTagsController ( self , self . _setting_table . force_tags ) <EOL> @ property <EOL> def dirty ( self ) : <EOL> return False <EOL> def keyword_info ( self , keyword_name ) : <EOL> return WithNamespace . keyword_info ( self , self . data , keyword_name ) <EOL> @ overrides ( _BaseController ) <EOL> def is_excluded ( self ) : <EOL> return True <EOL> def remove_from_excludes ( self ) : <EOL> self . _project . _settings . excludes . remove_path ( self . source ) <EOL> index = self . parent . children . index ( self ) <EOL> td = TestData ( self . data . source , self . parent . data , self . _project . _settings ) <EOL> result = TestDataDirectoryController ( td , self . _project , self . parent ) <EOL> self . parent . children [ index ] = result <EOL> return result <EOL> def iter_datafiles ( self ) : <EOL> return [ self ] <EOL> @ property <EOL> def name ( self ) : <EOL> return self . data . name <EOL> def is_directory_suite ( self ) : <EOL> return True <EOL> def add_child ( self , child ) : <EOL> self . children . append ( child ) </s>
