def test_trait(self): """ Method to test trait characterisitics: value, type. """ self.assertTrue(is_trait_value_defined(5)) self.assertFalse(is_trait_value_defined("")) self.assertFalse(is_trait_value_defined(None)) self.assertFalse(is_trait_value_defined(Undefined)) trait = CTrait(0) trait.handler = Float() self.assertFalse(is_trait_pathname(trait)) for handler in [File(), Directory()]: trait.handler = handler self.assertTrue(is_trait_pathname(trait))
def test_trait(self): """ Method to test trait characterisitics: value, type. """ self.assertTrue(is_trait_value_defined(5)) self.assertFalse(is_trait_value_defined("")) self.assertFalse(is_trait_value_defined(None)) self.assertFalse(is_trait_value_defined(traits.Undefined)) trait = traits.CTrait(0) trait.handler = traits.Float() self.assertFalse(is_trait_pathname(trait)) for handler in [traits.File(), traits.Directory()]: trait.handler = handler self.assertTrue(is_trait_pathname(trait))
def set_plug_value(self, plug_name, value): """ Set the plug value Parameters ---------- plug_name: str (mandatory) a plug name value: object (mandatory) the plug value we want to set """ if value in ["", "<undefined>"]: value = Undefined elif is_trait_pathname(self.process.trait(plug_name)) and value is None: value = Undefined setattr(self.process, plug_name, value)
def set_plug_value(self, plug_name, value): """ Set the plug value Parameters ---------- plug_name: str (mandatory) a plug name value: object (mandatory) the plug value we want to set """ if value in ["", "<undefined>"]: value = Undefined elif is_trait_pathname( self.process.trait(plug_name)) and value is None: value = Undefined self.process.set_parameter(plug_name, value)
def set_parameter(self, name, value): """ Method to set a process instance trait value. For File and Directory traits the None value is replaced by the special _Undefined trait value. Parameters ---------- name: str (mandatory) the trait name we want to modify value: object (mandatory) the trait value we want to set """ # Detect File and Directory trait types with None value if value is None and is_trait_pathname(self.trait(name)): # The None trait value is _Undefined, do the replacement value = _Undefined() # Set the new trait value setattr(self, name, value)
def get_commandline(self): """ Method to generate a comandline representation of the process. """ # Get command line arguments (ie., the process user traits) reserved_params = ("nodes_activation", "selection_changed") args = [ (trait_name, is_trait_pathname(trait)) for trait_name, trait in self.user_traits().iteritems() if (trait_name not in reserved_params and is_trait_value_defined(getattr(self, trait_name)))] # Build the python call expression, keeping apart file names. # File names are given separately since they might be modified # externally afterwards, typically to handle temporary files, or # file transfers with Soma-Workflow. argsdict = dict( (trait_name, getattr(self, trait_name)) for trait_name, is_pathname in args if not is_pathname) pathsdict = dict( (trait_name, getattr(self, trait_name)) for trait_name, is_pathname in args if is_pathname) # Get the module and class names module_name = self.__class__.__module__ class_name = self.name # Construct the command line commandline = [ "python", "-c", ("import sys; from {0} import {1}; kwargs={2}; " "kwargs.update(dict((sys.argv[i * 2 + 1], sys.argv[i * 2 + 2]) " "for i in range((len(sys.argv) - 1) / 2))); " "{1}()(**kwargs)").format(module_name, class_name, repr(argsdict)).replace("'", '"') ] + sum([list(x) for x in pathsdict.items()], []) return commandline
def get_commandline(self): """ Method to generate a comandline representation of the process. If not implemented, it will generate a commandline running python, instaitiating the current process, and calling its :meth:`_run_process` method. Returns ------- commandline: list of strings Arguments are in separate elements of the list. """ # Get command line arguments (ie., the process user traits) # Build the python call expression, keeping apart file names. # File names are given separately since they might be modified # externally afterwards, typically to handle temporary files, or # file transfers with Soma-Workflow. class ArgPicker(object): """ This small object is only here to have a __repr__() representation which will print sys.argv[n] in a list when writing the commandline code. """ def __init__(self, num): self.num = num def __repr__(self): return 'sys.argv[%d]' % self.num reserved_params = ("nodes_activation", "selection_changed") # pathslist is for files referenced from lists: a list of files will # look like [sys.argv[5], sys.argv[6]...], then the corresponding # path args will be in additional arguments, here stored in pathslist pathslist = [] # argsdict is the dict of non-path arguments, and will be printed # using repr() argsdict = {} # pathsdict is the dict of path arguments, and will be printed as a # series of arg_name, path_value, all in separate commandline arguments pathsdict = {} for trait_name, trait in six.iteritems(self.user_traits()): value = getattr(self, trait_name) if trait_name in reserved_params \ or not is_trait_value_defined(value): continue if is_trait_pathname(trait): pathsdict[trait_name] = value elif isinstance(trait.trait_type, List) \ and is_trait_pathname(trait.inner_traits[0]): plist = [] for pathname in value: if is_trait_value_defined(pathname): plist.append(ArgPicker(len(pathslist) + 1)) pathslist.append(pathname) else: plist.append(pathname) argsdict[trait_name] = plist else: argsdict[trait_name] = value # Get the module and class names if hasattr(self, '_function'): # function with xml decorator module_name = self._function.__module__ class_name = self._function.__name__ call_name = class_name else: module_name = self.__class__.__module__ class_name = self.name call_name = '%s()' % class_name # Construct the command line python_command = os.path.basename(sys.executable) commandline = [ python_command, "-c", ("import sys; from {0} import {1}; kwargs={2}; " "kwargs.update(dict((sys.argv[i * 2 + {3}], " "sys.argv[i * 2 + {4}]) " "for i in range(int((len(sys.argv) - {3}) / 2)))); " "{5}(**kwargs)").format(module_name, class_name, repr(argsdict), len(pathslist) + 1, len(pathslist) + 2, call_name).replace( "'", '"') ] + pathslist + sum([list(x) for x in pathsdict.items()], []) return commandline
def get_commandline(self): """ Method to generate a comandline representation of the process. """ # Get command line arguments (ie., the process user traits) # Build the python call expression, keeping apart file names. # File names are given separately since they might be modified # externally afterwards, typically to handle temporary files, or # file transfers with Soma-Workflow. class ArgPicker(object): """ This small object is only here to have a __repr__() representation which will print sys.argv[n] in a list when writing the commandline code. """ def __init__(self, num): self.num = num def __repr__(self): return 'sys.argv[%d]' % self.num reserved_params = ("nodes_activation", "selection_changed") # pathslist is for files referenced from lists: a list of files will # look like [sys.argv[5], sys.argv[6]...], then the corresponding # path args will be in additional arguments, here stored in pathslist pathslist = [] # argsdict is the dict of non-path arguments, and will be printed # using repr() argsdict = {} # pathsdict is the dict of path arguments, and will be printed as a # series of arg_name, path_value, all in separate commandline arguments pathsdict = {} for trait_name, trait in six.iteritems(self.user_traits()): value = getattr(self, trait_name) if trait_name in reserved_params \ or not is_trait_value_defined(value): continue if is_trait_pathname(trait): pathsdict[trait_name] = value elif isinstance(trait.trait_type, List) \ and is_trait_pathname(trait.inner_traits[0]): plist = [] for pathname in value: if is_trait_value_defined(pathname): plist.append(ArgPicker(len(pathslist) + 1)) pathslist.append(pathname) else: plist.append(pathname) argsdict[trait_name] = plist else: argsdict[trait_name] = value # Get the module and class names module_name = self.__class__.__module__ class_name = self.name # Construct the command line commandline = [ "python", "-c", ("import sys; from {0} import {1}; kwargs={2}; " "kwargs.update(dict((sys.argv[i * 2 + {3}], " "sys.argv[i * 2 + {4}]) " "for i in range(int((len(sys.argv) - {3}) / 2)))); " "{1}()(**kwargs)").format(module_name, class_name, repr(argsdict), len(pathslist) + 1, len(pathslist) + 2).replace("'", '"') ] + pathslist + sum([list(x) for x in pathsdict.items()], []) return commandline