completely changed this class, default no longer matters. Just restore settings that differ from current, ignore default flags closes #154

This commit is contained in:
Rob Weber 2019-09-11 10:01:49 -05:00
parent 1f6324b2d5
commit a4bb3f3feb
2 changed files with 36 additions and 42 deletions

View File

@ -323,7 +323,7 @@ class XbmcBackup:
#update the guisettings information (or what we can from it)
gui_settings = GuiSettingsManager('special://home/userdata/guisettings.xml')
gui_settings = GuiSettingsManager()
gui_settings.run()
#call update addons to refresh everything

View File

@ -6,49 +6,59 @@ import xbmc,xbmcvfs
class GuiSettingsManager:
settingsFile = None
doc = None
def __init__(self,settingsFile):
self._readFile(xbmc.translatePath(settingsFile))
def __init__(self):
#first make a copy of the file
xbmcvfs.copy(xbmc.translatePath('special://home/userdata/guisettings.xml'), xbmc.translatePath("special://home/userdata/guisettings.xml.restored"))
#read in the copy
self._readFile(xbmc.translatePath('special://home/userdata/guisettings.xml.restored'))
def run(self):
#get a list of all the settings we can manipulate via json
json_response = json.loads(xbmc.executeJSONRPC('{"jsonrpc":"2.0", "id":1, "method":"Settings.GetSettings","params":{"level":"advanced"}}'))
settings = json_response['result']['settings']
settingsAllowed = []
currentSettings = {}
for aSetting in settings:
settingsAllowed.append(aSetting['id'])
if('value' in aSetting):
currentSettings[aSetting['id']] = aSetting['value']
#parse the existing xml file and get all the settings we need to update
updateSettings = self.__parseNodes(self.doc.getElementsByTagName('setting'))
#parse the existing xml file and get all the settings we need to restore
restoreSettings = self.__parseNodes(self.doc.getElementsByTagName('setting'))
#get a list where the restore setting value != the current value
updateSettings = {k: v for k, v in restoreSettings.items() if (k in currentSettings and currentSettings[k] != v)}
#go through all the found settings and update them
for aSetting in updateSettings:
if(aSetting.name in settingsAllowed):
utils.log("updating: " + aSetting.name + ", value: " + aSetting.value)
jsonObj = {"jsonrpc":"2.0","id":1,"method":"Settings.SetSettingValue","params":{"setting":"","value":""}}
for anId, aValue in updateSettings.items():
utils.log("updating: " + anId + ", value: " + str(aValue))
jsonObj['params']['setting'] = anId
jsonObj['params']['value'] = aValue
#check for boolean and numeric values
if(aSetting.value.isdigit() or (aSetting.value == 'true' or aSetting.value == 'false')):
xbmc.executeJSONRPC('{"jsonrpc":"2.0", "id":1, "method":"Settings.SetSettingValue","params":{"setting":"' + aSetting.name + '","value":' + aSetting.value + '}}')
else:
xbmc.executeJSONRPC('{"jsonrpc":"2.0", "id":1, "method":"Settings.SetSettingValue","params":{"setting":"' + aSetting.name + '","value":"' + utils.encode(aSetting.value) + '"}}')
#make a copy of the guisettings file to make user based restores easier
xbmcvfs.copy(self.settingsFile, xbmc.translatePath("special://home/userdata/guisettings.xml.restored"))
xbmc.executeJSONRPC(json.dumps(jsonObj))
def __parseNodes(self,nodeList):
result = []
result = {}
for node in nodeList:
#only add if it's not a default setting
if('default' not in node.attributes.keys()):
aSetting = SettingNode(node.getAttribute('id'),node.firstChild.nodeValue)
aSetting.isDefault = False
result.append(aSetting)
nodeValue = ''
if(node.firstChild != None):
nodeValue = node.firstChild.nodeValue
#check for numbers and booleans
if(nodeValue.isdigit()):
nodeValue = int(nodeValue)
elif(nodeValue == 'true'):
nodeValue = True
elif(nodeValue == 'false'):
nodeValue = False
result[node.getAttribute('id')] = nodeValue
return result
@ -57,23 +67,7 @@ class GuiSettingsManager:
if(xbmcvfs.exists(fileLoc)):
try:
self.doc = minidom.parse(fileLoc)
self.settingsFile = fileLoc
except ExpatError:
utils.log("Can't read " + fileLoc)
class SettingNode:
name = ''
value = ''
isDefault = True
def __init__(self,name,value):
self.name = name
self.value= value
def __str__(self):
return "%s : %s" % (self.name,self.value)
def __repr__(self):
return self.__str__()