From 0890fdd951d021308550a0db2e7b6f2593512957 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Sat, 30 May 2020 17:27:04 +0200 Subject: initial site copied in --- cli/app/utils/click_utils.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 cli/app/utils/click_utils.py (limited to 'cli/app/utils/click_utils.py') diff --git a/cli/app/utils/click_utils.py b/cli/app/utils/click_utils.py new file mode 100644 index 0000000..83e4f75 --- /dev/null +++ b/cli/app/utils/click_utils.py @@ -0,0 +1,40 @@ +""" +Custom Click parameter types +""" +import click + +from app.settings import app_cfg +from app.models import types + +# -------------------------------------------------------- +# Click command helpers +# -------------------------------------------------------- + +def enum_to_names(enum_type): + return {x.name.lower(): x for x in enum_type} + +def show_help(enum_type): + names = enum_to_names(enum_type) + return 'Options: "{}"'.format(', '.join(list(names.keys()))) + +def get_default(opt): + return opt.name.lower() + + +# -------------------------------------------------------- +# Custom Click parameter class +# -------------------------------------------------------- + +class ParamVar(click.ParamType): + + name = 'default_type' + + def __init__(self, param_type): + self.opts = {x.name.lower(): x for x in param_type} + + def convert(self, value, param, ctx): + """converts (str) repr to Enum hash""" + try: + return self.opts[value.lower()] + except: + self.fail('{} is not a valid option'.format(value, param, ctx)) -- cgit v1.2.3-70-g09d2