forked from MiczFlor/RPi-Jukebox-RFID
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmisc.py
126 lines (97 loc) · 4.01 KB
/
misc.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
"""
Miscellaneous function package
"""
import os
import time
import logging.handlers
import jukebox
import jukebox.plugs as plugin
import jukebox.utils
from jukebox.daemon import get_jukebox_daemon
import jukebox.cfghandler
logger = logging.getLogger('jb.misc')
cfg = jukebox.cfghandler.get_handler('jukebox')
@plugin.register
def rpc_cmd_help():
"""Return all commands for RPC"""
return plugin.summarize()
@plugin.register
def get_all_loaded_packages():
"""Get all successfully loaded plugins"""
return plugin.get_all_loaded_packages()
@plugin.register
def get_all_failed_packages():
"""Get all plugins with error during load or initialization"""
return plugin.get_all_failed_packages()
@plugin.register
def get_start_time():
"""Time when JukeBox has been started"""
return time.ctime(get_jukebox_daemon().start_time)
def get_log(handler_name: str):
"""Get the log file from the loggers (debug_file_handler, error_file_handler)"""
# With the correct logger.yaml, there is up to two RotatingFileHandler attached
content = "No file handles configured"
for h in logging.getLogger('jb').handlers:
if isinstance(h, logging.handlers.RotatingFileHandler):
content = f"No file handler with name {handler_name} configured"
if h.name == handler_name:
try:
size = os.path.getsize(h.baseFilename)
if size == 0:
content = f"Log file {h.baseFilename} is empty. (Could be good or bad: " \
"Is the RotatingFileHandler configured as handler sink for jb in logger.yaml?)"
break
mtime = os.path.getmtime(h.baseFilename)
stime = get_jukebox_daemon().start_time
logger.debug(f"Accessing log file {h.baseFilename} modified time {time.ctime(mtime)} "
f"(JB start time {time.ctime(stime)})")
# Generous 3 second tolerance between file creation and jukebox start time recording
if mtime - stime < -3:
content = (f"Log file {h.baseFilename} too old for this Jukebox start! "
f"Is the RotatingFileHandler configured as handler sink for jb in logger.yaml?")
break
with open(h.baseFilename) as stream:
content = stream.read()
except Exception as e:
content = f"{e.__class__.__name__}: {e}"
logger.error(content)
break
return content
@plugin.register
def get_log_debug():
"""Get the log file (from the debug_file_handler)"""
return get_log('debug_file_handler')
@plugin.register
def get_log_error():
"""Get the log file (from the error_file_handler)"""
return get_log('error_file_handler')
@plugin.register
def get_version():
return jukebox.version()
@plugin.register
def get_git_state():
"""Return git state information for the current branch"""
return get_jukebox_daemon().git_state
@plugin.register
def empty_rpc_call(msg: str = ''):
"""This function does nothing.
The RPC command alias 'none' is mapped to this function.
This is also used when configuration errors lead to non existing RPC command alias definitions.
When the alias definition is void, we still want to return a valid function to simplify error handling
up the module call stack.
:param msg: If present, this message is send to the logger with severity warning
"""
if msg:
logger.warning(msg)
@plugin.register
def get_app_settings():
"""Return settings for web app stored in jukebox.yaml"""
show_covers = cfg.setndefault('webapp', 'show_covers', value=True)
return {
'show_covers': show_covers
}
@plugin.register
def set_app_settings(settings={}):
"""Set configuration settings for the web app."""
for key, value in settings.items():
cfg.setn('webapp', key, value=value)