added some comments so Panini will always know what is going on
parent
c704fda00a
commit
02d20128e1
48
sensors.py
48
sensors.py
|
@ -1,21 +1,41 @@
|
||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
import os, json, sys
|
import os, json, sys
|
||||||
|
|
||||||
try:
|
# comments added for Panini
|
||||||
sensor_group = sys.argv[1]
|
|
||||||
sensor = sys.argv[2]
|
|
||||||
except IndexError:
|
|
||||||
print("usage: python3 "+sys.argv[0]+" <sensor group> <sensor>")
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
sensors_process = os.popen("sensors -j")
|
|
||||||
temps_str = sensors_process.read()
|
|
||||||
sensors_process.close()
|
|
||||||
|
|
||||||
temps = json.loads(temps_str)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
# get arguments from command line
|
||||||
|
sensor_group = sys.argv[1] # 1st argument
|
||||||
|
sensor = sys.argv[2] # 2nd arggument
|
||||||
|
except IndexError: # could not get arguments bc the arguments array doesn't contain enough elements
|
||||||
|
print("usage: python3 "+sys.argv[0]+" <sensor group> <sensor>") # show usage information, sys.argv[0] is the name of the python file itself
|
||||||
|
sys.exit(1) #exit with error code 1 bc we want scripts to be able to reliably tell that soemthing went wrong
|
||||||
|
|
||||||
|
sensors_process = os.popen("sensors -j") # run 'sensors -j' to get a json formatted list of hardware sensors and their values
|
||||||
|
temps_str = sensors_process.read() # read output of 'sensors -j'
|
||||||
|
sensors_process.close() # close handle bc we don't need or want to have it around anymore
|
||||||
|
|
||||||
|
temps = json.loads(temps_str) # load json string into a python dict
|
||||||
|
|
||||||
|
try:
|
||||||
|
# this is ... well ... a oneliner I would use...
|
||||||
print([value for key, value in temps[sensor_group][sensor].items() if key.endswith("input")][0])
|
print([value for key, value in temps[sensor_group][sensor].items() if key.endswith("input")][0])
|
||||||
except KeyError:
|
# temps is a dict with multiple dicts inside it
|
||||||
|
# first step is to get the desired sensor group
|
||||||
|
# temps[sensor_group]
|
||||||
|
# then we get the desired sensor from that sensor group
|
||||||
|
# temps[sensor_group][sensor]
|
||||||
|
# from that, we go through all the items of that dict putting the keys and their corresponding values in two variables
|
||||||
|
# for key, value in temps[sensor_group][sensor].items()
|
||||||
|
# check whether the key ends with "input"
|
||||||
|
# if key.endswith("input")
|
||||||
|
# and if it does, we put it in an array
|
||||||
|
# [value for key, value in temps[sensor_group][sensor].items() if key.endswith("input")]
|
||||||
|
# we take the first entry of that array
|
||||||
|
# [value for key, value in temps[sensor_group][sensor].items() if key.endswith("input")][0]
|
||||||
|
# and put it on the console
|
||||||
|
# print([value for key, value in temps[sensor_group][sensor].items() if key.endswith("input")][0])
|
||||||
|
|
||||||
|
except KeyError: # if we can't find an element in the dict that corresponds to the sensor group / sensor given on the command line
|
||||||
print("Sensor \""+sensor_group+":"+sensor+" not found!")
|
print("Sensor \""+sensor_group+":"+sensor+" not found!")
|
||||||
sys.exit(1)
|
sys.exit(1) #exit with error code 1 bc we want scripts to be able to reliably tell that soemthing went wrong
|
||||||
|
|
Loading…
Reference in New Issue