Esempio n. 1
0
    def norm(self, valu, oldval=None):
        if s_compat.isstr(valu):
            return self._norm_str(valu, oldval=oldval)

        addr = valu >> 16
        port = valu & 0xffff
        return valu, {'port': port, 'ipv4': addr}
Esempio n. 2
0
 def frob(self, valu, oldval=None):
     if s_compat.isstr(valu):
         # handle decimal integer strings...
         if valu.isdigit():
             return int(valu) & 0xffffffff, {}
         return self.parse(valu, oldval=oldval)
     return self.norm(valu, oldval=oldval)
Esempio n. 3
0
    def loadDmonConf(self, conf):
        '''
        Process Daemon specific dmon config elements.

        # examples of additional config elements

        {
            "sessions":{

                "comment":"Maxtime (if set) sets the maxtime for the session cache (in seconds)",
                "maxtime":12345,

                "comment":"Curator (if set) uses dmoneval to set the session curator",
                "curator":"tcp://host.com:8899/synsess",

                "comment":"Comment (if set) saves sessions to the given path (sqlite cortex)",
                "savefile":"sessions.sql3"

            },

            "share":(
                ('fooname',{'optname':optval}),
                ...
            ),

            "listen":(
                'tcp://0.0.0.0:8899',
                ...
            ),

        }

        '''
        DmonConf.loadDmonConf(self, conf)

        for name, opts in conf.get('share', ()):
            asname = opts.get('name', name)

            item = self.locs.get(name)
            if item is None:
                raise s_common.NoSuchObj(name)

            # keeping as "onfini" for backward compat
            # FIXME CHANGE WITH MAJOR REV
            fini = opts.get('onfini', False)
            self.share(asname, item, fini=fini)

        # process the sessions config info
        sessinfo = conf.get('sessions')
        if sessinfo is not None:
            self._loadSessConf(sessinfo)

        # process a few daemon specific options
        for url in conf.get('listen', ()):
            if s_compat.isstr(url):
                self.listen(url)
                continue

            url, opts = url
            self.listen(url, **opts)
Esempio n. 4
0
    def _ingMergScope(self, core, data, info, scope):
        '''
        Extract variables from info and populate them into the current scope.
        Extract tags from the info and populate them into the current scope.
        '''

        vard = info.get('vars')
        if vard is not None:
            for varn, vnfo in vard:
                valu = self._get_prop(core, data, vnfo, scope)
                if valu is not None:
                    scope.set(varn, valu)

        for tagv in info.get('tags', ()):

            # if it's a simple tag string, add and move along
            if s_compat.isstr(tagv):
                scope.add('tags', tagv.lower())
                continue

            # otherwise it's an iteration compatible prop dict
            tags = [
                t.lower() for t in self._iter_prop(core, data, tagv, scope)
            ]

            scope.add('tags', *tags)
Esempio n. 5
0
    def parse(self, text, oldval=None):
        if not s_compat.isstr(text):
            self._raiseBadValu(text)

        text = text.strip().lower()
        text = (''.join([c for c in text if c.isdigit()]))

        tlen = len(text)
        if tlen == 4:
            dt = datetime.datetime.strptime(text, '%Y')

        elif tlen == 6:
            dt = datetime.datetime.strptime(text, '%Y%m')

        elif tlen == 8:
            dt = datetime.datetime.strptime(text, '%Y%m%d')

        elif tlen == 10:
            dt = datetime.datetime.strptime(text, '%Y%m%d%H')

        elif tlen == 12:
            dt = datetime.datetime.strptime(text, '%Y%m%d%H%M')

        elif tlen == 14:
            dt = datetime.datetime.strptime(text, '%Y%m%d%H%M%S')

        elif tlen in (15, 16, 17):
            dt = datetime.datetime.strptime(text, '%Y%m%d%H%M%S%f')

        else:
            self._raiseBadValu(text, mesg='Unknown time format')

        epoch = datetime.datetime(1970, 1, 1)
        return int((dt - epoch).total_seconds() * 1000), {}
