Skip to content

Commit 87acd81

Browse files
authored
Add cli configuration application (#418)
* Add a configuration typer application ... Creates a commands module to store the command logic in one location. Moves settings configuration into the config submodule in commands Attempts to create a clear logically distinction between cli logic and hub logic * Update the docstrings for both config commands * Add proper handling for local configuration file
1 parent 0b9a32d commit 87acd81

File tree

9 files changed

+417
-203
lines changed

9 files changed

+417
-203
lines changed

biothings/cli/__init__.py

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,40 @@
22
Entrypoint for the biothings-cli tool
33
"""
44

5+
from typing import Literal
56
import importlib.util
67
import logging
78
import os
89
import sys
910

11+
import typer
12+
from rich.logging import RichHandler
1013

11-
from biothings.cli.settings import (
12-
setup_biothings_configuration,
13-
setup_commandline_configuration,
14-
setup_logging_configuration,
15-
)
14+
from biothings.cli.commands.admin import build_admin_application
15+
from biothings.cli.commands.config import config_application, load_configuration
16+
from biothings.cli.commands.dataplugin import dataplugin_application
1617

1718

18-
def check_module_import_status(module: str) -> bool:
19+
def setup_logging_configuration(logging_level: Literal[10, 20, 30, 40, 50]) -> None:
1920
"""
20-
Verify that we can import a module prior to proceeding with creating our commandline
21-
tooling that depends on those modules
21+
Configures the logging based off our environment configuration
2222
"""
23-
module_specification = importlib.util.find_spec(module)
24-
status = module_specification is not None
25-
return status
23+
rich_handler = RichHandler(
24+
level=logging_level,
25+
markup=True,
26+
rich_tracebacks=False, # typer creates it already
27+
show_path=False,
28+
tracebacks_suppress=[typer],
29+
)
30+
logging.basicConfig(level=logging_level, format="%(message)s", datefmt="[%X]", handlers=[rich_handler])
2631

2732

2833
def main():
2934
"""
30-
The entrypoint for running the BioThings CLI to test your local data plugin
35+
The entrypoint for running the BioThings CLI
3136
"""
32-
typer_status = check_module_import_status("typer")
37+
module_specification = importlib.util.find_spec("typer")
38+
typer_status = module_specification is not None
3339
if not typer_status:
3440
logging.error(
3541
(
@@ -48,14 +54,13 @@ def main():
4854
cli_debug_flag = os.environ.get("BTCLI_DEBUG", False)
4955
cli_rich_traceback_flag = os.environ.get("BTCLI_RICH_TRACEBACK", False)
5056

51-
cli = setup_commandline_configuration(debug=cli_debug_flag, rich_traceback=cli_rich_traceback_flag)
57+
admin_application = build_admin_application(debug=cli_debug_flag, rich_traceback=cli_rich_traceback_flag)
5258
logging_level = logging.WARNING
5359
if cli_debug_flag:
5460
logging_level = logging.DEBUG
5561
setup_logging_configuration(logging_level)
56-
setup_biothings_configuration()
62+
load_configuration()
5763

58-
from biothings.cli.dataplugin import dataplugin_application
59-
60-
cli.add_typer(dataplugin_application, name="dataplugin")
61-
return cli()
64+
admin_application.add_typer(dataplugin_application, name="dataplugin")
65+
admin_application.add_typer(config_application, name="config")
66+
return admin_application()

biothings/cli/assistant.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class CLIAssistant(BaseAssistant):
3838

3939
plugin_type = "CLI"
4040

41-
def __init__(self, plugin_name: Optional[str] = None, job_manager: "JobManager" = None):
41+
def __init__(self, plugin_name: Optional[str] = None, job_manager: CLIJobManager = None):
4242
from biothings import config
4343
from biothings.hub.databuild.builder import BuilderManager
4444
from biothings.hub.dataindex.indexer import IndexManager

biothings/cli/commands/__init__.py

Whitespace-only changes.

biothings/cli/commands/admin.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""
2+
Configuration settings for the biothings-cli tool
3+
4+
> Logging
5+
> Tool Configuration
6+
> Creates a mock config used in the biothings.api backend
7+
"""
8+
9+
import sys
10+
11+
import typer
12+
13+
14+
def build_admin_application(debug: bool, rich_traceback: bool) -> typer.Typer:
15+
"""
16+
Builds the main administrative command line application for the
17+
biothings-cli application
18+
"""
19+
pretty_exceptions_show_locals = False
20+
pretty_exceptions_enable = False
21+
sys.tracebacklimit = 1
22+
23+
if rich_traceback:
24+
pretty_exceptions_enable = True
25+
sys.tracebacklimit = 1000
26+
27+
if debug:
28+
pretty_exceptions_enable = True
29+
pretty_exceptions_show_locals = True
30+
sys.tracebacklimit = 1000
31+
32+
# prevent dimming the help text from the 2nd line
33+
# see: https://github.com/tiangolo/typer/issues/437#issuecomment-1224149402
34+
typer.rich_utils.STYLE_HELPTEXT = ""
35+
36+
context_settings = {"help_option_names": ["-h", "--help"]}
37+
typer_instance = typer.Typer(
38+
help="[green]BioThings Admin CLI to test your local data plugins. See helps for each command for specific usage.[/green]",
39+
rich_help_panel="Help and Others",
40+
rich_markup_mode="rich",
41+
context_settings=context_settings,
42+
no_args_is_help=True,
43+
pretty_exceptions_show_locals=pretty_exceptions_show_locals,
44+
pretty_exceptions_enable=pretty_exceptions_enable,
45+
)
46+
47+
return typer_instance

0 commit comments

Comments
 (0)