Skip to content

Commit

Permalink
Add output filename
Browse files Browse the repository at this point in the history
  • Loading branch information
nineteendo committed Aug 3, 2024
1 parent 120440d commit 94f8bce
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 42 deletions.
4 changes: 2 additions & 2 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
copyright: str = "2024, Nice Zombies" # noqa: A001
author: str = "Nice Zombies"

release: str = "1.1"
version: str = "1.1.0"
release: str = "1.2"
version: str = "1.2.0"

# -- General configuration

Expand Down
28 changes: 9 additions & 19 deletions docs/source/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Using Decimal instead of float::
>>> json.loads("1.1", use_decimal=True)
Decimal('1.1')
>>> json.dump(Decimal("1.1"))
'1.1'
1.1

Using :mod:`jsonyx` from the shell to validate and pretty-print:

Expand Down Expand Up @@ -219,31 +219,19 @@ Command Line Interface
The :mod:`jsonyx` module provides a simple command line interface to
validate and pretty-print JSON objects.

If the optional ``filename`` argument is not specified, :data:`sys.stdin` will
be used:

.. code-block:: shell-session
$ echo '{"json": "obj"}' | python -m jsonyx --indent 4
{
"json": "obj"
}
$ echo '{1.2: 3.4}' | python -m jsonyx
File "<stdin>", line 1, column 2
{1.2: 3.4}
^
jsonyx._decoder.JSONSyntaxError: Expecting string
If the optional ``input_filename`` and ``output_filename`` arguments are not
specified, :data:`sys.stdin` and :data:`sys.stdout` will be used respectively.

Command line options
^^^^^^^^^^^^^^^^^^^^

.. option:: filename
.. option:: input_filename

The JSON file to be validated or pretty-printed:
The path to the input JSON file, or "-" for standard input.

.. code-block:: shell-session
$ python -m jsonyx mp_films.json
$ python -m jsonyx mp_films.json --indent 4
[
{
"title": "And Now for Something Completely Different",
Expand All @@ -255,7 +243,9 @@ Command line options
}
]
If *filename* is not specified, read from :data:`sys.stdin`.
.. option:: output_filename

The path to the output JSON file.

.. option:: -h, --help

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "jsonyx"
version = "1.1.0"
version = "1.2.0"
authors = [
{ name="Nice Zombies", email="nineteendo19d0@gmail.com" },
]
Expand Down
49 changes: 29 additions & 20 deletions src/jsonyx/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
from sys import stderr, stdin
from typing import TYPE_CHECKING

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

if TYPE_CHECKING:
from argparse import ArgumentParser
Expand All @@ -22,9 +22,10 @@ class JSONNamespace:
compact: bool
ensure_ascii: bool
indent: int | str | None
filename: str | None
input_filename: str | None
no_commas: bool
nonstrict: bool
output_filename: str | None
sort_keys: bool
trailing_comma: bool
use_decimal: bool
Expand Down Expand Up @@ -96,9 +97,14 @@ def register(parser: ArgumentParser) -> None:
help="indent using tabs",
)
parser.add_argument(
"filename",
"input_filename",
nargs="?",
help="the JSON file to be validated or pretty-printed",
help='the path to the input JSON file, or "-" for standard input',
)
parser.add_argument(
"output_filename",
nargs="?",
help="the path to the output JSON file",
)


Expand All @@ -109,23 +115,11 @@ def run(args: JSONNamespace) -> None:
:type args: JSONNamespace
"""
decoder: Decoder = Decoder(
allow=EVERYTHING - SURROGATES if args.nonstrict else NOTHING,
allow=EVERYTHING if args.nonstrict else NOTHING,
use_decimal=args.use_decimal,
)
try:
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)

dump(
obj,
allow=EVERYTHING - SURROGATES if args.nonstrict else NOTHING,
encoder: Encoder = Encoder(
allow=EVERYTHING if args.nonstrict else NOTHING,
ensure_ascii=args.ensure_ascii,
indent=args.indent,
item_separator=" " if args.no_commas else (
Expand All @@ -135,3 +129,18 @@ def run(args: JSONNamespace) -> None:
sort_keys=args.sort_keys,
trailing_comma=args.trailing_comma,
)
try:
if args.input_filename and args.input_filename != "-":
obj: object = decoder.read(args.input_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)

if args.output_filename:
encoder.write(obj, args.output_filename)
else:
encoder.dump(obj)

0 comments on commit 94f8bce

Please sign in to comment.