-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add grouped magpie-helper cli + all specific cli helpers prefixed wit…
…h magpie_ when installed
- Loading branch information
1 parent
338629d
commit 552fd6d
Showing
5 changed files
with
108 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import argparse | ||
import importlib | ||
import os | ||
import sys | ||
|
||
from magpie.__meta__ import __version__ | ||
|
||
|
||
def magpie_helper_cli(): | ||
""" | ||
Groups all sub-helper CLI listed in :py:mod:`magpie.helpers` as a common ``magpie_helper``. | ||
Dispatches the provided arguments to the appropriate sub-helper CLI as requested. Each sub-helper CLI must implement | ||
functions ``make_parser`` and ``main`` to generate the arguments and dispatch them to the corresponding caller. | ||
""" | ||
parser = argparse.ArgumentParser(description="Execute Magpie helper operations.") | ||
parser.add_argument("--version", action="version", version="%(prog)s {}".format(__version__), | ||
help="prints the version of the library and exits") | ||
subparsers = parser.add_subparsers(title="Helper", dest="helper", description="Name of the helper to execute.") | ||
helpers_dir = os.path.dirname(__file__) | ||
helper_mods = os.listdir(helpers_dir) | ||
helpers = dict() | ||
for module_item in sorted(helper_mods): | ||
helper_path = os.path.join(helpers_dir, module_item) | ||
if os.path.isfile(helper_path) and "__init__" not in module_item and module_item.endswith(".py"): | ||
helper_name = module_item.replace(".py", "") | ||
helper_module = importlib.import_module(helper_name, "magpie.helpers") | ||
parser_maker = getattr(helper_module, "make_parser", None) | ||
helper_caller = getattr(helper_module, "main", None) | ||
if parser_maker and helper_caller: | ||
# add help disabled otherwise conflicts with this main helper's help | ||
helper_parser = parser_maker() | ||
subparsers.add_parser(helper_name, parents=[helper_parser], add_help=False) | ||
helpers[helper_name] = {"caller": helper_caller, "parser": helper_parser} | ||
args = sys.argv[1:] # save as was parse args does, but we must provide them to subparser | ||
ns = parser.parse_args() # if 'helper' is unknown, auto prints the help message with exit(2) | ||
helper_name = vars(ns).pop("helper") | ||
helper_args = args[1:] | ||
helper_caller = helpers[helper_name]["caller"] | ||
helper_parser = helpers[helper_name]["parser"] | ||
result = helper_caller(args=helper_args, parser=helper_parser, namespace=ns) | ||
return 0 if result is None else result | ||
|
||
|
||
if __name__ == "__main__": | ||
magpie_helper_cli() |
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,18 +1,31 @@ | ||
import argparse | ||
from typing import TYPE_CHECKING | ||
|
||
from magpie.constants import MAGPIE_INI_FILE_PATH | ||
from magpie.db import run_database_migration | ||
|
||
if TYPE_CHECKING: | ||
# pylint: disable=W0611,unused-import | ||
from typing import Any, AnyStr, Optional, Sequence # noqa: F401 | ||
|
||
def run_database_migration_cli(): | ||
parser = argparse.ArgumentParser(description="Run Magpie database migration") | ||
|
||
def make_parser(): | ||
# type: () -> argparse.ArgumentParser | ||
parser = argparse.ArgumentParser(description="Run Magpie database migration.") | ||
parser.add_argument("-c", "--config-file", metavar="config_file", dest="config_file", type=str, | ||
default=MAGPIE_INI_FILE_PATH, | ||
help="configuration file to employ for database connection settings " | ||
help="Configuration file to employ for database connection settings " | ||
"(default: MAGPIE_INI_FILE_PATH='%(default)s)'") | ||
args = parser.parse_args() | ||
run_database_migration(settings={"magpie.ini_file_path": args.config_file}) | ||
return parser | ||
|
||
|
||
def main(args=None, parser=None, namespace=None): | ||
# type: (Optional[Sequence[AnyStr]], Optional[argparse.ArgumentParser], Optional[argparse.Namespace]) -> Any | ||
if not parser: | ||
parser = make_parser() | ||
args = parser.parse_args(args=args, namespace=namespace) | ||
return run_database_migration(settings={"magpie.ini_file_path": args.config_file}) | ||
|
||
|
||
if __name__ == "__main__": | ||
run_database_migration_cli() | ||
main() |
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