-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
152 lines (124 loc) · 3.93 KB
/
Copy path__init__.py
File metadata and controls
152 lines (124 loc) · 3.93 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""An example CRUD CLI app."""
from __future__ import (
absolute_import,
print_function,
unicode_literals
)
import argparse
import json
import logging
import sys
TAGS = (
'red',
'orange',
'yellow',
'green',
'blue',
'violet',
'black',
'white',
'gray',
)
class CLI(object):
"""A really basic version of the famous Python Click library."""
log = logging.getLogger(__name__)
common_args = argparse.ArgumentParser(add_help=False)
log_group = common_args.add_mutually_exclusive_group()
log_group.add_argument(
'-v',
'--verbose',
dest='verbosity',
default=[logging.INFO],
action='append_const',
const=-10,
help='more verbose',
)
log_group.add_argument(
'-q',
'--quiet',
dest='verbosity',
action='append_const',
const=10,
help='less verbose',
)
def __init__(self):
self.parser = argparse.ArgumentParser()
self.subparsers = self.parser.add_subparsers(help='subcommands',
dest='command')
self.subparsers.required = True
def command(self, name, *args, **kwargs):
"""Register a function to the command-line interface."""
def wrapper(f):
f.parser = self.subparsers.add_parser(
name, *args, description=f.__doc__,
parents=[self.common_args], **kwargs)
if getattr(f, 'cli_args', None) is not None:
for fargs, fkwargs in f.cli_args:
f.parser.add_argument(*fargs, **fkwargs)
f.parser.set_defaults(action=f)
return f
return wrapper
def option(self, *args, **kwargs):
"""Register CLI arguments for function.
Accepts the same arguments as ArgumentParser().add_argument(...)
"""
def wrapper(f):
if getattr(f, 'cli_args', None) is None:
f.cli_args = []
f.cli_args.append((args, kwargs))
return f
return wrapper
def run(self):
"""Parse arguments and run the default action."""
args = self.parser.parse_args()
# init logging
log_level = max(logging.DEBUG, min(logging.CRITICAL, sum(args.verbosity)))
debug_on = log_level <= logging.DEBUG
logging.basicConfig(level=log_level)
kwargs = dict(vars(args))
# sanitize excess arguments, obiously there are better ways!
kwargs.pop('action', None)
kwargs.pop('command', None)
kwargs.pop('verbosity', None)
try:
# callback action
args.action(**kwargs)
except Exception as e:
self.log.error(e, exc_info=debug_on)
sys.exit(1)
sys.exit(0)
cli = CLI()
@cli.command('create')
@cli.option('name')
@cli.option('-s', '--size', type=int, choices=range(3))
@cli.option('-t', '--tag', choices=TAGS)
def create_command(name, size, tag):
"""Creates a resource."""
cli.log.info('creating resource')
cli.log.info(json.dumps({'name': name, 'size': size, 'tag': tag}))
@cli.command('update')
@cli.option('name')
@cli.option('-s', '--size', type=int, choices=range(3))
@cli.option('-t', '--tag', choices=TAGS)
def update_command(name, size=None, tag=None):
"""Updates a resource."""
cli.log.info('updating resource')
cli.log.info(json.dumps({'name': name, 'size': size, 'tag': tag}))
@cli.command('delete')
@cli.option('name')
def delete_command(name):
"""Delete a resource."""
cli.log.info('deleting resource %s' % name)
@cli.command('list')
def list_command():
"""Lists resources."""
cli.log.info('listing all resources')
@cli.command('find')
@cli.option('query')
def find_command(query):
"""Finds resources by query."""
cli.log.info('listing resources matching query %r' % query)
if __name__ == '__main__':
cli.run()