mirror of
https://github.com/robweber/xbmcbackup.git
synced 2025-11-23 11:41:30 +01:00
ui settings restore upgrade
* added ability to export/save settings as json using GetSettings * added generic copyFile method instead of duplicating * copy and load settings file after file restore (right now only reads) * set settings values from backup when differ than current * store settings as part of validation file * prompt for settings restore or set always via toggle * unused import * added new strings for settings restore * updated changelog * fix pep8 syntax * swap setting to always prompt instead of always restore (invert)
This commit is contained in:
@@ -1,72 +1,47 @@
|
||||
import json
|
||||
import xbmc
|
||||
import xbmcvfs
|
||||
from . import utils as utils
|
||||
from xml.dom import minidom
|
||||
from xml.parsers.expat import ExpatError
|
||||
|
||||
|
||||
class GuiSettingsManager:
|
||||
doc = None
|
||||
filename = 'kodi_settings.json'
|
||||
systemSettings = None
|
||||
|
||||
def __init__(self):
|
||||
# first make a copy of the file
|
||||
xbmcvfs.copy(xbmcvfs.translatePath('special://home/userdata/guisettings.xml'), xbmcvfs.translatePath("special://home/userdata/guisettings.xml.restored"))
|
||||
# get all of the current Kodi settings
|
||||
json_response = json.loads(xbmc.executeJSONRPC('{"jsonrpc":"2.0", "id":1, "method":"Settings.GetSettings","params":{"level":"expert"}}'))
|
||||
|
||||
# read in the copy
|
||||
self._readFile(xbmcvfs.translatePath('special://home/userdata/guisettings.xml.restored'))
|
||||
self.systemSettings = json_response['result']['settings']
|
||||
|
||||
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"}}'))
|
||||
def backup(self):
|
||||
utils.log('Backing up Kodi settings')
|
||||
|
||||
settings = json_response['result']['settings']
|
||||
currentSettings = {}
|
||||
# return all current settings
|
||||
return self.systemSettings
|
||||
|
||||
for aSetting in settings:
|
||||
if('value' in aSetting):
|
||||
currentSettings[aSetting['id']] = aSetting['value']
|
||||
def restore(self, restoreSettings):
|
||||
utils.log('Restoring Kodi settings')
|
||||
|
||||
# parse the existing xml file and get all the settings we need to restore
|
||||
restoreSettings = self.__parseNodes(self.doc.getElementsByTagName('setting'))
|
||||
updateJson = {"jsonrpc": "2.0", "id": 1, "method": "Settings.SetSettingValue", "params": {"setting": "", "value": ""}}
|
||||
|
||||
# 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)}
|
||||
# create a setting=value dict of the current settings
|
||||
settingsDict = {}
|
||||
for aSetting in self.systemSettings:
|
||||
# ignore action types, no value
|
||||
if(aSetting['type'] != 'action'):
|
||||
settingsDict[aSetting['id']] = aSetting['value']
|
||||
|
||||
# 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))
|
||||
restoreCount = 0
|
||||
for aSetting in restoreSettings:
|
||||
# only update a setting if its different than the current (action types have no value)
|
||||
if(aSetting['type'] != 'action' and settingsDict[aSetting['id']] != aSetting['value']):
|
||||
if(utils.getSettingBool('verbose_logging')):
|
||||
utils.log('%s different than current: %s' % (aSetting['id'], str(aSetting['value'])))
|
||||
|
||||
jsonObj['params']['setting'] = anId
|
||||
jsonObj['params']['value'] = aValue
|
||||
updateJson['params']['setting'] = aSetting['id']
|
||||
updateJson['params']['value'] = aSetting['value']
|
||||
|
||||
xbmc.executeJSONRPC(json.dumps(jsonObj))
|
||||
xbmc.executeJSONRPC(json.dumps(updateJson))
|
||||
restoreCount = restoreCount + 1
|
||||
|
||||
def __parseNodes(self, nodeList):
|
||||
result = {}
|
||||
|
||||
for node in nodeList:
|
||||
nodeValue = ''
|
||||
if(node.firstChild is not 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
|
||||
|
||||
def _readFile(self, fileLoc):
|
||||
|
||||
if(xbmcvfs.exists(fileLoc)):
|
||||
try:
|
||||
self.doc = minidom.parse(fileLoc)
|
||||
except ExpatError:
|
||||
utils.log("Can't read " + fileLoc)
|
||||
utils.log('Update %d settings' % restoreCount)
|
||||
|
||||
Reference in New Issue
Block a user