reorganization of config management
This commit is contained in:
parent
d15365fe26
commit
29400eb185
9 changed files with 135 additions and 56 deletions
|
|
@ -20,8 +20,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import sys
|
|
||||||
|
|
||||||
from weboob import Weboob
|
from weboob import Weboob
|
||||||
from weboob.capabilities.messages import ICapMessages, ICapMessagesReply
|
from weboob.capabilities.messages import ICapMessages, ICapMessagesReply
|
||||||
from weboob.tools.application import BaseApplication
|
from weboob.tools.application import BaseApplication
|
||||||
|
|
@ -30,7 +28,9 @@ class Application(BaseApplication):
|
||||||
APPNAME = 'dummy'
|
APPNAME = 'dummy'
|
||||||
|
|
||||||
def main(self, argv):
|
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():
|
for name, backend in self.weboob.iter_backends():
|
||||||
print '= Processing backend name = %s' % name
|
print '= Processing backend name = %s' % name
|
||||||
if backend.has_caps(ICapMessages):
|
if backend.has_caps(ICapMessages):
|
||||||
|
|
@ -41,5 +41,4 @@ class Application(BaseApplication):
|
||||||
print '== Backend is ICapMessagesReply => TODO'
|
print '== Backend is ICapMessagesReply => TODO'
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
app = Application()
|
Application.run()
|
||||||
sys.exit(app.main(sys.argv))
|
|
||||||
|
|
|
||||||
|
|
@ -39,14 +39,15 @@ class Application(BaseApplication):
|
||||||
'smtp': 'localhost'}
|
'smtp': 'localhost'}
|
||||||
|
|
||||||
def main(self, argv):
|
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, "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"
|
print >>sys.stderr, "Also, you need to use 'weboobcfg' to set backend configs"
|
||||||
return -1
|
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()
|
self.weboob.loop()
|
||||||
|
|
||||||
def process(self):
|
def process(self):
|
||||||
|
|
@ -55,8 +56,8 @@ class Application(BaseApplication):
|
||||||
self.send_email(name, message)
|
self.send_email(name, message)
|
||||||
|
|
||||||
def send_email(self, backend_name, mail):
|
def send_email(self, backend_name, mail):
|
||||||
domain = self.config['domain']
|
domain = self.myconfig['domain']
|
||||||
recipient = self.config['recipient']
|
recipient = self.myconfig['recipient']
|
||||||
|
|
||||||
reply_id = ''
|
reply_id = ''
|
||||||
if mail.get_reply_id():
|
if mail.get_reply_id():
|
||||||
|
|
@ -110,12 +111,11 @@ class Application(BaseApplication):
|
||||||
msg['In-Reply-To'] = reply_id
|
msg['In-Reply-To'] = reply_id
|
||||||
|
|
||||||
# Send the message via SMTP to localhost:25
|
# 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.sendmail(sender, recipient, msg.as_string())
|
||||||
smtp.quit()
|
smtp.quit()
|
||||||
|
|
||||||
return msg['Message-Id']
|
return msg['Message-Id']
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
app = Application()
|
Application.run()
|
||||||
sys.exit(app.main(sys.argv))
|
|
||||||
|
|
|
||||||
|
|
@ -25,11 +25,10 @@ from types import MethodType
|
||||||
|
|
||||||
from weboob import Weboob
|
from weboob import Weboob
|
||||||
from weboob.capabilities.travel import ICapTravel
|
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'
|
APPNAME = 'travel'
|
||||||
CONFIG = {}
|
|
||||||
|
|
||||||
def main(self, argv):
|
def main(self, argv):
|
||||||
self.weboob.load_modules(ICapTravel)
|
self.weboob.load_modules(ICapTravel)
|
||||||
|
|
|
||||||
|
|
@ -21,8 +21,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
from weboob.frontends.travel import Application
|
from weboob.frontends.travel import Travel
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
app = Application()
|
Travel.run()
|
||||||
sys.exit(app.main(sys.argv))
|
|
||||||
|
|
|
||||||
46
weboob/iconfig.py
Normal file
46
weboob/iconfig.py
Normal file
|
|
@ -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()
|
||||||
|
|
@ -18,49 +18,45 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import os
|
|
||||||
import sched
|
import sched
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from weboob.modules import ModulesLoader
|
from weboob.modules import ModulesLoader
|
||||||
from weboob.config import Config
|
|
||||||
|
|
||||||
class Weboob:
|
class Weboob:
|
||||||
CONFIG_FILE = '%s/.weboob/config' % os.path.expanduser("~")
|
def __init__(self, app_name):
|
||||||
DATA_DIR = '%s/.weboob/' % os.path.expanduser("~")
|
|
||||||
|
|
||||||
def __init__(self, app_name, config_file=CONFIG_FILE, data_dir=DATA_DIR):
|
|
||||||
self.app_name = app_name
|
self.app_name = app_name
|
||||||
self.backends = {}
|
self.backends = {}
|
||||||
self.scheduler = sched.scheduler(time.time, time.sleep)
|
self.scheduler = sched.scheduler(time.time, time.sleep)
|
||||||
self.config = Config(self.CONFIG_FILE)
|
|
||||||
self.config.load()
|
|
||||||
self.modules_loader = ModulesLoader()
|
self.modules_loader = ModulesLoader()
|
||||||
self.modules_loader.load()
|
self.modules_loader.load()
|
||||||
|
|
||||||
def get_frontend_config(self, default={}):
|
def load_modules(self, caps=None, name=None, backends=None):
|
||||||
return self.config.get('frontends', self.app_name, default=default)
|
if backends is None:
|
||||||
|
|
||||||
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():
|
for name, module in self.modules_loader.modules.iteritems():
|
||||||
if (not caps or module.has_caps(caps)) and \
|
if (not caps or module.has_caps(caps)) and \
|
||||||
(not name or module.name == name):
|
(not name or module.name == name):
|
||||||
backend = module.create_backend(self)
|
backend = module.create_backend(self)
|
||||||
self.backends[module.name] = backend
|
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):
|
def load_module(self, modname, instname):
|
||||||
module = self.modules_loader[modname]
|
module = self.modules_loader[modname]
|
||||||
self.backends[instname] = module.create_backend(self)
|
self.backends[instname] = module.create_backend(self)
|
||||||
|
|
||||||
def iter_backends(self, caps=None):
|
def iter_backends(self, caps=None):
|
||||||
if caps is None:
|
for name, backend in self.backends.iteritems():
|
||||||
return self.backends.iteritems()
|
if caps is None or backend.has_caps(caps):
|
||||||
else:
|
yield (name, backend)
|
||||||
return dict((name, backend) for name, backend in self.backends.iteritems()
|
|
||||||
if backend.has_caps(caps)).iteritems()
|
|
||||||
|
|
||||||
def schedule(self, interval, function, *args):
|
def schedule(self, interval, function, *args):
|
||||||
self.scheduler.enter(interval, 1, 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
|
import re
|
||||||
|
|
||||||
from weboob import Weboob
|
from weboob import Weboob
|
||||||
|
|
@ -28,14 +28,46 @@ from weboob import Weboob
|
||||||
class BaseApplication(object):
|
class BaseApplication(object):
|
||||||
APPNAME = ''
|
APPNAME = ''
|
||||||
CONFIG = {}
|
CONFIG = {}
|
||||||
|
CONFDIR = '%s/.weboob/' % os.path.expanduser("~")
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.weboob = Weboob(self.APPNAME)
|
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):
|
def main(self, argv):
|
||||||
|
""" Main function """
|
||||||
raise NotImplementedError()
|
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):
|
def ask(self, question, default=None, masked=False, regexp=None):
|
||||||
"""
|
"""
|
||||||
Ask a question to user.
|
Ask a question to user.
|
||||||
|
|
@ -71,5 +103,3 @@ class BaseApplication(object):
|
||||||
correct = not regexp or re.match(regexp, str(line))
|
correct = not regexp or re.match(regexp, str(line))
|
||||||
|
|
||||||
return line
|
return line
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -21,10 +21,11 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||||
from __future__ import with_statement
|
from __future__ import with_statement
|
||||||
|
|
||||||
import yaml
|
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):
|
def __init__(self, path):
|
||||||
self.path = path
|
self.path = path
|
||||||
self.values = {}
|
self.values = {}
|
||||||
|
|
@ -39,6 +40,10 @@ class Config:
|
||||||
if self.values is None:
|
if self.values is None:
|
||||||
self.values = {}
|
self.values = {}
|
||||||
|
|
||||||
|
def save(self):
|
||||||
|
with open(self.path, 'w') as f:
|
||||||
|
yaml.dump(self.values, f)
|
||||||
|
|
||||||
def get(self, *args, **kwargs):
|
def get(self, *args, **kwargs):
|
||||||
default = None
|
default = None
|
||||||
if 'default' in kwargs:
|
if 'default' in kwargs:
|
||||||
|
|
@ -78,6 +83,14 @@ class Config:
|
||||||
|
|
||||||
v[args[-2]] = args[-1]
|
v[args[-2]] = args[-1]
|
||||||
|
|
||||||
def save(self):
|
def getfrontend(self, name):
|
||||||
with open(self.path, 'w') as f:
|
return self.get('frontends', name)
|
||||||
yaml.dump(self.values, f)
|
|
||||||
|
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
|
return db
|
||||||
|
|
||||||
def load(self):
|
def load(self):
|
||||||
|
|
||||||
db = self.__connect()
|
db = self.__connect()
|
||||||
if not db: return
|
if not db: return
|
||||||
|
|
||||||
|
|
@ -56,7 +55,6 @@ class FirefoxCookieJar(CookieJar):
|
||||||
WHERE host LIKE '%%%s%%'""" % self.domain)
|
WHERE host LIKE '%%%s%%'""" % self.domain)
|
||||||
|
|
||||||
for entry in cookies:
|
for entry in cookies:
|
||||||
|
|
||||||
domain = entry[0]
|
domain = entry[0]
|
||||||
initial_dot = domain.startswith(".")
|
initial_dot = domain.startswith(".")
|
||||||
domain_specified = initial_dot
|
domain_specified = initial_dot
|
||||||
|
|
@ -85,7 +83,6 @@ class FirefoxCookieJar(CookieJar):
|
||||||
self.set_cookie(c)
|
self.set_cookie(c)
|
||||||
|
|
||||||
def save(self):
|
def save(self):
|
||||||
|
|
||||||
db = self.__connect()
|
db = self.__connect()
|
||||||
if not db: return
|
if not db: return
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue