-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmfp.py
More file actions
306 lines (233 loc) · 8.46 KB
/
Copy pathmfp.py
File metadata and controls
306 lines (233 loc) · 8.46 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
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
from __future__ import annotations
import sys
from types import TracebackType
import pytermgui as ptg
import subprocess
import os
import time
from scripts.manage_profiles import create_profile
from scripts.manage_profiles import get_profiles
# from scripts.manage_profiles import run_profile
OUTPUT = {'action': 'REPEAT'}
PALETTE_LIGHT = "#FCBA03"
PALETTE_MID = "#8C6701"
PALETTE_DARK = "#4D4940"
PALETTE_DARKER = "#242321"
def submit_build(manager: ptg.WindowManager, window: ptg.Window) -> None:
n = 0
OUTPUT['action'] = 'BUILD'
for widget in window:
if isinstance(widget, ptg.InputField):
OUTPUT[str(n)] = widget.value
n = n + 1
continue
if isinstance(widget, ptg.Container):
OUTPUT[str(n)] = "Container"
n = n + 1
for inner in widget:
if isinstance(inner, ptg.InputField):
OUTPUT[str(n)] = inner.value
n = n + 1
if isinstance(inner, ptg.Splitter):
OUTPUT[str(n)] = "Splitter"
n = n + 1
for in_inner in inner:
if isinstance(in_inner, ptg.InputField):
OUTPUT[str(n)] = in_inner.value
n = n + 1
if isinstance(in_inner, ptg.Toggle):
OUTPUT[str(n)] = in_inner.checked
n = n + 1
manager.stop()
# subprocess.Popen('./sandbox/build.sh', shell=True)
# os.system('./sandbox/build.sh')
# main()
def _create_aliases() -> None:
ptg.tim.alias("app.text", "#cfc7b0")
ptg.tim.alias("app.header", f"bold @{PALETTE_MID} #d9d2bd")
ptg.tim.alias("app.header.fill", f"@{PALETTE_LIGHT}")
ptg.tim.alias("app.title", f"bold {PALETTE_LIGHT}")
ptg.tim.alias("app.button.label", f"bold @{PALETTE_DARK} app.text")
ptg.tim.alias("app.button.highlight", "inverse app.button.label")
ptg.tim.alias("app.footer", f"@{PALETTE_DARKER}")
def _configure_widgets() -> None:
ptg.boxes.SINGLE.set_chars_of(ptg.Window)
ptg.boxes.SINGLE.set_chars_of(ptg.Container)
ptg.Button.styles.label = "app.button.label"
ptg.Button.styles.highlight = "app.button.highlight"
ptg.Button.set_char("delimiter", [""] * 2)
ptg.Slider.styles.filled__cursor = PALETTE_MID
ptg.Slider.styles.filled_selected = PALETTE_LIGHT
ptg.Label.styles.value = "app.text"
ptg.Window.styles.border__corner = "#C2B280"
ptg.Container.styles.border__corner = PALETTE_DARK
ptg.Splitter.set_char("separator", "")
def _define_layout() -> ptg.Layout:
layout = ptg.Layout()
layout.add_slot("Header", height=3)
layout.add_break()
layout.add_slot("Body")
layout.add_break()
layout.add_slot("Footer", height=3)
return layout
def _confirm_exit(manager: ptg.WindowManager) -> None:
OUTPUT['action'] = 'EXIT'
manager.stop()
def create_button(caption):
return ptg.Button(caption, lambda *_, c=caption: create_profile(c), centered=True)
def _run_profile(manager: ptg.WindowManager) -> None:
container = ptg.Container(static_width=50)
modal = ptg.Window(
"[app.title]Select profile",
"",
).center()
profiles = get_profiles()
for caption in profiles:
button = create_button(caption)
container.lazy_add(button)
# print(f"{container.get_lines()}")
# time.sleep(5)
modal.lazy_add(container)
container_cancel = ptg.Container(
ptg.Button("Cancel", lambda *_: modal.close()),
)
modal.lazy_add(container_cancel)
manager.add(modal)
def _generate_profile(manager: ptg.WindowManager) -> None:
modal = ptg.Window(
# "[app.title]Generate Profile",
# "",
ptg.Container(
ptg.InputField("", prompt="Profile Name: "),
static_width=80,
),
"[app.title]CPU",
ptg.Container(
ptg.Splitter(
ptg.InputField(value="1", prompt="Qty: "),
ptg.InputField(value="50000", prompt="Quota: "),
"(μs)",
ptg.InputField(value="100000", prompt="Period: "),
"(μs)",
),
),
"[app.title]Memory",
ptg.Container(
ptg.Splitter(
ptg.InputField(value="1", prompt="Memory: "),
"(Gb)",
ptg.InputField(value="2", prompt="Swap: "),
"(Gb)",
),
),
"[app.title]Timezone",
ptg.Container(
ptg.InputField("Etc/Greenwich", prompt="Timezone: "),
),
# ptg.Container(
# "",
# ptg.Splitter(
# ptg.Label(" CPU "),
# ptg.Toggle(("False", "True")),
# ),
# ptg.Splitter(
# ptg.Label(" GPU "),
# ptg.Toggle(("False", "True")),
# ),
# ptg.Splitter(
# ptg.Label(" Memoria "),
# ptg.Toggle(("False", "True")),
# ),
# static_width=30,
# ),
"",
ptg.Container(
ptg.Splitter(
["Build", lambda *_: submit_build(manager, modal)],
ptg.Button("Cancel", lambda *_: modal.close()),
),
),
).center().set_title("[app.title]Generate profile")
modal.select(0)
manager.add(modal)
def _confirm_quit(manager: ptg.WindowManager) -> None:
modal = ptg.Window(
"[app.title]Are you sure you want to quit?",
"",
ptg.Container(
ptg.Splitter(
# ptg.Button("Yes", lambda *_: manager.stop()),
ptg.Button("Yes", lambda *_: _confirm_exit(manager)),
ptg.Button("No", lambda *_: modal.close()).set_char("delimiter", [""] * 2),
),
),
).center()
modal.select(0)
manager.add(modal)
def main() -> None:
"""Runs the application."""
_create_aliases()
_configure_widgets()
with ptg.WindowManager() as manager:
manager.layout = _define_layout()
header = ptg.Window(
"[app.header] ╭ᥥ╮(´• ᴗ •`˵)╭ᥥ╮ ",
box="EMPTY",
is_persistant=True,
)
header.styles.fill = "app.header.fill"
manager.add(header)
footer = ptg.Window(
ptg.Button("Quit", lambda *_: _confirm_quit(manager)),
box="EMPTY",
)
footer.styles.fill = "app.footer"
manager.add(footer, assign="footer")
manager.add(
ptg.Window(
"[app.title]Middle Fingerprint",
"",
ptg.Container(
ptg.Container(
ptg.Button("Generate Profile", lambda *_: _generate_profile(manager)),
),
ptg.Container(
ptg.Button("Run Profile", lambda *_: _run_profile(manager)),
),
static_width=60,
),
vertical_align=ptg.VerticalAlignment.CENTER,
overflow=ptg.Overflow.SCROLL,
),
assign="body",
)
# ptg.tim.print(f"[{PALETTE_LIGHT}]{OUTPUT}")
if (OUTPUT['action'] == 'BUILD'):
ptg.tim.print(f"[{PALETTE_LIGHT}]{OUTPUT}")
profile_name = OUTPUT['1']
cpu_qty = OUTPUT['4']
cpu_quota = OUTPUT['5']
cpu_period = OUTPUT['6']
# GPU = OUTPUT['9']
mem = OUTPUT['9']
swap = OUTPUT['10']
tz = OUTPUT['12']
create_profile(profile_name,
cpu_qty=cpu_qty,
cpu_quota=cpu_quota,
cpu_period=cpu_period,
mem=mem,
swap=swap,
tz=tz)
# os.system('./scripts/build.sh')
# if (OUTPUT['action'] == 'RUN'):
# os.system('./sandbox/run.sh')
if __name__ == "__main__":
while (OUTPUT['action'] != 'EXIT'):
main()
ptg.tim.print(f"")
ptg.tim.print(f"")
ptg.tim.print(f"[{PALETTE_LIGHT}]Thank for use MiddleFingerprint! [app.header] ╭ᥥ╮(´• ᴗ •`˵)╭ᥥ╮ ")
ptg.tim.print(f"")
# {'action': 'BUILD', '0': 'Container', '1': 'Mimi', '2': 'Container', '3': 'Splitter', '4': '1', '5': '50000', '6': '100000',
# '7': 'Container', '8': 'Splitter', '9': '1', '10': '2', '11': 'Container', '12': 'Etc/Greenwich', '13': 'Container', '14': 'Splitter'}