Commit 9ff2f702 authored by Romain Bignon's avatar Romain Bignon

support deinit

parent 37c0ee89
......@@ -34,7 +34,7 @@ class Weboob(object):
WORKDIR = os.path.join(os.path.expanduser('~'), '.weboob')
BACKENDS_FILENAME = 'backends'
def __init__(self, workdir=WORKDIR, backends_filename=None, scheduler=None):
def __init__(self, workdir=WORKDIR, backends_filename=None, scheduler=None, storage=None):
self.workdir = workdir
self.backend_instances = {}
......@@ -59,8 +59,20 @@ class Weboob(object):
backends_filename = os.path.join(self.workdir, backends_filename)
self.backends_config = BackendsConfig(backends_filename)
# Storage
self.storage = storage
def __deinit__(self):
self.deinit()
def deinit(self):
self.unload_backends()
def load_backends(self, caps=None, names=None, storage=None):
loaded = {}
if storage is None:
storage = self.storage
self.backends_loader.load_all()
for backend_name, backend in self.backends_loader.loaded.iteritems():
if caps is not None and not backend.has_caps(caps) or \
......@@ -72,6 +84,9 @@ class Weboob(object):
def load_configured_backends(self, caps=None, names=None, storage=None):
loaded = {}
if storage is None:
storage = self.storage
for instance_name, backend_name, params in self.backends_config.iter_backends():
if '_enabled' in params and not params['_enabled']:
continue
......
......@@ -115,12 +115,16 @@ class BaseApplication(object):
self._parser.add_option_group(logging_options)
self._parser.add_option('--shell-completion', action='store_true', help=optparse.SUPPRESS_HELP)
def create_storage(self, path=None, klass=None):
def deinit(self):
self.weboob.deinit()
def create_storage(self, path=None, klass=None, localonly=False):
"""
Create a storage object.
@param path [str] an optional specific path.
@param klass [IStorage] what klass to instance.
@param localonly [bool] if True, do not set it on the Weboob object.
@return a IStorage object
"""
if klass is None:
......@@ -135,6 +139,10 @@ class BaseApplication(object):
storage = klass(path)
self.storage = ApplicationStorage(self.APPNAME, storage)
self.storage.load(self.STORAGE)
if not localonly:
self.weboob.storage = storage
return storage
def load_config(self, path=None, klass=None):
......@@ -259,10 +267,13 @@ class BaseApplication(object):
app._handle_app_options()
try:
sys.exit(app.main(args))
except KeyboardInterrupt:
print 'Program killed by SIGINT'
sys.exit(0) # XXX is it really the right exit code? -romain
except ConfigError, e:
print 'Configuration error: %s' % e
sys.exit(1)
try:
sys.exit(app.main(args))
except KeyboardInterrupt:
print 'Program killed by SIGINT'
sys.exit(0) # XXX is it really the right exit code? -romain
except ConfigError, e:
print 'Configuration error: %s' % e
sys.exit(1)
finally:
app.deinit()
......@@ -130,6 +130,12 @@ class BaseBackend(object):
self.storage = BackendStorage(self.name, storage)
self.storage.load(self.STORAGE)
def deinit(self):
"""
This abstract method is called when the backend is unloaded.
"""
pass
@property
def browser(self):
"""
......
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