From 2ed7e22d3a9802545edc43b3525aca1d60f01043 Mon Sep 17 00:00:00 2001 From: Mathieu Virbel Date: Tue, 25 Sep 2012 18:07:58 +0200 Subject: [PATCH] config: add callbacks for specific section/token. Use it for logger. closes #660 --- kivy/config.py | 29 +++++++++++++++++++++++++++-- kivy/logger.py | 5 +++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/kivy/config.py b/kivy/config.py index 0bba7bbd3..7be5a88e8 100644 --- a/kivy/config.py +++ b/kivy/config.py @@ -170,7 +170,7 @@ from sys import platform from os import environ from os.path import exists from kivy import kivy_config_fn -from kivy.logger import Logger +from kivy.logger import Logger, logger_config_update from kivy.utils import OrderedDict # Version number of current configuration format @@ -191,6 +191,28 @@ class ConfigParser(PythonConfigParser): PythonConfigParser.__init__(self) self._sections = OrderedDict() self.filename = None + self._callbacks = [] + + def add_callback(self, callback, section=None, key=None): + '''Add a callback to be called when a specific section/key changed. If + you don't specify a section or a key, it will call the callback for all + section/keys. + + Callbacks will receive 3 arguments: the section, key and value. + + .. versionadded:: 1.4.1 + ''' + if section is None and key is not None: + raise Exception('You cannot specify a key without a section') + self._callbacks.append((callback, section, key)) + + def _do_callbacks(self, section, key, value): + for callback, csection, ckey in self._callbacks: + if csection is not None and csection != section: + continue + elif ckey is not None and ckey != key: + continue + callback(section, key, value) def read(self, filename): '''Read only one filename. In contrast to the original ConfigParser of @@ -206,7 +228,9 @@ class ConfigParser(PythonConfigParser): '''Functions similarly to PythonConfigParser's set method, except that the value is implicitly converted to a string. ''' - return PythonConfigParser.set(self, section, option, str(value)) + ret = PythonConfigParser.set(self, section, option, str(value)) + self._do_callbacks(section, option, str(value)) + return ret def setdefaults(self, section, keyvalues): '''Set a lot of keys/values in one section at the same time @@ -264,6 +288,7 @@ if not environ.get('KIVY_DOC_INCLUDE'): # Create default configuration Config = ConfigParser() + Config.add_callback(logger_config_update, 'kivy', 'log_level') # Read config file if exist if exists(kivy_config_fn) and \ diff --git a/kivy/logger.py b/kivy/logger.py index 2342c0435..232a5f36d 100644 --- a/kivy/logger.py +++ b/kivy/logger.py @@ -272,6 +272,11 @@ class LogFile(object): def flush(self): return +def logger_config_update(section, key, value): + if LOG_LEVELS.get(value) is None: + raise AttributeError('Loglevel {0!r} doesn\'t exists'.format(value)) + Logger.setLevel(level=LOG_LEVELS.get(value)) + #: Kivy default logger instance Logger = logging.getLogger('kivy') Logger.logfile_activated = False