Example #1
0
def run(filename: str):
    from prestring.python.parse import PyTreeDumper, parse_file

    PyTreeDumper().visit(parse_file(filename))
Example #2
0
from prestring.python.parse import parse_file, PyTreeDumper

t = parse_file("05config.py")
PyTreeDumper().visit(t)

# file_input [5 children]
#   simple_stmt [2 children]
#     import_from [4 children]
#       NAME('from') [lineno=1, column=0, prefix='']
#       NAME('handofcats') [lineno=1, column=5, prefix=' ']
#       NAME('import') [lineno=1, column=16, prefix=' ']
#       import_as_names [3 children]
#         NAME('Config') [lineno=1, column=23, prefix=' ']
#         COMMA(',') [lineno=1, column=29, prefix='']
#         NAME('as_subcommand') [lineno=1, column=31, prefix=' ']
#     NEWLINE('\n') [lineno=1, column=44, prefix='']
#   decorated [2 children]
#     decorator [3 children]
#       AT('@') [lineno=4, column=0, prefix='\n\n']
#       NAME('as_subcommand') [lineno=4, column=1, prefix='']
#       NEWLINE('\n') [lineno=4, column=14, prefix='']
#     funcdef[name='foo'] [5 children]
#       NAME('def') [lineno=5, column=0, prefix='']
#       NAME('foo') [lineno=5, column=4, prefix=' ']
#       parameters [2 children]
#         LPAR('(') [lineno=5, column=7, prefix='']
#         RPAR(')') [lineno=5, column=8, prefix='']
#       COLON(':') [lineno=5, column=9, prefix='']
#       suite [4 children]
#         NEWLINE('\n') [lineno=5, column=10, prefix='']
#         INDENT('    ') [lineno=6, column=0, prefix='']
Example #3
0
                                          id=id(node),
                                          from_=type_repr(node.type))
                sym_list.append(sym)

        # todo: relative
        for sym in sym_list:
            self.symbols[sym.name] = sym
        return True  # stop


def parse_imported_symbols(t: Node) -> t.Dict[str, Symbol]:
    v = CollectSymbolVisitor()
    v.visit(t)
    return v.symbols


t = parse_string("""
from handofcats import as_subcommand
from handofcats import as_subcommand as register
""")
D = parse_imported_symbols(t)
for name, sym in D.items():
    print(f"{name} -- {sym}")

print("\n--\n")

t = parse_file("_11parse_target.py")
D = parse_imported_symbols(t)
for name, sym in D.items():
    print(f"{name} -- {sym}")
Example #4
0
from prestring.python.parse import parse_file, PyTreeDumper
PyTreeDumper().visit(parse_file("hello.py"))
# file_input [2 children]
#   funcdef[name='hello'] [5 children]
#     NAME('def') [lineno=1, column=0, prefix='']
#     NAME('hello') [lineno=1, column=4, prefix=' ']
#     parameters [2 children]
#       LPAR('(') [lineno=1, column=9, prefix='']
#       RPAR(')') [lineno=1, column=10, prefix='']
#     COLON(':') [lineno=1, column=11, prefix='']
#     suite [7 children]
#       NEWLINE('\n') [lineno=1, column=12, prefix='']
#       INDENT('    ') [lineno=2, column=0, prefix='']
#       simple_stmt [2 children]
#         expr_stmt [3 children]
#           power [2 children]
#             NAME('hello') [lineno=2, column=4, prefix='']
#             trailer [2 children]
#               DOT('.') [lineno=2, column=9, prefix='']
#               NAME('__name__') [lineno=2, column=10, prefix='']
#           EQUAL('=') [lineno=2, column=19, prefix=' ']
#           STRING('"hellohello"') [lineno=2, column=21, prefix=' ']
#         NEWLINE('\n') [lineno=2, column=33, prefix='']
#       simple_stmt [2 children]
#         expr_stmt [3 children]
#           NAME('x') [lineno=3, column=4, prefix='    ']
#           EQUAL('=') [lineno=3, column=6, prefix=' ']
#           STRING('"world"') [lineno=3, column=8, prefix=' ']
#         NEWLINE('\n') [lineno=3, column=15, prefix='']
#       simple_stmt [2 children]
#         comparison [3 children]
Example #5
0
from prestring.python.parse import parse_file, dump_tree

t = parse_file("./hello.py")
print(str(t))
print("-")
dump_tree(t)
Example #6
0
from prestring.python.parse import parse_file, PyTreeDumper

t = parse_file("cli-exposed.py")
PyTreeDumper().visit(t)