-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathsession.py
197 lines (150 loc) · 5.09 KB
/
session.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
#!/usr/bin/env python3
#
# 2018-11-09 15:06:50
from configparser import ConfigParser
from widgets import (g, et)
LAST_TMP = 'static/last.tmp'
def load_settings():
_cfg = ConfigParser()
_cfg.optionxform = str
_cfg.read(LAST_TMP, 'utf8')
if not _cfg.has_section('Setting'):
_cfg.add_section('Setting')
_ = ['en', 'en']
try:
if _cfg['Setting']['language'] == 'zh':
_[0] = 'zh'
except KeyError:
pass
try:
if _cfg['Setting']['tooltips'] == 'zh':
_[1] = 'zh'
except KeyError:
pass
return _
class Session(object):
def __init__(self, m):
'''
m: model.Model
'''
self.m = m
self._cfg = ConfigParser()
# https://stackoverflow.com/questions/19359556/configparser-reads-capital-keys-and-make-them-lower-case
# 所有选项的key, 都会传给optionxform(), 该方法会将key转成小写!
# 将optionxform替换成str, 表示不做转换
self._cfg.optionxform = str
def save_to_tmp(self):
self._save_to_tmp_target()
self._save_to_tmp_ckbtn()
self._save_to_tmp_entry()
self._save_to_tmp_radio()
with open(LAST_TMP, 'w') as f:
self._cfg.write(f)
def load_from_tmp(self):
self._cfg.read(LAST_TMP, 'utf8')
self._load_from_tmp_target()
self._load_from_tmp_ckbtn()
self._load_from_tmp_entry()
self._load_from_tmp_radio()
def _save_to_tmp_target(self):
if self._cfg.has_section('Target'):
self._cfg.remove_section('Target')
self._cfg.add_section('Target')
_tmp_url = self.m._url_combobox.get_child().get_text().strip()
if _tmp_url:
self._cfg['Target']['_url_combobox'] = _tmp_url
def _save_to_tmp_ckbtn(self):
if self._cfg.has_section('CheckButton'):
self._cfg.remove_section('CheckButton')
self._cfg.add_section('CheckButton')
_checked = []
for _i in dir(self.m):
if _i.endswith('ckbtn'):
_tmp_ckbtn = getattr(self.m, _i)
if isinstance(_tmp_ckbtn, g.CheckButton) and _tmp_ckbtn.get_active():
_checked.append(_i)
if _i == 'tampers':
tampers = getattr(self.m, _i)
for _tamper, index in zip(tampers, range(len(tampers))):
if _tamper.get_active():
_checked.append('tamper_{}'.format(index))
# print(_checked)
self._cfg['CheckButton']['checked'] = ','.join(_checked)
def _save_to_tmp_entry(self):
if self._cfg.has_section('Entry'):
self._cfg.remove_section('Entry')
self._cfg.add_section('Entry')
for _i in dir(self.m):
if _i.endswith('entry'):
_tmp_entry = getattr(self.m, _i)
if isinstance(_tmp_entry, et) and _tmp_entry.get_text().strip():
self._cfg['Entry'][_i] = _tmp_entry.get_text()
def _save_to_tmp_radio(self):
if self._cfg.has_section('Setting'):
self._cfg.remove_section('Setting')
self._cfg.add_section('Setting')
self._cfg['Setting']['language'] = 'en'
self._cfg['Setting']['tooltips'] = 'en'
if self.m._page6_lang_zh_radio.get_active():
self._cfg['Setting']['language'] = 'zh'
if self.m._page6_tooltips_zh_radio.get_active():
self._cfg['Setting']['tooltips'] = 'zh'
def _load_from_tmp_target(self):
if not self._cfg.has_section('Target'):
self._cfg.add_section('Target')
for _i in self._cfg.options('Target'):
if _i == '_url_combobox':
# 不去手动改LAST_TMP, self.m就肯定有_i属性了
_tmp_url = self.m._url_combobox.get_child()
if self._cfg['Target'][_i]:
_tmp_url.set_text(self._cfg['Target'][_i])
break
def _load_from_tmp_ckbtn(self):
if not self._cfg.has_section('CheckButton'):
self._cfg.add_section('CheckButton')
try:
_checked = self._cfg['CheckButton']['checked'].split(',')
_tampers = list(self.m.tampers.keys())
for _i in _checked:
try:
if _i: # _i could be ''
if _i.endswith('_ckbtn'):
_tmp_ckbtn = getattr(self.m, _i)
_tmp_ckbtn.set_active(True)
if _i.startswith('tamper_'):
_tampers[int(_i[len('tamper_'):])].set_active(True)
else: # if _checked = [''], then use default
pass
except AttributeError:
pass
except KeyError:
# if no checked button, then pass
pass
def _load_from_tmp_entry(self):
if not self._cfg.has_section('Entry'):
self._cfg.add_section('Entry')
for _i in self._cfg.options('Entry'):
try:
_tmp_entry = getattr(self.m, _i)
if isinstance(_tmp_entry, et) and self._cfg['Entry'][_i]:
# print(type(self._cfg['Entry'][_i]))
_tmp_entry.set_text(self._cfg['Entry'][_i])
except AttributeError:
pass
def _load_from_tmp_radio(self):
if not self._cfg.has_section('Setting'):
self._cfg.add_section('Setting')
try:
if self._cfg['Setting']['language'] == 'zh':
self.m._page6_lang_zh_radio.set_active(True)
except KeyError:
pass
try:
if self._cfg['Setting']['tooltips'] == 'zh':
self.m._page6_tooltips_zh_radio.set_active(True)
except KeyError:
pass
def main():
pass
if __name__ == '__main__':
main()