def set_setting(self, setting_id, value): if isinstance(value, bool): value_string = unicode(value).lower() else: value_string = (unicode(value) if value is not None and not value == "" else self.EMPTY_PERSISTED_SETTING_VALUE) cache_value = self._SETTINGS_CACHE.get_setting(setting_id) if cache_value is not None and cache_value == value_string: return else: try: with self._settings_lock(): settings_list = self._get_settings_list_set() settings_list.add(setting_id) self._store_setting_list_set(settings_list) self._SETTINGS_CACHE.set_setting(setting_id, value_string) kodi_addon = xbmcaddon.Addon() if not kodi_addon.getSetting(setting_id) == ( value_string if not value_string == self.EMPTY_PERSISTED_SETTING_VALUE else ""): kodi_addon.setSetting( setting_id, value_string if not value_string == self.EMPTY_PERSISTED_SETTING_VALUE else "") self._set_settings_persisted_flag() del kodi_addon except self.KodiShutdown: return
def get_setting(self, setting_id, default_value=None): """ Get a setting value :param setting_id: The name of the setting :type setting_id: str|unicode :param default_value: :type default_value: str|unicode :return: The value of the setting as a string If the setting is not stored, the optional default_value if provided or None :rtype: str|unicode """ value = self._SETTINGS_CACHE.get_setting(setting_id) if value == self.EMPTY_PERSISTED_SETTING_VALUE: return unicode(default_value) if default_value else None if value is None or value == "": try: with self._settings_lock(): kodi_addon = xbmcaddon.Addon() value = kodi_addon.getSetting(setting_id) del kodi_addon if value is None or value == "": value = (unicode(default_value) if default_value is not None else self.EMPTY_PERSISTED_SETTING_VALUE) settings_list = self._get_settings_list_set() settings_list.add(setting_id) self._store_setting_list_set(settings_list) self._SETTINGS_CACHE.set_setting(setting_id, value) except self.KodiShutdown: return return value if not value == self.EMPTY_PERSISTED_SETTING_VALUE else None
def get_bool_setting(self, setting_id, default_value=None): """ Get a setting as a bool value :param setting_id: The name of the setting :type setting_id: str|unicode :param default_value: An optional default value to provide if the setting is not stored :type default_value: bool :return: The value of the setting. If the setting is not stored, the optional default_value if provided or False :rtype: bool """ value = self.get_setting(setting_id, default_value) if isinstance(value, bool): return value if value is not None and unicode(value).lower() in [ "true", "false", "1", "0" ]: return unicode(value).lower() in ["true", "1"] else: if default_value is not None: return default_value else: return False