-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathcli.py
More file actions
85 lines (71 loc) · 2.87 KB
/
Copy pathcli.py
File metadata and controls
85 lines (71 loc) · 2.87 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
import click
from patient import Patient, PatientCollection
from connection_config import config
import mysql.connector
class GetAllOption(click.Option):
def __init__(self, *args, **kwargs):
self.save_other_options = kwargs.pop('save_other_options', True)
nargs = kwargs.pop('nargs', -1)
assert nargs == -1, 'nargs, if set, must be -1 not {}'.format(nargs)
super(GetAllOption, self).__init__(*args, **kwargs)
self._previous_parser_process = None
self._eat_all_parser = None
def add_to_parser(self, parser, ctx):
def parser_process(value, state):
done = False
value = [value]
if self.save_other_options:
# grab everything up to the next option
while state.rargs and not done:
for prefix in self._eat_all_parser.prefixes:
if state.rargs[0].startswith(prefix):
done = True
if not done:
value.append(state.rargs.pop(0))
else:
value += state.rargs
state.rargs[:] = []
value = tuple(value)
self._previous_parser_process(value, state)
res = super(GetAllOption, self).add_to_parser(parser, ctx)
for name in self.opts:
our_parser = parser._long_opt.get(name) or parser._short_opt.get(name)
if our_parser:
self._eat_all_parser = our_parser
self._previous_parser_process = our_parser.process
our_parser.process = parser_process
break
return res
@click.group()
def cli():
pass
@cli.command('create')
@click.argument("first_name")
@click.argument("last_name")
@click.option('--birth-date', prompt='Birth date')
@click.option('--phone', cls=GetAllOption, prompt='Phone number')
@click.option('--document-type', cls=GetAllOption,
prompt='Document type (паспор/водительское удостоверение/заграничный паспорт)')
@click.option('--document-number', cls=GetAllOption, prompt='Document id')
def create(first_name, last_name, birth_date, phone, document_type, document_number):
try:
Patient(*[first_name, last_name, birth_date, ' '.join(phone), ' '.join(document_type),
''.join(document_number)]).save()
except ValueError:
click.echo('Creation aborted, wrong data format')
else:
click.echo('Patient created')
@cli.command('show')
@click.argument('num', default=10, type=int)
def show(num):
click.echo(PatientCollection(num))
@cli.command('count')
def count():
res = len(PatientCollection())
add = 's' if res > 1 or res == 0 else ''
click.echo(f'{res} patient{add} in the table')
cli.add_command(create)
cli.add_command(show)
cli.add_command(count)
if __name__ == '__main__':
cli()