Exemple #1
0
def parse_line(line):
    pos = 0
    stop = len(line)
    while pos < stop:
        match = python_re.search(line, pos)
        if match is None: break
        start, end = match.span()
        yield 'other', pos, start, line[pos:start]
        i = match.lastindex
        if i == 1: yield 'string', start, end, match.group()
        elif i == 2: yield 'other', start, end, match.group()
        elif i == 3:
            pos = start
            for x in re.split('(\W+)', match.group()):
                next = pos + len(x)
                if x in keyword.kwlist: yield 'keyword', pos, next, x
                elif is_ident(x):
                    if pos == start: yield 'identifier', pos, next, x
                    else: yield 'attribute', pos, next, x
                else: yield 'other', pos, next, x
                pos = next
        elif i == 4:
            yield 'comment', start, end, match.group()
        else:
            assert False  # pragma: no cover
        pos = end
    yield 'other', pos, stop, line[pos:]
def parse_line(line):
    pos = 0
    stop = len(line)
    while pos < stop:
        match = python_re.search(line, pos)
        if match is None: break
        start, end = match.span()
        yield 'other', pos, start, line[pos:start]
        i = match.lastindex
        if i == 1: yield 'string', start, end, match.group()
        elif i == 2: yield 'other', start, end, match.group()
        elif i == 3:
            pos = start
            for x in re.split('(\W+)', match.group()):
                next = pos + len(x)
                if x in keyword.kwlist: yield 'keyword', pos, next, x
                elif is_ident(x):
                    if pos == start: yield 'identifier', pos, next, x
                    else: yield 'attribute', pos, next, x
                else: yield 'other', pos, next, x
                pos = next
        elif i == 4: yield 'comment', start, end, match.group()
        else: assert False  # pragma: no cover
        pos = end
    yield 'other', pos, stop, line[pos:]
Exemple #3
0
 def eval_json_path(builder, values):
     result = []
     for value in values:
         if isinstance(value, int):
             result.append(str(value))
         elif isinstance(value, basestring):
             result.append(value if is_ident(value) else '"%s"' % value.replace('"', '\\"'))
         else: assert False, value
     return '{%s}' % ','.join(result)
Exemple #4
0
 def eval_json_path(cls, values):
     result = ['$']
     append = result.append
     empty_slice = slice(None, None, None)
     for value in values:
         if isinstance(value, int): append('[%d]' % value)
         elif isinstance(value, basestring):
             append('.' + value if is_ident(value) else '."%s"' % value.replace('"', '\\"'))
         elif value is Ellipsis: append('.*')
         elif value == empty_slice: append('[*]')
         else: assert False, value
     return ''.join(result)
Exemple #5
0
def get_template_name(frame):
    try:
        code = frame.f_code
        result = template_name_cache.get(code)
        if result: return result
        file_name = code.co_filename
        if file_name is None: return None

        code_name = code.co_name
        if not code_name or not is_ident(code_name): return None

        cls_name = get_class_name(frame)
        if cls_name is None:
            root, ext = os.path.splitext(file_name)
            result = '%s.%s' % (root, code_name)
        else:
            head, tail = os.path.split(file_name)
            tail = '%s.%s' % (cls_name, code_name)
            result = os.path.join(head, tail)
        template_name_cache[code] = result
        return result
    finally:
        frame = None
def get_template_name(frame):
    try:
        code = frame.f_code
        result = template_name_cache.get(code)
        if result: return result
        file_name = code.co_filename
        if file_name is None: return None

        code_name = code.co_name
        if not code_name or not is_ident(code_name): return None

        cls_name = get_class_name(frame)
        if cls_name is None:
            root, ext = os.path.splitext(file_name)
            result = '%s.%s' % (root, code_name)
        else:
            head, tail = os.path.split(file_name)
            tail = '%s.%s' % (cls_name, code_name)
            result = os.path.join(head, tail)
        template_name_cache[code] = result
        return result
    finally:
        frame = None
def check_identifier(s):
    if is_ident(s): return s
    raise ValueError()
Exemple #8
0
def check_identifier(s):
    if is_ident(s): return s
    raise ValueError()
