Skip to content

Commit

Permalink
Make module executable
Browse files Browse the repository at this point in the history
  • Loading branch information
nineteendo committed Aug 3, 2024
1 parent 59cf3fb commit d194d58
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 26 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.0"
version: str = "1.0.8"
release: str = "1.1"
version: str = "1.1.0"

# -- General configuration

Expand Down
16 changes: 8 additions & 8 deletions docs/source/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ Using Decimal instead of float::
>>> json.dump(Decimal("1.1"))
'1.1'

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

.. code-block:: shell-session
$ echo '{"json": "obj"}' | python -m jsonyx.tool --indent 4
$ echo '{"json": "obj"}' | python -m jsonyx --indent 4
{
"json": "obj"
}
$ echo '{1.2: 3.4}' | python -m jsonyx.tool
$ echo '{1.2: 3.4}' | python -m jsonyx
File "<stdin>", line 1, column 2
{1.2: 3.4}
^
Expand Down Expand Up @@ -211,24 +211,24 @@ Exceptions
.. autoexception:: jsonyx.JSONSyntaxError

.. _json-commandline:
.. program:: jsonyx.tool
.. program:: jsonyx

Command Line Interface
----------------------

The :mod:`jsonyx.tool` module provides a simple command line interface to
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.tool --indent 4
$ echo '{"json": "obj"}' | python -m jsonyx --indent 4
{
"json": "obj"
}
$ echo '{1.2: 3.4}' | python -m jsonyx.tool
$ echo '{1.2: 3.4}' | python -m jsonyx
File "<stdin>", line 1, column 2
{1.2: 3.4}
^
Expand All @@ -243,7 +243,7 @@ Command line options

.. code-block:: shell-session
$ python -m jsonyx.tool mp_films.json
$ python -m jsonyx mp_films.json
[
{
"title": "And Now for Something Completely Different",
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.0.8"
version = "1.1.0"
authors = [
{ name="Nice Zombies", email="[email protected]" },
]
Expand Down
27 changes: 27 additions & 0 deletions src/jsonyx/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env python
# Copyright (C) 2024 Nice Zombies
"""A command line utility to validate and pretty-print JSON objects."""
from __future__ import annotations

__all__: list[str] = []

import sys
from argparse import ArgumentParser

from jsonyx.tool import JSONNamespace, register, run


def _main() -> None:
parser: ArgumentParser = ArgumentParser(
description="a command line utility to validate and pretty-print JSON "
"objects.",
)
register(parser)
try:
run(parser.parse_args(namespace=JSONNamespace()))
except BrokenPipeError as exc:
sys.exit(exc.errno)


if __name__ == "__main__":
_main()
19 changes: 4 additions & 15 deletions src/jsonyx/tool.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
#!/usr/bin/env python
# Copyright (C) 2024 Nice Zombies
"""JSON tool."""
from __future__ import annotations

__all__: list[str] = ["JSONNamespace", "register", "run"]

import sys
from argparse import ArgumentParser
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.allow import EVERYTHING, NOTHING, SURROGATES

if TYPE_CHECKING:
from argparse import ArgumentParser


# pylint: disable-next=R0903
class JSONNamespace:
Expand Down Expand Up @@ -120,16 +122,3 @@ def run(args: JSONNamespace) -> None:
sort_keys=args.sort_keys,
trailing_comma=args.trailing_comma,
)


def _main() -> None:
parser: ArgumentParser = ArgumentParser()
register(parser)
try:
run(parser.parse_args(namespace=JSONNamespace()))
except BrokenPipeError as exc:
sys.exit(exc.errno)


if __name__ == "__main__":
_main()

0 comments on commit d194d58

Please sign in to comment.