<s> import xml . etree . ElementTree as ET <EOL> from omnilib . omnispec . omnispec import OmniSpec , OmniResource <EOL> ONE_NODE_RSPEC = """<STR_LIT>""" <EOL> def can_translate ( rspec ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' in rspec <EOL> def pg_tag ( tag ) : <EOL> return '<STR_LIT>' + tag <EOL> def add_options ( ospec , root ) : <EOL> onode = OmniResource ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> misc = onode [ '<STR_LIT>' ] <EOL> free = <NUM_LIT:0> <EOL> used = <NUM_LIT:0> <EOL> for res in root . findall ( pg_tag ( '<STR_LIT>' ) ) : <EOL> if res . find ( pg_tag ( '<STR_LIT>' ) ) is None : <EOL> continue <EOL> if res . find ( pg_tag ( '<STR_LIT>' ) ) . text . lower ( ) == '<STR_LIT:false>' : <EOL> used += <NUM_LIT:1> <EOL> else : <EOL> free += <NUM_LIT:1> <EOL> misc [ '<STR_LIT>' ] = free <EOL> misc [ '<STR_LIT>' ] = used <EOL> options = onode [ '<STR_LIT>' ] <EOL> options [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> options [ '<STR_LIT>' ] = True <EOL> options [ '<STR_LIT>' ] = <NUM_LIT:1> <EOL> options [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> ospec . add_resource ( '<STR_LIT>' , onode ) <EOL> def add_nodes ( ospec , root ) : <EOL> for res in root . findall ( pg_tag ( '<STR_LIT>' ) ) : <EOL> name = res . attrib [ '<STR_LIT>' ] <EOL> description = '<STR_LIT>' <EOL> type = '<STR_LIT>' <EOL> omni_res = OmniResource ( name , description , type ) <EOL> available = res . find ( pg_tag ( '<STR_LIT>' ) ) . text . lower ( ) == '<STR_LIT:true>' <EOL> omni_res . set_allocated ( not ( available ) ) <EOL> omni_res [ '<STR_LIT>' ] = ET . tostring ( res ) <EOL> id = res . attrib [ '<STR_LIT>' ] <EOL> ospec . add_resource ( id , omni_res ) <EOL> def add_links ( ospec , root ) : <EOL> for res in root . findall ( pg_tag ( '<STR_LIT>' ) ) : <EOL> name = res . attrib [ '<STR_LIT>' ] <EOL> description = '<STR_LIT>' <EOL> type = '<STR_LIT>' <EOL> omni_res = OmniResource ( name , description , type ) <EOL> available = True <EOL> omni_res . set_allocated ( not ( available ) ) <EOL> omni_res [ '<STR_LIT>' ] = ET . tostring ( res ) <EOL> id = res . attrib [ '<STR_LIT>' ] <EOL> ospec . add_resource ( id , omni_res ) <EOL> def rspec_to_omnispec ( urn , rspec ) : <EOL> ospec = OmniSpec ( "<STR_LIT>" , urn ) <EOL> doc = ET . fromstring ( rspec ) <EOL> add_options ( ospec , doc ) <EOL> return ospec <EOL> def add_node ( root , onode ) : <EOL> root . append ( ET . fromstring ( onode [ '<STR_LIT>' ] ) ) <EOL> def add_link ( root , olink ) : <EOL> root . append ( ET . fromstring ( olink [ '<STR_LIT>' ] ) ) <EOL> def wannabe_omnispec_to_rspec ( omnispec , filter_allocated ) : <EOL> root = ET . Element ( '<STR_LIT>' ) <EOL> for id , r in omnispec . get_resources ( ) . items ( ) : <EOL> if filter_allocated and not r . get_allocate ( ) : <EOL> continue <EOL> res_type = r . get_type ( ) <EOL> if res_type == '<STR_LIT>' : <EOL> add_node ( root , r ) <EOL> elif res_type == '<STR_LIT>' : <EOL> add_link ( root , r ) <EOL> else : <EOL> raise ( Exception ( '<STR_LIT>' + res_type ) ) <EOL> return ET . tostring ( root ) <EOL> def get_options_rspec ( omnispec , filter_allocated ) : <EOL> root = ET . Element ( '<STR_LIT>' ) <EOL> root . set ( '<STR_LIT>' , "<STR_LIT>" ) <EOL> for id , r in omnispec . get_resources ( ) . items ( ) : <EOL> if filter_allocated and not r . get_allocate ( ) : <EOL> continue <EOL> res_type = r . get_type ( ) <EOL> if res_type == '<STR_LIT>' : <EOL> for i in range ( <NUM_LIT:0> , r [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) : <EOL> node = ET . SubElement ( root , '<STR_LIT>' , virtual_id = '<STR_LIT>' % i , virtualization_type = '<STR_LIT>' , <EOL> startup_command = r [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> if r [ '<STR_LIT>' ] [ '<STR_LIT>' ] != '<STR_LIT>' : <EOL> ET . SubElement ( node , '<STR_LIT>' , name = '<STR_LIT>' % r [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> else : <EOL> raise ( Exception ( '<STR_LIT>' + res_type ) ) <EOL> return ET . tostring ( root ) <EOL> def omnispec_to_rspec ( omnispec , filter_allocated ) : <EOL> """<STR_LIT>""" <EOL> return get_options_rspec ( omnispec , filter_allocated ) </s>
