def addType(self, name, **info): ''' Add a type to the cached types. Args: name (str): Name of the type to add. **info (dict): Type properties to include. Example: Add a new foo:bar type:: tlib.addType('foo:bar', subof='str', doc='A foo bar.') Raises: DupTypeName: If the type already exists. ''' if self.types.get(name) is not None: raise s_common.DupTypeName(name=name) ctor = info.get('ctor') subof = info.get('subof') if ctor is None and subof is None: raise Exception('addType must have either ctor= or subof=') if ctor is not None: self.typeinfo[name] = info try: item = s_dyndeps.tryDynFunc(ctor, self, name, **info) self.types[name] = item self._bumpBasePend(name) return True except Exception as e: logger.warning('failed to ctor type %s', name, exc_info=True) logger.debug('failed to ctor type %s', name, exc_info=True) self.typeinfo.pop(name, None) try: base = self.reqDataType(subof) # inherit docs and examples from parent types self.typeinfo[name] = info item = base.extend(name, **info) self.types[name] = item self._bumpBasePend(name) self.typetree[name] = subof self.subscache.clear() return True except s_common.NoSuchType as e: tnam = e.errinfo.get('name') self.typeinfo.pop(name, None) self.pended[tnam].append((name, info)) return False
def test_dyndeps_alias(self): s_dyndeps.addDynAlias('unit_test_woot', woot) self.eq(s_dyndeps.getDynLocal('unit_test_woot'), woot) self.eq(s_dyndeps.tryDynFunc('unit_test_woot', 20, y=40), 60) self.eq(s_dyndeps.delDynAlias('unit_test_woot'), woot) self.none(s_dyndeps.getDynLocal('unit_test_woot')) self.raises(NoSuchDyn, s_dyndeps.tryDynFunc, 'unit_test_woot', 20, y=40)
def _loadCoreModule(self, ctor): try: modu = s_dyndeps.tryDynFunc(ctor, self) self.modules[ctor] = modu return modu except Exception as e: logger.exception('mod load fail: %s' % (ctor,)) return None
def addType(self, name, **info): ''' ''' if self.types.get(name) != None: raise DupTypeName(name=name) ctor = info.get('ctor') subof = info.get('subof') if ctor == None and subof == None: raise Exception('addType must have either ctor= or subof=') if ctor != None: self.typeinfo[name] = info try: item = s_dyndeps.tryDynFunc(ctor,self,name,**info) self.types[name] = item self._bumpBasePend(name) return True except Exception as e: logger.warning('failed to ctor type %s', name, exc_info=True) logger.debug('failed to ctor type %s', name, exc_info=True) self.typeinfo.pop(name,None) try: base = self.reqDataType(subof) # inherit docs and examples from parent types self.typeinfo[name] = info item = base.extend(name, **info) self.types[name] = item self._bumpBasePend(name) self.typetree[name] = subof self.subscache.clear() return True except NoSuchType as e: tnam = e.errinfo.get('name') self.typeinfo.pop(name,None) self.pended[tnam].append( (name,info) ) return False
def _loadDmonConf(self, conf): checkConfDict(conf) self.locs.update(conf.get('vars', {})) # handle forks first to prevent socket bind weirdness for name, subconf in conf.get('forks', ()): self._fireDmonFork(name, subconf) title = conf.get('title') if title is not None: s_thisplat.setProcName('dmon: %s' % title) # handle explicit module load requests for name, info in conf.get('modules', ()): modu = s_dyndeps.getDynMod(name) if modu is None: logger.warning('dmon mod not loaded: %s', name) # handle includes next for path in conf.get('includes', ()): fullpath = os.path.expanduser(path) try: self.loadDmonFile(fullpath) except Exception as e: raise Exception('Include Error (%s): %s' % (path, e)) configs = conf.get('configs', {}) for row in conf.get('ctors', ()): copts = {} # ctor options if len(row) == 2: name, url = row elif len(row) == 3: name, url, copts = row else: raise Exception('Invalid ctor row: %r' % (row, )) if url.find('://') == -1: # this is a (name,dynfunc,config) formatted ctor... item = s_dyndeps.tryDynFunc(url, copts) else: item = self.dmoneval(url) self.locs[name] = item # check for a ctor opt that wants us to load a config dict by name cfgname = copts.get('config') if cfgname is not None: if not isinstance(item, s_config.Configable): raise Exception('dmon ctor: %s does not support configs' % name) opts = configs.get(cfgname) if opts is None: raise s_common.NoSuchConf(name=cfgname) item.setConfOpts(opts) # check for a ctor opt that wants us to "flatten" several configs in order cfgnames = copts.get('configs') if cfgnames is not None: if not isinstance(item, s_config.Configable): raise Exception('dmon ctor: %s does not support configs' % name) opts = {} for cfgname in cfgnames: cfgopts = configs.get(cfgname) if cfgopts is None: raise s_common.NoSuchConf(name=cfgname) opts.update(cfgopts) item.setConfOpts(opts) # check for a match between config and ctor names opts = configs.get(name) if opts is not None: item.setConfOpts(opts) self.fire('dmon:conf:ctor', name=name, item=item) for busname, svcruns in conf.get('services', ()): svcbus = self.dmoneval(busname) if svcbus is None: raise s_common.NoSuchObj(name) for svcname, svcopts in svcruns: item = self.locs.get(svcname) if item is None: raise s_common.NoSuchObj(svcname) svcname = svcopts.get('name', svcname) s_service.runSynSvc(svcname, item, svcbus, **svcopts)
def addDataModels(self, mods): ''' Add a list of (name, mdef) tuples. A model definition (mdef) is structured as follows: { "ctors":( ('name', 'class.path.ctor', {}, {'doc': 'The foo thing.'}), ), "types":( ('name', ('basetype', {typeopts}), {info}), ), "forms":( (formname, (typename, typeopts), {info}, ( (propname, (typename, typeopts), {info}), )), ), "univs":( (propname, (typename, typeopts), {info}), ) } Args: mods (list); The list of tuples. ''' # load all the base type ctors in order... for modlname, mdef in mods: for name, ctor, opts, info in mdef.get('ctors', ()): item = s_dyndeps.tryDynFunc(ctor, self, name, info, opts) self.types[name] = item self._modeldef['ctors'].append((name, ctor, opts, info)) # load all the types in order... for modlname, mdef in mods: for typename, (basename, opts), info in mdef.get('types', ()): base = self.types.get(basename) if base is None: raise s_exc.NoSuchType(name=basename) self.types[typename] = base.extend(typename, opts, info) self._modeldef['types'].append( (typename, (basename, opts), info)) # Load all the universal properties for modlname, mdef in mods: for univname, typedef, univinfo in mdef.get('univs', ()): self.addUnivProp(univname, typedef, univinfo) # now we can load all the forms... for modlname, mdef in mods: for formname, forminfo, propdefs in mdef.get('forms', ()): _type = self.types.get(formname) if _type is None: raise s_exc.NoSuchType(name=formname) self._modeldef['forms'].append((formname, forminfo, propdefs)) form = Form(self, formname, forminfo) self.forms[formname] = form self.props[formname] = form for univname, typedef, univinfo in self.univs: self._addFormUniv(form, univname, typedef, univinfo) for propdef in propdefs: if len(propdef) != 3: raise s_exc.BadPropDef(valu=propdef) propname, typedef, propinfo = propdef prop = Prop(self, form, propname, typedef, propinfo) full = f'{formname}:{propname}' self.props[full] = prop self.props[(formname, propname)] = prop self._modelinfo.addDataModels(mods)