From d0bb0f9b7a1efb8995f20d4abcbb5d1206b90bfe Mon Sep 17 00:00:00 2001 From: tobi-wan-kenobi Date: Sat, 2 May 2020 13:43:17 +0200 Subject: [PATCH] [core/config] Add support for configuration files Load configuration files from a number of places and, if they exist, use the settings there as baseline settings that can be overwritten by more specific source (the modules themselves and the CLI `--parameter` option). TODO: document this --- core/config.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/core/config.py b/core/config.py index e236d5e..e78cf3e 100644 --- a/core/config.py +++ b/core/config.py @@ -3,6 +3,9 @@ try: import ast except ImportError: log.warning('--list modules will not work (module "ast" not found)') + +from configparser import RawConfigParser + import sys import glob import textwrap @@ -118,6 +121,10 @@ class Config(util.store.Store): self.__args = parser.parse_args(args) + for cfg in [ '~/.bumblebee-status.conf', '~/.config/bumblebee-status.conf', '~/.config/bumblebee-status/config' ]: + cfg = os.path.expanduser(cfg) + self.load_config(cfg) + parameters = [ item for sub in self.__args.parameters for item in sub ] for param in parameters: if not '=' in param: @@ -126,6 +133,16 @@ class Config(util.store.Store): key, value = param.split('=', 1) self.set(key, value) + def load_config(self, filename): + if os.path.exists(filename): + log.info('loading {}'.format(filename)) + tmp = RawConfigParser() + tmp.read(filename) + + if tmp.has_section('module-parameters'): + for key, value in tmp.items('module-parameters'): + self.set(key, value) + def modules(self): return [item for sub in self.__args.modules for item in sub]