Skip to content

Commit 91a4929

Browse files
#4070 Add function to read any Allsky variable
1 parent de656d8 commit 91a4929

File tree

1 file changed

+85
-9
lines changed

1 file changed

+85
-9
lines changed

scripts/modules/allsky_shared.py

+85-9
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import argparse
2323
import locale
2424
import tempfile
25+
import pathlib
26+
from pathlib import Path
2527

2628
try:
2729
locale.setlocale(locale.LC_ALL, '')
@@ -49,7 +51,6 @@ def getEnvironmentVariable(name, fatal=False):
4951
SETTINGS_FILE = getEnvironmentVariable("SETTINGS_FILE", fatal=True)
5052
ALLSKY_OVERLAY = getEnvironmentVariable("ALLSKY_OVERLAY", fatal=True)
5153

52-
5354
LOGLEVEL = 0
5455
SETTINGS = {}
5556
TOD = ''
@@ -159,7 +160,6 @@ def convertPath(path):
159160

160161
return path
161162

162-
163163
def startModuleDebug(module):
164164
global ALLSKY_TMP
165165

@@ -172,7 +172,6 @@ def startModuleDebug(module):
172172
except:
173173
log(0, f"ERROR: Unable to create {moduleTmpDir}")
174174

175-
176175
def writeDebugImage(module, fileName, image):
177176
global ALLSKY_TMP
178177

@@ -182,7 +181,6 @@ def writeDebugImage(module, fileName, image):
182181
cv2.imwrite(moduleTmpFile, image, params=None)
183182
log(4,"INFO: Wrote debug file {0}".format(moduleTmpFile))
184183

185-
186184
def setEnvironmentVariable(name, value, logMessage='', logLevel=4):
187185
result = True
188186

@@ -196,7 +194,6 @@ def setEnvironmentVariable(name, value, logMessage='', logLevel=4):
196194

197195
return result
198196

199-
200197
def setupForCommandLine():
201198
global ALLSKYPATH
202199

@@ -253,11 +250,9 @@ def updateSetting(values):
253250

254251
writeSettings()
255252

256-
257253
def var_dump(variable):
258254
pprint.PrettyPrinter(indent=2, width=128).pprint(variable)
259255

260-
261256
def log(level, text, preventNewline = False, exitCode=None, sendToAllsky=False):
262257
""" Very simple method to log data if in verbose mode """
263258
global LOGLEVEL, ALLSKY_SCRIPTS
@@ -370,7 +365,6 @@ def asfloat(val):
370365

371366
return val
372367

373-
374368
def getExtraDir():
375369
return getEnvironmentVariable("ALLSKY_EXTRA", fatal=True)
376370

@@ -390,7 +384,6 @@ def validateExtraFileName(params, module, fileKey):
390384

391385
params[fileKey] = extraDataFilename
392386

393-
394387
def save_extra_data(file_name, extra_data):
395388
saveExtraData(file_name, extra_data)
396389

@@ -554,3 +547,86 @@ def getGPIOPin(pin):
554547
result = board.D27
555548

556549
return result
550+
551+
552+
def _get_value_from_json_file(file_path, variable):
553+
"""
554+
Loads a json based extra data file and returns the value of a variable if found
555+
556+
Args:
557+
variable (string): The varible to get
558+
559+
Returns:
560+
result (various) The result or None if the variable could not be found
561+
"""
562+
result = None
563+
try:
564+
with open(file_path, encoding='utf-8') as file:
565+
json_data = json.load(file)
566+
for (name, value_data) in json_data.items():
567+
if name == variable:
568+
if isinstance(value_data, dict):
569+
if 'value' in value_data:
570+
result = value_data['value']
571+
else:
572+
result = value_data
573+
except: # pylint: disable=W0702
574+
pass
575+
576+
return result
577+
578+
def _get_value_from_text_file(file_path, variable):
579+
"""
580+
Loads a text based extra data file and returns the value of a variable if found
581+
582+
Args:
583+
variable (string): The varible to get
584+
585+
Returns:
586+
result (various) The result or None if the variable could not be found
587+
"""
588+
result = None
589+
590+
with open(file_path, encoding='utf-8') as file:
591+
for line in file:
592+
name, value = line.partition("=")[::2]
593+
name = name.rstrip()
594+
value = value.lstrip()
595+
value = value.strip()
596+
if name == variable:
597+
result = value
598+
break
599+
600+
return result
601+
602+
def get_allsky_variable(variable):
603+
"""
604+
Gets an Allsky variable either from the environment or extra data files
605+
606+
Args:
607+
variable (string): The varible to get
608+
609+
Returns:
610+
result (various) The result or None if the variable could not be found
611+
"""
612+
result = getEnvironmentVariable(variable)
613+
614+
if result is None:
615+
extra_data_path = getExtraDir()
616+
directory = Path(extra_data_path)
617+
618+
for file_path in directory.iterdir():
619+
if file_path.is_file() and isFileReadable(file_path):
620+
621+
file_extension = Path(file_path).suffix
622+
623+
if file_extension == '.json':
624+
result = _get_value_from_json_file(file_path, variable)
625+
626+
if file_extension == '.txt':
627+
result = _get_value_from_text_file(file_path, variable)
628+
629+
if result is not None:
630+
break
631+
632+
return result

0 commit comments

Comments
 (0)