Esempio n. 6
0
    def norm(self, valu, oldval=None):

        if not s_compat.isstr(valu) or len(valu) < 1:
            self._raiseBadValu(valu)

        # generate me one.  we dont care.
        if valu == '*':
            return s_common.guid(), {}

        if valu[0] != '$':
            retn = valu.lower().replace('-', '')
            if not isguid(retn):
                self._raiseBadValu(valu)

            return retn, {}

        if self._guid_alias is None:
            self._raiseBadValu(
                valu, mesg='guid resolver syntax used with non-aliased guid')

        if self._getTufoByProp is None:
            self._raiseBadValu(
                valu, mesg='guid resolver syntax used with non-cortex tlib')

        # ( sigh... eventually everything will be a cortex... )
        node = self._getTufoByProp(self._guid_alias, valu[1:])
        if node is None:
            self._raiseBadValu(valu, mesg='no result for guid resolver')

        iden = node[1].get(node[1].get('tufo:form'))
        return iden, {}
Esempio n. 7
0
    def norm(self, valu, oldval=None):

        if self.frobintfmt and s_compat.isint(valu):
            valu = self.frobintfmt % valu

        if not s_compat.isstr(valu):
            self._raiseBadValu(valu)

        if self.info.get('lower'):
            valu = valu.lower()

        if valu == self.nullval:
            return valu, {}

        if self.restrip:
            valu = self.restrip.sub('', valu)

        if self.strip:
            valu = valu.strip()

        if self.envals is not None and valu not in self.envals:
            self._raiseBadValu(valu, enums=self.info.get('enums'))

        if self.regex is not None and not self.regex.match(valu):
            self._raiseBadValu(valu, regex=self.info.get('regex'))

        return valu, {}
Esempio n. 8
0
    def norm(self, valu, oldval=None):
        if s_compat.isstr(valu):
            return self._norm_str(valu, oldval=oldval)

        if not s_compat.isint(valu):
            self._raiseBadValu(valu)

        return valu & 0xffffffff, {}
Esempio n. 9
0
    def norm(self, valu, oldval=None):

        if s_compat.isstr(valu):
            return self._norm_str(valu, oldval=oldval)

        if not islist(valu):
            self._raiseBadValu(valu, mesg='Expected guid, psv, or list')

        return self._norm_list(valu, oldval=None)
Esempio n. 10
0
    def norm(self, valu, oldval=None):

        if not s_compat.isstr(valu):
            return json.dumps(valu, separators=(',', ':')), {}

        try:
            return json.dumps(json.loads(valu), separators=(',', ':')), {}
        except Exception as e:
            self._raiseBadValu(valu)
Esempio n. 11
0
    def norm(self, valu, oldval=None):
        if not s_compat.isstr(valu):
            self._raiseBadValu(valu)

        subs = {}

        subs['norm'], subsubs = self.tlib.getTypeNorm('file:path', valu)
        subs.update(addpref('norm', subsubs))

        return valu, subs
Esempio n. 12
0
    def norm(self, valu, oldval=None):

        # if it's already a guid, we have nothing to normalize...
        if s_compat.isstr(valu):
            return self._norm_str(valu, oldval=oldval)

        if not islist(valu):
            self._raiseBadValu(valu, mesg='Expected guid or list/tuple')

        return self._norm_list(valu)
Esempio n. 13
0
    def norm(self, valu, oldval=None):

        if s_compat.isstr(valu):
            return self._norm_str(valu, oldval=oldval)

        if not s_compat.isint(valu):
            self._raiseBadValu(valu)

        if oldval is not None and self.minmax:
            valu = self.minmax(valu, oldval)

        return valu, {}
Esempio n. 14
0
    def norm(self, valu, oldval=None):
        if s_compat.isstr(valu):
            valu = valu.lower()
            if valu in ('true', 't', 'y', 'yes', '1', 'on'):
                return 1, {}

            if valu in ('false', 'f', 'n', 'no', '0', 'off'):
                return 0, {}

            self._raiseBadValu(valu, mesg='Invalid boolean string')

        return int(bool(valu)), {}
Esempio n. 15
0
    def norm(self, valu, oldval=None):

        subs = {}

        # make the string into int form then apply our min/max
        if s_compat.isstr(valu):
            valu, subs = self._norm_str(valu, oldval=oldval)

        if oldval is not None and self.minmax:
            valu = self.minmax(valu, oldval)

        return valu, subs
