-
Notifications
You must be signed in to change notification settings - Fork 8
/
utilities.py
283 lines (223 loc) · 8.73 KB
/
utilities.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
# Copyright (C) 2013-2018 Jean-Francois Romang ([email protected])
# Shivkumar Shivaji ()
# Jürgen Précour ([email protected])
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import logging
import queue
import os
import platform
import urllib.request
import socket
import json
import time
import copy
import configparser
from threading import Timer
from subprocess import Popen, PIPE
from dgt.translate import DgtTranslate
from dgt.api import Dgt
from configobj import ConfigObj, ConfigObjError, DuplicateError
# picochess version
version = '09n'
evt_queue = queue.Queue()
dispatch_queue = queue.Queue()
msgdisplay_devices = []
dgtdisplay_devices = []
class Observable(object):
"""Input devices are observable."""
def __init__(self):
super(Observable, self).__init__()
@staticmethod
def fire(event):
"""Put an event on the Queue."""
evt_queue.put(copy.deepcopy(event))
class DispatchDgt(object):
"""Input devices are observable."""
def __init__(self):
super(DispatchDgt, self).__init__()
@staticmethod
def fire(dgt):
"""Put an event on the Queue."""
dispatch_queue.put(copy.deepcopy(dgt))
class DisplayMsg(object):
"""Display devices (DGT XL clock, Piface LCD, pgn file...)."""
def __init__(self):
super(DisplayMsg, self).__init__()
self.msg_queue = queue.Queue()
msgdisplay_devices.append(self)
@staticmethod
def show(message):
"""Send a message on each display device."""
for display in msgdisplay_devices:
display.msg_queue.put(copy.deepcopy(message))
class DisplayDgt(object):
"""Display devices (DGT XL clock, Piface LCD, pgn file...)."""
def __init__(self):
super(DisplayDgt, self).__init__()
self.dgt_queue = queue.Queue()
dgtdisplay_devices.append(self)
@staticmethod
def show(message):
"""Send a message on each display device."""
for display in dgtdisplay_devices:
display.dgt_queue.put(copy.deepcopy(message))
class RepeatedTimer(object):
"""Call function on a given interval."""
def __init__(self, interval, function, *args, **kwargs):
self._timer = None
self.interval = interval
self.function = function
self.args = args
self.kwargs = kwargs
self.timer_running = False
def _run(self):
self.timer_running = False
self.start()
self.function(*self.args, **self.kwargs)
def is_running(self):
"""Return the running status."""
return self.timer_running
def start(self):
"""Start the RepeatedTimer."""
if not self.timer_running:
self._timer = Timer(self.interval, self._run)
self._timer.start()
self.timer_running = True
else:
logging.info('repeated timer already running - strange!')
def stop(self):
"""Stop the RepeatedTimer."""
if self.timer_running:
self._timer.cancel()
self.timer_running = False
else:
logging.info('repeated timer already stopped - strange!')
def get_opening_books():
"""Build an opening book lib."""
config = configparser.ConfigParser()
config.optionxform = str
program_path = os.path.dirname(os.path.realpath(__file__)) + os.sep
book_path = program_path + 'books'
config.read(book_path + os.sep + 'books.ini')
library = []
for section in config.sections():
text = Dgt.DISPLAY_TEXT(l=config[section]['large'], m=config[section]['medium'], s=config[section]['small'],
wait=True, beep=False, maxtime=0, devs={'ser', 'i2c', 'web'})
library.append(
{
'file': 'books' + os.sep + section,
'text': text
}
)
return library
def hms_time(seconds: int):
"""Transfer a seconds integer to hours,mins,secs."""
if seconds < 0:
logging.warning('negative time %i', seconds)
return 0, 0, 0
mins, secs = divmod(seconds, 60)
hours, mins = divmod(mins, 60)
return hours, mins, secs
def do_popen(command, log=True, force_en_env=False):
"""Connect via Popen and log the result."""
if force_en_env: # force an english environment
force_en_env = os.environ.copy()
force_en_env['LC_ALL'] = 'C'
stdout, stderr = Popen(command, stdout=PIPE, stderr=PIPE, env=force_en_env).communicate()
else:
stdout, stderr = Popen(command, stdout=PIPE, stderr=PIPE).communicate()
if log:
logging.debug([output.decode(encoding='UTF-8') for output in [stdout, stderr]])
return stdout.decode(encoding='UTF-8')
def git_name():
"""Get the git execute name."""
return 'git.exe' if platform.system() == 'Windows' else 'git'
def get_tags():
"""Get the last 3 tags from git."""
#git = git_name()
#tags = [(tags, tags[1] + tags[-2:]) for tags in do_popen([git, 'tag'], log=False).split('\n')[-4:-1]]
#return tags # returns something like [('v0.9j', 09j'), ('v0.9k', '09k'), ('v0.9l', '09l')]
def checkout_tag(tag):
"""Update picochess by tag from git."""
git = git_name()
do_popen([git, 'checkout', tag])
do_popen(['pip3', 'install', '-r', 'requirements.txt'])
def update_picochess(dgtpi: bool, auto_reboot: bool, dgttranslate: DgtTranslate):
"""Update picochess from git."""
git = git_name()
branch = do_popen([git, 'rev-parse', '--abbrev-ref', 'HEAD'], log=False).rstrip()
if branch == 'master':
# Fetch remote repo
do_popen([git, 'remote', 'update'])
# Check if update is needed - need to make sure, we get english answers
output = do_popen([git, 'status', '-uno'], force_en_env=True)
if 'up-to-date' not in output and 'Your branch is ahead of' not in output:
DispatchDgt.fire(dgttranslate.text('Y25_update'))
# Update
logging.debug('updating picochess')
do_popen([git, 'pull', 'origin', branch])
do_popen(['pip3', 'install', '-r', 'requirements.txt'])
if auto_reboot:
reboot(dgtpi, dev='web')
else:
time.sleep(2) # give time to display the "update" message
else:
logging.debug('no update available')
else:
logging.warning('wrong branch %s', branch)
def shutdown(dgtpi: bool, dev: str):
"""Shutdown picochess."""
logging.debug('shutting down system requested by (%s)', dev)
time.sleep(3) # give some time to send out the pgn file or speak the event
if platform.system() == 'Windows':
os.system('shutdown /s')
elif dgtpi:
os.system('systemctl isolate dgtpistandby.target')
else:
os.system('shutdown -h now')
def reboot(dgtpi: bool, dev: str):
"""Reboot picochess."""
logging.debug('rebooting system requested by (%s)', dev)
time.sleep(3) # give some time to send out the pgn file or speak the event
if platform.system() == 'Windows':
os.system('shutdown /r')
elif dgtpi:
os.system('systemctl restart picochess')
else:
os.system('reboot')
def get_location():
"""Return the location of the user and the external and interal ip adr."""
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.connect(('8.8.8.8', 80))
int_ip = sock.getsockname()[0]
sock.close()
response = urllib.request.urlopen('http://will6.de/freegeoip')
j = json.loads(response.read().decode())
country_name = j['country_name'] + ' ' if 'country_name' in j else ''
country_code = j['country_code'] + ' ' if 'country_code' in j else ''
ext_ip = j['ip'] if 'ip' in j else None
city = j['city'] + ', ' if 'city' in j else ''
return (city + country_name + country_code).strip(), ext_ip, int_ip
except:
return '?', None, None
def write_picochess_ini(key: str, value):
"""Update picochess.ini config file with key/value."""
try:
config = ConfigObj('picochess.ini')
config[key] = value
config.write()
except (ConfigObjError, DuplicateError) as conf_exc:
logging.exception(conf_exc)