Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
nineteendo committed Aug 3, 2024
1 parent 9818d27 commit 120440d
Showing 1 changed file with 16 additions and 22 deletions.
38 changes: 16 additions & 22 deletions src/jsonyx/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
__all__: list[str] = ["JSONNamespace", "register", "run"]

import sys
from pathlib import Path
from sys import stderr, stdin
from typing import TYPE_CHECKING

from jsonyx import JSONSyntaxError, dump, format_syntax_error, loads
from jsonyx import Decoder, JSONSyntaxError, dump, format_syntax_error
from jsonyx.allow import EVERYTHING, NOTHING, SURROGATES

if TYPE_CHECKING:
Expand Down Expand Up @@ -37,11 +36,6 @@ def register(parser: ArgumentParser) -> None:
:param parser: an argument parser
:type parser: ArgumentParser
"""
parser.add_argument(
"filename",
nargs="?",
help="the JSON file to be validated or pretty-printed",
)
parser.add_argument(
"-a",
"--ensure-ascii",
Expand Down Expand Up @@ -101,6 +95,11 @@ def register(parser: ArgumentParser) -> None:
dest="indent",
help="indent using tabs",
)
parser.add_argument(
"filename",
nargs="?",
help="the JSON file to be validated or pretty-printed",
)


def run(args: JSONNamespace) -> None:
Expand All @@ -109,22 +108,17 @@ def run(args: JSONNamespace) -> None:
:param args: the commandline arguments
:type args: JSONNamespace
"""
if args.filename:
filename: str = args.filename
s: bytes | str = Path(filename).read_bytes()
else:
filename = "<stdin>"
s = "\n".join(
iter(input, ""),
) if stdin.isatty() else stdin.buffer.read()

decoder: Decoder = Decoder(
allow=EVERYTHING - SURROGATES if args.nonstrict else NOTHING,
use_decimal=args.use_decimal,
)
try:
obj: object = loads(
s,
allow=EVERYTHING - SURROGATES if args.nonstrict else NOTHING,
filename=filename,
use_decimal=args.use_decimal,
)
if args.filename:
obj: object = decoder.read(args.filename)
elif stdin.isatty():
obj = decoder.loads("\n".join(iter(input, "")), filename="<stdin>")
else:
obj = decoder.load(stdin)
except JSONSyntaxError as exc:
stderr.write("".join(format_syntax_error(exc)))
sys.exit(1)
Expand Down

0 comments on commit 120440d

Please sign in to comment.