Esempio n. 16
0
def _de_base64(item, **opts):

    # transparently handle the strings/bytes issue...
    wasstr = s_compat.isstr(item)
    if wasstr:
        item = item.encode('utf8')

    item = base64.b64decode(item)

    if wasstr:
        item = item.decode('utf8')

    return item
Esempio n. 17
0
    def norm(self, valu, oldval=None):

        if not s_compat.isstr(valu):
            self._raiseBadValu(valu)

        if self.info.get('lower'):
            valu = valu.lower()

        enums = self.info.get('enums')
        if enums != None and valu not in enums:
            self._raiseBadValu(valu,enums=enums)

        if self.regex != None and not self.regex.match(valu):
            self._raiseBadValu(valu,regex=self.info.get('regex'))

        return valu
Esempio n. 18
0
    def norm(self, valu, oldval=None):

        if not s_compat.isstr(valu):
            self._raiseBadValu(valu)

        if self.info.get('lower'):
            valu = valu.lower()

        if valu == self.nullval:
            return valu,{}

        if self.envals != None and valu not in self.envals:
            self._raiseBadValu(valu,enums=self.info.get('enums'))

        if self.regex != None and not self.regex.match(valu):
            self._raiseBadValu(valu,regex=self.info.get('regex'))

        return valu,{}
Esempio n. 19
0
    def _elem_iter(self):

        # special case for dictionaries
        # to iterate children and keep track
        # of their names...
        if type(self._d_item) == dict:
            for name, item in self._d_item.items():
                yield initelem((name, item), name=self.name(), parent=self)
            return

        if s_compat.isint(self._d_item):
            return

        if s_compat.isstr(self._d_item):
            return

        for i, item in enumerate(self._d_item):
            yield initelem(item, name=str(i), parent=self)
Esempio n. 20
0
    def norm(self, valu, oldval=None):

        if s_compat.isstr(valu):
            valu = int(digits(valu))

        subs = {}
        try:
            valu = int(valu)
            info = s_l_phone.getPhoneInfo(valu)

            cc = info.get('cc')
            if cc is not None:
                subs['cc'] = cc

            # TODO prefix based validation?
            return valu, subs

        except TypeError as e:
            self._raiseBadValu(valu)
Esempio n. 21
0
    def norm(self, valu, oldval=None):

        if not s_compat.isstr(valu):
            self._raiseBadValu(valu)

        lead = ''

        valu = valu.replace('\\', '/').lower()
        if valu and valu[0] == '/':
            lead = '/'

        valu = valu.strip('/')

        vals = [v for v in valu.split('/') if v]

        fins = []

        # canonicalize . and ..
        for v in vals:
            if v == '.':
                continue

            if v == '..' and fins:
                fins.pop()
                continue

            fins.append(v)

        subs = {'dir': '', 'depth': len(fins)}
        valu = lead + ('/'.join(fins))

        if fins:
            base = fins[-1]
            subs['base'] = base

            pext = base.rsplit('.', 1)
            if len(pext) > 1:
                subs['ext'] = pext[1]

            if len(fins) > 1:
                subs['dir'] = lead + ('/'.join(fins[:-1]))

        return valu, subs
Esempio n. 22
0
    def _ingMergScope(self, core, data, info, scope):

        vard = info.get('vars')
        if vard != None:
            for varn, vnfo in vard:
                valu = self._get_prop(core, data, vnfo, scope)
                scope.set(varn, valu)

        for tagv in info.get('tags', ()):

            # if it's a simple tag string, add and move along
            if s_compat.isstr(tagv):
                scope.add('tags', tagv.lower())
                continue

            # otherwise it's an iteration compatible prop dict
            tags = [
                t.lower() for t in self._iter_prop(core, data, tagv, scope)
            ]

            scope.add('tags', *tags)
