File: future.py

package info (click to toggle)
python-baron 0.10.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,080 kB
  • sloc: python: 26,926; makefile: 126; sh: 27
file content (38 lines) | stat: -rw-r--r-- 1,067 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
def has_print_function(tokens):
    p = 0
    while p < len(tokens):
        if tokens[p][0] != 'FROM':
            p += 1
            continue
        if tokens[p + 1][0:2] != ('NAME', '__future__'):
            p += 1
            continue
        if tokens[p + 2][0] != 'IMPORT':
            p += 1
            continue

        current = p + 3
        # ignore LEFT_PARENTHESIS token
        if tokens[current][0] == 'LEFT_PARENTHESIS':
            current += 1

        while (current < len(tokens) and tokens[current][0] == 'NAME'):
            if tokens[current][1] == 'print_function':
                return True

            # ignore AS and NAME tokens if present
            # anyway, ignore COMMA token
            if current + 1 < len(tokens) and tokens[current + 1][0] == 'AS':
                current += 4
            else:
                current += 2
        p += 1

    return False


def replace_print_by_name(tokens):
    def is_print(token):
        return token[0] == 'PRINT'

    return [('NAME', 'print') if is_print(x) else x for x in tokens]