|
1 | 1 | import argparse |
| 2 | +import pprint |
2 | 3 | import sys |
3 | 4 |
|
4 | | -import bblfsh |
5 | | -from bblfsh.pyuast import filter |
6 | | - |
7 | 5 | from bblfsh.client import BblfshClient |
8 | 6 | from bblfsh.launcher import ensure_bblfsh_is_running |
9 | 7 |
|
10 | 8 |
|
11 | | -def setup(): |
| 9 | +def setup() -> argparse.Namespace: |
12 | 10 | parser = argparse.ArgumentParser( |
13 | 11 | description="Query for a UAST to Babelfish and dump it to stdout." |
14 | 12 | ) |
15 | 13 | parser.add_argument("-e", "--endpoint", default="0.0.0.0:9432", |
16 | | - help="bblfsh gRPC endpoint.") |
| 14 | + help="bblfsh gRPC endpoint.", type=str) |
17 | 15 | parser.add_argument("-f", "--file", required=True, |
18 | | - help="File to parse.") |
| 16 | + help="File to parse.", type=str) |
19 | 17 | parser.add_argument("-l", "--language", default=None, |
20 | | - help="File's language. The default is to autodetect.") |
| 18 | + help="File's language. The default is to autodetect.", type=str) |
21 | 19 | parser.add_argument("--disable-bblfsh-autorun", action="store_true", |
22 | 20 | help="Do not automatically launch Babelfish server " |
23 | 21 | "if it is not running.") |
24 | 22 |
|
25 | | - parser.add_argument("-q", "--query", default="", help="xpath query") |
26 | | - parser.add_argument("-m", "--mapn", default="", help="transform function of the results (n)") |
27 | | - parser.add_argument("-a", "--array", help='print results as an array', action='store_true') |
| 23 | + parser.add_argument("-q", "--query", default="", help="xpath query", type=str) |
| 24 | + parser.add_argument("-a", "--array", help='print results as a parseable Python array', action='store_true') |
28 | 25 |
|
29 | | - args = parser.parse_args() |
30 | | - return args |
| 26 | + return parser.parse_args() |
31 | 27 |
|
32 | | -def run_query(root: bblfsh.Node, query: str, mapn: str, as_array: bool) -> None: |
33 | | - result = list(filter(root, query)) |
34 | 28 |
|
35 | | - if not result: |
| 29 | +def run_query(uast, query: str, array: bool) -> None: |
| 30 | + result_iter = uast.filter(query) |
| 31 | + if not result_iter: |
36 | 32 | print("Nothing found") |
37 | 33 |
|
38 | | - else: |
39 | | - if mapn: |
40 | | - result = [eval(mapn) for n in result] |
| 34 | + result_list = [x.load() for x in result_iter] |
41 | 35 |
|
42 | | - if as_array: |
43 | | - print("results[{}] = {}".format(len(result), result)) |
44 | | - else: |
45 | | - print("Running xpath query: {}".format(query)) |
46 | | - print("FOUND {} roots".format(len(result))) |
| 36 | + if array: |
| 37 | + pprint.pprint(result_list) |
| 38 | + else: |
| 39 | + print("%d Results:" % len(result_list)) |
| 40 | + for i, node in enumerate(result_list): |
| 41 | + print("== {} ==================================".format(i+1)) |
| 42 | + print(node) |
47 | 43 |
|
48 | | - for i, node in enumerate(result): |
49 | | - print("== {} ==================================".format(i+1)) |
50 | | - print(node) |
51 | 44 |
|
52 | | -def main(): |
| 45 | +def main() -> int: |
53 | 46 | args = setup() |
54 | 47 | if not args.disable_bblfsh_autorun: |
55 | 48 | ensure_bblfsh_is_running() |
56 | 49 |
|
57 | 50 | client = BblfshClient(args.endpoint) |
58 | | - response = client.parse(args.file, args.language) |
59 | | - root = response.uast |
60 | | - if len(response.errors): |
61 | | - sys.stderr.write("\n".join(response.errors) + "\n") |
62 | | - query = args.query |
63 | | - if query: |
64 | | - run_query(root, query, args.mapn, args.array) |
| 51 | + ctx = client.parse(args.file, args.language) |
| 52 | + |
| 53 | + if args.query: |
| 54 | + run_query(ctx, args.query, array=args.array) |
65 | 55 | else: |
66 | | - print(root) |
| 56 | + pprint.pprint(ctx.load()) |
| 57 | + |
| 58 | + return 0 |
| 59 | + |
67 | 60 |
|
68 | 61 | if __name__ == "__main__": |
69 | 62 | sys.exit(main()) |
0 commit comments