コード例 #1
0
    def _parse_path(self, path):

        off = 0
        steps = []

        plen = len(path)
        while off < plen:

            # eat the next (or possibly a first) slash
            _, off = s_syntax.nom(path, off, ('/', ))

            if off >= plen:
                break

            if s_syntax.is_literal(path, off):
                elem, off = s_syntax.parse_literal(path, off)
                steps.append(elem)
                continue

            # eat until the next /
            elem, off = s_syntax.meh(path, off, ('/', ))
            if not elem:
                continue

            steps.append(elem)

        return steps
コード例 #2
0
ファイル: datapath.py プロジェクト: thpatel/synapse
def _parse_path(path):
    '''
    Parses a datapath into its parts
    '''
    off = 0
    steps = []
    if path is None:
        return steps

    plen = len(path)
    while off < plen:

        # eat the next (or possibly a first) slash
        _, off = s_syntax.nom(path, off, ('/',))

        if off >= plen:
            break

        if s_syntax.is_literal(path, off):
            elem, off = s_syntax.parse_literal(path, off)
            steps.append(elem)
            continue

        # eat until the next /
        elem, off = s_syntax.meh(path, off, ('/',))
        if not elem:
            continue

        steps.append(elem)

    return steps
コード例 #3
0
ファイル: geospace.py プロジェクト: vivisect/synapse
    def _normPyStr(self, text):
        valu, off = s_syntax.parse_float(text, 0)
        unit, off = s_syntax.nom(text, off, s_syntax.alphaset)

        mult = units.get(unit.lower())
        if mult is None:
            raise s_exc.BadTypeValu(valu=text, name=self.name,
                                    mesg='invalid/unknown dist unit: %s' % (unit,))

        return int(valu * mult), {}
コード例 #4
0
ファイル: geospace.py プロジェクト: theassyrian/synapse
    def _norm_str(self, text):

        valu, off = s_syntax.parse_float(text, 0)
        unit, off = s_syntax.nom(text, off, s_syntax.alphaset)

        mult = units.get(unit.lower())
        if mult is None:
            self._raiseBadValu(text,
                               mesg='invalid/unknown dist unit: %s' % (unit, ))

        return valu * mult, {}
コード例 #5
0
ファイル: geospace.py プロジェクト: rjammala/synapse
    def _normPyStr(self, text):
        valu, off = s_syntax.parse_float(text, 0)
        unit, off = s_syntax.nom(text, off, s_syntax.alphaset)

        mult = units.get(unit.lower())
        if mult is None:
            raise s_exc.BadTypeValu(valu=text,
                                    name=self.name,
                                    mesg='invalid/unknown dist unit: %s' %
                                    (unit, ))

        return int(valu * mult), {}
コード例 #6
0
def tokenize(text):
    '''
    Produce a token list from the given text string.

    [ (<tok>,<info>), ... ]
    '''
    off = 0
    tlen = len(text)

    toks = []

    while off < tlen:

        _, off = s_syntax.nom_whitespace(text, off)
        if off >= tlen:
            break

        if s_syntax.nextin(text, off, '"\''):
            tokn = ('valu', {'off': off, 'type': 'str'})
            tokn[1]['valu'], off = s_syntax.parse_string(text, off, trim=False)

            tokn[1]['end'] = off
            toks.append(tokn)

            continue

        if s_syntax.nextin(text, off, '0123456789'):
            tokn = ('valu', {'off': off, 'type': 'int'})
            tokn[1]['valu'], off = s_syntax.parse_int(text, off)

            tokn[1]['end'] = off
            toks.append(tokn)

            continue

        tokdone = False
        for tok in tokstrs:

            if text.startswith(tok, off):
                tokn = (tok, {'off': off})

                off += len(tok)
                tokn[1]['end'] = off

                toks.append(tokn)

                tokdone = True
                break

        if tokdone:
            continue

        if not s_syntax.nextin(text, off, varset):
            raise s_common.BadSyntaxError(at=off, mesg='no valid tokens found')

        tokn = ('var', {'off': off})
        tokn[1]['name'], off = s_syntax.nom(text, off, varset, trim=False)

        toks.append(tokn)

    for tokn in toks:
        tokn[1].update(tokninfo.get(tokn[0], {}))

    return toks
