コード例 #1
0
def import_file_to_module(module_name, fpath, lang):
    """Import content from fpath and put it into a Python module.
    If module_name already exists, the preexisting module
    is updated instead of a new module being created.
    If a new module is created, it is installed in sys.modules.

    Returns the module."""
    preexisting = module_name in sys.modules
    try:
        if lang == "hy":
            _ast = import_file_to_ast(fpath, module_name)
        mod = (sys.modules[module_name]
            if preexisting
            else imp.new_module(module_name))
        mod.__file__ = fpath
        if lang == "hy":
            eval(ast_compile(_ast, fpath, "exec"), mod.__dict__)
        elif lang == "python":
            exec(compile(open(fpath).read(), fpath, 'exec'), mod.__dict__)
        else:
            raise ValueError("Unknown language: {}".format(lang))
        sys.modules[module_name] = mod
    except (HyTypeError, LexException) as e:
        if e.source is None:
            with open(fpath, 'rt') as fp:
                e.source = fp.read()
            e.filename = fpath
        raise
    except Exception:
        if not preexisting:
            sys.modules.pop(module_name, None)
        raise
    return mod
コード例 #2
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)
コード例 #3
0
ファイル: loader.py プロジェクト: xlevus/dotops
def load_recipe(root: Path) -> None:
    """Import content from fpath and puts it into a Python module.
    Returns the module."""
    module_name = 'dotops.recipes.' + root.name
    fpath = find_recipe(root)

    try:
        _ast = import_file_to_ast(fpath, module_name)
        mod = imp.new_module(module_name)

        mod.__file__ = fpath
        eval(ast_compile(_ast, fpath, "exec"), _globals(root, mod))
    except (HyTypeError, LexException) as e:
        if e.source is None:
            with open(fpath, 'rt') as fp:
                e.source = fp.read()
            e.filename = fpath
        raise
    except Exception:
        sys.modules.pop(module_name, None)
        raise
コード例 #4
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)