-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
315 lines (285 loc) · 8.81 KB
/
main.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
from peewee import Model, IntegerField, ForeignKeyField
from playhouse.reflection import print_model
from PyInquirer import prompt, Validator, ValidationError
from collections import OrderedDict
from copy import copy
from tabulate import tabulate
from crated import make_model, make_field, db, type_to_fld_cls, open_database, delete_model
def push_command(k, v, commands):
commands[k] = v
commands.move_to_end(k, last=False)
return commands
def prompt_field(commands, field, current_value=None):
q = {
"type":"input",
"name": field.name,
"message": "{}:".format(field.name),
}
if current_value is not None:
q.update({"default": current_value})
a = prompt(q)
return a[field.name]
def prompt_instance(commands, model_cls, model_instance=None):
row = model_cls() if model_instance is None else model_instance
for field in model_cls._meta.sorted_fields:
if field.field_type != "AUTO" and field.name not in ['created','modified']:
current_value = getattr(model_instance, field.name, None) if model_instance else None
if isinstance(current_value, Model):
current_value = str(getattr(current_value, 'id'))
new_value = prompt_field(commands, field, current_value=current_value)
if isinstance(field, (ForeignKeyField,IntegerField)) and new_value == '':
new_value = None
setattr(row, field.name, new_value)
row.save()
list_instances(commands, model_cls)
return commands
def prompt_edit(commands, model_cls):
q={
"type":"list",
"name":"name",
"message": "Select to edit:",
"choices": [r.name for r in model_cls.select()]
}
a = prompt(q)
model_instance = model_cls.get(model_cls.name==a["name"])
return prompt_instance(commands, model_cls, model_instance=model_instance)
def list_instances(commands, model_cls):
print(tabulate(model_cls.select().order_by(model_cls.created).dicts(), headers="keys"))
return commands
def prompt_column(commands, model):
model = db.models.get(model)
print_model(model)
class DefualtRequired(Validator):
def validate(self, document):
if len(document.text)==0:
raise ValidationError(
message='Please enter a default value',
cursor_position=len(document.text))
q = [
{
"type": "input",
"name": "col_name",
"message": "Field name? (empty=cancel)"
},
{
"type": "list",
"name": "col_type",
"message": "Field type?",
"choices": [ k for k,_ in type_to_fld_cls.items()
],
"when": lambda a: a['col_name'] != ''
},
{
"type": "confirm",
"name": "required",
"message": "Required (If Y, provide default value)",
"default": False,
"when": lambda a: a['col_name'] != '' and not a["col_type"] == "Lookup"
},
{
"type": "input",
"name": "default",
"message": "Default value:",
"validate": DefualtRequired,
"when": lambda a: a['col_name'] != '' and not a["col_type"] == "Lookup" and a["required"]
},
{
"type": "input",
"name": "default",
"message": "Default value:",
"when": lambda a: a['col_name'] != '' and not a["col_type"] == "Lookup" and not a["required"]
},
{
"type": "list",
"name": "fk_cls",
"message": "Lookup to?",
"choices": [k for k in db.models.keys()],
"when": lambda a: a['col_name'] != '' and a["col_type"]=="Lookup",
},
{
"type": "input",
"name": "fk_backref",
"message": "Back reference name?",
"when": lambda a: a['col_name'] != '' and a["col_type"]=="Lookup",
},
{
"type": "confirm",
"name": "index",
"message": "Index?",
"default": False,
"when": lambda a: a['col_name'] != '' and not a["col_type"] == "Lookup"
}
]
a = prompt(q)
if a['col_name'] != '':
col_name = a.get("col_name")
fld_type = a.get("col_type")
# print(a.get('required'))
col_null = not a.get("required") if a.get("required") is not None else True
col_default = a.get("default")
fk_type = a.get("fk_cls")
fk_backref = a.get("fk_backref")
index = a.get("index")
m = make_field(model, col_name, fld_type, col_null, col_default, fk_type=fk_type, fk_backref=fk_backref, index=index)
print_model(m)
return commands
def prompt_model(commands):
q = [
{
"type": "input",
"name": "model_name",
"message": "Model name?"
}
]
a = prompt(q)
model_name = a.get("model_name")
m=make_model(model_name)
commands = update_model_commands(commands)
return commands
def prompt_delete_model(commands, model):
model_name = model._meta.name
q = [
{
"type": "confirm",
"name":"confirm_delete",
"message": "All {model} data will be lost. Are you sure you want to delete {model} model?".format(model=model_name),
"default": False
}
]
a = prompt(q)
confirm_delete = a.get("confirm_delete")
if confirm_delete:
delete_model(model)
prev = commands['Back'](commands)
prev.pop("{} model".format(model_name))
prev_prev = prev['Back'](commands)
prev_prev.pop(model_name)
prev["Back"] = lambda commands: prev_prev
return update_model_commands(prev)
return commands
def cons_menu(commands):
prev = copy(commands)
commands = OrderedDict({"Back": lambda commands: update_data_commands(prev)})
commands = push_command(
"New Model",
prompt_model,
commands)
commands = update_model_commands(commands)
return commands
def prompt_db(commands):
q = [
{
"type": "input",
"name": "file_name",
"message": "Crate name"
}
]
a = prompt(q)
open_database((a.get("file_name")))
commands = push_command(
"Manage Models",
cons_menu,
commands)
commands = update_data_commands(commands)
if len(db.get_tables()) == 1:
print("Hmm. This crate appears to be empty. Try making a new model.")
return commands
def update_data_commands(commands):
for name, model in db.models.items():
print_model(model)
commands = push_command(name, open_data_menu(commands, name), commands)
return commands
def open_data_menu(commands, name):
return lambda commands: data_menu(commands, db.models.get(name))
def data_menu(commands, model):
prev = copy(commands)
commands = OrderedDict({"Back": lambda commands: prev})
commands = add_data_commands(commands, model)
return commands
def add_data_commands(commands, m):
model_name = m._meta.table_name
commands = push_command(
"Edit {}".format(model_name),
lambda commands: prompt_edit(commands, db.models.get(model_name)),
commands
)
commands = push_command(
"New {}".format(model_name),
lambda commands: prompt_instance(commands, db.models.get(model_name)),
commands
)
commands = push_command(
"List {}s".format(model_name),
lambda commands: list_instances(commands, db.models.get(model_name)),
commands
)
return commands
def update_model_commands(commands):
for name, model in db.models.items():
print_model(model)
commands = push_command("{} model".format(name), open_model_menu(commands, name), commands)
return commands
def open_model_menu(commands, name):
return lambda commands: model_menu(commands, db.models.get(name))
def model_menu(commands, model):
prev = copy(commands)
commands = OrderedDict({"Back": lambda commands: prev})
commands = add_model_commands(commands, model)
return commands
def add_model_commands(commands, m):
model_name = m._meta.table_name
commands = push_command(
"Delete {} model".format(model_name),
lambda commands: prompt_delete_model(commands, db.models.get(model_name)),
commands
)
commands = push_command(
"Edit {} model".format(model_name),
lambda commands: prompt_edit_model(commands, db.models.get(model_name)),
commands
)
commands = push_command(
"New {} model field".format(model_name),
lambda commands: prompt_column(commands, model_name),
commands
)
commands = push_command(
"View {} model".format(model_name),
lambda commands: view_model(commands, model_name),
commands
)
return commands
def prompt_edit_model(commands, model):
print("not implemented")
return commands
def view_model(commands, name):
print_model(db.models.get(name))
return commands
def get_cmd_list(commands):
return [
k for k,_ in commands.items()
]
def main():
commands = OrderedDict({"Quit": exit})
commands = push_command(
"Open Crate",
prompt_db,
commands)
main_menu = {
"type": "list",
"name": "cmd",
"message": "Select:",
"choices": get_cmd_list(commands)
}
print("CrateD 0.1.0")
while True:
print("Current Crate: {}".format(db.database))
key = prompt(main_menu).get("cmd")
if key != "Quit":
if key is not None:
commands=commands.get(key)(commands)
main_menu["choices"] = get_cmd_list(commands)
else:
break
if __name__ == '__main__':
main()