new frontend configuration system

This commit is contained in:
Romain Bignon 2010-04-01 20:01:31 +02:00
commit 2cb52beba5
6 changed files with 19 additions and 44 deletions

View file

@ -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')

View file

@ -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()

View file

@ -28,7 +28,7 @@ from weboob import Weboob
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 @@ class BaseApplication(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
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 """

View file

View file

@ -0,0 +1,34 @@
# -*- 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 IConfig:
def load(self, default={}):
raise NotImplementedError()
def save(self):
raise NotImplementedError()
def set(self, *args):
raise NotImplementedError()
def get(self, *args, **kwargs):
raise NotImplementedError()

View file

@ -0,0 +1,85 @@
# -*- 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.
"""
from __future__ import with_statement
import yaml
from .iconfig import IConfig, ConfigError
class YamlConfig(IConfig):
def __init__(self, path):
self.path = path
self.values = {}
def load(self, default={}):
self.values = default.copy()
try:
with open(self.path, 'r') as f:
self.values = yaml.load(f)
except IOError:
pass
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:
default = kwargs['default']
v = self.values
for a in args[:-1]:
try:
v = v[a]
except KeyError:
if not default is None:
v[a] = {}
v = v[a]
else:
raise ConfigError()
except TypeError:
raise ConfigError()
try:
v = v[args[-1]]
except KeyError:
v[args[-1]] = default
v = v[args[-1]]
return v
def set(self, *args):
v = self.values
for a in args[:-2]:
try:
v = v[a]
except KeyError:
v[a] = {}
v = v[a]
except TypeError:
raise ConfigError()
v[args[-2]] = args[-1]