reorganization of config management
This commit is contained in:
parent
d15365fe26
commit
29400eb185
9 changed files with 135 additions and 56 deletions
|
|
@ -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
|
||||
|
||||
|
||||
|
|
|
|||
96
weboob/tools/config.py
Normal file
96
weboob/tools/config.py
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
# -*- 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 logging import warning
|
||||
|
||||
from weboob.iconfig import IConfig, ConfigError, BackendConfig
|
||||
|
||||
class Config(IConfig):
|
||||
def __init__(self, path):
|
||||
self.path = path
|
||||
self.values = {}
|
||||
|
||||
def load(self):
|
||||
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]
|
||||
|
||||
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
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue