summaryrefslogtreecommitdiff
path: root/megapixels/app/utils/click_utils.py
blob: dc00f58cb4d704fc47a8db8c6d0ee6bb7421784a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""
Custom Click parameter types
"""
import click

from app.settings import app_cfg as cfg
from app.settings 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.name = '{}'.format(param_type.name.lower())
    # sealf.
    self.ops =  {x.name.lower(): x for x in param_type}
  
  def convert(self, value, param, ctx):
    """converts (str) repr to Enum hash"""
    try:
      return self.ops[value.lower()]
    except:
      self.fail('{} is not a valid option'.format(value, param, ctx))