You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.8 KiB
Python
60 lines
1.8 KiB
Python
'''
|
|
model.py
|
|
|
|
Methods for interacting with Lark's database models.
|
|
'''
|
|
|
|
import lark.metadata
|
|
# TODO: This is a big fucking mess. Don't roll your own library, use prompt-toolkit or something.
|
|
IGNORED_CLASS_KEYS = ['id', '_sa_instance_state']
|
|
def editor(data_object):
|
|
|
|
data_dict = {}
|
|
for key, value in data_object.__dict__.items():
|
|
if key in IGNORED_CLASS_KEYS:
|
|
pass
|
|
else:
|
|
data_dict[key] = {'nullable': data_object.__table__.columns.get(key).nullable,
|
|
'auto': False,
|
|
'unique': data_object.__table__.columns.get(key).nullable,
|
|
'value': value}
|
|
|
|
if key == 'uuid':
|
|
if value is None:
|
|
data_dict[key]['auto'] = True
|
|
data_dict[key]['value'] = lark.metadata._uuidgen()
|
|
|
|
while True:
|
|
editable_list = []
|
|
for key, properties in data_dict.items():
|
|
editable_list.append('%s: %s' % (key, properties['value']))
|
|
|
|
user_input = prompt_select_from_list(editable_list, 'Select a value to edit:', ['abort'])
|
|
|
|
if user_input == 'abort':
|
|
break
|
|
|
|
def prompt_select_from_list(choices, prompt, commands=[]):
|
|
if len(choices) == 1:
|
|
return choices[0]
|
|
|
|
index = 0
|
|
user_choice = None
|
|
for choice in choices:
|
|
print('%s: %s' % (index, choice))
|
|
index += 1
|
|
|
|
print(prompt)
|
|
while user_choice is None:
|
|
user_input = input('[0-%s]: ' % (len(choices)-1))
|
|
if user_input.isdigit() and int(user_input) >= 0 and int(user_input) <= len(choices)-1:
|
|
user_choice = choices[int(user_input)]
|
|
|
|
elif str(user_input) in commands:
|
|
user_choice = user_input
|
|
|
|
else:
|
|
print('%s is neither a choice nor a command.' % user_input)
|
|
|
|
return user_choice
|