<s> import os <EOL> import shutil <EOL> import tempfile <EOL> from robotide . context import LOG <EOL> from robotide . controller . commands import NullObserver , SaveFile <EOL> from robotide . publish . messages import RideOpenSuite , RideNewProject , RideFileNameChanged <EOL> from . basecontroller import WithNamespace , _BaseController <EOL> from . dataloader import DataLoader <EOL> from . filecontrollers import DataController , ResourceFileControllerFactory <EOL> from . robotdata import NewTestCaseFile , NewTestDataDirectory <EOL> from robotide . spec . librarydatabase import DATABASE_FILE <EOL> from robotide . spec . librarymanager import LibraryManager <EOL> from robotide . spec . xmlreaders import SpecInitializer <EOL> from robotide . utils import overrides <EOL> class Project ( _BaseController , WithNamespace ) : <EOL> def __init__ ( self , namespace = None , settings = None , library_manager = None ) : <EOL> self . _library_manager = self . _construct_library_manager ( library_manager , settings ) <EOL> if not self . _library_manager . is_alive ( ) : <EOL> self . _library_manager . start ( ) <EOL> self . _set_namespace ( namespace ) <EOL> self . _settings = settings <EOL> self . _loader = DataLoader ( namespace , settings ) <EOL> self . _controller = None <EOL> self . name = None <EOL> self . external_resources = [ ] <EOL> self . _resource_file_controller_factory = ResourceFileControllerFactory ( namespace , self ) <EOL> self . _serializer = Serializer ( settings , LOG ) <EOL> def _construct_library_manager ( self , library_manager , settings ) : <EOL> return library_manager or LibraryManager ( DATABASE_FILE , <EOL> SpecInitializer ( settings . get ( '<STR_LIT>' , [ ] ) [ : ] ) ) <EOL> def __del__ ( self ) : <EOL> if self . _library_manager : <EOL> self . close ( ) <EOL> def close ( self ) : <EOL> self . _library_manager . stop ( ) <EOL> self . _library_manager = None <EOL> @ overrides ( WithNamespace ) <EOL> def _set_namespace ( self , namespace ) : <EOL> namespace . set_library_manager ( self . _library_manager ) <EOL> WithNamespace . _set_namespace ( self , namespace ) <EOL> @ property <EOL> def display_name ( self ) : <EOL> return '<STR_LIT>' <EOL> @ property <EOL> def default_dir ( self ) : <EOL> return os . path . abspath ( self . _settings [ '<STR_LIT>' ] ) <EOL> def update_default_dir ( self , path ) : <EOL> default_dir = path if os . path . isdir ( path ) else os . path . dirname ( path ) <EOL> self . _settings . set ( '<STR_LIT>' , default_dir ) <EOL> @ property <EOL> def data ( self ) : <EOL> return self . _controller <EOL> @ property <EOL> def suite ( self ) : <EOL> return self . _controller . data if self . _controller else None <EOL> @ property <EOL> def datafiles ( self ) : <EOL> return self . _suites ( ) + self . resources <EOL> @ property <EOL> def resources ( self ) : <EOL> return self . _resource_file_controller_factory . resources <EOL> @ property <EOL> def resource_file_controller_factory ( self ) : <EOL> return self . _resource_file_controller_factory <EOL> def find_controller_by_longname ( self , longname , testname = None ) : <EOL> return self . _controller . find_controller_by_longname ( longname , testname ) <EOL> def new_directory_project ( self , path ) : <EOL> self . _new_project ( NewTestDataDirectory ( path ) ) <EOL> def new_file_project ( self , path ) : <EOL> self . _new_project ( NewTestCaseFile ( path ) ) <EOL> def _new_project ( self , datafile ) : <EOL> self . update_default_dir ( datafile . directory ) <EOL> self . _controller = DataController ( datafile , self ) <EOL> self . _resource_file_controller_factory = ResourceFileControllerFactory ( self . _namespace , self ) <EOL> RideNewProject ( path = datafile . source , datafile = datafile ) . publish ( ) <EOL> def new_resource ( self , path , parent = None ) : <EOL> res = self . _namespace . new_resource ( path ) <EOL> self . update_default_dir ( path ) <EOL> resource_controller = self . _create_resource_controller ( res , parent ) <EOL> return resource_controller <EOL> def load_data ( self , path , load_observer = None ) : <EOL> load_observer = load_observer or NullObserver ( ) <EOL> if self . _load_initfile ( path , load_observer ) : <EOL> return <EOL> if self . _load_datafile ( path , load_observer ) : <EOL> return <EOL> if self . _load_resource ( path , load_observer ) : <EOL> return <EOL> load_observer . error ( "<STR_LIT>" <EOL> "<STR_LIT>" % path ) <EOL> def is_excluded ( self , source ) : <EOL> return self . _settings . excludes . contains ( source ) if self . _settings else False <EOL> def _load_initfile ( self , path , load_observer ) : <EOL> if not os . path . splitext ( os . path . split ( path ) [ <NUM_LIT:1> ] ) [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> return None <EOL> initfile = self . _loader . load_initfile ( path , load_observer ) <EOL> if not initfile : <EOL> return None <EOL> self . _populate_from_datafile ( path , initfile , load_observer ) <EOL> return initfile <EOL> def load_datafile ( self , path , load_observer ) : <EOL> datafile = self . _load_datafile ( path , load_observer ) <EOL> if datafile : <EOL> return datafile <EOL> load_observer . error ( "<STR_LIT>" % path ) <EOL> def _load_datafile ( self , path , load_observer ) : <EOL> datafile = self . _loader . load_datafile ( path , load_observer ) <EOL> if not datafile : <EOL> return None <EOL> self . _populate_from_datafile ( path , datafile , load_observer ) <EOL> return datafile <EOL> def _populate_from_datafile ( self , path , datafile , load_observer ) : <EOL> self . __init__ ( self . _namespace , self . _settings , library_manager = self . _library_manager ) <EOL> resources = self . _loader . resources_for ( datafile , load_observer ) <EOL> self . _create_controllers ( datafile , resources ) <EOL> RideOpenSuite ( path = path , datafile = self . _controller ) . publish ( ) <EOL> load_observer . finish ( ) <EOL> def _create_controllers ( self , datafile , resources ) : <EOL> self . clear_namespace_update_listeners ( ) <EOL> self . _controller = DataController ( datafile , self ) <EOL> new_resource_controllers = [ ] <EOL> for r in resources : <EOL> self . _create_resource_controller ( r , resource_created_callback = lambda controller : new_resource_controllers . append ( controller ) ) <EOL> for controller in new_resource_controllers : <EOL> self . _inform_resource_created ( controller ) <EOL> def load_resource ( self , path , load_observer ) : <EOL> resource = self . _load_resource ( path , load_observer ) <EOL> if resource : <EOL> return resource <EOL> load_observer . error ( "<STR_LIT>" % path ) <EOL> def _load_resource ( self , path , load_observer ) : <EOL> resource = self . _namespace . get_resource ( path ) <EOL> if not resource : <EOL> return None <EOL> ctrl = self . _create_resource_controller ( resource ) <EOL> load_observer . finish ( ) <EOL> return ctrl <EOL> def _create_resource_controller ( self , parsed_resource , parent = None , resource_created_callback = None ) : <EOL> old = self . _resource_file_controller_factory . find ( parsed_resource ) <EOL> if old : <EOL> return old <EOL> controller = self . _resource_file_controller_factory . create ( parsed_resource , parent = parent ) <EOL> self . insert_into_suite_structure ( controller ) <EOL> resource_created_callback = resource_created_callback or self . _inform_resource_created <EOL> resource_created_callback ( controller ) <EOL> return controller <EOL> def _inform_resource_created ( self , controller ) : <EOL> controller . notify_opened ( ) <EOL> def insert_into_suite_structure ( self , resource ) : <EOL> if self . _controller and self . _controller . is_inside_top_suite ( resource ) : <EOL> self . _controller . insert_to_test_data_directory ( resource ) <EOL> else : <EOL> self . external_resources . append ( resource ) <EOL> self . _sort_external_resources ( ) <EOL> def _sort_external_resources ( self ) : <EOL> self . external_resources . sort ( key = lambda resource : resource . name . lower ( ) ) <EOL> def get_all_keywords ( self ) : <EOL> return self . get_all_keywords_from ( ctrl . datafile for ctrl in self . datafiles if ctrl . datafile ) <EOL> def all_testcases ( self ) : <EOL> for df in self . _suites ( ) : <EOL> for test in df . tests : <EOL> yield test <EOL> def get_files_without_format ( self , controller = None ) : <EOL> if controller : <EOL> controller_list = [ controller ] <EOL> else : <EOL> controller_list = self . _get_all_dirty_controllers ( ) <EOL> return [ dc for dc in controller_list if dc . dirty and not dc . has_format ( ) ] <EOL> def get_root_suite_dir_path ( self ) : <EOL> return self . suite . get_dir_path ( ) <EOL> def is_dirty ( self ) : <EOL> if self . data and self . is_datafile_dirty ( self . data ) : <EOL> return True <EOL> for res in self . resources : <EOL> if res . dirty : <EOL> return True <EOL> return False <EOL> def is_datafile_dirty ( self , datafile ) : <EOL> if datafile . dirty : <EOL> return True <EOL> for df in datafile . children : <EOL> if self . is_datafile_dirty ( df ) : <EOL> return True <EOL> return False <EOL> def change_format ( self , controller , format ) : <EOL> if controller . is_same_format ( format ) : <EOL> return <EOL> old_path = controller . filename <EOL> controller . set_format ( format ) <EOL> controller . execute ( SaveFile ( ) ) <EOL> if old_path : <EOL> self . _remove_file ( old_path ) <EOL> RideFileNameChanged ( old_filename = old_path , <EOL> datafile = controller ) . publish ( ) <EOL> def _remove_file ( self , path ) : <EOL> if path and os . path . isfile ( path ) : <EOL> os . remove ( path ) <EOL> def change_format_recursive ( self , controller , format ) : <EOL> for datafile in controller . iter_datafiles ( ) : <EOL> self . change_format ( datafile , format ) <EOL> def remove_datafile ( self , controller ) : <EOL> if controller is self . _controller : <EOL> self . _controller = None <EOL> else : <EOL> self . _controller . remove_child ( controller ) <EOL> def remove_resource ( self , controller ) : <EOL> self . _resource_file_controller_factory . remove ( controller ) <EOL> def save ( self , controller ) : <EOL> assert controller is not None <EOL> self . _serializer . serialize_file ( controller ) <EOL> def _get_all_dirty_controllers ( self ) : <EOL> return [ controller for controller in self . datafiles if controller . dirty ] <EOL> def _suites ( self ) : <EOL> if not self . data : <EOL> return [ ] <EOL> return [ df for df in self . data . iter_datafiles ( ) if not df in self . resources ] <EOL> def resource_import_modified ( self , path , directory ) : <EOL> resource = self . _namespace . get_resource ( path , directory ) <EOL> if resource : <EOL> return self . _create_resource_controller ( resource ) <EOL> class Serializer ( object ) : <EOL> def __init__ ( self , settings , logger ) : <EOL> self . _settings = settings <EOL> self . _logger = logger <EOL> self . _errors = [ ] <EOL> def serialize_file ( self , controller ) : <EOL> try : <EOL> self . _serialize_file ( controller ) <EOL> finally : <EOL> self . _log_errors ( ) <EOL> def _serialize_file ( self , controller ) : <EOL> with Backup ( controller ) : <EOL> try : <EOL> controller . datafile . save ( ** self . _get_options ( ) ) <EOL> except Exception , err : <EOL> self . _cache_error ( controller , err ) <EOL> raise <EOL> def _get_options ( self ) : <EOL> return { '<STR_LIT>' : self . _get_line_separator ( ) , <EOL> '<STR_LIT>' : self . _get_pipe_separated ( ) , <EOL> '<STR_LIT>' : self . _get_separating_spaces ( ) } <EOL> def _get_line_separator ( self ) : <EOL> setting = self . _settings . get ( '<STR_LIT>' , '<STR_LIT>' ) . lower ( ) <EOL> if setting in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return '<STR_LIT:\r\n>' <EOL> if setting in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return '<STR_LIT:\n>' <EOL> return os . linesep <EOL> def _get_pipe_separated ( self ) : <EOL> return self . _settings . get ( '<STR_LIT>' , '<STR_LIT>' ) == '<STR_LIT>' <EOL> def _get_separating_spaces ( self ) : <EOL> return self . _settings . get ( '<STR_LIT>' , <NUM_LIT:4> ) <EOL> def _cache_error ( self , data , error ) : <EOL> self . _errors . append ( "<STR_LIT>" <EOL> % ( data . data . source , unicode ( error ) ) ) <EOL> def _log_errors ( self ) : <EOL> if self . _errors : <EOL> self . _logger . error ( '<STR_LIT>' . join ( self . _errors ) ) <EOL> self . _errors = [ ] <EOL> class Backup ( object ) : <EOL> def __init__ ( self , file_controller ) : <EOL> self . _path = file_controller . filename <EOL> self . _file_controller = file_controller <EOL> self . _backup = self . _get_backup_name ( self . _path ) <EOL> def _get_backup_name ( self , path ) : <EOL> if not os . path . isfile ( path ) : <EOL> return None <EOL> self . _backup_dir = tempfile . mkdtemp ( dir = os . path . dirname ( path ) ) <EOL> return os . path . join ( self . _backup_dir , os . path . basename ( path ) ) <EOL> def __enter__ ( self ) : <EOL> self . _make_backup ( ) <EOL> def _make_backup ( self ) : <EOL> if self . _backup : <EOL> try : <EOL> self . _move ( self . _path , self . _backup ) <EOL> except IOError : <EOL> self . _backup = None <EOL> self . _remove_backup_dir ( ) <EOL> def __exit__ ( self , * args ) : <EOL> if any ( args ) : <EOL> self . _restore_backup ( ) <EOL> else : <EOL> self . _remove_backup ( ) <EOL> def _remove_backup ( self ) : <EOL> if self . _backup : <EOL> os . remove ( self . _backup ) <EOL> self . _remove_backup_dir ( ) <EOL> def _restore_backup ( self ) : <EOL> if self . _backup : <EOL> self . _move ( self . _backup , self . _path ) <EOL> self . _file_controller . refresh_stat ( ) <EOL> self . _remove_backup_dir ( ) <EOL> def _move ( self , from_path , to_path ) : <EOL> shutil . move ( from_path , to_path ) <EOL> def _remove_backup_dir ( self ) : <EOL> os . rmdir ( self . _backup_dir ) </s>
