Lazily load storages

This commit is contained in:
Markus Unterwaditzer 2015-04-13 18:27:32 +02:00
parent e2eb79d656
commit ed12509c77
5 changed files with 40 additions and 32 deletions

View file

@ -15,6 +15,8 @@ Version 0.5.0
*yet to be released*
- Raise version of required requests-toolbelt to ``0.4.0``.
- Command line should be a lot faster when no work is done, e.g. for help
output.
Version 0.4.4
=============

View file

@ -61,7 +61,8 @@ def test_storage_instance_from_config(monkeypatch):
return 'OK'
import vdirsyncer.storage
monkeypatch.setitem(vdirsyncer.cli.utils.storage_names, 'lol', lol)
monkeypatch.setitem(vdirsyncer.cli.utils.storage_names._storages,
'lol', lol)
config = {'type': 'lol', 'foo': 'bar', 'baz': 1}
assert cli.utils.storage_instance_from_config(config) == 'OK'

View file

@ -11,10 +11,14 @@ import pytest
import requests
import vdirsyncer.doubleclick as doubleclick
import vdirsyncer.utils.http
import vdirsyncer.utils.password
import vdirsyncer.utils as utils
from vdirsyncer import doubleclick, utils
# These modules might be uninitialized and unavailable if not explicitly
# imported
import vdirsyncer.utils.compat # noqa
import vdirsyncer.utils.http # noqa
import vdirsyncer.utils.password # noqa
from .. import blow_up
@ -220,9 +224,9 @@ def test_request_ssl(httpsserver):
assert 'certificate verify failed' in str(excinfo.value)
utils.http.request('GET', httpsserver.url, verify=False)
utils.http.request('GET', httpsserver.url,
verify_fingerprint=sha1)
verify_fingerprint=sha1)
utils.http.request('GET', httpsserver.url, verify_fingerprint=md5)
with pytest.raises(requests.exceptions.SSLError) as excinfo:
utils.http.request('GET', httpsserver.url,
verify_fingerprint=''.join(reversed(sha1)))
verify_fingerprint=''.join(reversed(sha1)))
assert 'Fingerprints did not match' in str(excinfo.value)

View file

@ -2,6 +2,7 @@
import errno
import hashlib
import importlib
import json
import os
import string
@ -31,31 +32,30 @@ except ImportError:
import queue
def _generate_storage_dict():
from ..storage.dav import CaldavStorage, CarddavStorage
from ..storage.filesystem import FilesystemStorage
from ..storage.http import HttpStorage
from ..storage.singlefile import SingleFileStorage
class _StorageIndex(object):
def __init__(self):
self._storages = dict(
caldav='vdirsyncer.storage.dav.CaldavStorage',
carddav='vdirsyncer.storage.dav.CarddavStorage',
filesystem='vdirsyncer.storage.filesystem.FilesystemStorage',
http='vdirsyncer.storage.http.HttpStorage',
singlefile='vdirsyncer.storage.singlefile.SingleFileStorage',
)
classes = (
CaldavStorage,
CarddavStorage,
FilesystemStorage,
HttpStorage,
SingleFileStorage
)
def __getitem__(self, name):
item = self._storages[name]
if not isinstance(item, str):
return item
rv = {}
for cls in classes:
key = cls.storage_name
assert key
assert isinstance(key, str)
assert key not in rv
rv[key] = cls
return rv
modname, clsname = item.rsplit('.', 1)
mod = importlib.import_module(modname)
self._storages[name] = rv = getattr(mod, clsname)
assert rv.storage_name == name
return rv
storage_names = _generate_storage_dict()
del _generate_storage_dict
storage_names = _StorageIndex()
del _StorageIndex
cli_logger = log.get(__name__)
@ -444,8 +444,9 @@ def save_status(base_path, pair, collection=None, data_type=None, data=None):
def storage_class_from_config(config):
config = dict(config)
storage_name = config.pop('type')
cls = storage_names.get(storage_name, None)
if cls is None:
try:
cls = storage_names[storage_name]
except KeyError:
raise CliError('Unknown storage type: {}'.format(storage_name))
return cls, config

View file

@ -4,7 +4,7 @@ import os
import sys
from .compat import iteritems
from .. import exceptions, log
from .. import exceptions
_missing = object()