Skip to content

Commit

Permalink
Add -v and --unparser argument
Browse files Browse the repository at this point in the history
Allow users to check the version and choose the unparser.
By default the unparser will be ast.unparse for compatibility
  • Loading branch information
yunline committed Dec 3, 2024
1 parent 04df05b commit ed0d5fb
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
7 changes: 5 additions & 2 deletions oneliner/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
import symtable


def convert_code_string(code: str, filename="<string>"):
def convert_code_string(code: str, filename="<string>", use_new_unparser=False):
ast_root = ast.parse(code, filename, "exec")
symtable_root = symtable.symtable(code, filename, "exec")
out = OnelinerConvertor().cvt(ast_root, symtable_root)
return unparse(out)
if use_new_unparser:
return unparse(out)
else:
return ast.unparse(out).replace("\n", "")
22 changes: 21 additions & 1 deletion oneliner/__main__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import argparse

import oneliner
import oneliner.version

parser = argparse.ArgumentParser(
description="Convert python scripts into oneliner expression."
Expand All @@ -12,6 +13,15 @@
help="The filename of the python script to be converted",
)

ver = oneliner.version.oneliner_version
dev = "-dev" if oneliner.version.dev else ""
parser.add_argument(
"-v",
"--version",
action="version",
version=f"Oneliner-Py-{ver[0]}.{ver[1]}.{ver[2]}{dev}",
)

parser.add_argument(
"-o",
"--output",
Expand All @@ -20,12 +30,22 @@
"Oneliner-Py will print the result to the screen.",
)

parser.add_argument(
"--unparser",
type=str,
default="ast.unparse",
choices=["ast.unparse", "oneliner"],
)

args = parser.parse_args()

with open(args.input_filename, "r", encoding="utf8") as infile:
script = infile.read()

converted = oneliner.convert_code_string(script)
if args.unparser == "ast.unparse":
converted = oneliner.convert_code_string(script, use_new_unparser=False)
else:
converted = oneliner.convert_code_string(script, use_new_unparser=True)

if args.output is not None:
with open(args.output, "w", encoding="utf8") as outfile:
Expand Down
2 changes: 1 addition & 1 deletion oneliner/version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
oneliner_version = (1,1,0)
oneliner_version = (1, 1, 0)
dev = True

0 comments on commit ed0d5fb

Please sign in to comment.