コード例 #1
0
ファイル: cmdline.py プロジェクト: zenhack/hy
def hy2py_main():
    import platform
    module_name = "<STDIN>"

    options = dict(prog="hy2py", usage="%(prog)s [options] FILE",
                   formatter_class=argparse.RawDescriptionHelpFormatter)
    parser = argparse.ArgumentParser(**options)
    parser.add_argument("--with-source", "-s", action="store_true",
                        help="Show the parsed source structure")
    parser.add_argument("--with-ast", "-a", action="store_true",
                        help="Show the generated AST")
    parser.add_argument("--without-python", "-np", action="store_true",
                        help=("Do not show the Python code generated "
                              "from the AST"))
    parser.add_argument('args', nargs=argparse.REMAINDER,
                        help=argparse.SUPPRESS)

    options = parser.parse_args(sys.argv[1:])

    if not options.args:
        parser.exit(1, parser.format_help())

    if options.with_source:
        hst = import_file_to_hst(options.args[0])
        # need special printing on Windows in case the
        # codepage doesn't support utf-8 characters
        if PY3 and platform.system() == "Windows":
            for h in hst:
                try:
                    print(h)
                except:
                    print(str(h).encode('utf-8'))
        else:
            print(hst)
        print()
        print()

    _ast = import_file_to_ast(options.args[0], module_name)
    if options.with_ast:
        if PY3 and platform.system() == "Windows":
            _print_for_windows(astor.dump(_ast))
        else:
            print(astor.dump(_ast))
        print()
        print()

    if not options.without_python:
        if PY3 and platform.system() == "Windows":
            _print_for_windows(astor.codegen.to_source(_ast))
        else:
            print(astor.codegen.to_source(_ast))

    parser.exit(0)
コード例 #2
0
def hy2py_main():
    module_name = "<STDIN>"

    options = dict(prog="hy2py", usage="%(prog)s [options] FILE",
                   formatter_class=argparse.RawDescriptionHelpFormatter)
    parser = argparse.ArgumentParser(**options)
    parser.add_argument("--with-source", "-s", action="store_true",
                        help="Show the parsed source structure")
    parser.add_argument("--with-ast", "-a", action="store_true",
                        help="Show the generated AST")
    parser.add_argument("--without-python", "-np", action="store_true",
                        help=("Do not show the Python code generated "
                              "from the AST"))
    parser.add_argument('args', nargs=argparse.REMAINDER,
                        help=argparse.SUPPRESS)

    options = parser.parse_args(sys.argv[1:])

    if not options.args:
        parser.exit(1, parser.format_help())

    if options.with_source:
        hst = import_file_to_hst(options.args[0])
        print(hst)
        print()
        print()

    _ast = import_file_to_ast(options.args[0], module_name)
    if options.with_ast:
        print(astor.dump(_ast))
        print()
        print()

    if not options.without_python:
        print(astor.codegen.to_source(_ast))

    parser.exit(0)