xbmcbackup/resources/lib/guisettings.py

73 lines
2.5 KiB
Python
Raw Normal View History

2014-10-31 17:40:47 +01:00
import json
2019-11-22 21:53:20 +01:00
import xbmc, xbmcvfs
2019-08-27 21:55:22 +02:00
from . import utils as utils
from xml.dom import minidom
from xml.parsers.expat import ExpatError
2014-10-31 17:40:47 +01:00
class GuiSettingsManager:
doc = None
2019-11-25 22:33:34 +01:00
def __init__(self):
2019-11-25 22:19:57 +01:00
# first make a copy of the file
xbmcvfs.copy(xbmc.translatePath('special://home/userdata/guisettings.xml'), xbmc.translatePath("special://home/userdata/guisettings.xml.restored"))
2019-11-25 22:33:34 +01:00
2019-11-25 22:19:57 +01:00
# read in the copy
self._readFile(xbmc.translatePath('special://home/userdata/guisettings.xml.restored'))
2019-11-25 22:33:34 +01:00
2014-10-31 17:40:47 +01:00
def run(self):
2019-11-25 22:19:57 +01:00
# get a list of all the settings we can manipulate via json
2014-10-31 17:40:47 +01:00
json_response = json.loads(xbmc.executeJSONRPC('{"jsonrpc":"2.0", "id":1, "method":"Settings.GetSettings","params":{"level":"advanced"}}'))
2019-11-25 22:33:34 +01:00
2014-10-31 17:40:47 +01:00
settings = json_response['result']['settings']
currentSettings = {}
2019-11-25 22:33:34 +01:00
2014-10-31 17:40:47 +01:00
for aSetting in settings:
if('value' in aSetting):
currentSettings[aSetting['id']] = aSetting['value']
2019-11-25 22:33:34 +01:00
2019-11-25 22:19:57 +01:00
# parse the existing xml file and get all the settings we need to restore
restoreSettings = self.__parseNodes(self.doc.getElementsByTagName('setting'))
2019-11-25 22:33:34 +01:00
2019-11-25 22:19:57 +01:00
# get a list where the restore setting value != the current value
updateSettings = {k: v for k, v in list(restoreSettings.items()) if (k in currentSettings and currentSettings[k] != v)}
2019-11-25 22:33:34 +01:00
2019-11-25 22:19:57 +01:00
# go through all the found settings and update them
jsonObj = {"jsonrpc":"2.0","id":1,"method":"Settings.SetSettingValue","params":{"setting":"","value":""}}
for anId, aValue in list(updateSettings.items()):
utils.log("updating: " + anId + ", value: " + str(aValue))
2019-11-25 22:33:34 +01:00
jsonObj['params']['setting'] = anId
jsonObj['params']['value'] = aValue
2019-11-25 22:33:34 +01:00
xbmc.executeJSONRPC(json.dumps(jsonObj))
2019-11-25 22:33:34 +01:00
2014-10-31 17:40:47 +01:00
def __parseNodes(self,nodeList):
result = {}
2014-10-31 17:40:47 +01:00
for node in nodeList:
nodeValue = ''
if(node.firstChild != None):
nodeValue = node.firstChild.nodeValue
2019-11-25 22:33:34 +01:00
2019-11-25 22:19:57 +01:00
# check for numbers and booleans
if(nodeValue.isdigit()):
nodeValue = int(nodeValue)
elif(nodeValue == 'true'):
nodeValue = True
elif(nodeValue == 'false'):
nodeValue = False
2019-11-25 22:33:34 +01:00
result[node.getAttribute('id')] = nodeValue
2014-10-31 17:40:47 +01:00
return result
2019-11-25 22:33:34 +01:00
2014-10-31 17:40:47 +01:00
def _readFile(self,fileLoc):
2019-11-25 22:33:34 +01:00
2014-10-31 17:40:47 +01:00
if(xbmcvfs.exists(fileLoc)):
try:
self.doc = minidom.parse(fileLoc)
except ExpatError:
utils.log("Can't read " + fileLoc)
2019-11-25 22:33:34 +01:00