From 2cb52beba5b868051b4dbd2020f943ed8ad85cfc Mon Sep 17 00:00:00 2001 From: Romain Bignon Date: Thu, 1 Apr 2010 20:01:31 +0200 Subject: [PATCH] new frontend configuration system --- weboob/frontends/http/scripts/http | 2 +- weboob/frontends/monboob/scripts/monboob | 12 ++++++------ weboob/tools/application.py | 14 ++++++-------- weboob/tools/config/__init__.py | 0 weboob/{ => tools/config}/iconfig.py | 14 +------------- weboob/tools/{config.py => config/yaml.py} | 21 +++++---------------- 6 files changed, 19 insertions(+), 44 deletions(-) create mode 100644 weboob/tools/config/__init__.py rename weboob/{ => tools/config}/iconfig.py (76%) rename weboob/tools/{config.py => config/yaml.py} (78%) diff --git a/weboob/frontends/http/scripts/http b/weboob/frontends/http/scripts/http index f135b1e5fa..db524b0901 100755 --- a/weboob/frontends/http/scripts/http +++ b/weboob/frontends/http/scripts/http @@ -35,7 +35,7 @@ class Application(BaseApplication): APPNAME = 'http' def main(self, argv): - self.weboob.load_modules() + self.weboob.load_backends() template_lookup = TemplateLookup(directories=['%s/../templates' % os.path.dirname(__file__)], output_encoding='utf-8', encoding_errors='replace') diff --git a/weboob/frontends/monboob/scripts/monboob b/weboob/frontends/monboob/scripts/monboob index a7c3a318d1..985232773a 100755 --- a/weboob/frontends/monboob/scripts/monboob +++ b/weboob/frontends/monboob/scripts/monboob @@ -40,14 +40,14 @@ class Monboob(BaseApplication): def main(self, argv): self.load_config() - if not self.myconfig: + if not self.config: print >>sys.stderr, "Error: %s is not configured yet. Please call 'monboob -c'" % argv[0] print >>sys.stderr, "Also, you need to use 'weboobcfg' to set backend configs" return -1 - self.weboob.load_modules(ICapMessages, backends=self.config.getbackends()) + self.weboob.load_modules(ICapMessages) - self.weboob.schedule(self.myconfig['interval'], self.process) + self.weboob.schedule(self.config.get('interval'), self.process) self.weboob.loop() def process(self): @@ -56,8 +56,8 @@ class Monboob(BaseApplication): self.send_email(name, message) def send_email(self, backend_name, mail): - domain = self.myconfig['domain'] - recipient = self.myconfig['recipient'] + domain = self.config.get('domain') + recipient = self.config.get('recipient') reply_id = '' if mail.get_reply_id(): @@ -111,7 +111,7 @@ class Monboob(BaseApplication): msg['In-Reply-To'] = reply_id # Send the message via SMTP to localhost:25 - smtp = SMTP(self.myconfig['smtp']) + smtp = SMTP(self.config.get('smtp')) smtp.sendmail(sender, recipient, msg.as_string()) smtp.quit() diff --git a/weboob/tools/application.py b/weboob/tools/application.py index 137c05e510..ec07398c37 100644 --- a/weboob/tools/application.py +++ b/weboob/tools/application.py @@ -28,7 +28,7 @@ class BaseApplication(object): APPNAME = '' CONFIG = {} - CONFDIR = '%s/.weboob/' % os.path.expanduser("~") + CONFDIR = os.path.join(os.path.expanduser('~'), '.weboob') def __init__(self): self.weboob = Weboob(self.APPNAME) @@ -45,18 +45,16 @@ def load_config(self, path=None, klass=None): if klass is None: # load Config only here because some applications don't want # to depend on yaml and do not use this function - from weboob.tools.config import Config - klass = Config + from weboob.tools.config.yaml import YamlConfig + klass = YamlConfig if path is None: - path = self.CONFDIR + 'weboobrc' + path = os.path.join(self.CONFDIR, self.APPNAME) elif not path.startswith('/'): - path = self.CONFDIR + path + path = os.path.join(self.CONFDIR, path) self.config = klass(path) - self.config.load() - self.myconfig = self.CONFIG - self.myconfig.update(self.config.getfrontend(self.APPNAME)) + self.config.load(self.CONFIG) def main(self, argv): """ Main function """ diff --git a/weboob/tools/config/__init__.py b/weboob/tools/config/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/weboob/iconfig.py b/weboob/tools/config/iconfig.py similarity index 76% rename from weboob/iconfig.py rename to weboob/tools/config/iconfig.py index 5d4522c498..f0f5bfe293 100644 --- a/weboob/iconfig.py +++ b/weboob/tools/config/iconfig.py @@ -20,14 +20,8 @@ class ConfigError(Exception): pass -class BackendConfig(object): - def __init__(self, name, _type, config): - self.name = name - self.type = _type - self.config = config - class IConfig: - def load(self): + def load(self, default={}): raise NotImplementedError() def save(self): @@ -38,9 +32,3 @@ def set(self, *args): def get(self, *args, **kwargs): raise NotImplementedError() - - def getfrontend(self, name): - raise NotImplementedError() - - def getbackends(self): - raise NotImplementedError() diff --git a/weboob/tools/config.py b/weboob/tools/config/yaml.py similarity index 78% rename from weboob/tools/config.py rename to weboob/tools/config/yaml.py index f504184a59..8b70f4eee6 100644 --- a/weboob/tools/config.py +++ b/weboob/tools/config/yaml.py @@ -21,16 +21,17 @@ from __future__ import with_statement import yaml -from logging import warning -from weboob.iconfig import IConfig, ConfigError, BackendConfig +from .iconfig import IConfig, ConfigError -class Config(IConfig): +class YamlConfig(IConfig): def __init__(self, path): self.path = path self.values = {} - def load(self): + def load(self, default={}): + self.values = default.copy() + try: with open(self.path, 'r') as f: self.values = yaml.load(f) @@ -82,15 +83,3 @@ def set(self, *args): raise ConfigError() v[args[-2]] = args[-1] - - def getfrontend(self, name): - return self.get('frontends', name, default={}) - - def getbackends(self): - d = {} - for key, value in self.get('backends', default={}).iteritems(): - if not 'type' in value: - warning("Missing 'type' item in config of '%s' backend" % key) - else: - d[key] = BackendConfig(key, value['type'], value.get('config', {})) - return d -- GitLab