2023-07-22, 18:47
Hi there!
It seems that I cannot make xbmcaddon.Settings.setBool to work.
I've created a SettingsManager wrapper class that accepts the Settings class in its constructor (in order to be able to swap it during unit tests).
This is a Minimum reproducible example of what I'm trying to achieve, basically setting the "firstRun" value to False after the first start.
But if I restart Kodi, the setting is still set to True.
What am I doing wrong?
Also, the documentation for setXxx methods says that they return True if the settings are correctly sets and false otherwise, but my logs shows that the outcome is None, so I suppose that the documentation isn't right, is it?
(logger is another wrapper class I wrote that uses xbmc.log)
These are the settings (still have to upgrade to the new syntax):
It seems that I cannot make xbmcaddon.Settings.setBool to work.
I've created a SettingsManager wrapper class that accepts the Settings class in its constructor (in order to be able to swap it during unit tests).
This is a Minimum reproducible example of what I'm trying to achieve, basically setting the "firstRun" value to False after the first start.
But if I restart Kodi, the setting is still set to True.
What am I doing wrong?
Also, the documentation for setXxx methods says that they return True if the settings are correctly sets and false otherwise, but my logs shows that the outcome is None, so I suppose that the documentation isn't right, is it?
python:from typing import TYPE_CHECKING, Any
from resources.lib.logger import Logger
if TYPE_CHECKING:
import xbmcaddon
class SettingsManager:
"""Class which contains all addon settings."""
def __init__(self, settings: xbmcaddon.Settings, logger: Logger) -> None:
self._logger = logger
self._settings = settings
# Here I just declare the attributes and set the types, the value are not important
self.debug = True
self.first_run = True
# Here I read the actual settings
self.read_settings()
def read_settings(self) -> None:
"""Read all settings"""
settings = self._settings
get_bool = settings.getBool
self.debug = get_bool('debug')
self.first_run = get_bool('firstRun')
if self.debug:
log = self._logger.log
log('Current settings:')
log(f'Debug enabled: {self.debug}')
log(f'first run: {self.first_run}')
def set_first_run_done(self) -> None:
self._set_bool('firstRun', False)
def _set_bool(self, name: str, value: bool) -> None:
outcome = self._settings.setBool(name, value)
self._log_set_outcome(name, value, outcome)
def _log_set_outcome(self, name: str, value: Any, outcome: bool) -> None:
if not outcome:
self._logger.error(f"Error setting {name} to {value} (outcome={outcome})")
elif self.debug:
self._logger.log(f"Set {name} to {value}")
def main():
addon = xbmcaddon.Addon()
logger = Logger(addon.getAddonInfo('name'))
settings_manager = SettingsManager(addon.getSettings(), logger)
settings_manager.set_first_run_done()
if __name__ == '__main__':
main()
(logger is another wrapper class I wrote that uses xbmc.log)
These are the settings (still have to upgrade to the new syntax):
xml:<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<settings>
<category label="32000">
<setting id="debug" type="bool" label="32006" default="false" />
<setting id="firstRun" type="bool" label="32000" default="true" visible="false" />
</category>
</settings>