From a4bb3f3febae3cfb5de922ac28a183d0045ac809 Mon Sep 17 00:00:00 2001 From: Rob Weber Date: Wed, 11 Sep 2019 10:01:49 -0500 Subject: [PATCH] completely changed this class, default no longer matters. Just restore settings that differ from current, ignore default flags closes #154 --- resources/lib/backup.py | 2 +- resources/lib/guisettings.py | 76 +++++++++++++++++------------------- 2 files changed, 36 insertions(+), 42 deletions(-) diff --git a/resources/lib/backup.py b/resources/lib/backup.py index a1408dd..a2d190b 100644 --- a/resources/lib/backup.py +++ b/resources/lib/backup.py @@ -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 diff --git a/resources/lib/guisettings.py b/resources/lib/guisettings.py index b4e4c12..09aa8e1 100644 --- a/resources/lib/guisettings.py +++ b/resources/lib/guisettings.py @@ -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__() -