Make hy2py public.

This commit is contained in:
Berker Peksag 2014-04-10 07:45:11 +03:00
parent bdd8e3c82e
commit cdea12b276
5 changed files with 79 additions and 44 deletions

View File

@ -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 = "<STDIN>"
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))

View File

@ -2,6 +2,8 @@
Command Line Interface Command Line Interface
====================== ======================
.. _hy:
hy hy
-- --
@ -46,6 +48,8 @@ Command line options
Print the Hy version number and exit. Print the Hy version number and exit.
.. _hyc:
hyc hyc
--- ---
@ -71,3 +75,29 @@ Command line options
$ hyc hyname.hy $ hyc hyname.hy
$ python hyname.pyc $ python hyname.pyc
Hy Afroman! 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.

View File

@ -25,16 +25,21 @@
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE. # DEALINGS IN THE SOFTWARE.
from __future__ import print_function
import argparse import argparse
import code import code
import ast import ast
import sys import sys
import astor.codegen
import hy import hy
from hy.lex import LexException, PrematureEndOfInput, tokenize from hy.lex import LexException, PrematureEndOfInput, tokenize
from hy.compiler import hy_compile, HyTypeError 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.completer import completion
from hy.macros import macro, require from hy.macros import macro, require
@ -66,7 +71,6 @@ builtins.exit = HyQuitter('exit')
def print_python_code(_ast): def print_python_code(_ast):
import astor.codegen
# astor cannot handle ast.Interactive, so disguise it as a module # astor cannot handle ast.Interactive, so disguise it as a module
_ast_for_print = ast.Module() _ast_for_print = ast.Module()
_ast_for_print.body = _ast.body _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" % sys.stderr.write("hyc: Can't open file '%s': [Errno %d] %s\n" %
(x.filename, x.errno, x.strerror)) (x.filename, x.errno, x.strerror))
sys.exit(x.errno) sys.exit(x.errno)
# entry point for cmd line script "hy2py"
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)

View File

@ -59,7 +59,8 @@ setup(
entry_points={ entry_points={
'console_scripts': [ 'console_scripts': [
'hy = hy.cmdline:hy_main', '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*']), packages=find_packages(exclude=['tests*']),

View File

@ -129,7 +129,7 @@ def test_hy2py():
for f in filenames: for f in filenames:
if f.endswith(".hy"): if f.endswith(".hy"):
i += 1 i += 1
ret = run_cmd("python bin/hy2py -s -a " ret = run_cmd("hy2py -s -a "
+ os.path.join(dirpath, f)) + os.path.join(dirpath, f))
assert ret[0] == 0, f assert ret[0] == 0, f
assert len(ret[1]) > 1, f assert len(ret[1]) > 1, f