def parse_definition(self, item_type, item_name, string): """ Initializes a single definition, using the string, and adds it to the dictionary of items. Arguments: item_type -- The item's type. item_name -- The item's name. string -- The item's definition string. """ if plugins.is_plugin(item_type): # Load a plug-in try: item = plugins.load_plugin(item_type, item_name, self, \ string, self.item_prefix()) except Exception as e: raise osexception(u"Failed to load plugin '%s'" % \ item_type, exception=e) self.items[item_name] = item else: # Load one of the core items debug.msg(u"loading core item '%s' from '%s'" % (item_type, \ self.module_container())) item_module = __import__(u'%s.%s' % (self.module_container(), \ item_type), fromlist=[u'dummy']) item_class = getattr(item_module, item_type) item = item_class(item_name, self, string) self.items[item_name] = item
def parse_definition(self, item_type, item_name, string): """ Initialize a single definition, using the string, and add it to the dictionary of items Arguments: item_type -- the type of the item item_name -- the name of the item string -- the string containing the definition """ if plugins.is_plugin(item_type): # Load a plug-in if debug.enabled: debug.msg(u"loading plugin '%s'" % item_type) item = plugins.load_plugin(item_type, item_name, self, string, \ self.item_prefix()) else: try: item = plugins.load_plugin(item_type, item_name, self, \ string, self.item_prefix()) except: raise exceptions.script_error(u"Failed load plugin '%s'" % \ item_type) self.items[item_name] = item else: # Load the module from the regular items debug.msg(u"loading core plugin '%s'" % item_type) if debug.enabled: exec("from %s import %s" % (self.module_container(), item_type)) try: if not self.debug: exec("from %s import %s" % (self.module_container(), \ item_type)) except: raise exceptions.script_error( \ u"Failed to import item '%s' as '%s'. " \ % (item_type, item_name) + "Perhaps the experiment requires a plug-in that is not available on your system.", \ full=False) cmd = '%(item_type)s.%(item_type)s("%(item_name)s", self, u"""%(string)s""")' \ % {"item_type" : item_type, "item_name" : item_name, "string" \ : string.replace(u'"', u'\\"')} if debug.enabled: bytecode = compile(cmd, "<string>", "eval") self.items[item_name] = eval(bytecode) else: try: bytecode = compile(cmd, "<string>", "eval") self.items[item_name] = eval(bytecode) except Exception as e: raise exceptions.script_error( \ u"Failed to instantiate module '%s' as '%s': %s" % \ (item_type, item_name, e))
def parse_definition(self, item_type, item_name, string): """ Initialize a single definition, using the string, and add it to the dictionary of items Arguments: item_type -- the type of the item item_name -- the name of the item string -- the string containing the definition """ if plugins.is_plugin(item_type): # Load a plug-in if debug.enabled: debug.msg(u"loading plugin '%s'" % item_type) item = plugins.load_plugin(item_type, item_name, self, string, \ self.item_prefix()) else: try: item = plugins.load_plugin(item_type, item_name, self, \ string, self.item_prefix()) except: raise exceptions.script_error(u"Failed load plugin '%s'" % \ item_type) self.items[item_name] = item else: # Load one of the core items debug.msg(u"loading core item '%s' from '%s'" % (item_type, \ self.module_container())) item_module = __import__('%s.%s' % (self.module_container(), \ item_type), fromlist=['dummy']) item_class = getattr(item_module, item_type) item = item_class(item_name, self, string) self.items[item_name] = item
def new(self, _type, name=None, script=None): """ desc: Creates a new item. arguments: _type: desc: The item type. type: unicode keywords: name: desc: The item name, or None to choose a unique name based on the item type. type: [unicode, NoneType] script: desc: A definition script, or None to start with a blank item. type: [unicode, NoneType] returns: desc: The newly generated item. type: item example: | items.new(u'sketchpad', name=u'my_sketchpad') items[u'my_sketchpad'].prepare() items[u'my_sketchpad'].run() """ debug.msg(u'creating %s' % _type) name = self.valid_name(_type, suggestion=name) if plugins.is_plugin(_type): # Load a plug-in try: item = plugins.load_plugin(_type, name, self.experiment, script, self.experiment.item_prefix()) except Exception as e: raise osexception( u"Failed to load plugin '%s'" % _type, exception=e) self.__items__[name] = item else: # Load one of the core items debug.msg(u"loading core item '%s' from '%s'" % (_type, self.experiment.module_container())) item_module = __import__(u'%s.%s' % ( self.experiment.module_container(), _type), fromlist=[u'dummy']) item_class = getattr(item_module, _type) item = item_class(name, self.experiment, script) self.__items__[name] = item return item
def new(self, _type, name=None, script=None): """ desc: Creates a new item. arguments: _type: desc: The item type. type: unicode keywords: name: desc: The item name, or None to choose a unique name based on the item type. type: [unicode, NoneType] script: desc: A definition script, or None to start with a blank item. type: [unicode, NoneType] returns: desc: The newly generated item. type: item example: | items.new(u'sketchpad', name=u'my_sketchpad') items[u'my_sketchpad'].prepare() items[u'my_sketchpad'].run() """ debug.msg(u'creating %s' % _type) name = self.valid_name(_type, suggestion=name) if plugins.is_plugin(_type): # Load a plug-in try: item = plugins.load_plugin(_type, name, self.experiment, script, self.experiment.item_prefix()) except Exception as e: raise osexception(u"Failed to load plugin '%s'" % _type, exception=e) self.__items__[name] = item else: # Load one of the core items debug.msg(u"loading core item '%s' from '%s'" % (_type, self.experiment.module_container())) item_module = __import__( u'%s.%s' % (self.experiment.module_container(), _type), fromlist=[u'dummy']) item_class = getattr(item_module, _type) item = item_class(name, self.experiment, script) self.__items__[name] = item return item
def valid_type(self, _type): """ arguments: _type: desc: The item type to check. type: str returns: desc: True if _type is a valid type, False otherwise. type: bool """ if plugins.is_plugin(_type): return True if _type in self.built_in_types: return True return False
def parse_definition(self, item_type, item_name, string): """ Initialize a single definition, using the string, and add it to the dictionary of items """ if plugins.is_plugin(item_type): if self.debug: print "experiment.parse_definition(): loading plugin '%s'" % item_type try: item = plugins.load_plugin(item_type, item_name, self, string, self.item_prefix()) except: raise exceptions.script_error("Failed load plugin '%s'" % item_type) self.items[item_name] = item else: # Load the module from the regular items if self.debug: print "experiment.parse_definition(): loading core plugin '%s'" % item_type if self.debug: exec("from %s import %s" % (self.module_container(), item_type)) try: if not self.debug: exec("from %s import %s" % (self.module_container(), item_type)) except: raise exceptions.script_error("Failed to import module '%s' as '%s'" % (item_type, item_name)) cmd = "%(item_type)s.%(item_type)s(\"%(item_name)s\", self, \"\"\"%(string)s\"\"\")" % \ {"item_type" : item_type, "item_name" : item_name, "string" : string.replace("\"", "\\\"")} if self.debug: bytecode = compile(cmd, "<string>", "eval") self.items[item_name] = eval(bytecode) else: try: bytecode = compile(cmd, "<string>", "eval") self.items[item_name] = eval(bytecode) except Exception as e: raise exceptions.script_error("Failed to instantiate module '%s' as '%s': %s" % (item_type, item_name, e))