Exemple #9
0
def parse_markup(source, start_pos=0, nested=False):
    text = source[0]

    def remove_spaces(tree):
        for i in range(3, len(tree)):
            item = tree[i]
            prev = tree[i - 1]
            if isinstance(item, basestring) and isinstance(prev, tuple):
                prev_markups = prev[4]
                if prev_markups: tree[i] = newline_re.sub('', item)
        if not nested or len(tree) == 2: return tree
        assert text[start_pos - 1] == '{'
        start = tree[2]
        if text[start_pos - 2] != '\\':
            if isinstance(start, basestring):
                tree[2] = newline_re.sub('', start)
            return tree
        if isinstance(start, basestring): tree[2] = start.lstrip()
        end = tree[-1]
        if isinstance(end, basestring): tree[-1] = end.rstrip()
        return tree

    pos = start_pos
    result = [pos, None]
    brace_counter = 0
    while True:
        match = main_re.search(text, pos)
        if not match:
            if nested or brace_counter:
                raise ParseError('Unexpected end of text', source, len(text))
            result.append(text[pos:])
            end = len(text)
            result[1] = end - 1
            return remove_spaces(joinstrings(result)), end
        start, end = match.span()
        i = match.lastindex
        if start != pos: result.append(text[pos:start])
        if i == 1:  # {
            brace_counter += 1
            result.append('{')
        elif i == 2:  # }
            if not brace_counter:
                if nested:
                    result[1] = end - 1
                    return remove_spaces(joinstrings(result)), end
                raise ParseError("Unexpected symbol '}'", source, end)
            brace_counter -= 1
            result.append('}')
        elif i == 3:  # @@
            result.append('@')
        elif i == 4:  # @/* comment */ or @// comment
            pass
        elif i in (5, 6):  # @(expression) or @{i18n markup}
            command, end = parse_command(source, start, end - 1, None)
            result.append(command)
        elif i >= 7:
            cmd_name = match.group(7)
            if cmd_name is not None and cmd_name.startswith('+'):
                if not is_ident(cmd_name[1:]):
                    raise ParseError('Invalid method call', source, start)
            if i == 7:  # @expression
                try:
                    expr, _ = utils.parse_expr(text, start + 1)
                except ValueError:
                    raise ParseError('Invalid Python expression', source,
                                     start + 1)
                end = start + 1 + len(expr)
                if expr.endswith(';'): expr = expr[:-1]
                result.append((start, end, None, expr, None))
            elif i == 8:  # @function.call(...)
                command, end = parse_command(source, match.start(), end - 1,
                                             cmd_name)
                result.append(command)
        pos = end
def parse_markup(source, start_pos=0, nested=False):
    text = source[0]
    def remove_spaces(tree):
        for i in range(3, len(tree)):
            item = tree[i]
            prev = tree[i-1]
            if isinstance(item, basestring) and isinstance(prev, tuple):
                prev_markups = prev[4]
                if prev_markups: tree[i] = newline_re.sub('', item)
        if not nested or len(tree) == 2: return tree
        assert text[start_pos-1] == '{'
        start = tree[2]
        if text[start_pos-2] != '\\':
            if isinstance(start, basestring): tree[2] = newline_re.sub('', start)
            return tree
        if isinstance(start, basestring): tree[2] = start.lstrip()
        end = tree[-1]
        if isinstance(end, basestring): tree[-1] = end.rstrip()
        return tree
    pos = start_pos
    result = [pos, None]
    brace_counter = 0
    while True:
        match = main_re.search(text, pos)
        if not match:
            if nested or brace_counter:
                raise ParseError('Unexpected end of text', source, len(text))
            result.append(text[pos:])
            end = len(text)
            result[1] = end-1
            return remove_spaces(joinstrings(result)), end
        start, end = match.span()
        i = match.lastindex
        if start != pos: result.append(text[pos:start])
        if i == 1:  # {
            brace_counter += 1
            result.append('{')
        elif i == 2:  # }
            if not brace_counter:
                if nested:
                    result[1] = end-1
                    return remove_spaces(joinstrings(result)), end
                raise ParseError("Unexpected symbol '}'", source, end)
            brace_counter -= 1
            result.append('}')
        elif i == 3: # @@
            result.append('@')
        elif i == 4: # @/* comment */ or @// comment
            pass
        elif i in (5, 6): # @(expression) or @{i18n markup}
            command, end = parse_command(source, start, end-1, None)
            result.append(command)
        elif i >= 7:
            cmd_name = match.group(7)
            if cmd_name is not None and cmd_name.startswith('+'):
                if not is_ident(cmd_name[1:]): raise ParseError('Invalid method call', source, start)
            if i == 7: # @expression
                try: expr, _ = utils.parse_expr(text, start+1)
                except ValueError: raise ParseError('Invalid Python expression', source, start+1)
                end = start+1 + len(expr)
                if expr.endswith(';'): expr = expr[:-1]
                result.append((start, end, None, expr, None))
            elif i == 8: # @function.call(...)
                command, end = parse_command(source, match.start(), end-1, cmd_name)
                result.append(command)
        pos = end