<s> import os <EOL> import sys <EOL> from robotide import robotapi , utils <EOL> from robotide . utils . versioncomparator import cmp_versions <EOL> from iteminfo import _XMLKeywordContent <EOL> from robotide import context <EOL> class SpecInitializer ( object ) : <EOL> def __init__ ( self , directories = None ) : <EOL> self . _directories = directories or [ ] <EOL> self . _directories . append ( context . LIBRARY_XML_DIRECTORY ) <EOL> def init_from_spec ( self , name ) : <EOL> specfile = self . _find_from_pythonpath ( name ) or self . _find_from_library_xml_directories ( name ) <EOL> return self . _init_from_specfile ( specfile , name ) <EOL> def _find_from_library_xml_directories ( self , name ) : <EOL> for directory in self . _directories : <EOL> path = self . _find_from_library_xml_directory ( directory , name ) <EOL> if path : <EOL> return path <EOL> return None <EOL> def _find_from_library_xml_directory ( self , directory , name ) : <EOL> current_xml_file = None <EOL> for xml_file in self . _list_xml_files_in ( directory ) : <EOL> name_from_xml = get_name_from_xml ( xml_file ) <EOL> if name_from_xml == name : <EOL> current_xml_file = self . _get_newest_xml_file ( <EOL> xml_file , current_xml_file ) <EOL> return current_xml_file <EOL> def _get_newest_xml_file ( self , xml_file , current_xml_file ) : <EOL> version1 = self . _get_version ( xml_file ) <EOL> version2 = self . _get_version ( current_xml_file ) <EOL> if cmp_versions ( version1 , version2 ) == <NUM_LIT:1> : <EOL> return xml_file <EOL> return current_xml_file <EOL> def _get_version ( self , xml_file ) : <EOL> try : <EOL> return utils . ET . parse ( xml_file ) . getroot ( ) . find ( '<STR_LIT:version>' ) . text <EOL> except : <EOL> return None <EOL> def _list_xml_files_in ( self , directory ) : <EOL> for filename in os . listdir ( directory ) : <EOL> path = os . path . join ( directory , filename ) <EOL> if path . endswith ( '<STR_LIT>' ) and os . path . isfile ( path ) : <EOL> yield path <EOL> def _find_from_pythonpath ( self , name ) : <EOL> return utils . find_from_pythonpath ( name + '<STR_LIT>' ) <EOL> def _init_from_specfile ( self , specfile , name ) : <EOL> if not specfile : <EOL> return [ ] <EOL> try : <EOL> return _parse_xml ( specfile , name ) <EOL> except Exception : <EOL> return [ ] <EOL> def _parse_xml ( file , name ) : <EOL> root = utils . ET . parse ( file ) . getroot ( ) <EOL> if root . tag != '<STR_LIT>' : <EOL> return [ ] , '<STR_LIT>' <EOL> kw_nodes = root . findall ( '<STR_LIT>' ) + root . findall ( '<STR_LIT>' ) <EOL> source_type = root . get ( '<STR_LIT:type>' ) <EOL> if source_type == '<STR_LIT>' : <EOL> source_type += '<STR_LIT>' <EOL> return [ _XMLKeywordContent ( node , name , source_type ) for node in kw_nodes ] <EOL> def get_path ( name , basedir ) : <EOL> if not _is_library_by_path ( name ) : <EOL> return name . replace ( '<STR_LIT:U+0020>' , '<STR_LIT>' ) <EOL> return _resolve_path ( name . replace ( '<STR_LIT:/>' , os . sep ) , basedir ) <EOL> def _is_library_by_path ( path ) : <EOL> return path . lower ( ) . endswith ( ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:/>' , os . sep ) ) <EOL> def _resolve_path ( path , basedir ) : <EOL> for base in [ basedir ] + sys . path : <EOL> if not ( base and os . path . isdir ( base ) ) : <EOL> continue <EOL> ret = os . path . join ( base , path ) <EOL> if os . path . isfile ( ret ) : <EOL> return ret <EOL> if os . path . isdir ( ret ) and os . path . isfile ( os . path . join ( ret , '<STR_LIT>' ) ) : <EOL> return ret <EOL> raise robotapi . DataError <EOL> def _get_library_name ( name ) : <EOL> if os . path . exists ( name ) : <EOL> return name <EOL> return name . replace ( '<STR_LIT:U+0020>' , '<STR_LIT>' ) <EOL> def get_name_from_xml ( path ) : <EOL> try : <EOL> root = utils . ET . parse ( path ) . getroot ( ) <EOL> name = root . get ( '<STR_LIT:name>' ) <EOL> return name <EOL> except : <EOL> return None </s>
