Skip to content

Refactor: switch to typer to reduce boilerplate code for CLI #123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ dependencies = [
"cryptography",
"python-box",
"do-sdk-platform==1.0.0",
"typer",
]
dynamic = ["version"]

Expand Down
101 changes: 5 additions & 96 deletions src/cli/__init__.py
Original file line number Diff line number Diff line change
@@ -1,100 +1,9 @@
import functools
import sys
import warnings
import typer
from deeporigin.data_hub import api as data_hub_api

import cement
import termcolor
from deeporigin import auth

from .. import __version__
from ..exceptions import DeepOriginException
from ..warnings import DeepOriginWarning
from .config import CONTROLLERS as CONFIG_CONTROLLERS
from .context import CONTROLLERS as CONTEXT_CONTROLLERS
from .data import CONTROLLERS as DATA_CONTROLLERS
from .variables import CONTROLLERS as VARIABLE_CONTROLLERS

__all__ = ["main", "App"]


class BaseController(cement.Controller):
class Meta:
label = "base"
help = "Client for Deep Origin"
description = (
"Client for Deep Origin such as for downloading data and installing variables and "
"secrets into workstations."
)
arguments = [
(
["-v", "--version"],
{
"action": "store_true",
"help": "Display the version of this application",
},
),
]

@cement.ex(hide=True)
def _default(self):
args = self.app.pargs
if args.version:
print(__version__)
else:
raise SystemExit(self._parser.print_help())

@cement.ex(
help="Authenticate to Deep Origin",
)
def authenticate(self):
"""list the columns of the row and their values, where applicable"""
auth.get_tokens(refresh=False)


class App(cement.App):
class Meta:
label = "deeporigin"
base_controller = "base"
handlers = (
[BaseController]
+ VARIABLE_CONTROLLERS
+ CONTEXT_CONTROLLERS
+ DATA_CONTROLLERS
+ CONFIG_CONTROLLERS
)


def except_hook(built_in_except_hook, type, value, tb):
if issubclass(type, DeepOriginException):
sys.stderr.write(termcolor.colored(value, "red") + "\n")
else:
built_in_except_hook(type, value, tb)


def set_highlighted_except_hook():
built_in_except_hook = sys.excepthook

sys.excepthook = functools.partial(except_hook, built_in_except_hook)


def format_warning(built_in_format_warning, msg, category, *args, **kwargs):
if issubclass(category, DeepOriginWarning):
return termcolor.colored(str(msg), "yellow") + "\n"
else:
return built_in_format_warning(msg, category, *args, **kwargs)


def set_format_warning():
built_in_format_warning = warnings.formatwarning
warnings.formatwarning = functools.partial(format_warning, built_in_format_warning)
app = typer.Typer()
app.add_typer(data_hub_api.app, name="data")


def main():
set_highlighted_except_hook()
set_format_warning()
with App() as app:
app.run()


if __name__ == "__main__":
main()
app()
Loading
Loading