Smarter function to set advancedsettings.xml values

This commit is contained in:
tomkat83 2017-05-06 09:35:05 +02:00
parent 7f083272c0
commit ee85db4570
3 changed files with 63 additions and 57 deletions

View file

@ -7,7 +7,7 @@ import xbmc
import xbmcgui import xbmcgui
from utils import settings, window, language as lang, tryEncode, \ from utils import settings, window, language as lang, tryEncode, \
get_advancessettings_xml_setting advancessettings_xml
import downloadutils import downloadutils
from userclient import UserClient from userclient import UserClient
@ -401,7 +401,7 @@ class InitialSetup():
dialog = self.dialog dialog = self.dialog
# Get current Kodi video cache setting # Get current Kodi video cache setting
cache = get_advancessettings_xml_setting(['cache', 'memorysize']) cache = advancessettings_xml(['cache', 'memorysize'])
if cache is not None: if cache is not None:
cache = str(cache.text) cache = str(cache.text)
else: else:
@ -478,8 +478,8 @@ class InitialSetup():
log.debug("User opted to disable Plex music library.") log.debug("User opted to disable Plex music library.")
settings('enableMusic', value="false") settings('enableMusic', value="false")
else: else:
from utils import advancedSettingsXML from utils import advancedsettings_tweaks
advancedSettingsXML() advancedsettings_tweaks()
# Download additional art from FanArtTV # Download additional art from FanArtTV
if dialog.yesno(heading=lang(29999), line1=lang(39061)): if dialog.yesno(heading=lang(29999), line1=lang(39061)):

View file

@ -12,7 +12,7 @@ import xbmcgui
from utils import window, settings, getUnixTimestamp, sourcesXML,\ from utils import window, settings, getUnixTimestamp, sourcesXML,\
ThreadMethods, ThreadMethodsAdditionalStop, LogTime, getScreensaver,\ ThreadMethods, ThreadMethodsAdditionalStop, LogTime, getScreensaver,\
setScreensaver, playlistXSP, language as lang, DateToKodi, reset,\ setScreensaver, playlistXSP, language as lang, DateToKodi, reset,\
advancedSettingsXML, tryDecode, deletePlaylists, deleteNodes, \ advancedsettings_tweaks, tryDecode, deletePlaylists, deleteNodes, \
ThreadMethodsAdditionalSuspend, create_actor_db_index, dialog ThreadMethodsAdditionalSuspend, create_actor_db_index, dialog
import downloadutils import downloadutils
import itemtypes import itemtypes
@ -1461,7 +1461,7 @@ class LibrarySync(Thread):
self.initializeDBs() self.initializeDBs()
if self.enableMusic: if self.enableMusic:
advancedSettingsXML() advancedsettings_tweaks()
if settings('FanartTV') == 'true': if settings('FanartTV') == 'true':
self.fanartthread.start() self.fanartthread.start()

View file

