<s> from __future__ import division <EOL> from vistrails . core . system import _defaultPkgPrefix , get_vistrails_basic_pkg_id , get_module_registry <EOL> def load_cls ( cls_item , prefix = None ) : <EOL> path = None <EOL> if isinstance ( cls_item , basestring ) : <EOL> ( path , cls_name ) = cls_item . split ( '<STR_LIT::>' ) [ : <NUM_LIT:2> ] <EOL> elif isinstance ( cls_item , tuple ) : <EOL> ( path , cls_name ) = cls_item <EOL> if path is not None : <EOL> try : <EOL> module = __import__ ( path , globals ( ) , locals ( ) , [ cls_name ] ) <EOL> except ImportError : <EOL> if prefix is None : <EOL> raise <EOL> path = '<STR_LIT:.>' . join ( [ prefix , path ] ) <EOL> module = __import__ ( path , globals ( ) , locals ( ) , [ cls_name ] ) <EOL> return getattr ( module , cls_name ) <EOL> return cls_item <EOL> def create_descriptor_string ( package , name , namespace = None , <EOL> use_package = True ) : <EOL> package_str = "<STR_LIT>" <EOL> namespace_str = "<STR_LIT>" <EOL> if use_package : <EOL> package_str = "<STR_LIT>" % package <EOL> if namespace : <EOL> namespace_str = "<STR_LIT>" % namespace <EOL> return "<STR_LIT>" % ( package_str , namespace_str , name ) <EOL> def parse_descriptor_string ( d_string , cur_package = None ) : <EOL> """<STR_LIT>""" <EOL> package = '<STR_LIT>' <EOL> qual_name = '<STR_LIT>' <EOL> name = '<STR_LIT>' <EOL> namespace = None <EOL> parts = d_string . strip ( ) . split ( '<STR_LIT::>' , <NUM_LIT:1> ) <EOL> if len ( parts ) > <NUM_LIT:1> : <EOL> qual_name = parts [ <NUM_LIT:1> ] <EOL> if '<STR_LIT:.>' in parts [ <NUM_LIT:0> ] : <EOL> package = parts [ <NUM_LIT:0> ] <EOL> else : <EOL> package = '<STR_LIT>' % ( _defaultPkgPrefix , parts [ <NUM_LIT:0> ] ) <EOL> else : <EOL> qual_name = d_string <EOL> if cur_package is None : <EOL> reg = get_module_registry ( ) <EOL> if reg . _current_package is not None : <EOL> package = reg . _current_package . identifier <EOL> else : <EOL> package = get_vistrails_basic_pkg_id ( ) <EOL> else : <EOL> package = cur_package <EOL> qual_parts = qual_name . rsplit ( '<STR_LIT:|>' , <NUM_LIT:1> ) <EOL> if len ( qual_parts ) > <NUM_LIT:1> : <EOL> namespace , name = qual_parts <EOL> else : <EOL> name = qual_name <EOL> return ( package , name , namespace ) <EOL> def parse_port_spec_item_string ( spec , cur_package = None ) : <EOL> spec = spec . strip ( ) <EOL> spec_arr = spec . split ( '<STR_LIT::>' , <NUM_LIT:2> ) <EOL> if len ( spec_arr ) > <NUM_LIT:2> : <EOL> spec = '<STR_LIT>' % ( spec_arr [ <NUM_LIT:0> ] , spec_arr [ <NUM_LIT:2> ] , spec_arr [ <NUM_LIT:1> ] ) <EOL> return parse_descriptor_string ( spec , cur_package ) <EOL> def create_port_spec_item_string ( package , name , namespace = None , <EOL> old_style = False ) : <EOL> if old_style : <EOL> if namespace : <EOL> namespace = '<STR_LIT::>' + namespace <EOL> return '<STR_LIT>' % ( package , name , namespace ) <EOL> else : <EOL> return create_descriptor_string ( package , name , namespace ) <EOL> def parse_port_spec_string ( p_string , cur_package = None ) : <EOL> port_spec = p_string . strip ( ) <EOL> if port_spec . startswith ( '<STR_LIT:(>' ) : <EOL> port_spec = port_spec [ <NUM_LIT:1> : ] <EOL> if port_spec . endswith ( '<STR_LIT:)>' ) : <EOL> port_spec = port_spec [ : - <NUM_LIT:1> ] <EOL> if port_spec . strip ( ) == '<STR_LIT>' : <EOL> return [ ] <EOL> specs_list = [ ] <EOL> for spec in port_spec . split ( '<STR_LIT:U+002C>' ) : <EOL> specs_list . append ( parse_port_spec_item_string ( spec , <EOL> cur_package ) ) <EOL> return specs_list <EOL> def create_port_spec_string ( specs_list , old_style = False ) : <EOL> spec_items = [ ] <EOL> for specs in specs_list : <EOL> if len ( specs ) == <NUM_LIT:3> : <EOL> pkg , name , ns = specs <EOL> elif len ( specs ) == <NUM_LIT:2> : <EOL> pkg , name = specs <EOL> ns = None <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" <EOL> "<STR_LIT>" % len ( specs ) ) <EOL> spec_items . append ( create_port_spec_item_string ( pkg , name , ns , <EOL> old_style ) ) <EOL> return '<STR_LIT>' % '<STR_LIT:U+002C>' . join ( spec_items ) <EOL> def expand_port_spec_string ( p_string , cur_package = None , <EOL> old_style = False ) : <EOL> specs_list = parse_port_spec_string ( p_string , cur_package ) <EOL> return create_port_spec_string ( specs_list , old_style ) <EOL> def make_modules_dict ( * dcts , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> namespace = kwargs . pop ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if kwargs : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> if namespace : <EOL> build_namespace = lambda n : '<STR_LIT>' % ( namespace , n ) <EOL> else : <EOL> build_namespace = lambda n : n <EOL> dct = dict ( ) <EOL> for d in dcts : <EOL> if not isinstance ( d , dict ) : <EOL> d = { '<STR_LIT>' : d } <EOL> for subname , sublist in d . iteritems ( ) : <EOL> if subname : <EOL> name = build_namespace ( subname ) <EOL> else : <EOL> name = namespace <EOL> dct . setdefault ( name , [ ] ) . extend ( sublist ) <EOL> return dct <EOL> import unittest <EOL> class UnorderedList ( object ) : <EOL> def __init__ ( self , * elems ) : <EOL> self . _set = set ( elems ) <EOL> def __eq__ ( self , other ) : <EOL> return set ( other ) == self . _set <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % '<STR_LIT:U+002CU+0020>' . join ( repr ( e ) for e in self . _set ) <EOL> def __repr__ ( self ) : <EOL> return str ( self ) <EOL> class TestModulesDict ( unittest . TestCase ) : <EOL> def test_modulesdict ( self ) : <EOL> ul = UnorderedList <EOL> pkg_a_aa = { '<STR_LIT>' : [ <NUM_LIT:1> , <NUM_LIT:2> ] , '<STR_LIT>' : [ <NUM_LIT:3> ] } <EOL> pkg_a_ab = { '<STR_LIT>' : [ <NUM_LIT:4> , <NUM_LIT:5> ] } <EOL> pkg_a = make_modules_dict ( pkg_a_aa , pkg_a_ab , [ <NUM_LIT:12> , <NUM_LIT> ] , namespace = '<STR_LIT:a>' ) <EOL> self . assertEqual ( set ( pkg_a . keys ( ) ) , <EOL> set ( [ '<STR_LIT:a>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) ) <EOL> pkg_b = { '<STR_LIT:b>' : [ <NUM_LIT:6> , <NUM_LIT:7> ] } <EOL> glob = [ <NUM_LIT:10> , <NUM_LIT:11> ] <EOL> more = { '<STR_LIT>' : [ <NUM_LIT:8> , <NUM_LIT:9> ] } <EOL> pkg = make_modules_dict ( pkg_a , pkg_b , glob , more ) <EOL> self . assertEqual ( <EOL> pkg , <EOL> { <EOL> '<STR_LIT>' : ul ( <NUM_LIT:10> , <NUM_LIT:11> ) , <EOL> '<STR_LIT:a>' : ul ( <NUM_LIT:12> , <NUM_LIT> ) , <EOL> '<STR_LIT>' : ul ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:8> , <NUM_LIT:9> ) , <EOL> '<STR_LIT>' : ul ( <NUM_LIT:3> ) , <EOL> '<STR_LIT>' : ul ( <NUM_LIT:4> , <NUM_LIT:5> ) , <EOL> '<STR_LIT:b>' : ul ( <NUM_LIT:6> , <NUM_LIT:7> ) } ) </s>
