Skip to content

Commit

Permalink
chore: reorganize cli files
Browse files Browse the repository at this point in the history
  • Loading branch information
monosans committed Nov 25, 2024
1 parent b3c70d7 commit 53065c4
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 68 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ classifiers = [
dependencies = ["typing-extensions>=3.7.4.2"]
urls.documentation = "https://pyromark.readthedocs.io"
urls.repository = "https://github.com/monosans/pyromark"
scripts.pyromark = "pyromark.__main__:_main"
scripts.pyromark = "pyromark._cli:main"

[dependency-groups]
dev = ["mypy[faster-cache]==1.13", "pytest==8.3.3", "ruff==0.8.0"]
Expand Down
69 changes: 3 additions & 66 deletions python/pyromark/__main__.py
Original file line number Diff line number Diff line change
@@ -1,69 +1,6 @@
# ruff: noqa: T201
from __future__ import annotations

import argparse
import sys
from collections.abc import Iterator, Sequence
from contextlib import contextmanager
from typing import Optional

import pyromark


@contextmanager
def _parse_args(
args: Optional[Sequence[str]], /
) -> Iterator[argparse.Namespace]:
parser = argparse.ArgumentParser(
pyromark.__name__, description=pyromark.__doc__
)
parser.add_argument(
"-v", "--version", action="version", version=pyromark.__version__
)
parser.add_argument(
"-o",
"--output",
type=argparse.FileType("w", encoding="utf-8"),
help="output file path, default is stdout",
)
for opt_name in pyromark.Options.__members__:
parser.add_argument(
"--" + opt_name.lower().replace("_", "-"), action="store_true"
)
parser.add_argument(
"file",
type=argparse.FileType("r", encoding="utf-8"),
help="utf-8 input file path or '-' for stdin",
)
parsed_args = parser.parse_args(args)
try:
yield parsed_args
finally:
parsed_args.file.close()
if parsed_args.output is not None:
parsed_args.output.close()


def _main(args: Optional[Sequence[str]] = None, /) -> None:
with _parse_args(args) as parsed_args:
content = parsed_args.file.read()
parsed_args.file.close()

opts = pyromark.Options(0)
for opt_name, opt in pyromark.Options.__members__.items():
if getattr(parsed_args, opt_name.lower()):
opts |= opt

html = pyromark.html(content, options=opts)
if parsed_args.output is None:
print(html, end="")
else:
parsed_args.output.write(html)
parsed_args.output.close()


if __name__ == "__main__":
try:
_main()
except KeyboardInterrupt:
sys.exit(130)
from pyromark._cli import main

main()
55 changes: 55 additions & 0 deletions python/pyromark/_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# ruff: noqa: T201
from __future__ import annotations

import argparse
from collections.abc import Sequence
from typing import Optional

import pyromark


def _parse_args(args: Optional[Sequence[str]], /) -> argparse.Namespace:
parser = argparse.ArgumentParser(
pyromark.__name__, description=pyromark.__doc__
)
parser.add_argument(
"-v", "--version", action="version", version=pyromark.__version__
)
parser.add_argument(
"-o",
"--output",
type=argparse.FileType("w", encoding="utf-8"),
help="output file path, default is stdout",
)
for opt_name in pyromark.Options.__members__:
parser.add_argument(
"--" + opt_name.lower().replace("_", "-"), action="store_true"
)
parser.add_argument(
"file",
type=argparse.FileType("r", encoding="utf-8"),
help="utf-8 input file path or '-' for stdin",
)
return parser.parse_args(args)


def main(args: Optional[Sequence[str]] = None, /) -> None:
parsed_args = _parse_args(args)
try:
content = parsed_args.file.read()
parsed_args.file.close()

opts = pyromark.Options(0)
for opt_name, opt in pyromark.Options.__members__.items():
if getattr(parsed_args, opt_name.lower()):
opts |= opt

html = pyromark.html(content, options=opts)
if parsed_args.output is None:
print(html, end="")
else:
parsed_args.output.write(html)
finally:
parsed_args.file.close()
if parsed_args.output is not None:
parsed_args.output.close()
2 changes: 1 addition & 1 deletion tests/test_pyromark.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import pyromark
import pytest
from pyromark.__main__ import _main as pyromark_cli # noqa: PLC2701
from pyromark._cli import main as pyromark_cli # noqa: PLC2701

TABLE = """\
| foo | bar |
Expand Down

0 comments on commit 53065c4

Please sign in to comment.