[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:
parent
36faceef68
commit
d0bb0f9b7a
1 changed files with 17 additions and 0 deletions
|
@ -3,6 +3,9 @@ try:
|
||||||
import ast
|
import ast
|
||||||
except ImportError:
|
except ImportError:
|
||||||
log.warning('--list modules will not work (module "ast" not found)')
|
log.warning('--list modules will not work (module "ast" not found)')
|
||||||
|
|
||||||
|
from configparser import RawConfigParser
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import glob
|
import glob
|
||||||
import textwrap
|
import textwrap
|
||||||
|
@ -118,6 +121,10 @@ class Config(util.store.Store):
|
||||||
|
|
||||||
self.__args = parser.parse_args(args)
|
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 ]
|
parameters = [ item for sub in self.__args.parameters for item in sub ]
|
||||||
for param in parameters:
|
for param in parameters:
|
||||||
if not '=' in param:
|
if not '=' in param:
|
||||||
|
@ -126,6 +133,16 @@ class Config(util.store.Store):
|
||||||
key, value = param.split('=', 1)
|
key, value = param.split('=', 1)
|
||||||
self.set(key, value)
|
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):
|
def modules(self):
|
||||||
return [item for sub in self.__args.modules for item in sub]
|
return [item for sub in self.__args.modules for item in sub]
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue