[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
This commit is contained in:
tobi-wan-kenobi 2020-05-02 13:43:17 +02:00
parent 36faceef68
commit d0bb0f9b7a

View file

@ -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]