コード例 #7
0
ファイル: cli.py プロジェクト: thpatel/synapse
    def getCmdOpts(self, text):
        '''
        Use the _cmd_syntax def to split/parse/normalize the cmd line.

        NOTE: This is implemented indepedent of argparse (et.al) due to
              the need for syntax aware argument splitting.
              ( also, allows different split per command type )
        '''
        off = 0

        _, off = s_syntax.nom(text, off, s_syntax.whites)

        name, off = s_syntax.meh(text, off, s_syntax.whites)

        _, off = s_syntax.nom(text, off, s_syntax.whites)

        opts = {}

        args = collections.deque(
            [synt for synt in self._cmd_syntax if not synt[0].startswith('-')])

        switches = {
            synt[0]: synt
            for synt in self._cmd_syntax if synt[0].startswith('-')
        }

        # populate defaults and lists
        for synt in self._cmd_syntax:
            snam = synt[0].strip('-')

            defval = synt[1].get('defval')
            if defval is not None:
                opts[snam] = defval

            if synt[1].get('type') in ('list', 'kwlist'):
                opts[snam] = []

        def atswitch(t, o):
            # check if we are at a recognized switch.  if not
            # assume the data is part of regular arguments.
            if not text.startswith('-', o):
                return None, o

            name, x = s_syntax.meh(t, o, s_syntax.whites)
            swit = switches.get(name)
            if swit is None:
                return None, o

            return swit, x

        while off < len(text):

            _, off = s_syntax.nom(text, off, s_syntax.whites)

            swit, off = atswitch(text, off)
            if swit is not None:

                styp = swit[1].get('type', 'flag')
                snam = swit[0].strip('-')

                if styp == 'valu':
                    valu, off = s_syntax.parse_cmd_string(text, off)
                    opts[snam] = valu

                elif styp == 'list':
                    valu, off = s_syntax.parse_cmd_string(text, off)
                    if not isinstance(valu, list):
                        valu = valu.split(',')
                    opts[snam].extend(valu)

                elif styp == 'enum':
                    vals = swit[1].get('enum:vals')
                    valu, off = s_syntax.parse_cmd_string(text, off)
                    if valu not in vals:
                        raise s_common.BadSyntaxError(
                            mesg='%s (%s)' % (swit[0], '|'.join(vals)),
                            text=text)

                    opts[snam] = valu

                else:
                    opts[snam] = True

                continue

            if not args:
                raise s_common.BadSyntaxError(mesg='trailing text: [%s]' %
                                              (text[off:], ),
                                              text=text)

            synt = args.popleft()
            styp = synt[1].get('type', 'valu')

            # a glob type eats the remainder of the string
            if styp == 'glob':
                opts[synt[0]] = text[off:]
                break

            # eat the remainder of the string as separate vals
            if styp == 'list':
                valu = []

                while off < len(text):
                    item, off = s_syntax.parse_cmd_string(text, off)
                    valu.append(item)

                opts[synt[0]] = valu
                break

            if styp == 'kwlist':
                kwlist, off = s_syntax.parse_cmd_kwlist(text, off)
                opts[snam] = kwlist
                break

            valu, off = s_syntax.parse_cmd_string(text, off)
            opts[synt[0]] = valu

        return opts
コード例 #8
0
ファイル: cli.py プロジェクト: vivisect/synapse
    def getCmdOpts(self, text):
        '''
        Use the _cmd_syntax def to split/parse/normalize the cmd line.

        Args:
            text (str): Command to process.

        Notes:
            This is implemented independent of argparse (et al) due to the
            need for syntax aware argument splitting. Also, allows different
            split per command type

        Returns:
            dict: An opts dictionary.
        '''
        off = 0

        _, off = s_syntax.nom(text, off, s_syntax.whites)

        name, off = s_syntax.meh(text, off, s_syntax.whites)

        _, off = s_syntax.nom(text, off, s_syntax.whites)

        opts = {}

        args = collections.deque([synt for synt in self._cmd_syntax if not synt[0].startswith('-')])

        switches = {synt[0]: synt for synt in self._cmd_syntax if synt[0].startswith('-')}

        # populate defaults and lists
        for synt in self._cmd_syntax:
            snam = synt[0].strip('-')

            defval = synt[1].get('defval')
            if defval is not None:
                opts[snam] = defval

            if synt[1].get('type') in ('list', 'kwlist'):
                opts[snam] = []

        def atswitch(t, o):
            # check if we are at a recognized switch.  if not
            # assume the data is part of regular arguments.
            if not text.startswith('-', o):
                return None, o

            name, x = s_syntax.meh(t, o, s_syntax.whites)
            swit = switches.get(name)
            if swit is None:
                return None, o

            return swit, x

        while off < len(text):

            _, off = s_syntax.nom(text, off, s_syntax.whites)

            swit, off = atswitch(text, off)
            if swit is not None:

                styp = swit[1].get('type', 'flag')
                snam = swit[0].strip('-')

                if styp == 'valu':
                    valu, off = s_syntax.parse_cmd_string(text, off)
                    opts[snam] = valu

                elif styp == 'list':
                    valu, off = s_syntax.parse_cmd_string(text, off)
                    if not isinstance(valu, list):
                        valu = valu.split(',')
                    opts[snam].extend(valu)

                elif styp == 'enum':
                    vals = swit[1].get('enum:vals')
                    valu, off = s_syntax.parse_cmd_string(text, off)
                    if valu not in vals:
                        raise s_exc.BadSyntax(mesg='%s (%s)' % (swit[0], '|'.join(vals)),
                                                   text=text)

                    opts[snam] = valu

                else:
                    opts[snam] = True

                continue

            if not args:
                raise s_exc.BadSyntax(mesg='trailing text: [%s]' % (text[off:],),
                                           text=text)

            synt = args.popleft()
            styp = synt[1].get('type', 'valu')

            # a glob type eats the remainder of the string
            if styp == 'glob':
                opts[synt[0]] = text[off:]
                break

            # eat the remainder of the string as separate vals
            if styp == 'list':
                valu = []

                while off < len(text):
                    item, off = s_syntax.parse_cmd_string(text, off)
                    valu.append(item)

                opts[synt[0]] = valu
                break

            if styp == 'kwlist':
                kwlist, off = s_syntax.parse_cmd_kwlist(text, off)
                opts[snam] = kwlist
                break

            valu, off = s_syntax.parse_cmd_string(text, off)
            opts[synt[0]] = valu

        return opts