@ -451,21 +451,24 @@ def normalize_string(text):
return text return text
def indent(elem, level=0): def indent(elem, level=0):
# Prettify xml trees """
Prettifies xml trees. Pass the etree root in
"""
i = "\n" + level*" " i = "\n" + level*" "
if len(elem): if len(elem):
if not elem.text or not elem.text.strip(): if not elem.text or not elem.text.strip():
elem.text = i + " " elem.text = i + " "
if not elem.tail or not elem.tail.strip(): if not elem.tail or not elem.tail.strip():
elem.tail = i elem.tail = i
for elem in elem: for elem in elem:
indent(elem, level+1) indent(elem, level+1)
if not elem.tail or not elem.tail.strip(): if not elem.tail or not elem.tail.strip():
elem.tail = i elem.tail = i
else: else:
if level and (not elem.tail or not elem.tail.strip()): if level and (not elem.tail or not elem.tail.strip()):
elem.tail = i elem.tail = i
def guisettingsXML(): def guisettingsXML():
@ -519,7 +522,7 @@ def __setSubElement(element, subelement):
return answ return answ
def get_advancessettings_xml_setting(node_list): def advancessettings_xml(node_list, new_value=None, attrib=None):
""" """
Returns the etree element for nodelist (if it exists) and None if not set Returns the etree element for nodelist (if it exists) and None if not set
@ -529,7 +532,7 @@ def get_advancessettings_xml_setting(node_list):
<busydialogdelayms>750</busydialogdelayms> <busydialogdelayms>750</busydialogdelayms>
Example xml: for the following example xml:
<?xml version="1.0" encoding="UTF-8" ?> <?xml version="1.0" encoding="UTF-8" ?>
<advancedsettings> <advancedsettings>
@ -537,60 +540,63 @@ def get_advancessettings_xml_setting(node_list):
<busydialogdelayms>750</busydialogdelayms> <busydialogdelayms>750</busydialogdelayms>
</video> </video>
</advancedsettings> </advancedsettings>
If new_value is set, '750' will be replaced accordingly, returning the new
etree Element. Advancedsettings might be generated if it did not exist
already
If the dict attrib is set, the Element's attributs will be appended
accordingly
""" """
path = tryDecode(xbmc.translatePath("special://profile/")) path = '%sadvancedsettings.xml' % xbmc.translatePath("special://profile/")
try: try:
xmlparse = etree.parse("%sadvancedsettings.xml" % path) xml = etree.parse(path)
except: except IOError:
log.debug('Could not parse advancedsettings.xml, returning None') # Document is blank or missing
return if new_value is None and attrib is None:
root = xmlparse.getroot() log.debug('Could not parse advancedsettings.xml, returning None')
return
# Create topmost xml entry
root = etree.Element('advancedsettings')
else:
root = xml.getroot()
element = root
# Reading values
if new_value is None and attrib is None:
for node in node_list:
element = element.find(node)
if element is None:
break
return element
# Setting new values. Get correct element first
for node in node_list: for node in node_list:
root = root.find(node) element = __setSubElement(element, node)
if root is None: # Write new values
break element.text = new_value or ''
return root if attrib is not None:
for key, attribute in attrib.iteritems():
element.set(key, attribute)
# Indent and make readable
indent(root)
# Safe the changed xml
try:
xml.write(path)
except NameError:
etree.ElementTree(root).write(path)
return element
def advancedSettingsXML(): def advancedsettings_tweaks():
""" """
Kodi tweaks Kodi tweaks
Changes advancedsettings.xml, musiclibrary: Changes advancedsettings.xml, musiclibrary:
backgroundupdate set to "true" backgroundupdate set to "true"
Overrides guisettings.xml in Kodi userdata folder:
updateonstartup : set to "false"
usetags : set to "false"
findremotethumbs : set to "false"
""" """
path = tryDecode(xbmc.translatePath("special://profile/")) advancessettings_xml(['musiclibrary', 'backgroundupdate'],
xmlpath = "%sadvancedsettings.xml" % path new_value='true')
try:
xmlparse = etree.parse(xmlpath)
except:
# Document is blank or missing
root = etree.Element('advancedsettings')
else:
root = xmlparse.getroot()
music = __setSubElement(root, 'musiclibrary')
__setXMLTag(music, 'backgroundupdate', "true")
# __setXMLTag(music, 'updateonstartup', "false")
# Subtag 'musicfiles'
# music = __setSubElement(root, 'musicfiles')
# __setXMLTag(music, 'usetags', "false")
# __setXMLTag(music, 'findremotethumbs', "false")
# Prettify and write to file
try:
indent(root)
except:
pass
etree.ElementTree(root).write(xmlpath)
def sourcesXML(): def sourcesXML():
@ -851,7 +857,7 @@ def LogTime(func):
result = func(*args, **kwargs) result = func(*args, **kwargs)
elapsedtotal = datetime.now() - starttotal elapsedtotal = datetime.now() - starttotal
log.info('It took %s to run the function %s' log.info('It took %s to run the function %s'
% (elapsedtotal, func.__name__)) % (elapsedtotal, func.__name__))
return result return result
return wrapper return wrapper