File size: 1,222 Bytes
096c926
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
39
40
41
42
43
44
45
46
47
48
49
from __future__ import print_function
import sys
import os
import argparse
from .unparser import roundtrip
from . import dump


def roundtrip_recursive(target, dump_tree=False):
    if os.path.isfile(target):
        print(target)
        print("=" * len(target))
        if dump_tree:
            dump(target)
        else:
            roundtrip(target)
        print()
    elif os.path.isdir(target):
        for item in os.listdir(target):
            if item.endswith(".py"):
                roundtrip_recursive(os.path.join(target, item), dump_tree)
    else:
        print(
            "WARNING: skipping '%s', not a file or directory" % target,
            file=sys.stderr
        )


def main(args):
    parser = argparse.ArgumentParser(prog="astunparse")
    parser.add_argument(
        'target',
        nargs='+',
        help="Files or directories to show roundtripped source for"
    )
    parser.add_argument(
        '--dump',
        type=bool,
        help="Show a pretty-printed AST instead of the source"
    )

    arguments = parser.parse_args(args)
    for target in arguments.target:
        roundtrip_recursive(target, dump_tree=arguments.dump)


if __name__ == "__main__":
    main(sys.argv[1:])