-
Notifications
You must be signed in to change notification settings - Fork 0
/
onefad_web.py
790 lines (581 loc) · 24.7 KB
/
onefad_web.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
from onefad import *
load_global('strings',{# todo migrate more from code and clean up
'responsive': '<meta name="viewport" content="width=device-width, initial-scale=1.0">\n',
'menubtn-narrow-icons': '<span class="menubtn"><a href="{href}" alt="{alt}"><span class="iconsheet" style="background-position:{x}px {y}px;"></span>{label}</a></span>\n',
'menubtn-wide-icons': '<span class="menubtn wide"><a href="{href}" alt="{alt}"><span class="iconsheet" style="background-position:{x}px {y}px;"></span> {label}</a></span>\n',
'menubtn-list': '<a class="btn wide" style="font-size:initial;" href="{href}" alt="{alt}">{label}</a>\n',
'menubtn-narrow-icons-flat': '<a href="{href}" alt="{alt}">\n<div class="menu-button">\n<span class="iconsheet" style="background-position:{x}% {y}%;"></span>\n<span class="menu-label">{label}</span>\n</div>\n</a>\n',
'menubtn-wide-icons-flat': '<a href="{href}" alt="{alt}">\n<div class="menu-button menu-button-wide">\n<span class="iconsheet" style="background-position:{x}% {y}%;"></span>\n<span class="menu-label">{label}</span>\n</div>\n</a>\n',
'nb': '<a class="btn{2}" href="{1}">{0}</a>\n',
'popdown': '<div class="pdbox{}"><button class="mbutton" onclick="popdown(this);">▼</button>\n<div id="popdown" class="popdown">',
'error': '<div class="errorpage">\n<div>\n<h2>{}</h2>{}</div><img src="/parrot.svg" alt="Got his fibre, on his way." /></span>\n<br>',
'cfg.developer.name': 'Developer Mode',
'cfg.developer.label': 'Enables some debugging features, including reloading resources.',
'cfg.apd_dir.name': 'Append Data Directory',
'cfg.apd_dir.label': 'Path to core server files',
'cfg.res_dir.name': 'Resource Directory',
'cfg.res_dir.label': 'Path to server resources',
'cfg.server_name.name': 'Server Name',
'cfg.server_name.label': 'Set a different name for identifying.',
'cfg.server_addr.name': 'Server Address',
'cfg.server_addr.label': 'IP Address, Changes will not be reflected until restart!',
'cfg.server_port.name': 'Server Port',
'cfg.server_port.label': 'Port Number, Changes will not be reflected until restart!',
'cfg.homepage_menu.name': 'Homepage Menu',
'cfg.homepage_menu.label': 'Choose which menu should be displayed as home',
'cfg.dropdown_menu.name': 'Dropdown Menu',
'cfg.dropdown_menu.label': 'Choose which menu should drop down\nLeave blank for no dropdown',
'cfg.static_cfg.name': 'Static Config',
'cfg.static_cfg.label': 'Disable changing this config from the web',
'cfg.allow_data.name': 'Allow /data/*',
'cfg.allow_data.label': 'Enable data access over http, used for tools'
})
def serve_resource(handle, resource, code=200):
global ent
handle.send_response(code)
handle.send_header('Content-type', ext_content(resource))
fn = cfg.get('res_dir', 'res/') + resource
rf = f'_{resource}'
should_load = cfg.get('developer') or f'_{resource}' not in ent
if should_load and os.path.isfile(fn):
logging.debug(f'loading resource {resource} from {fn}')
ent[rf] = readfile(fn, mode='rb')
else:
handle.send_header('Cache-Control', 'max-age=3600, must-revalidate')
handle.end_headers()
handle.wfile.write(ent.get(rf, b''))
def template_nav(title, index, last, enc=True):
nav = ''
if 1 < index <= last:
nav += strings['nb'].format('<', index-1, ' nav-prev')
elif index != last:
nav += strings['nb'].format('>|', last, ' nav-last')
nav += f'<h2 class="pagetitle">{title}'
if last != 1:
nav += f' - {index:,}'
nav += '</h2>\n'
if last <= index and index != 1:
nav += strings['nb'].format('|<', 1, ' nav-first')
elif index != last:
nav += strings['nb'].format('>', index+1, ' nav-next')
if enc:
nav = bytes(nav, 'utf-8')
return nav
class mark_button(object):
def __init__(self, mark, action=None):
self.mark = mark
self.data = apdmm[mark]
self.btype = self.data.get('type')
self.action = action
def disabled(self):
return self.data.get('disabled', False) or cfg.get('static')
def pick_action(self):
if self.disabled():
return ''
if self.action != None:
return self.action
if self.btype == 'collection':
return 'setsGetMagic(this)'
return 'aprefMagic(this)'
def icon_html(self, value, size):
if type(value) == list:
return f'<i class="iconsheet ico{-size} {markicon(*value, m=-size)}"></i>'
return f'<span>{value}</span>'
def pick_icon(self, state, size):
icon = self.data.get('icon')
if state in self.data.get('values', []):
icon, ch = valueicon(state,
self.data.get('valueicon', []),
icon, self.data['values'])
return self.icon_html(icon, size)
def build_for(self, thing, state=None, size=60):
if self.data.get('hidden'):
return '', ''
state = mark_state(self.mark, thing)
self.col = []
if type(state) == list:# collections
state, self.col = state[0], state[1]
pressed = 'on '
if state is None:
state, pressed = '', ''
cla = ''
if self.disabled():
cla = 'disabled '
build_input = {
'text': self.build_input_text,
'int': self.build_input_int,
'multibutt': self.build_input_multibutt,
'list': self.build_input_list,
'collection': self.build_input_collection
}.get(self.btype, None)
if build_input is None:
logging.warning(f'{self.mark} has unhandled mark type: {self.btype}')
build_input = self.build_input_text
self.t_thing = thing
self.t_state = state
self.t_press = pressed
self.t_class = cla
self.t_size = size
return build_input()
def build_wrap(self, namep, cla, arg, inner):
if namep[0] is None:return ''
return f'<div name="{"@".join(namep)}" class="markbutton mbutton {cla}"{arg}>{inner}</div>\n'
def build_input_text(self):
#todo add icon
inner = f'<input type="text" class="niceinp" size="1" value="{self.t_state}">'
return self.build_wrap(
[self.t_thing, self.mark],
self.t_press + self.t_class,
'',
inner), ''
def build_input_int(self):
#todo add icon
inner = f'<input type=""number" class="niceinp" size="1" value="{self.t_state}">'
return self.build_wrap(
[self.t_thing, self.mark],
self.t_press + self.t_class,
'',
inner), ''
def build_input_multibutt(self):
values = self.data.get('values', [])
out = f'<span name="{self.t_thing}@{self.mark}">\n'
action = f' onclick="{self.pick_action()}"'
for value in values:
out += self.build_wrap(
[self.t_thing, value, self.mark],
['', self.t_press][value == self.t_state] + self.t_class,
action,
self.pick_icon(value, self.t_size))
return out + '</span>\n', ''
def build_input_collection(self):
links = []
action = f' onclick="{self.pick_action()}"'
inner = self.pick_icon(self.t_state, self.t_size)
for coln in self.col:
links.append(
create_linktodathing(
self.mark,
coln[0],
con=self.data.get('name', self.mark)
))
link = ''
if links:
link = f'<div class="tags">\n{self.data["name_plural"]}:<br>\n' + '\n'.join(links) + '</div>\n'
return self.build_wrap(
[self.t_thing, self.mark],
self.t_press + self.t_class,
action,
inner), link
def build_input_list(self):
inner = self.pick_icon(self.t_state, self.t_size)
inner += f'<select class="niceinp" onchange="{self.pick_action()}">\n'
for value in [''] + self.data['values']:
inner += '<option value="{0}" {1}>{0}</option>\n'.format(value, ['', 'selected'][value == self.t_state])
inner += '</select>'
return self.build_wrap(
[self.t_thing, self.mark],
self.t_press + self.t_class,
'',
inner), ''
class builtin_base(object):
def __init__(self, title='', icon=59):
self.pagetype = 'builtin'
self.title = title
self.pages = False
self.do_time = True
self.metric = None
self.do_menu = True
self.do_menu_name = ''
self.do_script = True
self.do_style = True
self.do_rebut = True
self.do_head = True
self.do_foot = True
self.do_wrap = True
if icon is None:icon = 59
if isinstance(icon, int):
icon = ii(icon)
self.icon = icon
self.content_type = 'text/html'
self.path_parts = 1
self.page_options = []
self.modes_default = ''
self.modes = {}
@staticmethod
def write(handle, h):
handle.wfile.write(bytes(h, 'utf-8'))
def get_icon(self, path):
return self.icon
def metric_start(self):
self.metric = time.perf_counter()
def metric_stop(self):
if self.metric is None:
return 'ERR'
m = time.perf_counter() - self.metric
self.metric = None
return int(m * 1000)
def error(self, handle, ti, message, status=404):
if self.do_head:
self.head(handle, status=status)
h = strings['error'].format(ti, message)
self.write(handle, h)
self.menu_ele(handle, ['error'])
self.foot(handle)
def building_entries(self, handle):
self.head(handle)
handle.wfile.write(b'''<div class="errorpage">
<div>
<h1>Building Entries</h1>
<p>The program is busy building entries.</p>
<br>
<div class="lds-ring parrot-ring">
<div></div><div></div><div></div><div></div></div>
<br><br>
<a href="/unstuck">Click Here if the program is stuck.</a>
</div>
<script>
var t = setTimeout(function(){window.location.reload(1);}, 5000);
</script>''')
def head(self, handle, status=200):
if self.metric is None:
self.metric_start()
handle.send_response(status)
handle.send_header('Content-type', self.content_type)
handle.end_headers()
handle.wfile.write(b'<meta charset="utf-8">\n')
h = ''
if self.do_style:
h += '<link rel="stylesheet" href="/style.css">\n'
if self.do_script:
h += '<script src="/mark.js"></script>\n'
if cfg.get('responsive'):
h += strings.get('responsive', '')
h += '</head>\n'
if self.do_wrap:
h += '<body onload="page_load()">\n<div class="pageinner">'
self.write(handle, h)
def titledoc(self, handle):
self.write(handle, f'<html>\n<head><title>{self.title}</title>\n')
def newparg(self, path, pargs, v, i):
r = pargs + [i]
if v in pargs:
r = list(pargs)
r[pargs.index(v)] = i
if v == i:
r.remove(i)
r = ' '.join(r)
outp = list(path)
if len(path) == self.path_parts:
outp.append(path[-1])
if len(outp) > 1:
outp[-2] = r
return '/'.join([str(x) for x in outp])
def modes_fallback(self):
return self.modes_default
def mode_picker(self, path):
# create a nice picker element and give the mode
pargs = []
if len(path) > 1:
pargs = path[-2]
pdat = max(self.path_parts-2, 0)
if pargs == path[pdat]:
pargs = []# path has no data
else:
pargs = pargs.split(' ')
current = self.modes_fallback()
# find the current mode
for i in self.modes:
if i in pargs:
current = i
break
out = ''
for i, n in self.modes.items():
r = self.newparg(path, pargs, current, i)
if i == current or i in pargs:
n = f'<b>{n}</b>'# selected
if out:out += ' - '# spacer
out += f'<a href="/{r}">{n}</a>\n'
return current, f'<p>{out}</p>\n'
def optiondoc(self, handle, path):
if not self.page_options:return
outopt = []
if not path:
pargs = []
elif isinstance(path[-1], int):
path[-1] = str(path[-1])
pargs = path[-2]
else:
pargs = path[-1]
if len(path) == self.path_parts:
pargs = []
elif path:
pargs = pargs.split(' ')
for cat in self.page_options:
outopt.append('')
for i in cat:
if i in pargs:
if not outopt[-1]:
outopt[-1] = i
h = strings['popdown'].format(' pageopt')
h += '<div class="page-toptions">\n'
h += '<h3>Page Parameters</h3>\n'
for n, v in enumerate(outopt):
for i in self.page_options[n]:
on = ['', 'on'][i in pargs]
r = self.newparg(path, pargs, v, i)
h += f'<a class="option {on}" href="/{r}">{i}</a>\n'
h += '<br>\n'
h += '</div></div></div>'
self.write(handle, h)
def popdown(self, handle, name):
drop = ent['builtin'].get(name)
if drop is None:
return False
h = strings['popdown'].format('')
self.write(handle, h)
drop.page(handle, [])
handle.wfile.write(b'</div></div>')
return True
def bookmark_hook(self, handle):
pass
def foot(self, handle):
dur = self.metric_stop()
h = ''
butts = ent.get('reentry_buttons', [])
if self.do_rebut and butts:
h += '<div class="reentry">'
for clas, func, text in butts:
h += f'<a class="btn{clas}" onclick="postReload(this, \'/{func}\')">{text}</a>\n'
h += '<div id="rebuildLoad" class="lds-ring hide"><div></div><div></div><div></div><div></div></div>'
h += '</div>'
srvname = cfg.get('server_name')
if type(srvname) != str or not srvname:
srvname = 'FADSRV'
h += f'\n<p>{srvname} build#{ent.get("version", "unknown")}'
if self.do_time:
h += f' - Served in {wrapme(dur)}ms'
h += '</p>'
self.bookmark_hook(handle)
self.write(handle, h)
@staticmethod
def page(handle, path):
handle.wfile.write(b'<p>Sphinx of black quartz, judge my vow.</p>\n')
def menu_ele(self, handle, path):
self.write(handle, '\n</div>\n')
self.optiondoc(handle, path)
p = self.do_menu_name
d = path + [cfg.get('homepage_menu')]
if not p:p = cfg.get('dropdown_menu')
if d[0] != p:
self.popdown(handle, p)
def serve(self, handle, path, head=True, foot=True, menu=True):
if self.do_head and head:self.head(handle)
self.page(handle, path)
self.titledoc(handle)
if self.do_foot and foot:self.foot(handle)
if self.do_menu and menu:self.menu_ele(handle, path)
class builtin_menu(builtin_base):
def __init__(self, title=None, icon=None, which=None):
data = menus['pages'].get(which, {})
if icon is None:
icon = data.get('icon')
if title is None:
title = data.get('title', 'Menu')
super().__init__(title, icon)
if not which:
which = 'all_pages'
self.which = which
self.pagetype = 'builtin_menu'
self.page_options = []
def build_menu(self, handle, which, minfo, eles):
title = minfo.get('title', f'Undefined: {which}')
htmlout = f'<div class="head"><h2 class="pagetitle"><a href="/{which}">{title}</a></h2></div>\n'
htmlout += '<div class="container list">\n'
mode = minfo.get('mode', 'list')
btn = strings.get(f'menubtn-{mode}', None)
if btn is None:
logging.warning(f'Missing markup string for menubtn-{mode}')
btn = '<a href="{href}" alt="{alt}">{label}</a><br>\n'
for d in eles:
if d.get('type', 'button') == 'section':
htmlout += f'<h2>{d.get("label", "Section")}</h2>\n'
continue
i = {'href': d.get('href', ''),
'label': d.get('label'),
'alt': d.get('alt', ''),
'x': 1, 'y': 5}
part = i['href'].lower().split('/')
if i['href'] and '/' not in i['href']:
i['href'] = f'/{i["href"]}/1'
else:
part = part[1:]
if 'icon' in d:
icon = d['icon']
if isinstance(icon, int):
icon = ii(icon)
i['x'], i['y'] = icon
elif 'x' in d and 'y' in d:
i['x'], i['y'] = d['x'], d['y']
elif part and part[0] in ent['builtin']:
icon = ent['builtin'][part[0]].get_icon(part)
if not i['label']:
i['label'] = ent['builtin'][part[0]].title
if type(icon) == list and len(icon) == 2:
i['x'], i['y'] = icon
else:
i['x'], i['y'] = 9, 9
i['x'] *= -100
i['y'] *= -100
if not i['label']:label = 'Error'
htmlout += btn.format(**i)
self.write(handle, htmlout + '</div>\n')
def reverse(self, eles):
buf = []
out = []
for e in reversed(eles):
if e.get('type', 'button') == 'button':
buf.append(e)
continue
out = [e] + buf + out
buf = []
return buf + out
def page(self, handle, path):
minfo = menus['pages'].get(self.which, {})
butts = minfo.get('buttons', f'{self.which}_buttons')
eles = menus.get(butts, {})
if 'reversed' in path[1:]:
eles = self.reverse(eles)
self.build_menu(handle, self.which, minfo, eles)
class builtin_config(builtin_base):
def __init__(self, title='Configure', icon=12, name='cfg'):
super().__init__(title, icon)
self.name = name
if self.name != 'cfg' and self.title == 'Configure':
self.title += f': {self.name}'
def page(self, handle, path):
handle.wfile.write(b'<div class="head">')
nav = template_nav(self.title, 1, 1)
handle.wfile.write(nav + b'</div>\n')
handle.wfile.write(b'<div class="container">\n')
self.mode = globals().get(self.name, {})
cfgsub = 'cfg.'
if self.name != 'cfg':
cfgsub += f'{self.name}.'
body = ''
for k, v in self.mode.items():
name = strings.get(cfgsub + f'{k}.name', k)
label = strings.get(cfgsub + f'{k}.label', '').replace('\n', '<br>')
inner = f'<div class="info">\n<h2>{name}</h2>\n<p>{label}</p>\n</div>'
inptype = 'unknown'
if isinstance(v, bool):
inptype = 'checkbox'
if self.mode.get(k, False):
v = '" checked nul="'
elif isinstance(v, int):
inptype = 'number'
elif isinstance(v, str):
inptype = 'text'
if inptype == 'unknown':
if not label:
inptype = type(v)
inner += f'<div>\n<p>Unrepresentable type: {inptype.__name__}'
if inptype in [dict, list, set]:
inner += f'<br>Contains {len(v):,} items'
if cfg.get('allow_data'):
inner += f'<br><a href=/data/{self.name}/{k}>View as data</a>'
inner += '</p>\n</div>'
else:
scr = f"cfg('{k}', '{self.name}')"
inner += f'''<div>
<input class="niceinp" id="{k}" type="{inptype}" value="{v}">
<button class="mbutton" onclick="{scr}">Apply</button>\n</div>'''
body += f'\n<div class="setting">{inner}\n</div>'
handle.wfile.write(bytes(body, 'utf-8'))
handle.wfile.write(b'</div>\n')
class builtin_entpoke(builtin_config):
def __init__(self, title='Ent Poke', icon=12, name='ent'):
super().__init__(title, icon, name)
class post_base(builtin_base):
def __init__(self, title='', icon=99):
super().__init__(title, icon)
self.pagetype = 'post'
self.content_type_post = 'application/json'
def post_head(self, handle, status=200):
handle.send_response(status)
handle.send_header('Content-type', self.content_type_post)
handle.end_headers()
def post_logic(self, handle, pargs, data):
return "pog"
def serve_post(self, handle, pargs, data):
self.post_head(handle)
ret = self.post_logic(handle, pargs, data)
handle.wfile.write(bytes(json.dumps(ret), 'utf-8'))
def read_config():
global strings
load_global('cfg', ent['config_file'])
load_global('menus', ent['menu_file'])
dd = cfg.get('apd_dir', 'data/')
sf = ent.get('strings_file')
if not sf or not os.path.isfile(dd + sf):
return
sf = safe_readfile(dd + sf)
for line in sf.split('\n')[1:]:
line = line.split('\t')
if len(line) == 2:
line[1] = line[1].replace('\\n', '\n')
if line[0].startswith('b.'):
line[1] = bytes(line[1][2:-1], 'utf8')
strings[line[0]] = line[1]
def save_config():
if ent.get('config_read_error'):
logging.warning('Preventing accidental config loss, please fix config file.')
return
dd = cfg.get('apd_dir', 'data/')
if ent.get('config_file'):
user_friendly_dict_saver(
dd + ent['config_file'], cfg)
if ent.get('menu_file'):
user_friendly_dict_saver(
dd + ent['menu_file'], menus,
ignore_keys=['all_pages_buttons', 'page_buttons'])
sf = ent.get('strings_file')
if sf and not os.path.isfile(dd + sf):
out = '// strings for FADSRV, name <tab> value'
for k, v in strings.items():
out += '\n{}\t{}'.format(k, str(v).replace('\n', '\\n'))
with open(dd + sf, 'w') as fh:
fh.write(out)
fh.close()
class post__flag(post_base):
# code used to be for marks
# back when they were much, much simpler
# using json files to store them
# bad performance when files got big
# but stiill works well for simple data modifications
def __init__(self, title='', icon=99):
super().__init__(title, icon)
def post_logic(self, handle, pargs, data):
global cfg, ent
if cfg.get('static_cfg'):
return {'status': 'Server set to Static for cfg'}
dd = cfg.get('apd_dir', 'data/')
ret = {}
flag = pargs[1]
pref = {}
logging.debug(f'Legacy flag: {flag} {json.dumps(data)}')
if flag == 'cfg':
pref['cfg'] = cfg
if flag == 'ent':
pref['ent'] = ent
for file in data:
if data[file] is None:continue
ret[file] = [flag, file not in pref[flag]]
pref[flag][file] = data[file]
if flag == 'cfg':
cfg = pref['cfg']
save_config()
del pref['cfg']
if flag == 'ent':
ent = pref['ent']
del pref['ent']
return ret