22
22
import argparse
23
23
import locale
24
24
import tempfile
25
+ import pathlib
26
+ from pathlib import Path
25
27
26
28
try :
27
29
locale .setlocale (locale .LC_ALL , '' )
@@ -49,7 +51,6 @@ def getEnvironmentVariable(name, fatal=False):
49
51
SETTINGS_FILE = getEnvironmentVariable ("SETTINGS_FILE" , fatal = True )
50
52
ALLSKY_OVERLAY = getEnvironmentVariable ("ALLSKY_OVERLAY" , fatal = True )
51
53
52
-
53
54
LOGLEVEL = 0
54
55
SETTINGS = {}
55
56
TOD = ''
@@ -159,7 +160,6 @@ def convertPath(path):
159
160
160
161
return path
161
162
162
-
163
163
def startModuleDebug (module ):
164
164
global ALLSKY_TMP
165
165
@@ -172,7 +172,6 @@ def startModuleDebug(module):
172
172
except :
173
173
log (0 , f"ERROR: Unable to create { moduleTmpDir } " )
174
174
175
-
176
175
def writeDebugImage (module , fileName , image ):
177
176
global ALLSKY_TMP
178
177
@@ -182,7 +181,6 @@ def writeDebugImage(module, fileName, image):
182
181
cv2 .imwrite (moduleTmpFile , image , params = None )
183
182
log (4 ,"INFO: Wrote debug file {0}" .format (moduleTmpFile ))
184
183
185
-
186
184
def setEnvironmentVariable (name , value , logMessage = '' , logLevel = 4 ):
187
185
result = True
188
186
@@ -196,7 +194,6 @@ def setEnvironmentVariable(name, value, logMessage='', logLevel=4):
196
194
197
195
return result
198
196
199
-
200
197
def setupForCommandLine ():
201
198
global ALLSKYPATH
202
199
@@ -253,11 +250,9 @@ def updateSetting(values):
253
250
254
251
writeSettings ()
255
252
256
-
257
253
def var_dump (variable ):
258
254
pprint .PrettyPrinter (indent = 2 , width = 128 ).pprint (variable )
259
255
260
-
261
256
def log (level , text , preventNewline = False , exitCode = None , sendToAllsky = False ):
262
257
""" Very simple method to log data if in verbose mode """
263
258
global LOGLEVEL , ALLSKY_SCRIPTS
@@ -370,7 +365,6 @@ def asfloat(val):
370
365
371
366
return val
372
367
373
-
374
368
def getExtraDir ():
375
369
return getEnvironmentVariable ("ALLSKY_EXTRA" , fatal = True )
376
370
@@ -390,7 +384,6 @@ def validateExtraFileName(params, module, fileKey):
390
384
391
385
params [fileKey ] = extraDataFilename
392
386
393
-
394
387
def save_extra_data (file_name , extra_data ):
395
388
saveExtraData (file_name , extra_data )
396
389
@@ -554,3 +547,86 @@ def getGPIOPin(pin):
554
547
result = board .D27
555
548
556
549
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