-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.py
70 lines (65 loc) · 2.04 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
import os
import sys
import StringIO
import contextlib
import netifaces as ni
@contextlib.contextmanager
def stdoutIO(stdout=None):
old = sys.stdout
if stdout is None:
stdout = StringIO.StringIO()
sys.stdout = stdout
yield stdout
sys.stdout = old
# A dictionary of parameter names and their shell command names
commandsDictionarySh = {
'temperature_gpu': '/opt/vc/bin/vcgencmd measure_temp | awk \'{print substr($1, 6, 4)}\'',
'temperature_cpu': 'cat /sys/class/thermal/thermal_zone0/temp',
'uname': 'uname -a',
'reference': 'cat /etc/rpi-issue',
'release_os': 'cat /etc/os-release',
'revision_processor': 'cat /proc/cpuinfo | grep "Revision" | cut -d \' \' -f 2',
'serialnumber_processor': 'cat /proc/cpuinfo | grep "Serial" | cut -d \' \' -f 2',
'hostname': 'echo $HOSTNAME',
'username': 'whoami',
'date': 'date'
}
# A dictionary of parameter names and their Python command names
commandsDictionaryPy = {
'ipv4_eth0': 'ni.ifaddresses(\'eth0\')[2][0][\'addr\']',
'ipv4_wlan0': 'ni.ifaddresses(\'wlan0\')[2][0][\'addr\']'
}
# A dictionary of formatting elements for different types of response formats
styleguide = {
'header': {
'html': '<html>\n<body>\n<table>\n<tbody>\n'
},
'simpleheader': {
'html': '<html>\n<body>\n'
},
'entryLeft': {
'html': '\t<tr>\n\t\t<td id=\"'
},
'entryMid': {
'html': '\">'
},
'entryRight': {
'html': '</td>\n\t</tr>\n'
},
'footer': {
'html': '</tbody>\n</table>\n</body>\n</html>'
},
'simplefooter': {
'html': '</body>\n</html>'
}
}
# A wrapper function to retrieve values of parameters using either Shell or Python
def getParameterHandler(parameterName):
if parameterName in commandsDictionarySh:
return os.popen(commandsDictionarySh[parameterName]).read().strip('\n')
elif parameterName in commandsDictionaryPy:
with stdoutIO() as s:
exec(commandsDictionaryPy[parameterName])
return s.getvalue()
else:
return ''