<s> import os <EOL> from foam . sfa . util . faults import InvalidRSpec , UnsupportedRSpecVersion <EOL> from foam . sfa . rspecs . version import RSpecVersion <EOL> class VersionManager : <EOL> def __init__ ( self ) : <EOL> self . versions = [ ] <EOL> self . load_versions ( ) <EOL> def load_versions ( self ) : <EOL> path = os . path . dirname ( os . path . abspath ( __file__ ) ) <EOL> versions_path = path + os . sep + '<STR_LIT>' <EOL> versions_module_path = '<STR_LIT>' <EOL> valid_module = lambda x : os . path . isfile ( os . sep . join ( [ versions_path , x ] ) ) and x . endswith ( '<STR_LIT>' ) and x != '<STR_LIT>' <EOL> files = [ f for f in os . listdir ( versions_path ) if valid_module ( f ) ] <EOL> for filename in files : <EOL> basename = filename . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] <EOL> module_path = versions_module_path + '<STR_LIT:.>' + basename <EOL> try : <EOL> module = __import__ ( module_path , fromlist = module_path ) <EOL> except : <EOL> module = __import__ ( '<STR_LIT>' + module_path , fromlist = '<STR_LIT>' + module_path ) <EOL> for attr_name in dir ( module ) : <EOL> attr = getattr ( module , attr_name ) <EOL> if hasattr ( attr , '<STR_LIT:version>' ) and hasattr ( attr , '<STR_LIT>' ) and attr . enabled == True : <EOL> self . versions . append ( attr ( ) ) <EOL> def _get_version ( self , type , version_num = None , content_type = None ) : <EOL> retval = None <EOL> for version in self . versions : <EOL> if type is None or type . lower ( ) == version . type . lower ( ) : <EOL> if version_num is None or str ( version_num ) == version . version : <EOL> if content_type is None or content_type . lower ( ) == version . content_type . lower ( ) or version . content_type == '<STR_LIT:*>' : <EOL> retval = version <EOL> break <EOL> if not retval : <EOL> i = <NUM_LIT:0> <EOL> for version in self . versions : <EOL> i += <NUM_LIT:1> <EOL> retval = self . versions [ <NUM_LIT:0> ] <EOL> return retval <EOL> def get_version ( self , version = None ) : <EOL> print self . versions <EOL> retval = None <EOL> if isinstance ( version , dict ) : <EOL> retval = self . _get_version ( version . get ( '<STR_LIT:type>' ) , version . get ( '<STR_LIT:version>' ) , version . get ( '<STR_LIT>' ) ) <EOL> elif isinstance ( version , basestring ) : <EOL> version_parts = version . split ( '<STR_LIT:U+0020>' ) <EOL> num_parts = len ( version_parts ) <EOL> type = version_parts [ <NUM_LIT:0> ] <EOL> version_num = None <EOL> content_type = None <EOL> if num_parts > <NUM_LIT:1> : <EOL> version_num = version_parts [ <NUM_LIT:1> ] <EOL> if num_parts > <NUM_LIT:2> : <EOL> content_type = version_parts [ <NUM_LIT:2> ] <EOL> retval = self . _get_version ( type , version_num , content_type ) <EOL> elif isinstance ( version , RSpecVersion ) : <EOL> retval = version <EOL> else : <EOL> raise UnsupportedRSpecVersion ( "<STR_LIT>" % str ( version ) ) <EOL> return retval <EOL> def get_version_by_schema ( self , schema ) : <EOL> retval = None <EOL> for version in self . versions : <EOL> if schema == version . schema : <EOL> retval = version <EOL> if not retval : <EOL> raise InvalidRSpec ( "<STR_LIT>" % schema ) <EOL> return retval <EOL> def show_by_string ( string ) : <EOL> try : <EOL> print v . get_version ( string ) <EOL> except Exception , e : <EOL> print e <EOL> def show_by_schema ( string ) : <EOL> try : <EOL> print v . get_version_by_schema ( string ) <EOL> except Exception , e : <EOL> print e <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> v = VersionManager ( ) <EOL> print v . versions <EOL> show_by_string ( '<STR_LIT>' ) <EOL> show_by_string ( '<STR_LIT>' ) <EOL> show_by_string ( '<STR_LIT>' ) <EOL> show_by_schema ( '<STR_LIT>' ) <EOL> show_by_schema ( '<STR_LIT>' ) </s>
