wip: intergrating clockyconfig in main code

This commit is contained in:
Jan
2019-09-27 17:34:25 +02:00
parent dd1355509b
commit f147bc55c6
3 changed files with 137 additions and 138 deletions

View File

@@ -1,16 +1,16 @@
#!/usr/bin/python
#!/usr/bin/python3
import json
import os
import time
import copy
CONFIGFILEVERSION="1.0"
CONFIGFILEVERSION="1.1"
class ClockyConfig:
"""Read/write clocky config files."""
def __init__(self,filename,verbose=0):
def __init__(self,filename="clocky.json",verbose=0):
"""Open existing Clocky config file and read config data. If no file can be found create a new one with default values."""
self.filename=filename
self.__verbose=verbose
@@ -20,13 +20,22 @@ class ClockyConfig:
else:
self.__data={}
self.__data["clocky"]={}
self.__data["clocky"]["CONFIGFILEVERSION"]=CONFIGFILEVERSION
self.__data["clocky"]["version"]=CONFIGFILEVERSION
self.__data["clocky"]["settings"]={}
self.__data["clocky"]["settings"]["alarmtime"]="9:00"
self.__data["clocky"]["settings"]["alarmtime"]="09:00"
self.__data["clocky"]["settings"]["alarmdisabled"]=0
self.__data["clocky"]["settings"]["maxalarmtime"]=30
self.__data["clocky"]["settings"]["snoozetime"]=9
self.__data["clocky"]["settings"]["showalarmtime"]=3
self.__data["clocky"]["settings"]["showtracktime"]=2
self.__data["clocky"]["settings"]["editdelaytime"]=3
self.__data["clocky"]["settings"]["edittimeout"]=5
self.__data["clocky"]["settings"]["timetomaxvolume"]=150
self.__data["clocky"]["settings"]["soundpreviewtime"]=5
self.__data["clocky"]["settings"]["defaultbrightness"]=0
self.__data["clocky"]["settings"]["playmode"]="random"
self.__data["clocky"]["tracks"]={}
@@ -45,8 +54,12 @@ class ClockyConfig:
with open(self.filename) as json_data_file:
self.__data = json.load(json_data_file)
if(self.__data["clocky"]["version"] != CONFIGFILEVERSION):
print ("ERROR: configfile \""+self.filename+"\" CONFIGFILEVERSION ("+self.__data["clocky"]["version"]+") does not match program CONFIGFILEVERSION ("+CONFIGFILEVERSION+")!")
print ("ERROR: configfile \""+self.filename+"\" version ("+self.__data["clocky"]["version"]+") does not match program version ("+CONFIGFILEVERSION+")!")
exit();
if(self.__data["clocky"]["settings"]["alarmtime"][1]==':'): # make sure time data is like "09:00", not "9:00"
self.__data["clocky"]["settings"]["alarmtime"]="0"+self.__data["clocky"]["settings"]["alarmtime"]
self.__readdata=copy.deepcopy(self.__data)
self.__timestamp=os.stat(self.filename).st_mtime
@@ -54,9 +67,13 @@ class ClockyConfig:
self.starttrack=self.__data["clocky"]["starttrack"]
self.tracklist=self.__data["clocky"]["tracklist"]
self.sequence=self.__data["clocky"]["tracksequence"]
def __Write(self):
"""Internal function to do the actual writing of data into file"""
if(self.__data["clocky"]["settings"]["alarmtime"][1]==':'): # make sure time data is like "09:00", not "9:00"
self.__data["clocky"]["settings"]["alarmtime"]="0"+self.__data["clocky"]["settings"]["alarmtime"]
with open(self.filename,"w") as json_data_file:
json.dump(self.__data,json_data_file)
self.__readdata=copy.deepcopy(self.__data)
@@ -72,7 +89,7 @@ class ClockyConfig:
"""Print current config data in human readable format"""
print("--ClockyConfig-------------")
print("filename =",self.filename)
print("CONFIGFILEVERSION =",self.__data["clocky"]["version"])
print("version =",self.__data["clocky"]["version"])
for x in self.__data["clocky"]["settings"].keys():
print("%-14s= %s" %(x,self.__data["clocky"]["settings"][x]))
print("...........................")
@@ -94,23 +111,28 @@ class ClockyConfig:
print(self.__data)
def CheckConfig(self):
"""Check if config file has changed or removed since reading, or if the settings have changed.
Re-write or Re-read when necessary"""
if not os.access(self.filename,os.R_OK):
self.__Write()
if(self.__verbose):
print("Re-write settings because of file removal!")
self.Print()
"""Check if config file has changed or removed since reading, or if the settings have changed.
Re-write or Re-read when necessary"""
if not os.access(self.filename,os.R_OK):
self.__Write()
if(self.__verbose):
print("Re-write settings because of config file removal!")
self.Print()
return True
elif(os.stat(self.filename).st_mtime != self.__timestamp):
self.__Read()
if(self.__verbose):
print("Re-read settings because of config file change!")
self.Print()
elif(os.stat(self.filename).st_mtime != self.__timestamp):
self.__Read()
if(self.__verbose):
print("Re-read settings because of config file change!")
self.Print()
return True
if(self.__data != self.__readdata):
self.__Write()
if(self.__verbose):
print("Re-write settings because of settings change!")
self.Print()
if(self.__data != self.__readdata):
self.__Write()
if(self.__verbose):
print("Re-write settings because of settings change!")
self.Print()
return True
return False