<s> from __future__ import division <EOL> import contextlib <EOL> from itertools import izip <EOL> import subprocess <EOL> import vistrails . core . application <EOL> import vistrails . core . db . action <EOL> import vistrails . core . db . io <EOL> from vistrails . core . db . locator import UntitledLocator , FileLocator <EOL> from vistrails . core . interpreter . default import get_default_interpreter <EOL> from vistrails . core . modules . module_registry import get_module_registry <EOL> from vistrails . core . modules . package import Package as _Package <EOL> from vistrails . core . modules . sub_module import get_port_spec_info <EOL> from vistrails . core . modules . utils import parse_port_spec_string <EOL> from vistrails . core . packagemanager import get_package_manager <EOL> from vistrails . core . system import get_vistrails_basic_pkg_id <EOL> from vistrails . core . utils import DummyView <EOL> from vistrails . core . vistrail . controller import VistrailController <EOL> from vistrails . core . vistrail . pipeline import Pipeline as _Pipeline <EOL> from vistrails . core . vistrail . vistrail import Vistrail as _Vistrail <EOL> from vistrails . db . domain import IdScope <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] <EOL> class NoSuchVersion ( KeyError ) : <EOL> """<STR_LIT>""" <EOL> class NoSuchPackage ( ValueError ) : <EOL> """<STR_LIT>""" <EOL> is_initialized = False <EOL> _application = None <EOL> def initialize ( ) : <EOL> """<STR_LIT>""" <EOL> global is_initialized <EOL> global _application <EOL> if is_initialized : <EOL> return False <EOL> _application = vistrails . core . application . init ( <EOL> options_dict = { <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : True } , <EOL> args = [ ] ) <EOL> is_initialized = True <EOL> return True <EOL> def ipython_mode ( use_notebook = True ) : <EOL> """<STR_LIT>""" <EOL> if use_notebook : <EOL> try : <EOL> import IPython . core . display <EOL> except ImportError : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> from vistrails . core . modules . output_modules import IPythonMode <EOL> IPythonMode . notebook_override = use_notebook <EOL> class Vistrail ( object ) : <EOL> """<STR_LIT>""" <EOL> _current_pipeline = None <EOL> _html = None <EOL> def __init__ ( self , arg = None ) : <EOL> initialize ( ) <EOL> if arg is None : <EOL> locator = UntitledLocator ( ) <EOL> loaded_objs = vistrails . core . db . io . load_vistrail ( locator ) <EOL> self . controller = VistrailController ( loaded_objs [ <NUM_LIT:0> ] , locator , <EOL> * loaded_objs [ <NUM_LIT:1> : ] ) <EOL> elif isinstance ( arg , ( _Pipeline , Pipeline ) ) : <EOL> if isinstance ( arg , Pipeline ) : <EOL> pipeline = arg . pipeline <EOL> else : <EOL> pipeline = arg <EOL> vistrail = _Vistrail ( ) <EOL> ops = [ ] <EOL> for module in pipeline . module_list : <EOL> ops . append ( ( '<STR_LIT>' , module ) ) <EOL> for connection in pipeline . connection_list : <EOL> ops . append ( ( '<STR_LIT>' , connection ) ) <EOL> action = vistrails . core . db . action . create_action ( ops ) <EOL> vistrail . add_action ( action , <NUM_LIT:0> L ) <EOL> vistrail . update_id_scope ( ) <EOL> vistrail . change_description ( "<STR_LIT>" , <NUM_LIT:0> L ) <EOL> self . controller = VistrailController ( vistrail , UntitledLocator ( ) ) <EOL> elif isinstance ( arg , VistrailController ) : <EOL> self . controller = arg <EOL> elif isinstance ( arg , basestring ) : <EOL> raise TypeError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % type ( arg ) . __name__ ) <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" <EOL> "<STR_LIT>" % type ( arg ) . __name__ ) <EOL> def get_pipeline ( self , version ) : <EOL> """<STR_LIT>""" <EOL> vistrail = self . controller . vistrail <EOL> if isinstance ( version , ( int , long ) ) : <EOL> if not vistrail . db_has_action_with_id ( version ) : <EOL> raise NoSuchVersion ( "<STR_LIT>" % <EOL> version ) <EOL> return Pipeline ( vistrail . getPipelineVersionNumber ( version ) ) <EOL> elif isinstance ( version , basestring ) : <EOL> if not vistrail . has_tag_str ( version ) : <EOL> raise NoSuchVersion ( "<STR_LIT>" % version ) <EOL> return Pipeline ( vistrail . getPipelineVersionName ( version ) ) <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" <EOL> "<STR_LIT>" % type ( version ) . __name__ ) <EOL> def select_version ( self , version ) : <EOL> """<STR_LIT>""" <EOL> vistrail = self . controller . vistrail <EOL> if isinstance ( version , ( int , long ) ) : <EOL> if not vistrail . db_has_action_with_id ( version ) : <EOL> raise NoSuchVersion ( "<STR_LIT>" % <EOL> version ) <EOL> elif ( isinstance ( version , basestring ) ) : <EOL> if not vistrail . has_tag_str ( version ) : <EOL> raise NoSuchVersion ( "<STR_LIT>" % version ) <EOL> version = vistrail . get_tag_str ( version ) . action_id <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" <EOL> "<STR_LIT>" % type ( version ) . __name__ ) <EOL> self . controller . do_version_switch ( version ) <EOL> self . _current_pipeline = None <EOL> self . _html = None <EOL> def select_latest_version ( self ) : <EOL> """<STR_LIT>""" <EOL> self . controller . do_version_switch ( <EOL> self . controller . get_latest_version_in_graph ( ) ) <EOL> self . _current_pipeline = None <EOL> self . _html = None <EOL> @ property <EOL> def current_pipeline ( self ) : <EOL> if self . _current_pipeline is None : <EOL> self . _current_pipeline = Pipeline ( <EOL> self . controller . current_pipeline , <EOL> vistrail = ( self , self . current_version ) ) <EOL> return self . _current_pipeline <EOL> @ property <EOL> def current_version ( self ) : <EOL> return self . controller . current_version <EOL> def set_tag ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> if len ( args ) == <NUM_LIT:1> : <EOL> version , ( tag , ) = self . controller . current_version , args <EOL> elif len ( args ) == <NUM_LIT:2> : <EOL> version , tag = args <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" % <EOL> len ( args ) ) <EOL> if isinstance ( version , ( int , long ) ) : <EOL> if not self . controller . vistrail . db_has_action_with_id ( version ) : <EOL> raise NoSuchVersion ( "<STR_LIT>" % <EOL> version ) <EOL> elif isinstance ( version , basestring ) : <EOL> if not self . controller . vistrail . has_tag_str ( version ) : <EOL> raise NoSuchVersion ( "<STR_LIT>" % version ) <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" <EOL> "<STR_LIT>" % type ( version ) . __name__ ) <EOL> self . controller . vistrail . set_tag ( version , tag ) <EOL> def tag ( self , tag ) : <EOL> """<STR_LIT>""" <EOL> self . set_tag ( tag ) <EOL> def execute ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return self . current_pipeline . execute ( * args , ** kwargs ) <EOL> @ property <EOL> def changed ( self ) : <EOL> return self . controller . changed <EOL> def __repr__ ( self ) : <EOL> version_nb = self . controller . current_version <EOL> if self . controller . vistrail . has_tag ( version_nb ) : <EOL> version = "<STR_LIT>" % ( <EOL> version_nb , <EOL> self . controller . vistrail . get_tag ( version_nb ) ) <EOL> else : <EOL> version = version_nb <EOL> return "<STR_LIT>" % ( <EOL> self . __class__ . __name__ , <EOL> self . controller . name , <EOL> version , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) [ self . controller . changed ] ) <EOL> def _repr_html_ ( self ) : <EOL> if self . _html is None : <EOL> import cgi <EOL> try : <EOL> from cStringIO import StringIO <EOL> except ImportError : <EOL> from StringIO import StringIO <EOL> self . _html = '<STR_LIT>' <EOL> stream = StringIO ( ) <EOL> self . controller . recompute_terse_graph ( ) <EOL> self . controller . save_version_graph ( <EOL> stream , <EOL> highlight = self . controller . current_version ) <EOL> stream . seek ( <NUM_LIT:0> ) <EOL> dot = stream . read ( ) <EOL> try : <EOL> proc = subprocess . Popen ( [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> stdin = subprocess . PIPE , <EOL> stdout = subprocess . PIPE ) <EOL> svg , _ = proc . communicate ( dot ) <EOL> if proc . wait ( ) == <NUM_LIT:0> : <EOL> self . _html += svg <EOL> except OSError : <EOL> pass <EOL> self . _html += '<STR_LIT>' + cgi . escape ( repr ( self ) ) + '<STR_LIT>' <EOL> return self . _html <EOL> def get_inputoutput_name ( module ) : <EOL> for function in module . functions : <EOL> if function . name == '<STR_LIT:name>' : <EOL> if len ( function . params ) == <NUM_LIT:1> : <EOL> return function . params [ <NUM_LIT:0> ] . strValue <EOL> return None <EOL> class Pipeline ( object ) : <EOL> """<STR_LIT>""" <EOL> vistrail = None <EOL> version = None <EOL> _inputs = None <EOL> _outputs = None <EOL> _html = None <EOL> def __init__ ( self , pipeline = None , vistrail = None ) : <EOL> initialize ( ) <EOL> if pipeline is None : <EOL> self . pipeline = _Pipeline ( ) <EOL> elif isinstance ( pipeline , _Pipeline ) : <EOL> self . pipeline = pipeline <EOL> elif isinstance ( pipeline , basestring ) : <EOL> raise TypeError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % type ( pipeline ) . __name__ ) <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" <EOL> "<STR_LIT>" % type ( pipeline ) . __name__ ) <EOL> if vistrail is not None : <EOL> if ( isinstance ( vistrail , tuple ) and len ( vistrail ) == <NUM_LIT:2> and <EOL> isinstance ( vistrail [ <NUM_LIT:0> ] , Vistrail ) ) : <EOL> self . vistrail , self . version = vistrail <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" <EOL> "<STR_LIT>" % type ( vistrail ) . __name__ ) <EOL> @ property <EOL> def modules ( self ) : <EOL> for module in self . pipeline . module_list : <EOL> yield Module ( descriptor = module . module_descriptor , <EOL> module_id = module . id , <EOL> pipeline = self ) <EOL> def execute ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> sinks = set ( ) <EOL> inputs = { } <EOL> reg = get_module_registry ( ) <EOL> InputPort_desc = reg . get_descriptor_by_name ( <EOL> get_vistrails_basic_pkg_id ( ) , <EOL> '<STR_LIT>' ) <EOL> for arg in args : <EOL> if isinstance ( arg , ModuleValuePair ) : <EOL> if arg . module . id in inputs : <EOL> raise ValueError ( <EOL> "<STR_LIT>" % <EOL> get_inputoutput_name ( arg . module ) ) <EOL> if not reg . is_descriptor_subclass ( arg . module . module_descriptor , <EOL> InputPort_desc ) : <EOL> raise ValueError ( "<STR_LIT>" % <EOL> arg . module . id ) <EOL> inputs [ arg . module . id ] = arg . value <EOL> elif isinstance ( arg , Module ) : <EOL> sinks . add ( arg . module_id ) <EOL> for key , value in kwargs . iteritems ( ) : <EOL> key = self . get_input ( key ) <EOL> if key . module_id in inputs : <EOL> raise ValueError ( "<STR_LIT>" % <EOL> get_inputoutput_name ( key . module ) ) <EOL> inputs [ key . module_id ] = value <EOL> reason = "<STR_LIT>" <EOL> sinks = sinks or None <EOL> if ( not inputs and self . vistrail is not None and <EOL> self . vistrail . current_version == self . version ) : <EOL> controller = self . vistrail . controller <EOL> results , changed = controller . execute_workflow_list ( [ [ <EOL> controller . locator , <EOL> self . version , <EOL> self . pipeline , <EOL> DummyView ( ) , <EOL> None , <EOL> None , <EOL> reason , <EOL> sinks , <EOL> None , <EOL> ] ] ) <EOL> result , = results <EOL> else : <EOL> pipeline = self . pipeline <EOL> if inputs : <EOL> id_scope = IdScope ( <NUM_LIT:1> ) <EOL> pipeline = pipeline . do_copy ( False , id_scope ) <EOL> id_scope . getNewId = lambda t , g = id_scope . getNewId : - g ( t ) <EOL> create_module = VistrailController . create_module_from_descriptor_static <EOL> create_function = VistrailController . create_function_static <EOL> create_connection = VistrailController . create_connection_static <EOL> for module_id , values in inputs . iteritems ( ) : <EOL> module = pipeline . modules [ module_id ] <EOL> if not isinstance ( values , ( list , tuple ) ) : <EOL> values = [ values ] <EOL> _ , sigstrings , _ , _ , _ = get_port_spec_info ( pipeline , module ) <EOL> sigstrings = parse_port_spec_string ( sigstrings ) <EOL> values = [ reg . convert_port_val ( val , sigstring , None ) <EOL> for val , sigstring in izip ( values , sigstrings ) ] <EOL> if len ( values ) == <NUM_LIT:1> : <EOL> constant_desc = reg . get_descriptor_by_name ( <EOL> * sigstrings [ <NUM_LIT:0> ] ) <EOL> constant_mod = create_module ( id_scope , constant_desc ) <EOL> func = create_function ( id_scope , constant_mod , <EOL> '<STR_LIT:value>' , values ) <EOL> constant_mod . add_function ( func ) <EOL> pipeline . add_module ( constant_mod ) <EOL> conn = create_connection ( id_scope , <EOL> constant_mod , '<STR_LIT:value>' , <EOL> module , '<STR_LIT>' ) <EOL> pipeline . db_add_connection ( conn ) <EOL> else : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> interpreter = get_default_interpreter ( ) <EOL> result = interpreter . execute ( pipeline , <EOL> reason = reason , <EOL> sinks = sinks ) <EOL> if result . errors : <EOL> raise ExecutionErrors ( self , result ) <EOL> else : <EOL> return ExecutionResults ( self , result ) <EOL> def get_module ( self , module_id ) : <EOL> if isinstance ( module_id , ( int , long ) ) : <EOL> module = self . pipeline . modules [ module_id ] <EOL> elif isinstance ( module_id , basestring ) : <EOL> def desc ( mod ) : <EOL> if '<STR_LIT>' in mod . db_annotations_key_index : <EOL> return mod . get_annotation_by_key ( '<STR_LIT>' ) . value <EOL> else : <EOL> return None <EOL> modules = [ mod <EOL> for mod in self . pipeline . modules . itervalues ( ) <EOL> if desc ( mod ) == module_id ] <EOL> if not modules : <EOL> raise KeyError ( "<STR_LIT>" % module_id ) <EOL> elif len ( modules ) > <NUM_LIT:1> : <EOL> raise ValueError ( "<STR_LIT>" % <EOL> module_id ) <EOL> else : <EOL> module , = modules <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" <EOL> "<STR_LIT>" % type ( module_id ) . __name__ ) <EOL> return Module ( descriptor = module . module_descriptor , <EOL> module_id = module . id , <EOL> pipeline = self ) <EOL> def _get_inputs_or_outputs ( self , module_name ) : <EOL> reg = get_module_registry ( ) <EOL> desc = reg . get_descriptor_by_name ( <EOL> '<STR_LIT>' , <EOL> module_name ) <EOL> modules = { } <EOL> for module in self . pipeline . modules . itervalues ( ) : <EOL> if module . module_descriptor is desc : <EOL> name = get_inputoutput_name ( module ) <EOL> if name is not None : <EOL> modules [ name ] = module <EOL> return modules <EOL> def get_input ( self , name ) : <EOL> try : <EOL> module = self . _get_inputs_or_outputs ( '<STR_LIT>' ) [ name ] <EOL> except KeyError : <EOL> raise KeyError ( "<STR_LIT>" % name ) <EOL> else : <EOL> return Module ( descriptor = module . module_descriptor , <EOL> module_id = module . id , <EOL> pipeline = self ) <EOL> def get_output ( self , name ) : <EOL> try : <EOL> module = self . _get_inputs_or_outputs ( '<STR_LIT>' ) [ name ] <EOL> except KeyError : <EOL> raise KeyError ( "<STR_LIT>" % name ) <EOL> else : <EOL> return Module ( descriptor = module . module_descriptor , <EOL> module_id = module . id , <EOL> pipeline = self ) <EOL> @ property <EOL> def inputs ( self ) : <EOL> if self . _inputs is None : <EOL> self . _inputs = self . _get_inputs_or_outputs ( '<STR_LIT>' ) . keys ( ) <EOL> return self . _inputs <EOL> @ property <EOL> def outputs ( self ) : <EOL> if self . _outputs is None : <EOL> self . _outputs = self . _get_inputs_or_outputs ( '<STR_LIT>' ) . keys ( ) <EOL> return self . _outputs <EOL> def __repr__ ( self ) : <EOL> desc = "<STR_LIT>" % ( <EOL> self . __class__ . __name__ , <EOL> len ( self . pipeline . modules ) , <EOL> len ( self . pipeline . connections ) ) <EOL> inputs = self . inputs <EOL> if inputs : <EOL> desc += "<STR_LIT>" % "<STR_LIT:U+002CU+0020>" . join ( inputs ) <EOL> outputs = self . outputs <EOL> if outputs : <EOL> desc += "<STR_LIT>" % "<STR_LIT:U+002CU+0020>" . join ( outputs ) <EOL> return desc + "<STR_LIT:>>" <EOL> def _repr_html_ ( self ) : <EOL> if self . _html is None : <EOL> import cgi <EOL> try : <EOL> from cStringIO import StringIO <EOL> except ImportError : <EOL> from StringIO import StringIO <EOL> self . _html = '<STR_LIT>' <EOL> dot = [ '<STR_LIT>' ] <EOL> modules = dict ( ( mod . id , ( set ( ) , set ( ) ) ) <EOL> for mod in self . pipeline . module_list ) <EOL> for conn in self . pipeline . connection_list : <EOL> src , dst = conn . source , conn . destination <EOL> modules [ src . moduleId ] [ <NUM_LIT:1> ] . add ( src . name ) <EOL> modules [ dst . moduleId ] [ <NUM_LIT:0> ] . add ( dst . name ) <EOL> modules = dict ( ( mod_id , <EOL> ( dict ( ( n , i ) for i , n in enumerate ( mod_ports [ <NUM_LIT:0> ] ) ) , <EOL> dict ( ( n , i ) for i , n in enumerate ( mod_ports [ <NUM_LIT:1> ] ) ) ) ) <EOL> for mod_id , mod_ports in modules . iteritems ( ) ) <EOL> for mod , port_lists in modules . iteritems ( ) : <EOL> labels = [ ] <EOL> for port_type , ports in izip ( ( '<STR_LIT>' , '<STR_LIT>' ) , port_lists ) : <EOL> label = ( '<STR_LIT>' % ( port_type , port_num , cgi . escape ( port_name ) ) <EOL> for port_name , port_num in ports . iteritems ( ) ) <EOL> labels . append ( '<STR_LIT>' . join ( label ) ) <EOL> label = [ '<STR_LIT>' ] <EOL> if labels [ <NUM_LIT:0> ] : <EOL> label += [ '<STR_LIT>' , labels [ <NUM_LIT:0> ] , '<STR_LIT>' ] <EOL> mod_obj = self . pipeline . modules [ mod ] <EOL> if '<STR_LIT>' in mod_obj . db_annotations_key_index : <EOL> name = ( mod_obj . get_annotation_by_key ( '<STR_LIT>' ) <EOL> . value . strip ( ) ) <EOL> else : <EOL> name = mod_obj . label <EOL> label += [ '<STR_LIT>' , cgi . escape ( name ) , '<STR_LIT>' ] <EOL> if labels [ <NUM_LIT:1> ] : <EOL> label += [ '<STR_LIT>' , labels [ <NUM_LIT:1> ] , '<STR_LIT>' ] <EOL> label += [ '<STR_LIT>' ] <EOL> dot . append ( '<STR_LIT>' % ( mod , '<STR_LIT:\n>' . join ( label ) ) ) <EOL> dot . append ( '<STR_LIT>' ) <EOL> for conn in self . pipeline . connection_list : <EOL> src , dst = conn . source , conn . destination <EOL> dot . append ( '<STR_LIT>' % ( <EOL> src . moduleId , <EOL> modules [ src . moduleId ] [ <NUM_LIT:1> ] [ src . name ] , <EOL> dst . moduleId , <EOL> modules [ dst . moduleId ] [ <NUM_LIT:0> ] [ dst . name ] ) ) <EOL> dot . append ( '<STR_LIT:}>' ) <EOL> try : <EOL> proc = subprocess . Popen ( [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> stdin = subprocess . PIPE , <EOL> stdout = subprocess . PIPE ) <EOL> svg , _ = proc . communicate ( '<STR_LIT:\n>' . join ( dot ) ) <EOL> if proc . wait ( ) == <NUM_LIT:0> : <EOL> self . _html += svg <EOL> except OSError : <EOL> pass <EOL> self . _html += '<STR_LIT>' + cgi . escape ( repr ( self ) ) + '<STR_LIT>' <EOL> return self . _html <EOL> class ModuleClass ( type ) : <EOL> def __new__ ( cls , descriptor ) : <EOL> return type . __new__ ( cls , descriptor . name , ( object , ) , { } ) <EOL> def __init__ ( self , descriptor ) : <EOL> self . descriptor = descriptor <EOL> def __call__ ( self , * args , ** kwargs ) : <EOL> raise TypeError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . descriptor . name , <EOL> self . descriptor . identifier ) <EOL> __str__ = __repr__ <EOL> __unicode__ = __repr__ <EOL> def __instancecheck__ ( self , instance ) : <EOL> return ( isinstance ( instance , Module ) and <EOL> instance . descriptor == self . descriptor ) <EOL> def __subclasscheck__ ( self , other ) : <EOL> if not issubclass ( other , type ) : <EOL> raise TypeError <EOL> if not isinstance ( other , ModuleClass ) : <EOL> return False <EOL> reg = get_module_registry ( ) <EOL> return reg . is_descriptor_subclass ( self . descriptor , other . descriptor ) <EOL> class ModuleValuePair ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , module , value ) : <EOL> self . module = module <EOL> self . value = value <EOL> def __nonzero__ ( self ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> class Module ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , descriptor , pipeline , module_id ) : <EOL> if not ( isinstance ( module_id , ( int , long ) ) and <EOL> isinstance ( pipeline , Pipeline ) ) : <EOL> raise TypeError <EOL> self . descriptor = descriptor <EOL> self . pipeline = pipeline <EOL> self . module_id = module_id <EOL> @ property <EOL> def module ( self ) : <EOL> return self . pipeline . pipeline . modules [ self . module_id ] <EOL> @ property <EOL> def module_class ( self ) : <EOL> return ModuleClass ( self . descriptor ) <EOL> @ property <EOL> def name ( self ) : <EOL> mod = self . pipeline . pipeline . modules [ self . module_id ] <EOL> if '<STR_LIT>' in mod . db_annotations_key_index : <EOL> return mod . get_annotation_by_key ( '<STR_LIT>' ) . value <EOL> else : <EOL> return None <EOL> def __repr__ ( self ) : <EOL> desc = "<STR_LIT>" % ( self . descriptor . name , <EOL> self . descriptor . identifier ) <EOL> desc += "<STR_LIT>" % self . module_id <EOL> mod = self . pipeline . pipeline . modules [ self . module_id ] <EOL> if '<STR_LIT>' in mod . db_annotations_key_index : <EOL> desc += ( "<STR_LIT>" % <EOL> mod . get_annotation_by_key ( '<STR_LIT>' ) . value ) <EOL> return desc + "<STR_LIT:>>" <EOL> def __eq__ ( self , other ) : <EOL> if isinstance ( other , Module ) : <EOL> return ( self . module_id == other . module_id and <EOL> self . pipeline == other . pipeline ) <EOL> else : <EOL> return ModuleValuePair ( self . module , other ) <EOL> class ModuleNamespace ( object ) : <EOL> def __init__ ( self , identifier , namespace = '<STR_LIT>' ) : <EOL> self . identifier = identifier <EOL> self . _namespace = namespace <EOL> self . _namespaces = { } <EOL> def __getattr__ ( self , name ) : <EOL> if name in self . _namespaces : <EOL> return self . _namespaces [ name ] <EOL> else : <EOL> return self [ name ] <EOL> def __getitem__ ( self , name ) : <EOL> name = name . rsplit ( '<STR_LIT:|>' , <NUM_LIT:1> ) <EOL> if len ( name ) == <NUM_LIT:2> : <EOL> if self . _namespace : <EOL> namespace = self . _namespace + '<STR_LIT:|>' + name [ <NUM_LIT:0> ] <EOL> else : <EOL> namespace = name [ <NUM_LIT:0> ] <EOL> name = name [ <NUM_LIT:1> ] <EOL> else : <EOL> name , = name <EOL> namespace = self . _namespace <EOL> reg = get_module_registry ( ) <EOL> descr = reg . get_descriptor_by_name ( self . identifier , <EOL> name , <EOL> namespace ) <EOL> return ModuleClass ( descr ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . _namespace , <EOL> self . identifier ) <EOL> class Package ( ModuleNamespace ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , package ) : <EOL> if not isinstance ( package , _Package ) : <EOL> raise TypeError ( "<STR_LIT>" <EOL> "<STR_LIT>" % type ( package ) . __name__ ) <EOL> ModuleNamespace . __init__ ( self , package . identifier ) <EOL> self . _package = package <EOL> for mod , namespaces in self . _package . descriptors . iterkeys ( ) : <EOL> if not namespaces : <EOL> continue <EOL> ns = self <EOL> fullname = None <EOL> for name in namespaces . split ( '<STR_LIT:|>' ) : <EOL> if fullname is not None : <EOL> fullname += '<STR_LIT:|>' + name <EOL> else : <EOL> fullname = name <EOL> if name not in ns . _namespaces : <EOL> ns_ = ns . _namespaces [ name ] = ModuleNamespace ( <EOL> self . identifier , <EOL> fullname ) <EOL> ns = ns_ <EOL> else : <EOL> ns = ns . _namespaces [ name ] <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . identifier , <EOL> len ( self . _package . descriptors ) ) <EOL> def __eq__ ( self , other ) : <EOL> return isinstance ( other , Package ) and self . _package is other . _package <EOL> def __ne__ ( self , other ) : <EOL> return not self == other <EOL> class ExecutionErrors ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , pipeline , resultobj ) : <EOL> self . pipeline = pipeline <EOL> self . _errors = resultobj . errors <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( <EOL> len ( self . _errors ) , <EOL> '<STR_LIT:s>' if len ( self . _errors ) >= <NUM_LIT:2> else '<STR_LIT>' , <EOL> '<STR_LIT:\n>' . join ( '<STR_LIT>' % p for p in self . _errors . iteritems ( ) ) ) <EOL> class ExecutionResults ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , pipeline , resultobj ) : <EOL> self . pipeline = pipeline <EOL> self . _objects = resultobj . objects <EOL> def output_port ( self , output ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( output , basestring ) : <EOL> outputs = self . pipeline . _get_inputs_or_outputs ( '<STR_LIT>' ) <EOL> module_id = outputs [ output ] . id <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" % <EOL> type ( output ) . __name__ ) <EOL> return self . _objects [ module_id ] . get_output ( '<STR_LIT>' ) <EOL> def module_output ( self , module ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( module , Module ) : <EOL> module = self . pipeline . get_module ( module ) <EOL> return self . _objects [ module . module_id ] . outputPorts <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % len ( self . _objects ) <EOL> class Function ( object ) : <EOL> """<STR_LIT>""" <EOL> def load_vistrail ( filename , version = None ) : <EOL> """<STR_LIT>""" <EOL> initialize ( ) <EOL> if not isinstance ( filename , basestring ) : <EOL> raise TypeError ( "<STR_LIT>" % <EOL> type ( filename ) . __name__ ) <EOL> locator = FileLocator ( filename ) <EOL> loaded_objs = vistrails . core . db . io . load_vistrail ( locator ) <EOL> controller = VistrailController ( loaded_objs [ <NUM_LIT:0> ] , locator , <EOL> * loaded_objs [ <NUM_LIT:1> : ] ) <EOL> vistrail = Vistrail ( controller ) <EOL> if version is not None : <EOL> vistrail . select_version ( version ) <EOL> return vistrail <EOL> def load_pipeline ( filename ) : <EOL> """<STR_LIT>""" <EOL> initialize ( ) <EOL> if not isinstance ( filename , basestring ) : <EOL> raise TypeError ( "<STR_LIT>" % <EOL> type ( filename ) . __name__ ) <EOL> locator = FileLocator ( filename ) <EOL> pipeline = locator . load ( _Pipeline ) <EOL> return Pipeline ( pipeline ) <EOL> def load_package ( identifier , autoload = True ) : <EOL> """<STR_LIT>""" <EOL> initialize ( ) <EOL> pm = get_package_manager ( ) <EOL> pkg = pm . identifier_is_available ( identifier ) <EOL> if pm . has_package ( identifier ) : <EOL> return Package ( pkg ) <EOL> elif pkg is None : <EOL> raise NoSuchPackage ( "<STR_LIT>" % identifier ) <EOL> dep_graph = pm . build_dependency_graph ( [ identifier ] ) <EOL> deps = pm . get_ordered_dependencies ( dep_graph ) <EOL> for pkg_id in deps : <EOL> if not do_enable_package ( pm , pkg_id ) : <EOL> raise NoSuchPackage ( "<STR_LIT>" % pkg_id ) <EOL> return Package ( pkg ) <EOL> def do_enable_package ( pm , identifier ) : <EOL> pkg = pm . identifier_is_available ( identifier ) <EOL> if pm . has_package ( pkg . identifier ) : <EOL> return True <EOL> if pkg and not pm . has_package ( pkg . identifier ) : <EOL> pm . late_enable_package ( pkg . codepath ) <EOL> pkg = pm . get_package_by_codepath ( pkg . codepath ) <EOL> if pkg . identifier != identifier : <EOL> if ( hasattr ( pkg . module , '<STR_LIT>' ) and <EOL> pkg . module . can_handle_identifier ( identifier ) ) : <EOL> pkg . init_module . load_from_identifier ( identifier ) <EOL> return True <EOL> if ( pkg and pkg . identifier != identifier and <EOL> hasattr ( pkg . module , '<STR_LIT>' ) and <EOL> pkg . module . can_handle_identifier ( identifier ) and <EOL> hasattr ( pkg . init_module , '<STR_LIT>' ) ) : <EOL> pkg . init_module . load_from_identifier ( identifier ) <EOL> return True <EOL> return False <EOL> @ contextlib . contextmanager <EOL> def output_mode ( output , mode , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> yield <EOL> def run_vistrail ( filename , version = None , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return load_vistrail ( filename , version ) . execute ( * args , ** kwargs ) </s>
