def get_iterator_default_script(iterator_type): """ Returns: sub_scripts: a dictionary with the default scripts for the script_iterator script_settings: a dictionary with the script_settingsfor the default scripts """ sub_scripts = {} script_settings = {} package = 'b26_toolkit' # todo JG: mabye find a dynamic whay to get this # for point iteration we add some default scripts if iterator_type == 'iter nvs': module = Script.get_script_module('SelectPoints')# Select points is actually in pylabcontrol sub_scripts.update( {'select_points': getattr(module, 'SelectPoints')} ) module = Script.get_script_module('FindNV', package) sub_scripts.update( # {'find_nv': getattr(module, 'FindNV_cDAQ')} {'find_nv': getattr(module, 'FindNV')} ) module = Script.get_script_module('Take_And_Correlate_Images', package) sub_scripts.update( {'correlate_iter': getattr(module, 'Take_And_Correlate_Images', package)} ) script_settings['script_order'] = {'select_points': -3, 'correlate_iter': -2, 'find_nv': -1} elif iterator_type == 'iter points': module = Script.get_script_module('SelectPoints', 'pylabcontrol') sub_scripts.update( {'select_points': getattr(module, 'SelectPoints')} ) module = Script.get_script_module('SetLaser', package) sub_scripts.update( {'set_laser': getattr(module, 'SetLaser')} ) module = Script.get_script_module('Take_And_Correlate_Images', package) sub_scripts.update( {'correlate_iter': getattr(module, 'Take_And_Correlate_Images')} ) script_settings['script_order']={'select_points': -3, 'correlate_iter': -2, 'set_laser': -1} elif iterator_type == 'test': module = Script.get_script_module('Wait', 'pylabcontrol') sub_scripts.update( {'wait': getattr(module, 'Wait')} ) return sub_scripts, script_settings
def set_up_dynamic_script(script_information, script_iterators, verbose=verbose): ''' Args: script_information: information about the script as required by Script.get_script_information() Returns: script_default_settings: the default settings of the dynamically created script as a list of parameters sub_scripts script_iterators: dictionary of the script_iterator classes of the form {'package_name': <script_iterator_classe>} package: name of the package of the script_iterator ''' if verbose: print(('script_information', script_information)) sub_scripts = { } # dictonary of script classes that are to be subscripts of the dynamic class. Should be in the dictionary form {'class_name': <class_object>} (btw. class_object is not the instance) script_order = [ ] # A list of parameters giving the order that the scripts in the ScriptIterator should be executed. Must be in the form {'script_name': int}. Scripts are executed from lowest number to highest script_execution_freq = [ ] # A list of parameters giving the frequency with which each script should be executed _, script_class_name, script_settings, _, script_sub_scripts, _, package = Script.get_script_information( script_information) if package not in script_iterators: script_iterators.update( ScriptIterator.get_script_iterator(package)) assert package in script_iterators iterator_type = getattr(script_iterators[package], 'get_iterator_type')(script_settings, script_sub_scripts) if verbose: print(('iterator_type JG', iterator_type)) if isinstance(script_information, dict): for sub_script_name, sub_script_class in script_sub_scripts.items( ): if isinstance(sub_script_class, Script): # script already exists # To implement: add a function to scripts that outputs a script_information dict corresponding # to the current settings. This can then be fed into Script.get_script_information and things # can proceed as below. We may also need to add an additional tracker to the dialogue window # to differentiate between the case of wanting a new script from scratch when there is an # identically named one already loaded, vs using that loaded script raise NotImplementedError elif script_sub_scripts[sub_script_name][ 'class'] == 'ScriptIterator': # raise NotImplementedError # has to be dynamic maybe??? script_information_subclass, script_iterators = ScriptIterator.create_dynamic_script_class( script_sub_scripts[sub_script_name], script_iterators) subscript_class_name = script_information_subclass[ 'class'] #previously in python 2 had used: import pylabcontrol.core.script_iterator #however, this shouldn't have worked, as this was already imported as part of pylabcontrol, so nothing #happens and the old version without the dynamic script is used. Here, we force import the new #version of the module which now contains the script_iterator and everything works as expected script_iterator_module = __import__( 'pylabcontrol.core.script_iterator') sub_scripts.update({ sub_script_name: getattr(script_iterator_module, subscript_class_name) }) else: if verbose: print(('script_sub_scripts[sub_script_name]', sub_script_class)) module = Script.get_script_module(sub_script_class, verbose=verbose) if verbose: print(('module', module)) new_subscript = getattr( module, script_sub_scripts[sub_script_name]['class']) sub_scripts.update({sub_script_name: new_subscript}) # for some iterators have default scripts, e.g. point iteration has select points default_sub_scripts, default_script_settings = getattr( script_iterators[package], 'get_iterator_default_script')(iterator_type) sub_scripts.update(default_sub_scripts) for k, v in default_script_settings.items(): if k in script_settings: script_settings[k].update(v) elif isinstance(script_information, Script): print('old code - DOUBLE CHECK') raise NotImplementedError # if the script already exists, just update the script order parameter sub_scripts.update({script_class_name: script_information}) else: raise TypeError( 'create_dynamic_script_class: unknown type of script_information' ) script_order, script_execution_freq = getattr( script_iterators[package], 'get_script_order')(script_settings['script_order']) script_default_settings = getattr( script_iterators[package], 'get_default_settings')(sub_scripts, script_order, script_execution_freq, iterator_type) return script_default_settings, sub_scripts, script_iterators, package
def get_iterator_default_script(iterator_type): """ Returns: sub_scripts: a dictionary with the default scripts for the script_iterator script_settings: a dictionary with the script_settingsfor the default scripts """ sub_scripts = {} script_settings = {} package = 'b26_toolkit' # todo JG: mabye find a dynamic whay to get this # for point iteration we add some default scripts if iterator_type == 'iter nvs': module = Script.get_script_module( 'SelectPoints') # Select points is actually in pylabcontrol sub_scripts.update( {'select_points': getattr(module, 'SelectPoints')}) module = Script.get_script_module('FindNV', package) sub_scripts.update( # {'find_nv': getattr(module, 'FindNV_cDAQ')} {'find_nv': getattr(module, 'FindNV')}) module = Script.get_script_module('Take_And_Correlate_Images', package) sub_scripts.update({ 'correlate_iter': getattr(module, 'Take_And_Correlate_Images', package) }) script_settings['script_order'] = { 'select_points': -3, 'correlate_iter': -2, 'find_nv': -1 } elif iterator_type == 'iter points': module = Script.get_script_module('SelectPoints', 'pylabcontrol') sub_scripts.update( {'select_points': getattr(module, 'SelectPoints')}) module = Script.get_script_module('SetLaser', package) sub_scripts.update({'set_laser': getattr(module, 'SetLaser')}) module = Script.get_script_module('Take_And_Correlate_Images', package) sub_scripts.update({ 'correlate_iter': getattr(module, 'Take_And_Correlate_Images') }) script_settings['script_order'] = { 'select_points': -3, 'correlate_iter': -2, 'set_laser': -1 } elif iterator_type == 'test': module = Script.get_script_module('Wait', 'pylabcontrol') sub_scripts.update({'wait': getattr(module, 'Wait')}) return sub_scripts, script_settings