From cdea12b2764d5c417df55a9499ad3bde17fb3e0c Mon Sep 17 00:00:00 2001 From: Berker Peksag Date: Thu, 10 Apr 2014 07:45:11 +0300 Subject: [PATCH] Make hy2py public. --- bin/hy2py | 40 ------------------------------------ docs/language/cli.rst | 30 +++++++++++++++++++++++++++ hy/cmdline.py | 48 +++++++++++++++++++++++++++++++++++++++++-- setup.py | 3 ++- tests/test_bin.py | 2 +- 5 files changed, 79 insertions(+), 44 deletions(-) delete mode 100755 bin/hy2py diff --git a/bin/hy2py b/bin/hy2py deleted file mode 100755 index 69b151b..0000000 --- a/bin/hy2py +++ /dev/null @@ -1,40 +0,0 @@ -#!/usr/bin/env python -from __future__ import print_function - -from hy.importer import import_file_to_ast, import_file_to_hst - -import argparse -import sys - -import astor.codegen - -module_name = "" - -parser = argparse.ArgumentParser( - prog="hy2py", - usage="%(prog)s [options] FILE", - formatter_class=argparse.RawDescriptionHelpFormatter) -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 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)) diff --git a/docs/language/cli.rst b/docs/language/cli.rst index ab9d8aa..98e853e 100644 --- a/docs/language/cli.rst +++ b/docs/language/cli.rst @@ -2,6 +2,8 @@ Command Line Interface ====================== +.. _hy: + hy -- @@ -46,6 +48,8 @@ Command line options Print the Hy version number and exit. +.. _hyc: + hyc --- @@ -71,3 +75,29 @@ Command line options $ hyc hyname.hy $ python hyname.pyc Hy Afroman! + + +.. _hy2py: + +hy2py +----- + +.. versionadded:: 0.10.1 + +Command line options +^^^^^^^^^^^^^^^^^^^^ + +.. cmdoption:: -s + --with-source + + Show the parsed source structure. + +.. cmdoption:: -a + --with-ast + + Show the generated AST. + +.. cmdoption:: -np + --without-python + + Do not show the Python code generated from the AST. diff --git a/hy/cmdline.py b/hy/cmdline.py index 98f10cd..6ccbb8f 100644 --- a/hy/cmdline.py +++ b/hy/cmdline.py @@ -25,16 +25,21 @@ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER # DEALINGS IN THE SOFTWARE. +from __future__ import print_function + import argparse import code import ast import sys +import astor.codegen + import hy from hy.lex import LexException, PrematureEndOfInput, tokenize from hy.compiler import hy_compile, HyTypeError -from hy.importer import ast_compile, import_buffer_to_module +from hy.importer import (ast_compile, import_buffer_to_module, + import_file_to_ast, import_file_to_hst) from hy.completer import completion from hy.macros import macro, require @@ -66,7 +71,6 @@ builtins.exit = HyQuitter('exit') def print_python_code(_ast): - import astor.codegen # astor cannot handle ast.Interactive, so disguise it as a module _ast_for_print = ast.Module() _ast_for_print.body = _ast.body @@ -313,3 +317,43 @@ def hyc_main(): sys.stderr.write("hyc: Can't open file '%s': [Errno %d] %s\n" % (x.filename, x.errno, x.strerror)) sys.exit(x.errno) + + +# entry point for cmd line script "hy2py" +def hy2py_main(): + module_name = "" + + 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) diff --git a/setup.py b/setup.py index 40bb4e0..b357a9e 100755 --- a/setup.py +++ b/setup.py @@ -59,7 +59,8 @@ setup( entry_points={ 'console_scripts': [ 'hy = hy.cmdline:hy_main', - 'hyc = hy.cmdline:hyc_main' + 'hyc = hy.cmdline:hyc_main', + 'hy2py = hy.cmdline:hy2py_main', ] }, packages=find_packages(exclude=['tests*']), diff --git a/tests/test_bin.py b/tests/test_bin.py index eb0d595..ed74ae8 100644 --- a/tests/test_bin.py +++ b/tests/test_bin.py @@ -129,7 +129,7 @@ def test_hy2py(): for f in filenames: if f.endswith(".hy"): i += 1 - ret = run_cmd("python bin/hy2py -s -a " + ret = run_cmd("hy2py -s -a " + os.path.join(dirpath, f)) assert ret[0] == 0, f assert len(ret[1]) > 1, f