<s> def convert_to_vt_type ( t ) : <EOL> vt_type_dict = { '<STR_LIT:str>' : '<STR_LIT>' , <EOL> '<STR_LIT:float>' : '<STR_LIT>' , <EOL> '<STR_LIT:int>' : '<STR_LIT>' , <EOL> '<STR_LIT:bool>' : '<STR_LIT>' , <EOL> } <EOL> if vt_type_dict . has_key ( t ) : <EOL> return vt_type_dict [ t ] <EOL> else : <EOL> print "<STR_LIT>" % t <EOL> return None <EOL> do_not_cache_me = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> class CDATModule : <EOL> _extra_modules = [ ] <EOL> _extra_vistrails_modules = { } <EOL> def __init__ ( self , author = None , language = None , type = None , url = None , <EOL> codepath = None , version = None , actions = None ) : <EOL> self . _author = author <EOL> self . _language = language <EOL> self . _type = type <EOL> self . _url = url <EOL> self . _codepath = codepath <EOL> self . _version = version <EOL> self . _actions = actions <EOL> ( self . _namespace , self . _name ) = self . split ( codepath ) <EOL> @ staticmethod <EOL> def split ( codepath ) : <EOL> dot = codepath . rfind ( '<STR_LIT:.>' ) <EOL> if dot != - <NUM_LIT:1> : <EOL> name = codepath [ dot + <NUM_LIT:1> : ] <EOL> data = codepath [ : dot ] <EOL> namespace = data . replace ( '<STR_LIT:.>' , '<STR_LIT:|>' ) <EOL> else : <EOL> name = codepath <EOL> namespace = codepath <EOL> return ( namespace , name ) <EOL> def write_extra_module_definition ( self , lines , name ) : <EOL> lines . append ( "<STR_LIT>" % ( name , name ) ) <EOL> @ staticmethod <EOL> def write_extra_module_definitions_init ( lines ) : <EOL> lines . append ( "<STR_LIT>" ) <EOL> lines . append ( "<STR_LIT>" ) <EOL> lines . append ( "<STR_LIT>" ) <EOL> @ staticmethod <EOL> def write_extra_module_definitions ( lines ) : <EOL> for t in CDATModule . _extra_modules : <EOL> namespace , name = CDATModule . split ( t ) <EOL> lines . append ( "<STR_LIT>" % ( name , name ) ) <EOL> lines . append ( "<STR_LIT>" % ( t , name ) ) <EOL> CDATModule . _extra_vistrails_modules [ name ] = namespace <EOL> lines . append ( "<STR_LIT>" ) <EOL> @ staticmethod <EOL> def register_extra_vistrails_modules ( lines , ident = '<STR_LIT>' ) : <EOL> for ( name , namespace ) in CDATModule . _extra_vistrails_modules . iteritems ( ) : <EOL> lines . append ( ident + "<STR_LIT>" % ( name , <EOL> namespace ) ) <EOL> def register_extra_vistrails_module ( self , lines , name , ident = '<STR_LIT>' ) : <EOL> lines . append ( ident + "<STR_LIT>" % ( name , <EOL> self . _namespace ) ) <EOL> def write_module_definitions ( self , lines ) : <EOL> for a in self . _actions : <EOL> a . write_module_definition ( lines ) <EOL> def register_vistrails_modules ( self , lines ) : <EOL> for a in self . _actions : <EOL> a . register_itself ( lines , self . _namespace ) <EOL> def build_vistrails_modules_dict ( self ) : <EOL> for a in self . _actions : <EOL> types = a . check_output_types ( ) <EOL> for t in types : <EOL> if t not in CDATModule . _extra_modules : <EOL> CDATModule . _extra_modules . append ( t ) <EOL> def add_extra_input_port_to_all_modules ( self , lines , port_name , port_type , <EOL> doc , optional = False ) : <EOL> lines . append ( "<STR_LIT>" ) <EOL> for a in self . _actions : <EOL> a . register_extra_input_port ( port_name , port_type , lines , doc , <EOL> optional ) <EOL> def add_extra_output_port_to_all_modules ( self , lines , port_name , port_type , <EOL> doc , optional = False ) : <EOL> lines . append ( "<STR_LIT>" ) <EOL> for a in self . _actions : <EOL> a . register_extra_output_port ( port_name , port_type , lines , doc , <EOL> optional ) <EOL> class CDATAction : <EOL> def __init__ ( self , name = None , type = None , options = None , inputs = None , <EOL> outputs = None , doc = None ) : <EOL> self . _name = name <EOL> self . _type = type <EOL> self . _options = options <EOL> self . _inputs = inputs <EOL> self . _outputs = outputs <EOL> self . _doc = doc <EOL> def write_module_definition ( self , lines , ident = '<STR_LIT>' , compute_method = None ) : <EOL> def write_compute_method ( self , lines , ident ) : <EOL> lines . append ( ident + "<STR_LIT>" ) <EOL> lines . append ( ident + "<STR_LIT>" ) <EOL> lines . append ( ident + "<STR_LIT>" ) <EOL> lines . append ( ident + "<STR_LIT>" ) <EOL> lines . append ( ident + "<STR_LIT>" ) <EOL> lines . append ( ident + "<STR_LIT>" ) <EOL> for inp in self . _inputs : <EOL> lines . append ( ident + "<STR_LIT>" % inp . _name ) <EOL> for inst in inp . _valid_instances : <EOL> if inp . _valid_instances . index ( inst ) == <NUM_LIT:0> : <EOL> lines . append ( ident + "<STR_LIT>" % inst ) <EOL> lines . append ( ident + "<STR_LIT>" % ( inp . _name , inst ) ) <EOL> lines . append ( ident + "<STR_LIT>" % inp . _name ) <EOL> else : <EOL> lines . append ( ident + "<STR_LIT>" % inst ) <EOL> lines . append ( ident + "<STR_LIT>" % ( inp . _name , inst ) ) <EOL> lines . append ( ident + "<STR_LIT>" % inp . _name ) <EOL> if inp . _required : <EOL> lines . append ( "<STR_LIT:\n>" + ident + "<STR_LIT>" % inp . _name ) <EOL> lines . append ( ident + "<STR_LIT>" % inp . _name ) <EOL> lines . append ( ident + "<STR_LIT>" % inp . _name ) <EOL> lines . append ( "<STR_LIT:\n>" + ident + "<STR_LIT>" ) <EOL> lines . append ( ident + "<STR_LIT>" ) <EOL> for opt in self . _options : <EOL> for inst in opt . _valid_instances : <EOL> if opt . _valid_instances . index ( inst ) == <NUM_LIT:0> : <EOL> lines . append ( ident + "<STR_LIT>" % inst ) <EOL> lines . append ( ident + "<STR_LIT>" % ( opt . _name , inst ) ) <EOL> else : <EOL> lines . append ( ident + "<STR_LIT>" % inst ) <EOL> lines . append ( ident + "<STR_LIT>" % ( opt . _name , inst ) ) <EOL> lines . append ( ident + "<STR_LIT>" ) <EOL> lines . append ( ident + "<STR_LIT>" ) <EOL> lines . append ( ident + "<STR_LIT>" % self . _name ) <EOL> lines . append ( ident + "<STR_LIT>" % ( self . _outputs [ <NUM_LIT:0> ] . _name ) ) <EOL> lines . append ( ident + "<STR_LIT>" ) <EOL> lines . append ( "<STR_LIT:\n>" ) <EOL> if self . _name in do_not_cache_me : <EOL> lines . append ( ident + "<STR_LIT>" % self . _name ) <EOL> else : <EOL> lines . append ( ident + "<STR_LIT>" % self . _name ) <EOL> lines . append ( ident + '<STR_LIT>' % self . _doc ) <EOL> lines . append ( ident + '<STR_LIT>' ) <EOL> if not compute_method : <EOL> write_compute_method ( self , lines , ident = "<STR_LIT:U+0020>" ) <EOL> else : <EOL> lines . extend ( compute_method ) <EOL> def register_itself ( self , lines , namespace ) : <EOL> lines . append ( "<STR_LIT>" % self . _name ) <EOL> lines . append ( "<STR_LIT>" % ( self . _name , namespace ) ) <EOL> for inp in self . _inputs : <EOL> inp . write_input_ports ( self . _name , lines ) <EOL> for opt in self . _options : <EOL> opt . write_input_ports ( self . _name , lines , True , force = False ) <EOL> for out in self . _outputs : <EOL> out . write_output_ports ( self . _name , lines , force = True ) <EOL> def check_output_types ( self ) : <EOL> types = [ ] <EOL> for out in self . _outputs : <EOL> if out . _instance [ <NUM_LIT:0> ] not in types : <EOL> types . append ( out . _instance [ <NUM_LIT:0> ] ) <EOL> return types <EOL> def register_extra_input_port ( self , port_name , port_type , lines , doc , <EOL> optional = False ) : <EOL> self . _write_port ( '<STR_LIT:input>' , port_name , port_type , lines , doc , optional ) <EOL> def register_extra_output_port ( self , port_name , port_type , lines , doc , <EOL> optional = False ) : <EOL> self . _write_port ( '<STR_LIT>' , port_name , port_type , lines , doc , optional ) <EOL> def _write_port ( self , io_type , port_name , port_type , <EOL> lines , doc , optional = False ) : <EOL> lines . append ( "<STR_LIT>" % ( io_type , <EOL> self . _name , <EOL> port_name ) ) <EOL> lines . append ( "<STR_LIT:U+0020>" ) <EOL> lines . append ( "<STR_LIT>" % port_type ) <EOL> lines . append ( "<STR_LIT:U+0020>" ) <EOL> if not optional : <EOL> lines . append ( "<STR_LIT>" % doc ) <EOL> else : <EOL> lines . append ( "<STR_LIT>" % doc ) <EOL> class CDATItem : <EOL> def __init__ ( self , tag = None , doc = None , instance = None , required = False ) : <EOL> self . _name = tag <EOL> self . _doc = doc <EOL> self . _instance = [ i . strip ( "<STR_LIT>" ) for i in instance . split ( '<STR_LIT:/>' ) ] <EOL> self . _valid_instances = [ ] <EOL> if required == None : <EOL> self . _required = False <EOL> else : <EOL> self . _required = required <EOL> def write_input_ports ( self , module_name , lines , optional = False , force = False ) : <EOL> self . _write_ports ( '<STR_LIT:input>' , module_name , lines , optional , force ) <EOL> def write_output_ports ( self , module_name , lines , optional = False , force = False ) : <EOL> self . _write_ports ( '<STR_LIT>' , module_name , lines , optional , force ) <EOL> def _write_ports ( self , port_type , module_name , lines , optional = False , <EOL> force = False ) : <EOL> if len ( self . _instance ) == <NUM_LIT:1> : <EOL> type = convert_to_vt_type ( self . _instance [ <NUM_LIT:0> ] ) <EOL> if type == None and self . _instance [ <NUM_LIT:0> ] in CDATModule . _extra_modules : <EOL> force = True <EOL> if force and type == None : <EOL> type = "<STR_LIT>" % self . _instance [ <NUM_LIT:0> ] <EOL> if type != None : <EOL> self . _valid_instances . append ( self . _name ) <EOL> lines . append ( "<STR_LIT>" % ( port_type , <EOL> module_name , <EOL> self . _name ) ) <EOL> lines . append ( "<STR_LIT:U+0020>" ) <EOL> lines . append ( "<STR_LIT>" % type ) <EOL> lines . append ( "<STR_LIT:U+0020>" ) <EOL> if not optional : <EOL> lines . append ( "<STR_LIT>" % self . _doc ) <EOL> else : <EOL> lines . append ( "<STR_LIT>" % self . _doc ) <EOL> else : <EOL> count = <NUM_LIT:0> <EOL> for i in xrange ( len ( self . _instance ) ) : <EOL> type = convert_to_vt_type ( self . _instance [ i ] ) <EOL> new_force = force <EOL> if type == None and self . _instance [ i ] in CDATModule . _extra_modules : <EOL> new_force = True <EOL> if new_force and type == None : <EOL> type = "<STR_LIT>" % self . _instance [ i ] <EOL> if type != None : <EOL> name = "<STR_LIT>" % ( self . _name , count ) <EOL> self . _valid_instances . append ( name ) <EOL> count += <NUM_LIT:1> <EOL> lines . append ( "<STR_LIT>" % ( module_name , <EOL> name ) ) <EOL> lines . append ( "<STR_LIT:U+0020>" ) <EOL> lines . append ( "<STR_LIT>" % type ) <EOL> lines . append ( "<STR_LIT:U+0020>" ) <EOL> if not optional : <EOL> lines . append ( "<STR_LIT>" % self . _doc ) <EOL> else : <EOL> lines . append ( "<STR_LIT>" % self . _doc ) <EOL> class CDATOption ( CDATItem ) : <EOL> def __init__ ( self , tag = None , default = None , doc = None , instance = None ) : <EOL> CDATItem . __init__ ( self , tag , doc , instance , False ) <EOL> self . _default = default <EOL> class CDATPort ( CDATItem ) : <EOL> def __init__ ( self , tag = None , doc = None , instance = None , position = None , required = False ) : <EOL> CDATItem . __init__ ( self , tag , doc , instance , required ) <EOL> self . _position = position </s>
