new frontend configuration system
This commit is contained in:
parent
e119a70cec
commit
2cb52beba5
6 changed files with 19 additions and 44 deletions
|
|
@ -35,7 +35,7 @@ class Application(BaseApplication):
|
||||||
APPNAME = 'http'
|
APPNAME = 'http'
|
||||||
|
|
||||||
def main(self, argv):
|
def main(self, argv):
|
||||||
self.weboob.load_modules()
|
self.weboob.load_backends()
|
||||||
|
|
||||||
template_lookup = TemplateLookup(directories=['%s/../templates' % os.path.dirname(__file__)],
|
template_lookup = TemplateLookup(directories=['%s/../templates' % os.path.dirname(__file__)],
|
||||||
output_encoding='utf-8', encoding_errors='replace')
|
output_encoding='utf-8', encoding_errors='replace')
|
||||||
|
|
|
||||||
|
|
@ -40,14 +40,14 @@ class Monboob(BaseApplication):
|
||||||
|
|
||||||
def main(self, argv):
|
def main(self, argv):
|
||||||
self.load_config()
|
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, "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"
|
print >>sys.stderr, "Also, you need to use 'weboobcfg' to set backend configs"
|
||||||
return -1
|
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()
|
self.weboob.loop()
|
||||||
|
|
||||||
def process(self):
|
def process(self):
|
||||||
|
|
@ -56,8 +56,8 @@ class Monboob(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.myconfig['domain']
|
domain = self.config.get('domain')
|
||||||
recipient = self.myconfig['recipient']
|
recipient = self.config.get('recipient')
|
||||||
|
|
||||||
reply_id = ''
|
reply_id = ''
|
||||||
if mail.get_reply_id():
|
if mail.get_reply_id():
|
||||||
|
|
@ -111,7 +111,7 @@ class Monboob(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.myconfig['smtp'])
|
smtp = SMTP(self.config.get('smtp'))
|
||||||
smtp.sendmail(sender, recipient, msg.as_string())
|
smtp.sendmail(sender, recipient, msg.as_string())
|
||||||
smtp.quit()
|
smtp.quit()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,7 @@ from weboob import Weboob
|
||||||
class BaseApplication(object):
|
class BaseApplication(object):
|
||||||
APPNAME = ''
|
APPNAME = ''
|
||||||
CONFIG = {}
|
CONFIG = {}
|
||||||
CONFDIR = '%s/.weboob/' % os.path.expanduser("~")
|
CONFDIR = os.path.join(os.path.expanduser('~'), '.weboob')
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.weboob = Weboob(self.APPNAME)
|
self.weboob = Weboob(self.APPNAME)
|
||||||
|
|
@ -45,18 +45,16 @@ class BaseApplication(object):
|
||||||
if klass is None:
|
if klass is None:
|
||||||
# load Config only here because some applications don't want
|
# load Config only here because some applications don't want
|
||||||
# to depend on yaml and do not use this function
|
# to depend on yaml and do not use this function
|
||||||
from weboob.tools.config import Config
|
from weboob.tools.config.yaml import YamlConfig
|
||||||
klass = Config
|
klass = YamlConfig
|
||||||
|
|
||||||
if path is None:
|
if path is None:
|
||||||
path = self.CONFDIR + 'weboobrc'
|
path = os.path.join(self.CONFDIR, self.APPNAME)
|
||||||
elif not path.startswith('/'):
|
elif not path.startswith('/'):
|
||||||
path = self.CONFDIR + path
|
path = os.path.join(self.CONFDIR, path)
|
||||||
|
|
||||||
self.config = klass(path)
|
self.config = klass(path)
|
||||||
self.config.load()
|
self.config.load(self.CONFIG)
|
||||||
self.myconfig = self.CONFIG
|
|
||||||
self.myconfig.update(self.config.getfrontend(self.APPNAME))
|
|
||||||
|
|
||||||
def main(self, argv):
|
def main(self, argv):
|
||||||
""" Main function """
|
""" Main function """
|
||||||
|
|
|
||||||
0
weboob/tools/config/__init__.py
Normal file
0
weboob/tools/config/__init__.py
Normal file
|
|
@ -20,14 +20,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||||
|
|
||||||
class ConfigError(Exception): pass
|
class ConfigError(Exception): pass
|
||||||
|
|
||||||
class BackendConfig(object):
|
|
||||||
def __init__(self, name, _type, config):
|
|
||||||
self.name = name
|
|
||||||
self.type = _type
|
|
||||||
self.config = config
|
|
||||||
|
|
||||||
class IConfig:
|
class IConfig:
|
||||||
def load(self):
|
def load(self, default={}):
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
def save(self):
|
def save(self):
|
||||||
|
|
@ -38,9 +32,3 @@ class IConfig:
|
||||||
|
|
||||||
def get(self, *args, **kwargs):
|
def get(self, *args, **kwargs):
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
def getfrontend(self, name):
|
|
||||||
raise NotImplementedError()
|
|
||||||
|
|
||||||
def getbackends(self):
|
|
||||||
raise NotImplementedError()
|
|
||||||
|
|
@ -21,16 +21,17 @@ 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
|
|
||||||
|
|
||||||
from weboob.iconfig import IConfig, ConfigError, BackendConfig
|
from .iconfig import IConfig, ConfigError
|
||||||
|
|
||||||
class Config(IConfig):
|
class YamlConfig(IConfig):
|
||||||
def __init__(self, path):
|
def __init__(self, path):
|
||||||
self.path = path
|
self.path = path
|
||||||
self.values = {}
|
self.values = {}
|
||||||
|
|
||||||
def load(self):
|
def load(self, default={}):
|
||||||
|
self.values = default.copy()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with open(self.path, 'r') as f:
|
with open(self.path, 'r') as f:
|
||||||
self.values = yaml.load(f)
|
self.values = yaml.load(f)
|
||||||
|
|
@ -82,15 +83,3 @@ class Config(IConfig):
|
||||||
raise ConfigError()
|
raise ConfigError()
|
||||||
|
|
||||||
v[args[-2]] = args[-1]
|
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
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue