diff --git a/weboob/frontends/dummy/scripts/dummy b/weboob/frontends/dummy/scripts/dummy index 8e0af547203476b413b77f71176efb7ea4fb782f..486747bab02227c0a98d2e20d77f71b8c89b0714 100755 --- a/weboob/frontends/dummy/scripts/dummy +++ b/weboob/frontends/dummy/scripts/dummy @@ -20,8 +20,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. """ -import sys - from weboob import Weboob from weboob.capabilities.messages import ICapMessages, ICapMessagesReply from weboob.tools.application import BaseApplication @@ -30,7 +28,9 @@ class Application(BaseApplication): APPNAME = 'dummy' def main(self, argv): - self.weboob.load_modules() + self.load_config() + self.weboob.load_modules(backends=self.config.getbackends()) + for name, backend in self.weboob.iter_backends(): print '= Processing backend name = %s' % name if backend.has_caps(ICapMessages): @@ -41,5 +41,4 @@ class Application(BaseApplication): print '== Backend is ICapMessagesReply => TODO' if __name__ == '__main__': - app = Application() - sys.exit(app.main(sys.argv)) + Application.run() diff --git a/weboob/frontends/mail/scripts/mail b/weboob/frontends/mail/scripts/mail index 330a2342b2519b330adfd6dd20dff6cc7559feef..e465ee55d59a134b7cb05c48a3516a366dd33379 100755 --- a/weboob/frontends/mail/scripts/mail +++ b/weboob/frontends/mail/scripts/mail @@ -39,14 +39,15 @@ class Application(BaseApplication): 'smtp': 'localhost'} def main(self, argv): - if not self.config: + self.load_config() + if not self.myconfig: print >>sys.stderr, "Error: %s is not configured yet. Please call 'weboob2mail -c'" % argv[0] print >>sys.stderr, "Also, you need to use 'weboobcfg' to set backend configs" return -1 - self.weboob.load_modules(ICapMessages) + self.weboob.load_modules(ICapMessages, backends=self.config.getbackends()) - self.weboob.schedule(self.config['interval'], self.process) + self.weboob.schedule(self.myconfig['interval'], self.process) self.weboob.loop() def process(self): @@ -55,8 +56,8 @@ class Application(BaseApplication): self.send_email(name, message) def send_email(self, backend_name, mail): - domain = self.config['domain'] - recipient = self.config['recipient'] + domain = self.myconfig['domain'] + recipient = self.myconfig['recipient'] reply_id = '' if mail.get_reply_id(): @@ -110,12 +111,11 @@ class Application(BaseApplication): msg['In-Reply-To'] = reply_id # Send the message via SMTP to localhost:25 - smtp = SMTP(self.config['smtp']) + smtp = SMTP(self.myconfig['smtp']) smtp.sendmail(sender, recipient, msg.as_string()) smtp.quit() return msg['Message-Id'] if __name__ == '__main__': - app = Application() - sys.exit(app.main(sys.argv)) + Application.run() diff --git a/weboob/frontends/travel/application.py b/weboob/frontends/travel/application.py index ffbc75f804bce397ce41ffcc254c5235eb50b972..01d3e8af1030e5ab07fcf793ec1e1c14e996dc3e 100644 --- a/weboob/frontends/travel/application.py +++ b/weboob/frontends/travel/application.py @@ -25,11 +25,10 @@ from weboob import Weboob from weboob.capabilities.travel import ICapTravel -from weboob.tools.application import BaseApplication +from weboob.tools.application import ConsoleApplication -class Application(BaseApplication): +class Travel(ConsoleApplication): APPNAME = 'travel' - CONFIG = {} def main(self, argv): self.weboob.load_modules(ICapTravel) diff --git a/weboob/frontends/travel/scripts/travel b/weboob/frontends/travel/scripts/travel index d9ed3c0d128d2379d332651dd7f46b4b72459e8d..c612fba2d02575e8cfee5586167b1b9e7b936288 100755 --- a/weboob/frontends/travel/scripts/travel +++ b/weboob/frontends/travel/scripts/travel @@ -21,8 +21,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. """ import sys -from weboob.frontends.travel import Application +from weboob.frontends.travel import Travel if __name__ == '__main__': - app = Application() - sys.exit(app.main(sys.argv)) + Travel.run() diff --git a/weboob/iconfig.py b/weboob/iconfig.py new file mode 100644 index 0000000000000000000000000000000000000000..5d4522c4982f445eb278a95dde4b4605d308dd17 --- /dev/null +++ b/weboob/iconfig.py @@ -0,0 +1,46 @@ +# -*- coding: utf-8 -*- + +""" +Copyright(C) 2010 Romain Bignon + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, version 3 of the License. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +""" + +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): + raise NotImplementedError() + + def save(self): + raise NotImplementedError() + + def set(self, *args): + raise NotImplementedError() + + def get(self, *args, **kwargs): + raise NotImplementedError() + + def getfrontend(self, name): + raise NotImplementedError() + + def getbackends(self): + raise NotImplementedError() diff --git a/weboob/ouiboube.py b/weboob/ouiboube.py index b5f0cb1a2cced35bf8b1897dc44d629bb95fca24..15cfc60f6083e38e2313fddf063e4a0f058a73f6 100644 --- a/weboob/ouiboube.py +++ b/weboob/ouiboube.py @@ -18,49 +18,45 @@ """ -import os import sched import time from weboob.modules import ModulesLoader -from weboob.config import Config class Weboob: - CONFIG_FILE = '%s/.weboob/config' % os.path.expanduser("~") - DATA_DIR = '%s/.weboob/' % os.path.expanduser("~") - - def __init__(self, app_name, config_file=CONFIG_FILE, data_dir=DATA_DIR): + def __init__(self, app_name): self.app_name = app_name self.backends = {} self.scheduler = sched.scheduler(time.time, time.sleep) - self.config = Config(self.CONFIG_FILE) - self.config.load() self.modules_loader = ModulesLoader() self.modules_loader.load() - def get_frontend_config(self, default={}): - return self.config.get('frontends', self.app_name, default=default) - - def get_backend_config(self, backend_name, default={}): - return self.config.get('backends', backend_name, default=default) - - def load_modules(self, caps=None, name=None): - for name, module in self.modules_loader.modules.iteritems(): - if (not caps or module.has_caps(caps)) and \ - (not name or module.name == name): - backend = module.create_backend(self) - self.backends[module.name] = backend + def load_modules(self, caps=None, name=None, backends=None): + if backends is None: + for name, module in self.modules_loader.modules.iteritems(): + if (not caps or module.has_caps(caps)) and \ + (not name or module.name == name): + backend = module.create_backend(self) + self.backends[module.name] = backend + else: + for key, backendcfg in backends.iteritems(): + try: + module = self.modules_loader[backendcfg.type] + except KeyError: + continue + if (caps and not module.has_caps(caps)) or \ + (name and module.name != name): + continue + self.backends[backendcfg.name] = module.create_backend(self, backendcfg) def load_module(self, modname, instname): module = self.modules_loader[modname] self.backends[instname] = module.create_backend(self) def iter_backends(self, caps=None): - if caps is None: - return self.backends.iteritems() - else: - return dict((name, backend) for name, backend in self.backends.iteritems() - if backend.has_caps(caps)).iteritems() + for name, backend in self.backends.iteritems(): + if caps is None or backend.has_caps(caps): + yield (name, backend) def schedule(self, interval, function, *args): self.scheduler.enter(interval, 1, function, args) diff --git a/weboob/tools/application.py b/weboob/tools/application.py index 08bcfb6eda543b033a81ab7332e48e22aa23e471..137c05e510a6f461efdca47675d9bfa8cb17e58b 100644 --- a/weboob/tools/application.py +++ b/weboob/tools/application.py @@ -20,7 +20,7 @@ """ -import sys, tty, termios +import sys, tty, termios, os import re from weboob import Weboob @@ -28,14 +28,46 @@ class BaseApplication(object): APPNAME = '' CONFIG = {} + CONFDIR = '%s/.weboob/' % os.path.expanduser("~") def __init__(self): self.weboob = Weboob(self.APPNAME) - self.config = self.weboob.get_frontend_config(self.CONFIG) + self.config = None + + def load_config(self, path=None, klass=None): + """ + Load a configuration file and get his object. + + @param path [str] an optional specific path. + @param klass [IConfig] what klass to instance. + @return a IConfig object + """ + 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 + + if path is None: + path = self.CONFDIR + 'weboobrc' + elif not path.startswith('/'): + path = self.CONFDIR + path + + self.config = klass(path) + self.config.load() + self.myconfig = self.CONFIG + self.myconfig.update(self.config.getfrontend(self.APPNAME)) def main(self, argv): + """ Main function """ raise NotImplementedError() + @classmethod + def run(klass): + app = klass() + sys.exit(app.main(sys.argv)) + +class ConsoleApplication(BaseApplication): def ask(self, question, default=None, masked=False, regexp=None): """ Ask a question to user. @@ -71,5 +103,3 @@ def ask(self, question, default=None, masked=False, regexp=None): correct = not regexp or re.match(regexp, str(line)) return line - - diff --git a/weboob/config.py b/weboob/tools/config.py similarity index 79% rename from weboob/config.py rename to weboob/tools/config.py index 8925f3775d51da511de0aa7f555209118d33f242..5d8f8f280b94d9dbf3ea0c8a36a3173598223278 100644 --- a/weboob/config.py +++ b/weboob/tools/config.py @@ -21,10 +21,11 @@ from __future__ import with_statement import yaml +from logging import warning -class ConfigError(Exception): pass +from weboob.iconfig import IConfig, ConfigError, BackendConfig -class Config: +class Config(IConfig): def __init__(self, path): self.path = path self.values = {} @@ -39,6 +40,10 @@ def load(self): if self.values is None: self.values = {} + def save(self): + with open(self.path, 'w') as f: + yaml.dump(self.values, f) + def get(self, *args, **kwargs): default = None if 'default' in kwargs: @@ -78,6 +83,14 @@ def set(self, *args): v[args[-2]] = args[-1] - def save(self): - with open(self.path, 'w') as f: - yaml.dump(self.values, f) + def getfrontend(self, name): + return self.get('frontends', name) + + 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 diff --git a/weboob/tools/firefox_cookies.py b/weboob/tools/firefox_cookies.py index a8466b382343230cfbf287b81641654709d2a258..21610b9abd04e968e57dd8d75055998856b93fa0 100644 --- a/weboob/tools/firefox_cookies.py +++ b/weboob/tools/firefox_cookies.py @@ -47,7 +47,6 @@ def __connect(self): return db def load(self): - db = self.__connect() if not db: return @@ -56,7 +55,6 @@ def load(self): WHERE host LIKE '%%%s%%'""" % self.domain) for entry in cookies: - domain = entry[0] initial_dot = domain.startswith(".") domain_specified = initial_dot @@ -85,7 +83,6 @@ def load(self): self.set_cookie(c) def save(self): - db = self.__connect() if not db: return