Skip to content

Commit

Permalink
Merge pull request #18 from moshez/simplify-main
Browse files Browse the repository at this point in the history
simplify main
  • Loading branch information
moshez authored Jan 6, 2024
2 parents 41db9f6 + 693aeed commit 52564d8
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 97 deletions.
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ dynamic = ["version"]
description = "A Python tooling nexus"
readme = "README.rst"
authors = [{name = "Moshe Zadka", email = "moshez@zadka.club"}]
dependencies = ["gather>=2024.1.5.1", "copier", "tomlkit", "hyperlink"]
dependencies = ["gather>=2024.1.5.3", "copier", "tomlkit", "hyperlink", "toolz"]

[project.optional-dependencies]
tests = ["virtue", "pyhamcrest", "coverage"]
Expand Down Expand Up @@ -52,5 +52,5 @@ is_main_var = "GITHUB_REF"
is_main_match = ".*/trunk$"

[project.scripts]
nexor = "nexor.cli:main_command"
nexor-version = "nexor.cli:sub_command"
nexor = "nexor.cli:ENTRY_DATA.main_command"
nexor-version = "nexor.cli:ENTRY_DATA.sub_command"
6 changes: 5 additions & 1 deletion requirements-docs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ dunamai==1.19.0
# via copier
funcy==2.0
# via copier
gather==2024.1.5.1
gather==2024.1.5.3
# via nexor (pyproject.toml)
hyperlink==21.0.0
# via nexor (pyproject.toml)
Expand Down Expand Up @@ -103,6 +103,10 @@ sphinxcontrib-serializinghtml==1.1.9
# via sphinx
tomlkit==0.12.3
# via nexor (pyproject.toml)
toolz==0.12.0
# via
# gather
# nexor (pyproject.toml)
typing-extensions==4.9.0
# via
# pydantic
Expand Down
6 changes: 5 additions & 1 deletion requirements-lint.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ flake8==6.1.0
# via nexor (pyproject.toml)
funcy==2.0
# via copier
gather==2024.1.5.1
gather==2024.1.5.3
# via nexor (pyproject.toml)
hyperlink==21.0.0
# via nexor (pyproject.toml)
Expand Down Expand Up @@ -80,6 +80,10 @@ questionary==2.0.1
# via copier
tomlkit==0.12.3
# via nexor (pyproject.toml)
toolz==0.12.0
# via
# gather
# nexor (pyproject.toml)
typing-extensions==4.8.0
# via
# pydantic
Expand Down
6 changes: 5 additions & 1 deletion requirements-mypy.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ dunamai==1.19.0
# via copier
funcy==2.0
# via copier
gather==2024.1.5.1
gather==2024.1.5.3
# via nexor (pyproject.toml)
hyperlink==21.0.0
# via nexor (pyproject.toml)
Expand Down Expand Up @@ -65,6 +65,10 @@ questionary==2.0.1
# via copier
tomlkit==0.12.3
# via nexor (pyproject.toml)
toolz==0.12.0
# via
# gather
# nexor (pyproject.toml)
typing-extensions==4.8.0
# via
# mypy
Expand Down
6 changes: 5 additions & 1 deletion requirements-tests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ dunamai==1.19.0
# via copier
funcy==2.0
# via copier
gather==2024.1.5.1
gather==2024.1.5.3
# via nexor (pyproject.toml)
hyperlink==21.0.0
# via
Expand Down Expand Up @@ -85,6 +85,10 @@ six==1.16.0
# via automat
tomlkit==0.12.3
# via nexor (pyproject.toml)
toolz==0.12.0
# via
# gather
# nexor (pyproject.toml)
twisted==23.10.0
# via virtue
typing-extensions==4.8.0
Expand Down
21 changes: 4 additions & 17 deletions src/nexor/__main__.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,7 @@
import os
import logging
import subprocess
import sys

from gather import entry
from . import cli

