def __init__(self, modl, name, info): self.modl = modl self.name = name self.full = name # so a Form() can act like a Prop(). self.info = info self.waits = collections.defaultdict(list) self.isform = True self.isrunt = bool(info.get('runt', False)) self.onadds = [] self.ondels = [] self.type = modl.types.get(name) if self.type is None: raise s_exc.NoSuchType(name=name) self.type.form = self # pre-compute our byprop table prefix self.pref = name.encode('utf8') + b'\x00\x00' self.utf8name = name.encode('utf8') self.props = {} # name: Prop() self.defvals = {} # name: valu self.refsout = None
async def _evalStorm(self, text, cast=None): text = await s_stormtypes.tostr(text) cast = await s_stormtypes.tostr(cast, noneok=True) casttype = None if cast: casttype = self.runt.model.type(cast) if casttype is None: castprop = self.runt.model.prop(cast) if castprop is not None: casttype = castprop.type if casttype is None: mesg = f'No type or property found for name: {cast}' raise s_exc.NoSuchType(mesg=mesg) asteval = await self.runt.snap.core._getStormEval(text) valu = await asteval.compute(self.runt, None) if casttype: valu, _ = casttype.norm(valu) return valu
def fromprim(valu, path=None): if isinstance(valu, str): return Str(valu, path=path) # TODO: make s_node.Node a storm type itself? if isinstance(valu, s_node.Node): return Node(valu, path=path) if isinstance(valu, s_node.Path): return Path(valu, path=path) if isinstance(valu, StormType): return valu if isinstance(valu, (tuple, list)): return List(valu, path=path) if isinstance(valu, dict): return Dict(valu, path=path) if isinstance(valu, bytes): return Bytes(valu, path=path) raise s_exc.NoSuchType(name=valu.__class__.__name__)
def getTypeClone(self, typedef): base = self.types.get(typedef[0]) if base is None: raise s_exc.NoSuchType(name=typedef[0]) return base.clone(typedef[1])
def addForm(self, formname, forminfo, propdefs): if not s_grammar.isFormName(formname): mesg = f'Invalid form name {formname}' raise s_exc.BadFormDef(name=formname, mesg=mesg) _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 self._addFormProp(form, propname, typedef, propinfo)
def fromprim(valu, path=None): if isinstance(valu, str): return Str(valu, path=path) # TODO: make s_node.Node a storm type itself? if isinstance(valu, s_node.Node): return Node(valu, path=path) if isinstance(valu, s_node.Path): return Path(valu, path=path) if isinstance(valu, (tuple, list)): return List(valu, path=path) if isinstance(valu, dict): return Dict(valu, path=path) if isinstance(valu, bytes): return Bytes(valu, path=path) if isinstance(valu, StormType): return valu mesg = 'Unable to convert python primitive to StormType.' raise s_exc.NoSuchType(mesg=mesg, python_type=valu.__class__.__name__)
def __init__(self, modl, name, info): self.modl = modl self.name = name self.full = name # so a Form() can act like a Prop(). self.info = info self.isform = True self.isrunt = bool(info.get('runt', False)) self.onadds = [] self.ondels = [] self.type = modl.types.get(name) if self.type is None: raise s_exc.NoSuchType(name=name) self.type.form = self self.props = {} # name: Prop() self.refsout = None if self.info.get('deprecated') or self.type.deprecated: async def depfunc(node): mesg = f'The form {self.full} is deprecated or using a deprecated type and will be removed in 3.0.0' await node.snap.warnonce(mesg) self.onAdd(depfunc)
def addForm(self, formname, forminfo, propdefs): if not s_grammar.isFormName(formname): mesg = f'Invalid form name {formname}' raise s_exc.BadFormDef(name=formname, mesg=mesg) _type = self.types.get(formname) if _type is None: raise s_exc.NoSuchType(name=formname) form = Form(self, formname, forminfo) self.forms[formname] = form self.props[formname] = form if isinstance(form.type, s_types.Array): self.arraysbytype[form.type.arraytype.name].append(form) for univname, typedef, univinfo in (u.getPropDef() for u in self.univs.values()): self._addFormUniv(form, univname, typedef, univinfo) for propdef in propdefs: if len(propdef) != 3: raise s_exc.BadPropDef(valu=propdef) propname, typedef, propinfo = propdef self._addFormProp(form, propname, typedef, propinfo) return form
def addType(self, typename, basename, typeopts, typeinfo): base = self.types.get(basename) if base is None: raise s_exc.NoSuchType(name=basename) self.types[typename] = base.extend(typename, typeopts, typeinfo) self._modeldef['types'].append( (typename, (basename, typeopts), typeinfo))
async def nodesByPropTypeValu(self, name, valu): _type = self.core.model.types.get(name) if _type is None: raise s_exc.NoSuchType(name=name) for prop in self.core.model.getPropsByType(name): async for node in self.nodesByPropValu(prop.full, '=', valu): yield node
def delType(self, typename): _type = self.types.get(typename) if _type is None: raise s_exc.NoSuchType(name=typename) if self.propsbytype.get(typename): raise s_exc.CantDelType(name=typename) self.types.pop(typename, None) self.propsbytype.pop(typename, None)
def toprim(valu, path=None): if isinstance(valu, (str, tuple, list, dict, int)): return valu if isinstance(valu, Prim): return valu.value() if isinstance(valu, s_node.Node): return valu.ndef[1] raise s_exc.NoSuchType(name=valu.__class__.__name__)
def toprim(valu, path=None): if isinstance(valu, (str, tuple, list, dict, int)) or valu is None: return valu if isinstance(valu, Prim): return valu.value() if isinstance(valu, s_node.Node): return valu.ndef[1] mesg = 'Unable to convert object to Storm primitive.' raise s_exc.NoSuchType(mesg=mesg, name=valu.__class__.__name__)
def addType(self, typename, basename, typeopts, typeinfo): base = self.types.get(basename) if base is None: raise s_exc.NoSuchType(name=basename) newtype = base.extend(typename, typeopts, typeinfo) if newtype.deprecated and typeinfo.get('custom'): mesg = f'The type {typename} is based on a deprecated type {newtype.name} which ' \ f'which which will be removed in 3.0.0.' logger.warning(mesg) self.types[typename] = newtype self._modeldef['types'].append(newtype.getTypeDef())
def __init__(self, model, name, tdef, info): self.name = name self.info = info self.tdef = tdef self.model = model self.utf8 = name.encode() self.nenc = name.encode() + b'\x00' self.base = self.model.types.get(tdef[0]) if self.base is None: raise s_exc.NoSuchType(name=tdef[0]) self.type = self.base.clone(tdef[1])
def _initFeedLoops(self): ''' feeds: - cryotank: tcp://cryo.vertex.link/cryo00/tank01 type: syn.splice ''' feeds = self.conf.get('feeds', ()) if not feeds: return for feed in feeds: # do some validation before we fire tasks... typename = feed.get('type') if self.getFeedFunc(typename) is None: raise s_exc.NoSuchType(name=typename) self.schedCoro(self._runFeedLoop(feed))
async def _getNodesByType(self, name, valu=None, addform=True): _type = self.model.types.get(name) if _type is None: raise s_exc.NoSuchType(name=name) if addform: form = self.model.forms.get(name) if form is not None: lops = form.getLiftOps(valu) async for row, node in self.getLiftNodes( lops, '*' + form.name): yield node for prop in self.model.getPropsByType(name): lops = prop.getLiftOps(valu) async for row, node in self.getLiftNodes(lops, prop.name): yield node
def __init__(self, model, name, tdef, info): self.name = name self.info = info self.tdef = tdef self.model = model self.utf8 = name.encode() self.nenc = name.encode() + b'\x00' self.base = self.model.types.get(tdef[0]) if self.base is None: raise s_exc.NoSuchType(name=tdef[0]) self.type = self.base.clone(tdef[1]) if isinstance(self.type, s_types.Array): mesg = 'Tag props may not be array types (yet).' raise s_exc.BadPropDef(mesg=mesg)
def __init__(self, modl, name, info): self.modl = modl self.name = name self.full = name # so a Form() can act like a Prop(). self.info = info self.isform = True self.isrunt = bool(info.get('runt', False)) self.onadds = [] self.ondels = [] self.type = modl.types.get(name) if self.type is None: raise s_exc.NoSuchType(name=name) self.type.form = self self.props = {} # name: Prop() self.refsout = None
def addForm(self, formname, forminfo, propdefs): if not s_grammar.isFormName(formname): mesg = f'Invalid form name {formname}' raise s_exc.BadFormDef(name=formname, mesg=mesg) _type = self.types.get(formname) if _type is None: raise s_exc.NoSuchType(name=formname) form = Form(self, formname, forminfo) self.forms[formname] = form self.props[formname] = form if isinstance(form.type, s_types.Array): self.arraysbytype[form.type.arraytype.name].append(form) for univname, typedef, univinfo in (u.getPropDef() for u in self.univs.values()): self._addFormUniv(form, univname, typedef, univinfo) for propdef in propdefs: if len(propdef) != 3: raise s_exc.BadPropDef(valu=propdef) propname, typedef, propinfo = propdef self._addFormProp(form, propname, typedef, propinfo) # interfaces are listed in typeinfo for the form to # maintain backward compatibility for populated models for ifname in form.type.info.get('interfaces', ()): self._addFormIface(form, ifname) return form
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)