-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcommon_util.py
More file actions
78 lines (62 loc) · 1.96 KB
/
common_util.py
File metadata and controls
78 lines (62 loc) · 1.96 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
import functools
import sys
from lib.chatmark import Checkbox, Form, Radio, TextEditor
def create_ui_objs(ui_decls, args):
ui_objs = []
editors = []
for i, ui in enumerate(ui_decls):
editor = ui[0](args[i])
if ui[1]:
# this is the title of UI object
editors.append(ui[1])
editors.append(editor)
ui_objs.append(editor)
return ui_objs, editors
def edit_form(uis, args):
ui_objs, editors = create_ui_objs(uis, args)
form = Form(editors)
form.render()
values = []
for obj in ui_objs:
if isinstance(obj, TextEditor):
values.append(obj.new_text)
elif isinstance(obj, Radio):
values.append(obj.selection)
else:
# TODO
pass
return values
def editor(description):
def decorator_edit(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
uis = wrapper.uis[::-1]
return edit_form(uis, args)
if hasattr(func, "uis"):
wrapper.uis = func.uis
else:
wrapper.uis = []
wrapper.uis.append((TextEditor, description))
return wrapper
return decorator_edit
def ui_edit(ui_type, description):
def decorator_edit(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
uis = wrapper.uis[::-1]
return edit_form(uis, args)
if hasattr(func, "uis"):
wrapper.uis = func.uis
else:
wrapper.uis = []
ui_type_class = {"editor": TextEditor, "radio": Radio, "checkbox": Checkbox}[ui_type]
wrapper.uis.append((ui_type_class, description))
return wrapper
return decorator_edit
def assert_exit(condition, message, exit_code=-1):
if condition:
if exit_code == 0:
print(message, end="\n\n", flush=True)
else:
print(message, end="\n\n", file=sys.stderr, flush=True)
sys.exit(exit_code)