Ejemplo n.º 1
0
    def __init__(self, path, traverser, engine):
        self._traverser = traverser
        self._engine = engine

        # Parse path
        compiledpath = []
        currentpath = []
        for element in str(path).strip().split('/'):
            if not element:
                raise engine.getCompilerError()(
                    'Path element may not be empty in %r' % path)
            if element.startswith('?'):
                if currentpath:
                    compiledpath.append(tuple(currentpath))
                    currentpath = []
                if not _valid_name(element[1:]):
                    raise engine.getCompilerError()(
                        'Invalid variable name "%s"' % element[1:])
                compiledpath.append(element[1:])
            else:
                match = namespace_re.match(element)
                if match:
                    if currentpath:
                        compiledpath.append(tuple(currentpath))
                        currentpath = []
                    namespace, functionname = match.groups()
                    if not _valid_name(namespace):
                        raise engine.getCompilerError()(
                            'Invalid namespace name "%s"' % namespace)
                    try:
                        compiledpath.append(
                            self._engine.getFunctionNamespace(namespace))
                    except KeyError:
                        raise engine.getCompilerError()(
                            'Unknown namespace "%s"' % namespace)
                    currentpath.append(functionname)
                else:
                    currentpath.append(element)

        if currentpath:
            compiledpath.append(tuple(currentpath))

        first = compiledpath[0]
        base = first[0]

        if callable(first):
            # check for initial function
            raise engine.getCompilerError()(
                'Namespace function specified in first subpath element')
        elif isinstance(first, basestring):
            # check for initial ?
            raise engine.getCompilerError()(
                'Dynamic name specified in first subpath element')

        if base and not _valid_name(base):
            raise engine.getCompilerError()(
                'Invalid variable name "%s"' % element)
        self._base = base
        compiledpath[0] = first[1:]
        self._compiled_path = tuple(compiledpath)
Ejemplo n.º 2
0
    def __init__(self, path, traverser, engine):
        self._traverser = traverser
        self._engine = engine

        # Parse path
        compiledpath = []
        currentpath = []
        for element in str(path).strip().split('/'):
            if not element:
                raise engine.getCompilerError()(
                    'Path element may not be empty in %r' % path)
            if element.startswith('?'):
                if currentpath:
                    compiledpath.append(tuple(currentpath))
                    currentpath = []
                if not _valid_name(element[1:]):
                    raise engine.getCompilerError()(
                        'Invalid variable name "%s"' % element[1:])
                compiledpath.append(element[1:])
            else:
                match = namespace_re.match(element)
                if match:
                    if currentpath:
                        compiledpath.append(tuple(currentpath))
                        currentpath = []
                    namespace, functionname = match.groups()
                    if not _valid_name(namespace):
                        raise engine.getCompilerError()(
                            'Invalid namespace name "%s"' % namespace)
                    try:
                        compiledpath.append(
                            self._engine.getFunctionNamespace(namespace))
                    except KeyError:
                        raise engine.getCompilerError()(
                            'Unknown namespace "%s"' % namespace)
                    currentpath.append(functionname)
                else:
                    currentpath.append(element)

        if currentpath:
            compiledpath.append(tuple(currentpath))

        first = compiledpath[0]

        if callable(first):
            # check for initial function
            raise engine.getCompilerError()(
                'Namespace function specified in first subpath element')
        elif isinstance(first, six.string_types):
            # check for initial ?
            raise engine.getCompilerError()(
                'Dynamic name specified in first subpath element')

        base = first[0]
        if base and not _valid_name(base):
            raise engine.getCompilerError()('Invalid variable name "%s"' %
                                            element)
        self._base = base
        compiledpath[0] = first[1:]
        self._compiled_path = tuple(compiledpath)