This repository has been archived by the owner on Jun 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
paperpi.py
596 lines (419 loc) · 19.3 KB
/
paperpi.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
#!/usr/bin/env python3
# coding: utf-8
import os
import logging
import logging.config
import shutil
import sys
from itertools import cycle
from inspect import getfullargspec
from importlib import import_module
from time import sleep
from pathlib import Path
from distutils.util import strtobool
import waveshare_epd
import ArgConfigParse
from epdlib import Screen
from epdlib.Screen import Update
from library.CacheFiles import CacheFiles
from library.Plugin import Plugin
from library.InterruptHandler import InterruptHandler
from library import get_help
from library import run_module
import constants
def do_exit(status=0, message=None, **kwargs):
if message:
if status > 0:
logging.error(f'failure caused exit: {message}')
border = '\n'+'#'*70 + '\n'
message = border + message + border + '\n***Exiting***'
print(message)
try:
sys.exit(status)
except Exception as e:
pass
def clean_up(cache=None, screen=None):
logging.info('cleaning up cache and screen')
try:
cache.cleanup()
except AttributeError:
logging.debug('no cache passed, skipping')
try:
# screen.initEPD()
screen.clearEPD()
except AttributeError:
logging.debug('no screen passed, skipping')
return
def get_cmd_line_args():
cmd_args = ArgConfigParse.CmdArgs()
cmd_args.add_argument('-c', '--config', ignore_none=True, metavar='CONFIG_FILE.ini',
type=str, dest='user_config',
help='use the specified configuration file')
cmd_args.add_argument('-l', '--log_level', ignore_none=True, metavar='LOG_LEVEL',
type=str, choices=['DEBUG', 'INFO', 'WARNING', 'ERROR'],
dest='main__log_level', help='change the log output level')
cmd_args.add_argument('--plugin_info', metavar='[plugin|plugin.function]',
required=False, default=None,
ignore_none=True,
help='get information for plugins and user-facing functions provided by a plugin')
cmd_args.add_argument('--list_plugins', required=False,
default=False, action='store_true',
help='list all available plugins')
cmd_args.add_argument('--run_plugin_func',
required=False, default=None, nargs='+',
metavar=('plugin.function', 'optional_arg1 arg2 argN'),
ignore_none=True,
help='run a user-facing function for a plugin')
cmd_args.add_argument('-d', '--daemon', required=False, default=False,
dest='main__daemon', action='store_true',
help='run in daemon mode (ignore user configuration if found)')
cmd_args.add_argument('-V', '--version', required=False, default=False, ignore_false=True,
action='store_true',
help='display version and exit')
cmd_args.parse_args()
return cmd_args
def get_config_files(cmd_args):
'''read config.ini style file(s)
Args:
cmd_args(`ArgConfigParse.CmdArgs()` object)
Returns:
`ArgConfigParse.ConfigFile`'''
config_files_list = [constants.config_base, constants.config_system, constants.config_user]
daemon = False
config_exists = True
if hasattr(cmd_args.options, "main__daemon"):
logging.debug('-d specified on command line')
if cmd_args.options.main__daemon:
logging.debug('excluding user config files')
config_files_list.pop()
daemon = True
else:
daemon = False
else:
daemon = False
if not daemon:
# create a user config directory
if not constants.config_user.exists():
config_exists = False
logging.info('creating user config directory and inserting config file')
try:
constants.config_user.parent.mkdir(parents=True, exist_ok=True)
except PermissionError as e:
logging.warning(f'could not create {constants.config_user}: {e}')
if not constants.config_user.exists():
try:
shutil.copy(constants.config_base, constants.config_user)
except Exception as e:
logging.critical(f'could not copy configuration file to {constants.config_user}: {e}')
if not config_exists:
msg = f'''This appears to be the first time PaperPi has been run.
A user configuration file created: {constants.config_user}
At minimum you edit this file and add a display_type
Edit the configu file with "$ nano {constants.config_user}"'''
do_exit(0, msg)
logging.debug(f'using configuration files to configure PaperPi: {config_files_list}')
config_files = ArgConfigParse.ConfigFile(config_files_list, ignore_missing=True)
config_files.parse_config()
return config_files
def sanitize_vals(config):
'''attempt to convert all the strings into appropriate formats
integer/float like strings ('7', '100', '-1.3') -> int
boolean like strings (yes, no, Y, t, f, on, off) -> 0 or 1
Args:
config(`dict`): nested config.ini style dictionary
Returns:
`dict`'''
def convert(d, func, exceptions):
'''convert values in nested dictionary using a specified function
values that raise an exception are left unchanged
Args:
d(`dict`): nested dictionary {'Section': {'key': 'value'}}
func(`function`): function such as int() or strtobool()
exceptions(`tuple`):
Returns:
`dict`'''
for section, values in d.items():
for key, value in values.items():
try:
sanitized = func(value)
except exceptions:
sanitized = value
d[section][key] = sanitized
return d
#attempt to convert any string that looks like an int/float into a string
str_to_ints = convert(config, int, (ValueError))
# attempt to convert any string that looks like a bool into a bool
str_to_bool = convert(str_to_ints, strtobool, (ValueError, AttributeError))
config = str_to_bool
return config
def setup_splash(config, resolution):
logging.debug('checking splash settings')
if 'splash' in config['main']:
logging.debug('checking splash screen settings')
if config['main']['splash']:
logging.debug('splash enabled in confg file')
splash = True
else:
logging.debug('splash disabled in config file')
splash = False
else:
splash = True
if splash:
logging.debug('splash screen enabled')
from plugins.splash_screen import splash_screen
splash_config = {
'name': 'Splash Screen',
'layout': splash_screen.layout.layout,
'update_function': splash_screen.update_function,
'resolution': resolution
}
logging.debug(f'configuring splash screen: {splash_config}')
splash = Plugin(**splash_config)
splash.update(constants.app_name, constants.version, constants.url)
logging.debug(f'splash screen image type: {type(splash.image)}')
return splash
def setup_display(config):
def ret_obj(obj=None, status=0, message=None):
return{'obj': obj, 'status': status, 'message': message}
keyError_fmt = 'configuration KeyError: section[{}], key: {}'
moduleNotFoundError_fmt = 'could not load module: {} -- error: {}'
try:
logging.debug('setting display type')
epd_module = '.'.join([constants.waveshare_epd, config['main']['display_type']])
epd = import_module(epd_module)
except KeyError as e:
return_val = ret_obj(obj=None, status=1, message=keyError_fmt.format('main', 'display_type'))
logging.error(return_val['message'])
return return_val
except ModuleNotFoundError as e:
logging.error('Check your config files and ensure a known waveshare_epd display is specified!')
return_val = ret_obj(None, 1, moduleNotFoundError_fmt.format(config["main"]["display_type"], e))
return return_val
except FileNotFoundError as e:
msg = f''''Error loading waveshare_epd module: {e}
This is typically due to SPI not being enabled, or the current user is
not a member of the SPI group.
"$ sudo raspi-config nonint get_spi" will return 0 if SPI is enabled
Try enabling SPI and run this program again. '''
logging.error(msg)
return_val = ret_obj(obj=None, status=1, message=msg)
return return_val
screen = Screen()
try:
screen.epd = epd
except PermissionError as e:
logging.critical(f'Error initializing EPD: {e}')
logging.critical(f'The user executing {constants.app_name} does not have access to the SPI device.')
return_val = ret_obj(None, 1, 'This user does not have access to the SPI group\nThis can typically be resolved by running:\n$ sudo groupadd <username> spi')
return return_val
try:
config['main']['rotation'] = int(config['main']['rotation'])
except KeyError as e:
logging.info(keyError_fmt.format('main', 'rotation'))
logging.info('using default: 0')
try:
screen.rotation = config['main']['rotation']
except ValueError as e:
logging.error('invalid rotation; valid values are: 0, 90, -90, 180')
return_val = ret_obj(None, 1, keyError_fmt.format('main', 'rotation'))
return ret_obj(obj=screen)
def build_plugin_list(config, resolution, cache):
# get the expected key-word args from the Plugin() spec
spec_kwargs = getfullargspec(Plugin).args
plugins = []
for section, values in config.items():
# ignore the other sections
if section.startswith('Plugin:'):
logging.info(f'[[ {section} ]]')
my_config = {}
# add all the spec_kwargs from the config
plugin_kwargs = {}
for key, val in values.items():
if key in spec_kwargs:
my_config[key] = val
else:
# add everything that is not one of the spec_kwargs to this dict
plugin_kwargs[key] = val
# populate the kwargs my_config dict that will be passed to the Plugin() object
my_config['name'] = section
my_config['resolution'] = resolution
my_config['config'] = plugin_kwargs
my_config['cache'] = cache
try:
module = import_module(f'{constants.plugins}.{values["plugin"]}')
my_config['update_function'] = module.update_function
my_config['layout'] = getattr(module.layout, values['layout'])
except KeyError as e:
logging.info('no module specified; skipping update_function and layout')
except ModuleNotFoundError as e:
logging.warning(f'error: {e} while loading module {constants.plugins}.{values["plugin"]}')
logging.warning(f'skipping module')
continue
my_plugin = Plugin(**my_config)
try:
my_plugin.update()
except AttributeError as e:
logging.warning(f'ignoring plugin {my_plugin.name} due to missing update_function')
logging.warning(f'plugin threw error: {e}')
continue
logging.info(f'appending plugin {my_plugin.name}')
plugins.append(my_plugin)
if len(plugins) < 1:
my_config = {}
logging.warning('no plugins were loaded! falling back to default plugin.')
my_config['name'] = 'default plugin'
my_config['resolution'] = resolution
my_config['cache'] = cache
try:
module = import_module(f'{constants.plugins}.default')
except ModuleNotFoundError as e:
msg = f'could not load {constants.plugins}.default'
logging.error(msg)
do_exit(1, msg)
my_config['update_function'] = module.update_function
my_config['layout'] = getattr(module.layout, 'default')
my_plugin = Plugin(**my_config)
plugins.append(my_plugin)
return plugins
def update_loop(plugins, screen):
exit_code = 1
logging.info('starting update loop')
def update_plugins():
'''run through all active plugins and update while recording the priority'''
my_list = []
logging.info('*'*15)
logging.info(f'My PID: {os.getppid()}')
logging.info(f'updating {len(plugins)} plugins')
for plugin in plugins:
plugin.update()
logging.info(f'update: [{plugin.name}]-p: {plugin.priority}')
my_list.append(plugin.priority)
logging.info(f'priorities: {my_list}')
return my_list
# use itertools cycle to move between list elements
plugin_cycle = cycle(plugins)
plugin_is_active = False
# current plugin for display
this_plugin = next(plugin_cycle)
# track time plugin is displayed for
this_plugin_timer = Update()
# each plugin generates a unique hash whenever it is updated
this_hash = ''
# update all the plugins and record priority
priority_list = update_plugins()
# this var name is confusing -- it's actually the lowest number to indicate **maximum** priority
max_priority = min(priority_list)
# record for comparison
last_priority = max_priority
logging.info(f'max_priority: {max_priority}')
for plugin in plugins:
if plugin.priority <= max_priority:
this_hash = plugin.hash
logging.info(f'**** displaying {plugin.name} ****')
# screen.initEPD()
screen.writeEPD(plugin.image)
break
with InterruptHandler() as h:
while True:
if h.interrupted:
logging.info('caught interrupt -- stoping execution')
exit_code = 0
break
priority_list = update_plugins()
# priority increases as it gets lower; 0 is considered the bottom,
# but some modules may temporarily have a negative priority to indicate a critical
# update
last_priority = max_priority
max_priority = min(priority_list)
logging.debug(f'{this_plugin.name}: last updated: {this_plugin_timer.last_updated}, min_display_time: {this_plugin.min_display_time}')
# if the timer has expired OR a module has changed the priority setting begin the update procedure
if this_plugin_timer.last_updated > this_plugin.min_display_time or max_priority < last_priority:
logging.info(f'plugin expired -- switching plugin')
plugin_is_active = False
# cycle through plugins, looking for the next plugin that has high priority
while not plugin_is_active:
this_plugin = next(plugin_cycle)
logging.debug(f'checking priority of {plugin.name}')
if this_plugin.priority <= max_priority:
plugin_is_active = True
else:
logging.debug('trying next plugin')
pluggin_is_active = False
logging.info(f'displaying {this_plugin.name} -- priority: {this_plugin.priority}/{max_priority}')
if this_hash != this_plugin.hash:
logging.debug('data refreshed, refreshing screen')
this_hash = this_plugin.hash
# screen.initEPD()
logging.debug(f'image type: {type(this_plugin.image)}')
if screen.writeEPD(this_plugin.image):
logging.debug('successfully wrote image')
else:
logging.warning('#=#=# failed to write image #=#=#')
logging.info('trying next plugin')
plugin_is_active = False
else:
logging.debug('plugin data not refreshed -- skipping screen refresh')
this_plugin_timer.update()
sleep(1)
return exit_code
def main():
# change the working directory -- this simplifies all path work later on
os.chdir(constants.absolute_path)
# set the absolute path to the current directory
absolute_path = constants.absolute_path
# set up logging
logging.config.fileConfig(constants.logging_config)
logger = logging.getLogger(__name__)
# get command line and config file arguments
cmd_args = get_cmd_line_args()
if hasattr(cmd_args, 'unknown'):
print(f'Unknown arguments: {cmd_args.unknown}\n\n')
cmd_args.parser.print_help()
return
config_files = get_config_files(cmd_args)
# merge file and commandline (right-most over-writes left)
config = ArgConfigParse.merge_dict(config_files.config_dict, cmd_args.nested_opts_dict)
if cmd_args.options.version:
print(constants.version_string)
return
if cmd_args.options.plugin_info:
print(get_help.get_help(cmd_args.options.plugin_info))
return
if cmd_args.options.list_plugins:
print(get_help.get_help())
return
if cmd_args.options.run_plugin_func:
run_module.run_module(cmd_args.options.run_plugin_func)
return
# make sure all the integer-like strings are converted into integers
config = sanitize_vals(config)
logger.setLevel(config['main']['log_level'])
logging.root.setLevel(config['main']['log_level'])
logging.debug(f'********** PaperPi {constants.version} Starting **********')
# configure screen
screen_return = setup_display(config)
if screen_return['obj']:
screen = screen_return['obj']
else:
clean_up(None, None)
logging.error(f'config files used: {config_files.config_files}')
do_exit(**screen_return)
# try to set up the splash screen several times here -- this may solve the None image problem.
splash = setup_splash(config, screen.resolution)
if splash:
logging.debug('displaying splash screen')
logging.debug(f'image type: {type(splash.image)}')
screen.writeEPD(splash.image)
cache = CacheFiles(path_prefix=constants.app_name)
plugins = build_plugin_list(config, screen.resolution, cache)
exit_code = update_loop(plugins, screen)
logging.info('caught terminate signal -- cleaning up and exiting')
clean_up(cache, screen)
return exit_code
if __name__ == "__main__":
# remove jupyter runtime junk for testing
if len(sys.argv) >= 2 and 'ipykernel' in sys.argv[0]:
sys.argv = [sys.argv[0]]
sys.argv.extend(sys.argv[3:])
exit_code = main()
sys.exit(exit_code)