Commit 29400eb1 authored by Romain Bignon's avatar Romain Bignon

reorganization of config management

parent d15365fe
......@@ -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()
......@@ -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()
......@@ -25,11 +25,10 @@ from types import MethodType
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)
......
......@@ -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()
# -*- 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()
......@@ -18,49 +18,45 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
"""
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):
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)
......
......@@ -20,7 +20,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
"""
import sys, tty, termios
import sys, tty, termios, os
import re
from weboob import Weboob
......@@ -28,14 +28,46 @@ from weboob import Weboob
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 @@ class BaseApplication(object):
correct = not regexp or re.match(regexp, str(line))
return line
......@@ -21,10 +21,11 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
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 @@ class Config:
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 @@ class Config:
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
......@@ -47,7 +47,6 @@ class FirefoxCookieJar(CookieJar):
return db
def load(self):
db = self.__connect()
if not db: return
......@@ -56,7 +55,6 @@ class FirefoxCookieJar(CookieJar):
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 @@ class FirefoxCookieJar(CookieJar):
self.set_cookie(c)
def save(self):
db = self.__connect()
if not db: return
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment