-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.py
262 lines (213 loc) · 6.11 KB
/
common.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
#! env python
# -= encoding=utf-8 =-
'''
Copyright (c) 2018, 2017, shimoda as kuri65536 _dot_ hot mail _dot_ com
( email address: convert _dot_ to . and joint string )
This Source Code Form is subject to the terms of the Mozilla Public License,
v.2.0. If a copy of the MPL was not distributed with this file,
You can obtain one at https://mozilla.org/MPL/2.0/.
'''
from __future__ import print_function
import subprocess as sp
import sys
try:
from typing import (Any, IO, List, Optional,
Text, Tuple, Union, )
Any, IO, List, Optional, Text, Tuple, Union
except:
pass
if sys.version_info[0] == 3:
import tkinter as tk
from tkinter import ttk
else:
import Tkinter as tk
import ttk
import codecs
tk, ttk
class opts(object): # {{{1
fDryrun = True
file_encoding = "utf-8"
fnameIn = "/usr/share/X11/xorg.conf.d/70-synaptics.conf"
fnameOut = "99-synaptics.conf"
xsection = "touchpad catchall"
def allok(seq):
# type: (List[Text]) -> bool
return True
class Percent(object):
def __init__(self, rate): # {{{1
# type: (float) -> None
self.rate = rate # FIXME: decimal?
def __repr__(self): # {{{1
# type: () -> Text
return "{}%".format(self.rate)
def parseInt(src): # {{{1
# type: (str) -> Optional[int]
src = src.strip()
try:
ret = int(src)
except:
return None
return ret
def parseIntOrPercent(src): # {{{1
# type: (str) -> Union[None, int, Percent]
src = src.strip()
if src.endswith("%"):
try:
ret = float(src[:-1])
return Percent(ret)
except:
pass
return None
try:
return int(src)
except:
pass
return None
def parseBool(src):
# type: (str) -> Optional[bool]
ret = parseInt(src)
if ret is None:
return None
if ret == 1:
return True
return False
def parseFloat(src):
# type: (str) -> Optional[float]
src = src.strip()
try:
ret = float(src)
except:
return None
return ret
def compose_format(fmt, vals): # {{{2
# type: (Text, List[Any]) -> Tuple[Text, List[Text]]
class Term(object):
def __init__(self):
# type: () -> None
self.type = u"undefined"
self.sArg = u""
self.nArg = -2
def compose(self, arg):
# type: (Any) -> Text
return str(arg)
ret1 = u""
chEscape = u""
seq = []
for ch in fmt:
if chEscape != u"":
chEscape = u""
ret1 = ret1 + chEscape + ch
continue
if ch == u"{":
term = Term()
ret1 += ch
continue
if term is None:
ret1 += ch
continue
if term.nArg == -2: # not parsed
if ch == u":":
if not term.sArg.isdigit():
term.nArg = -1 # invalid
else:
term.nArg = int(term.sArg)
continue
if ch.isdigit():
term.sArg += ch
continue
term.nArg = -1 # not set
if ch == u"}":
ret1 += ch
seq.append(term)
continue
if ch == u"P":
term.type = "int or percent"
elif ch == u"d":
term.type = u"int"
elif ch == u"f":
term.type = u"float"
elif ch == u"b":
term.type = u"bool"
else:
assert False, u"new type of compose: {}".format(ch)
terms = ["" for i in range(len(vals))]
for n, term in enumerate(seq):
if term.nArg > 0:
i = term.nArg
else:
i = n
if i >= len(terms):
assert False, u"format arguments too much than params->{}".format(
fmt)
terms[i] = term.compose(vals[i])
import pdb
pdb.set_trace()
return ret1, terms
# wrapper for mypy {{{1
class GuiVar(object): # {{{1
def get(self): # {{{1
# type: () -> Text
assert False
class IntVar(GuiVar): # {{{1
def __init__(self, v):
# type: (tk.IntVar) -> None
self._val = v
def get(self):
# type: () -> Text
ret = int(self._val.get()) # type: ignore # for Tk
return Text(ret)
class FltVar(GuiVar): # {{{1
def __init__(self, v):
# type: (float) -> None
self._val = tk.DoubleVar() # type: ignore # for Tk
self._val.set(v) # type: ignore
def get(self):
# type: () -> Text
assert self._val is not None
ret = float(self._val.get()) # type: ignore # for Tk
return Text(ret)
def get_var(self):
# type: () -> tk.DoubleVar
return self._val # type: ignore # for Tk
class BoolVar(GuiVar): # {{{1
def __init__(self, v):
# type: (Optional[tk.IntVar]) -> None
self._val = v
def get(self):
# type: () -> Text
assert self._val is not None
return int(self._val.get()) == 1 # type: ignore # for Tk
def set(self, v):
# type: (bool) -> bool
assert self._val is not None
self._val.set(1 if v else 0) # type: ignore # for Tk
return v
class CmbVar(GuiVar): # {{{1
def __init__(self, v):
# type: (Optional[ttk.Combobox]) -> None
self._val = v
def get(self):
# type: () -> Text
assert self._val is not None
ret = int(self._val.current()) # type: ignore # for Tk
return Text(ret)
def open_file(fname, mode): # {{{2
# type: (str, str) -> IO[Any]
enc = opts.file_encoding
if sys.version_info[0] == 2:
return codecs.open(fname, mode, enc)
else:
return open(fname, mode + "t", encoding=enc)
def check_output(args): # {{{1
# type: (List[Text]) -> Text
enc = opts.file_encoding
curb = sp.check_output(args)
curs = curb.decode(enc)
return curs # type: ignore
# main {{{1
def main(): # {{{1
# type: () -> int
pass # TODO: launch test
if __name__ == "__main__": # end of file {{{1
main()
# vi: ft=python:et:fdm=marker:nowrap:tw=80