<s> from __future__ import division <EOL> from . gmap_cell import _modules as gmap_modules <EOL> from . vis import _modules as vis_modules <EOL> from . identifiers import identifier <EOL> from vistrails . core . modules . utils import create_port_spec_string <EOL> from vistrails . core . vistrail . port import Port <EOL> from vistrails . core . vistrail . port_spec import PortSpec <EOL> from vistrails . core . vistrail . port_spec_item import PortSpecItem <EOL> _modules = gmap_modules + vis_modules <EOL> def handle_module_upgrade_request ( controller , module_id , pipeline ) : <EOL> from vistrails . core . modules . module_descriptor import ModuleDescriptor <EOL> from vistrails . core . upgradeworkflow import UpgradeWorkflowHandler , UpgradePackageRemap , UpgradeModuleRemap <EOL> from vistrails . core . modules . basic_modules import identifier as basic_pkg <EOL> def remap_functions ( old_module , new_module , function_remap ) : <EOL> use_registry = True <EOL> function_ops = [ ] <EOL> for function in old_module . functions : <EOL> if function . name in function_remap : <EOL> remap = function_remap [ function . name ] <EOL> if remap is None : <EOL> continue <EOL> elif not isinstance ( remap , basestring ) : <EOL> function_ops . extend ( remap ( function , new_module ) ) <EOL> continue <EOL> else : <EOL> function_name = remap <EOL> if len ( function . parameters ) > <NUM_LIT:0> : <EOL> new_param_vals , aliases = zip ( * [ ( p . strValue , p . alias ) <EOL> for p in function . parameters ] ) <EOL> else : <EOL> new_param_vals = [ ] <EOL> aliases = [ ] <EOL> if use_registry : <EOL> function_port_spec = function_name <EOL> else : <EOL> def mk_psi ( pos ) : <EOL> psi = PortSpecItem ( module = "<STR_LIT>" , package = basic_pkg , <EOL> namespace = "<STR_LIT>" , pos = pos ) <EOL> return psi <EOL> n_items = len ( new_param_vals ) <EOL> function_port_spec = PortSpec ( name = function_name , <EOL> items = [ mk_psi ( i ) <EOL> for i in xrange ( n_items ) ] ) <EOL> new_function = controller . create_function ( new_module , <EOL> function_port_spec , <EOL> new_param_vals , <EOL> aliases ) <EOL> new_module . add_function ( new_function ) <EOL> if None in function_remap : <EOL> remap = function_remap [ None ] <EOL> function_ops . extend ( remap ( None , new_module ) ) <EOL> return function_ops <EOL> def remap_dst_connections ( old_module , new_module , port_remap ) : <EOL> use_registry = True <EOL> create_new_connection = UpgradeWorkflowHandler . create_new_connection <EOL> ops = [ ] <EOL> for _ , conn_id in pipeline . graph . edges_to ( old_module . id ) : <EOL> old_conn = pipeline . connections [ conn_id ] <EOL> if old_conn . destination . name in port_remap : <EOL> remap = port_remap [ old_conn . destination . name ] <EOL> if remap is None : <EOL> continue <EOL> elif not isinstance ( remap , basestring ) : <EOL> ops . extend ( remap ( old_conn , new_module ) ) <EOL> continue <EOL> else : <EOL> destination_name = remap <EOL> old_src_module = pipeline . modules [ old_conn . source . moduleId ] <EOL> if use_registry : <EOL> destination_port = destination_name <EOL> else : <EOL> destination_port = Port ( name = destination_name , <EOL> type = '<STR_LIT>' , <EOL> signature = create_port_spec_string ( <EOL> [ ( basic_pkg , '<STR_LIT>' , '<STR_LIT>' ) ] ) ) <EOL> new_conn = create_new_connection ( controller , <EOL> old_src_module , <EOL> old_conn . source , <EOL> new_module , <EOL> destination_port ) <EOL> ops . append ( ( '<STR_LIT>' , new_conn ) ) <EOL> return ops <EOL> def insert_vis ( vis_name , vis_port_remap ) : <EOL> def remap_vis ( old_conn , new_cell_module ) : <EOL> ops = [ ] <EOL> old_src_module = pipeline . modules [ old_conn . source . moduleId ] <EOL> old_cell_module = pipeline . modules [ old_conn . destination . moduleId ] <EOL> new_x = ( old_src_module . location . x + new_cell_module . location . x ) / <NUM_LIT> <EOL> new_y = ( old_src_module . location . y + new_cell_module . location . y ) / <NUM_LIT> <EOL> new_vis_desc = ModuleDescriptor ( package = identifier , <EOL> name = vis_name , <EOL> version = '<STR_LIT>' ) <EOL> new_vis_module = controller . create_module_from_descriptor ( new_vis_desc , <EOL> new_x , new_y ) <EOL> function_ops = remap_functions ( old_cell_module , <EOL> new_vis_module , <EOL> vis_port_remap ) <EOL> ops . append ( ( '<STR_LIT>' , new_vis_module ) ) <EOL> ops . extend ( function_ops ) <EOL> ops . extend ( remap_dst_connections ( old_cell_module , <EOL> new_vis_module , <EOL> vis_port_remap ) ) <EOL> create_new_connection = UpgradeWorkflowHandler . create_new_connection <EOL> new_conn_1 = create_new_connection ( controller , <EOL> old_src_module , <EOL> old_conn . source , <EOL> new_vis_module , <EOL> "<STR_LIT>" ) <EOL> ops . append ( ( '<STR_LIT>' , new_conn_1 ) ) <EOL> new_conn_2 = create_new_connection ( controller , <EOL> new_vis_module , <EOL> "<STR_LIT>" , <EOL> new_cell_module , <EOL> "<STR_LIT>" ) <EOL> ops . append ( ( '<STR_LIT>' , new_conn_2 ) ) <EOL> return ops <EOL> return remap_vis <EOL> remap = UpgradePackageRemap ( ) <EOL> def add_legacy ( fname , module ) : <EOL> new_function = controller . create_function ( module , <EOL> "<STR_LIT>" , <EOL> [ "<STR_LIT:True>" ] ) <EOL> return [ ( '<STR_LIT>' , new_function , '<STR_LIT>' , module . id ) ] <EOL> remap . add_module_remap ( UpgradeModuleRemap ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> new_module = "<STR_LIT>" , <EOL> module_name = "<STR_LIT>" , <EOL> dst_port_remap = { '<STR_LIT>' : insert_vis ( "<STR_LIT>" , <EOL> { None : add_legacy } ) , <EOL> '<STR_LIT>' : None } ) ) <EOL> remap . add_module_remap ( UpgradeModuleRemap ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> new_module = "<STR_LIT>" , <EOL> module_name = "<STR_LIT>" , <EOL> dst_port_remap = { '<STR_LIT>' : insert_vis ( "<STR_LIT>" , <EOL> { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } ) , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> } ) ) <EOL> remap . add_module_remap ( UpgradeModuleRemap ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> new_module = "<STR_LIT>" , <EOL> module_name = "<STR_LIT>" , <EOL> dst_port_remap = { '<STR_LIT>' : insert_vis ( "<STR_LIT>" , <EOL> { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } ) , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> } ) ) <EOL> remap . add_module_remap ( UpgradeModuleRemap ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> new_module = "<STR_LIT>" , <EOL> module_name = "<STR_LIT>" , <EOL> dst_port_remap = { '<STR_LIT>' : insert_vis ( "<STR_LIT>" , <EOL> { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } ) , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> } ) ) <EOL> return UpgradeWorkflowHandler . remap_module ( controller , module_id , pipeline , <EOL> remap ) </s>