Esempio n. 23
0
    def norm(self, valu, oldval=None):

        if s_compat.isstr(valu):
            try:
                valu = int(valu, 0)
            except ValueError as e:
                self._raiseBadValu(valu)

        if not s_compat.isint(valu):
            self._raiseBadValu(valu)

        if oldval is not None and self.minmax:
            valu = self.minmax(valu, oldval)

        if self.minval is not None and valu < self.minval:
            self._raiseBadValu(valu, minval=self.minval)

        if self.maxval is not None and valu > self.maxval:
            self._raiseBadValu(valu, maxval=self.maxval)

        return valu, {}
Esempio n. 24
0
    def addCortex(self, name, url, tags=()):
        '''
        Tell the MetaCortex about a Cortex instance.

        Example:

            meta.addCortex('woot0','ram:///',tags=('woot.bar',))

        '''
        if self.coresbyname.get(name) != None:
            raise DupCortexName(name)

        if not s_compat.isstr(name):
            raise InvalidParam('name','must be a string!')

        event = self.fire('meta:cortex:add',name=name,url=url,tags=tags)
        if not event[1].get('allow',True):
            raise PermDenied()

        self.cordefs[name] = (name,url,tags)
        return self._tryAddCorUrl(name, url, tags=tags)
Esempio n. 25
0
    def norm(self, valu, oldval=None):

        if not (s_compat.isstr(valu) and len(valu) > 0):
            self._raiseBadValu(valu)

        leadingslash = '/' if valu.startswith('/') else ''
        newval = valu.replace('\\', '/').lower().strip('/')
        parts = newval.split('/')

        dirname = leadingslash + '/'.join(parts[0:-1])
        newval = leadingslash + newval

        props = {}
        base = parts[-1]
        if base:
            props['base'] = base

        if dirname:
            props['dir'] = dirname

        return newval, props
Esempio n. 26
0
    def addCortex(self, name, url, tags=()):
        '''
        Tell the MetaCortex about a Cortex instance.

        Example:

            meta.addCortex('woot0','ram:///',tags=('woot.bar',))

        '''
        if self.coresbyname.get(name) != None:
            raise DupCortexName(name)

        if not s_compat.isstr(name):
            raise InvalidParam('name', 'must be a string!')

        event = self.fire('meta:cortex:add', name=name, url=url, tags=tags)
        if not event[1].get('allow', True):
            raise PermDenied()

        self.cordefs[name] = (name, url, tags)
        return self._tryAddCorUrl(name, url, tags=tags)
Esempio n. 27
0
    def norm(self, valu, oldval=None):
        subs = {}
        reprs = []

        if s_compat.isstr(valu):
            valu = self._split_str(valu)

        # only other possiblity should be that it was a list
        for part, (name, tobj) in self._zipvals(valu):

            if tobj == self:
                norm, nsub = part, {}
                reprs.append(norm)
            else:
                norm, nsub = tobj.norm(part)
                reprs.append(tobj.repr(norm))

            subs[name] = norm
            for subn, subv in nsub.items():
                subs['%s:%s' % (name, subn)] = subv

        return self.sepr.join(reprs), subs
Esempio n. 28
0
 def frob(self, valu, oldval=None):
     if s_compat.isstr(valu):
         return self.parse(valu, oldval=oldval)
     return self.norm(valu, oldval=oldval)
Esempio n. 29
0
def reprvalu(valu):
    if s_compat.isstr(valu):
        return repr(valu)
    return '%d (0x%x)' % (valu, valu)
Esempio n. 30
0
    def norm(self, valu, oldval=None):
        if not (s_compat.isstr(valu) and not valu.find('/') > -1):
            self._raiseBadValu(valu)

        return valu.lower(), {}
Esempio n. 31
0
 def frob(self, valu, oldval=None):
     if s_compat.isstr(valu):
         return self.parse(valu, oldval=oldval)
     return self.norm(valu, oldval=oldval)
Esempio n. 32
0
 def norm(self, valu, oldval=None):
     if not s_compat.isstr(valu):
         self._raiseBadValu(valu)
     return ''.join([c for c in valu if c.isdigit()]), {}
Esempio n. 33
0
 def parse(self, text, oldval=None):
     if not s_compat.isstr(text):
         self._raiseBadValu(text)
     return (''.join([c for c in text if c.isdigit()])), {}