-
Notifications
You must be signed in to change notification settings - Fork 1
/
peewee2click.py
293 lines (233 loc) · 8.76 KB
/
peewee2click.py
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import collections
import datetime
import functools
import itertools
import warnings
from tabulate import tabulate
import click
class DateParamType(click.ParamType):
name = 'date'
def convert(self, value, param, ctx):
try:
# date will fail if it doesn't get exactly three params
result = datetime.date(*[int(x) for x in value.split('-')])
except (ValueError, TypeError):
self.fail("{} is not a date in the form YYYY-MM-DD".format(value))
else:
return result
DATE_PARAM_TYPE = DateParamType()
def _number_of_arguments_in_list(ctx, *what):
"""
Return the number of `what` found as `ctx` dict keys with a value
other than `None`.
:param ctx: Arguments to check.
:type ctx: dict
:param what: List of arguments corresponding to `ctx` keys.
:type what: list
"""
return sum(1 for w in what if ctx.get(w) is not None)
def one_and_only_one(ctx, *what):
"""
Check the non-repetition of arguments.
:param ctx: Arguments to check.
:type ctx: dict
:param what: List of arguments that must appear only once.
:type what: list
"""
if _number_of_arguments_in_list(ctx, *what) != 1:
raise click.UsageError(
("One and only one of these parameters can be provided "
"at a time: %r") % [what])
def max_one(ctx, *what):
"""
Implements a method like `mutually_exclusive_group`
:param ctx: Arguments to check.
:type ctx: dict
:param what: List of arguments that are mutually exclusive.
:type what: list
"""
if _number_of_arguments_in_list(ctx, *what) > 1:
raise click.UsageError(
("At most one of these parameters can be provided "
"at a time: %r") % [what])
class CRUDL:
"""
CRUD+L over peewee models.
"""
TABLEFMT = "plain"
@classmethod
def print_table(cls, *args, **kwargs):
table = tabulate(*args, tablefmt=cls.TABLEFMT, **kwargs)
click.echo("\n{}\n".format(table))
@staticmethod
def format_single_element(elem, fields):
return [(k, repr(getattr(elem, k))) for k in fields]
@staticmethod
def format_multiple_elements(elems, fields):
res = []
for e in elems:
res.append([repr(getattr(e, f)) for f in fields])
return res
@staticmethod
def click_options_from_model_fields(model, skip=None):
def _options_from_model():
DBFIELD_TO_TYPE = {
("int",
"int unsigned"): int,
("bool", ): bool,
("text", "string"): str,
("float", ): float,
("date", ): DATE_PARAM_TYPE,
("primary_key", ): None
}
fields = sorted(model._meta.fields.values(), reverse=True)
for field in fields:
if skip and field.name in skip:
continue
for db_fields, asc_type in DBFIELD_TO_TYPE.items():
if field.get_db_field() in db_fields:
type_ = asc_type
break
else:
warnings.warn(
("Unknown database type `{field.db_field}` option "
"`{model._meta.name}.{field.name}` can't be "
"rendered.").format(model=model, field=field),
SyntaxWarning)
type_ = str
if type_ is None:
# This type of field doesn't map to an argument
continue
if field.help_text is None:
help = "No help. Please, document the model."
else:
help = field.help_text
name = '--' + field.name.replace('_', '-')
if field.null:
yield click.option(
name + "-set-null",
is_flag=True,
help="Set {} to NULL.".format(field.name))
yield click.option(name,
type=type_,
help=help)
# Using function composition we compose all the decorators generated by
# `_options_from_model` and we return one that groups them all.
return lambda f: functools.reduce(lambda g, h: h(g),
_options_from_model(),
f)
@staticmethod
def fields_from_options(options):
null_fields = {k[:-len('_set_null')]: None
for k, v in options.items()
if k.endswith('_set_null') and v}
non_null_fields = {k: v for k, v in options.items()
if not k.endswith('_set_null') and v is not None}
return collections.ChainMap(null_fields, non_null_fields)
@classmethod
def create(cls, model, force, **options):
"""
C: CREATE
"""
fields = cls.fields_from_options(options)
obj = model(**fields)
def _create():
# Force insert to prevent peewee trying to do the update in case of
# non auto-incremental ID.
obj.save(force_insert=True)
click.echo("The following entry was created:")
cls.show(model, obj.get_id())
return True
if force:
return _create()
else:
click.echo("You are about to create the following entry:")
fields = sorted(model._meta.fields.keys())
try:
preview = cls.format_single_element(obj, fields)
cls.print_table(preview)
except Exception as exc:
click.echo("Malformed entry: %s" % exc.__class__.__name__)
return False
if click.confirm("Are you sure?"):
return _create()
@classmethod
def show(cls, model, pk):
"""
R: READ
"""
fields = sorted(model._meta.fields.keys())
try:
# We get the key through meta, as it could be a compose key
obj = model.get(model._meta.primary_key == pk)
except model.DoesNotExist:
click.echo("Registry {} does not exists.".format(pk))
return False
else:
data = cls.format_single_element(obj, fields)
cls.print_table(data)
return True
@classmethod
def update(cls, model, pk, force, **options):
"""
U: UPDATE
"""
changes = cls.fields_from_options(options)
if not changes:
click.echo("Nothing to change.")
return False
def _update():
# We get the key through meta, as it could be a compose key
records = (model.update(**changes)
.where(model._meta.primary_key == pk)
.execute())
click.echo("Changed {} records.".format(records))
cls.show(model, pk)
return records > 0
if force:
return _update()
else:
click.echo("You are about to update the following record:")
cls.show(model, pk)
click.echo("With the following information:")
cls.print_table([[k, v] for k, v in changes.items()])
if click.confirm("Are you sure?"):
return _update()
@classmethod
def delete(cls, model, pk, force):
"""
D: DELETE
"""
def _delete():
try:
# We get the key through meta, as it could be a compose key
obj = model.get(model._meta.primary_key == pk)
except model.DoesNotExist:
click.echo("Registry {} does not exists.".format(pk))
else:
obj.delete_instance(recursive=True, delete_nullable=True)
click.echo("Registry {} removed.".format(pk))
return True
if force:
return _delete()
else:
click.echo("You are about to remove the following record:")
if cls.show(model, pk) and click.confirm("Are you sure?"):
return _delete()
else:
return False
@classmethod
def list(cls, model, base_fields, extra_fields=None):
"""
L: LIST
"""
# We concatenate base fields with extra_fields, removing duplicates
# and keeping the order.
fields = list(base_fields)
if extra_fields is not None:
fields += list(extra_fields)
fields = [f for f, _ in itertools.groupby(fields)]
objs = model.select()
data = cls.format_multiple_elements(objs, fields)
cls.print_table(data, headers=fields)
return True