-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from moshez/simplify-main
simplify main
- Loading branch information
Showing
9 changed files
with
41 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters