Ensure that only one thread accesses settings.xml

Avoids corruption of settings.xml
This commit is contained in:
croneter 2018-11-26 07:35:19 +01:00
parent 7d2f785a8d
commit 030c381f65

View file

@ -8,6 +8,7 @@ from logging import getLogger
from sqlite3 import connect, OperationalError
from datetime import datetime
from unicodedata import normalize
from threading import Lock
try:
import xml.etree.cElementTree as etree
import defusedxml.cElementTree as defused_etree # etree parse unsafe
@ -36,6 +37,10 @@ LOG = getLogger('PLEX.utils')
WINDOW = xbmcgui.Window(10000)
ADDON = xbmcaddon.Addon(id='plugin.video.plexkodiconnect')
# If several threads access the settings.xml file concurrently, it gets
# corrupted
SETTINGS_LOCK = Lock()
# Grab Plex id from '...plex_id=XXXX....'
REGEX_PLEX_ID = re.compile(r'''plex_id=(\d+)''')
# Return the numbers at the end of an url like '.../.../XXXX'
@ -122,13 +127,14 @@ def settings(setting, value=None):
setting and value can either be unicode or string
"""
# We need to instantiate every single time to read changed variables!
addon = xbmcaddon.Addon(id='plugin.video.plexkodiconnect')
if value is not None:
# Takes string or unicode by default!
addon.setSetting(try_encode(setting), try_encode(value))
else:
# Should return unicode by default, but just in case
return try_decode(addon.getSetting(setting))
with SETTINGS_LOCK:
addon = xbmcaddon.Addon(id='plugin.video.plexkodiconnect')
if value is not None:
# Takes string or unicode by default!
addon.setSetting(try_encode(setting), try_encode(value))
else:
# Should return unicode by default, but just in case
return try_decode(addon.getSetting(setting))
def lang(stringid):