Skip to content

Commit

Permalink
add --no-eval option
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshix-1 committed Oct 1, 2024
1 parent b201822 commit 1cda1b2
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions typed_stream/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class Options:
debug: bool
bytes: bool
keep_ends: bool
no_eval: bool
actions: tuple[str, ...]


Expand All @@ -53,7 +54,8 @@ def run_program(options: Options) -> str | None: # noqa: C901
... debug=True,
... bytes=False,
... keep_ends=False,
... actions=("map", "int", "sum")
... no_eval=False,
... actions=("map", "int", "sum"),
... ))
1234
>>> print("\\n".join(err.getvalue().split("\\n")[-2:]))
Expand All @@ -65,6 +67,7 @@ def run_program(options: Options) -> str | None: # noqa: C901
... debug=True,
... bytes=False,
... keep_ends=False,
... no_eval=False,
... actions=("map", "int", "collect", "builtins.sum")
... ))
1324
Expand All @@ -77,6 +80,7 @@ def run_program(options: Options) -> str | None: # noqa: C901
... debug=True,
... bytes=False,
... keep_ends=False,
... no_eval=False,
... actions=("map", "int", "(°_°)")
... ))
>>> assert not err.getvalue()
Expand All @@ -89,6 +93,7 @@ def run_program(options: Options) -> str | None: # noqa: C901
... debug=True,
... bytes=False,
... keep_ends=False,
... no_eval=False,
... actions=("map", "xxx")
... ))
>>> assert not err.getvalue()
Expand All @@ -101,6 +106,19 @@ def run_program(options: Options) -> str | None: # noqa: C901
... debug=True,
... bytes=False,
... keep_ends=False,
... no_eval=True,
... actions=("map", "xxx")
... ))
>>> assert not err.getvalue()
>>> print(ret)
Can't parse 'xxx' without eval.
>>> sys.stdin = io.StringIO("")
>>> with contextlib.redirect_stderr(io.StringIO()) as err:
... ret = run_program(Options(
... debug=True,
... bytes=False,
... keep_ends=False,
... no_eval=True,
... actions=("map", "int", "collect", "sum")
... ))
>>> assert not err.getvalue()
Expand All @@ -113,6 +131,7 @@ def run_program(options: Options) -> str | None: # noqa: C901
... debug=True,
... bytes=True,
... keep_ends=True,
... no_eval=True,
... actions=("flat_map", "iter", "map", "hex", "collect", "Counter")
... ))
Counter({'0x30': 6, '0xa': 3, '0x32': 1, '0x31': 1, '0x33': 1, '0x34': 1})
Expand All @@ -125,6 +144,7 @@ def run_program(options: Options) -> str | None: # noqa: C901
... debug=True,
... bytes=False,
... keep_ends=True,
... no_eval=False,
... actions=("map", "int", "filter", "is_even", "map", "mul", "10")
... ))
20
Expand Down Expand Up @@ -203,6 +223,11 @@ def run_program(options: Options) -> str | None: # noqa: C901
elif action in operator.__all__:
args.append(getattr(operator, action))
full_action_qual = f"operator.{action}"
elif hasattr(builtins, action):
args.append(getattr(builtins, action))
full_action_qual = f"{action}"
elif options.no_eval:
return f"Can't parse {action!r} without eval."
else:
try:
# pylint: disable-next=eval-used
Expand Down Expand Up @@ -247,14 +272,16 @@ def main() -> str | None: # noqa: C901
)
arg_parser.add_argument("--debug", action="store_true")
arg_parser.add_argument("--bytes", action="store_true")
arg_parser.add_argument("--keep_ends", action="store_true")
arg_parser.add_argument("--keep-ends", action="store_true")
arg_parser.add_argument("--no-eval", action="store_true")
arg_parser.add_argument("actions", nargs="+")

args = arg_parser.parse_args()
options = Options(
debug=bool(args.debug),
bytes=bool(args.bytes),
keep_ends=bool(args.keep_ends),
no_eval=bool(args.no_eval),
actions=tuple(map(str, args.actions)),
)
if options.actions and options.actions[0] == "help":
Expand Down

0 comments on commit 1cda1b2

Please sign in to comment.