if __name__ != "__main__":
raise ImportError("module cannot be imported", __name__)

logging.basicConfig(
format="%(asctime)s:%(levelname)s:%(name)s:%(message)s",
level=logging.INFO,
)
cli.main(
argv=sys.argv,
env=os.environ,
run=subprocess.run,
is_subcommand=globals().get("IS_SUBCOMMAND", False),
entry.dunder_main(
globals_dct=globals(),
command_data=cli.ENTRY_DATA,
)
53 changes: 5 additions & 48 deletions src/nexor/cli.py
Original file line number Diff line number Diff line change
@@ -1,54 +1,11 @@
import argparse
import logging
from typing import Sequence, Mapping, Callable
import runpy

import gather
from gather import commands as commandslib

from gather import entry
from . import __version__

LOGGER = logging.getLogger(__name__)

_SUBCOMMANDS = gather.Collector()

command = commandslib.make_command_register(_SUBCOMMANDS)
ENTRY_DATA = entry.EntryData.create("nexor")
command = ENTRY_DATA.register


@command()
def version(args):
@command(name="version")
def _version(args: argparse.ArgumentParser) -> None:
print(__version__)


def main(
*, argv: Sequence[str], env: Mapping[str, str], run: Callable, is_subcommand: bool
) -> None:
def error(args):
parser.print_help()
raise SystemExit(1)

parser = argparse.ArgumentParser()
parser.set_defaults(__gather_command__=error)

argv = list(argv)
if is_subcommand:
argv[0:0] = ["nexor"]
argv[1] = argv[1].rsplit("/", 1)[-1]
argv[1] = argv[1].removeprefix("nexor-")

commandslib.run_maybe_dry(
parser=commandslib.set_parser(parser=parser, collected=_SUBCOMMANDS.collect()),
argv=argv,
env=env,
sp_run=run,
)


def main_command(): # pragma: no cover
runpy.run_module("nexor", run_name="__main__")


def sub_command(): # pragma: no cover
runpy.run_module(
"nexor", run_name="__main__", init_globals=dict(IS_SUBCOMMAND=True)
)
30 changes: 9 additions & 21 deletions src/nexor/tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,27 @@
import contextlib
import unittest
import logging
from hamcrest import (
assert_that,
calling,
contains_string,
raises,
)
from unittest import mock
from io import StringIO

from gather import entry

from .. import cli


class TestMain(unittest.TestCase): # pragma: no cover
def test_help(self):
with contextlib.ExitStack() as stack:
stdout = stack.enter_context(mock.patch("sys.stdout", new=StringIO()))
assert_that(
calling(cli.main).with_args(
run=lambda: None,
argv=["nexor"],
env={},
is_subcommand=False,
),
raises(SystemExit),
)
assert_that(stdout.getvalue(), contains_string("usage:"))

def test_version(self):
with contextlib.ExitStack() as stack:
stdout = stack.enter_context(mock.patch("sys.stdout", new=StringIO()))
cli.main(
run=lambda: None,
argv=["nexor-version"],
env={},
is_subcommand=True,
argv = stack.enter_context(mock.patch("sys.argv", new=[]))
argv[:] = ["nexor", "version"]
entry.dunder_main(
globals_dct=dict(__name__="__main__"),
command_data=cli.ENTRY_DATA,
logger=logging.Logger("nonce"),
)
assert_that(stdout.getvalue(), contains_string("20"))
4 changes: 0 additions & 4 deletions src/nexor/tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@
from hamcrest import assert_that, contains_string

from .. import __version__
from .. import cli
from .. import lock


class TestInit(unittest.TestCase):
def test_version(self):
assert_that(__version__, contains_string("."))

def test_cli(self):
assert_that(cli.__doc__ or "", contains_string(""))

def test_lock(self):
assert_that(lock.__doc__ or "", contains_string(""))

0 comments on commit 52564d8

Please sign in to comment.