mirror of
https://github.com/samsonjs/vdirsyncer.git
synced 2026-04-27 14:57:41 +00:00
Lazily load storages
This commit is contained in:
parent
e2eb79d656
commit
ed12509c77
5 changed files with 40 additions and 32 deletions
|
|
@ -15,6 +15,8 @@ Version 0.5.0
|
||||||
*yet to be released*
|
*yet to be released*
|
||||||
|
|
||||||
- Raise version of required requests-toolbelt to ``0.4.0``.
|
- 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
|
Version 0.4.4
|
||||||
=============
|
=============
|
||||||
|
|
|
||||||
|
|
@ -61,7 +61,8 @@ def test_storage_instance_from_config(monkeypatch):
|
||||||
return 'OK'
|
return 'OK'
|
||||||
|
|
||||||
import vdirsyncer.storage
|
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}
|
config = {'type': 'lol', 'foo': 'bar', 'baz': 1}
|
||||||
assert cli.utils.storage_instance_from_config(config) == 'OK'
|
assert cli.utils.storage_instance_from_config(config) == 'OK'
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,10 +11,14 @@ import pytest
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
import vdirsyncer.doubleclick as doubleclick
|
from vdirsyncer import doubleclick, utils
|
||||||
import vdirsyncer.utils.http
|
|
||||||
import vdirsyncer.utils.password
|
# These modules might be uninitialized and unavailable if not explicitly
|
||||||
import vdirsyncer.utils as utils
|
# imported
|
||||||
|
import vdirsyncer.utils.compat # noqa
|
||||||
|
import vdirsyncer.utils.http # noqa
|
||||||
|
import vdirsyncer.utils.password # noqa
|
||||||
|
|
||||||
|
|
||||||
from .. import blow_up
|
from .. import blow_up
|
||||||
|
|
||||||
|
|
@ -220,9 +224,9 @@ def test_request_ssl(httpsserver):
|
||||||
assert 'certificate verify failed' in str(excinfo.value)
|
assert 'certificate verify failed' in str(excinfo.value)
|
||||||
utils.http.request('GET', httpsserver.url, verify=False)
|
utils.http.request('GET', httpsserver.url, verify=False)
|
||||||
utils.http.request('GET', httpsserver.url,
|
utils.http.request('GET', httpsserver.url,
|
||||||
verify_fingerprint=sha1)
|
verify_fingerprint=sha1)
|
||||||
utils.http.request('GET', httpsserver.url, verify_fingerprint=md5)
|
utils.http.request('GET', httpsserver.url, verify_fingerprint=md5)
|
||||||
with pytest.raises(requests.exceptions.SSLError) as excinfo:
|
with pytest.raises(requests.exceptions.SSLError) as excinfo:
|
||||||
utils.http.request('GET', httpsserver.url,
|
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)
|
assert 'Fingerprints did not match' in str(excinfo.value)
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
import errno
|
import errno
|
||||||
import hashlib
|
import hashlib
|
||||||
|
import importlib
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import string
|
import string
|
||||||
|
|
@ -31,31 +32,30 @@ except ImportError:
|
||||||
import queue
|
import queue
|
||||||
|
|
||||||
|
|
||||||
def _generate_storage_dict():
|
class _StorageIndex(object):
|
||||||
from ..storage.dav import CaldavStorage, CarddavStorage
|
def __init__(self):
|
||||||
from ..storage.filesystem import FilesystemStorage
|
self._storages = dict(
|
||||||
from ..storage.http import HttpStorage
|
caldav='vdirsyncer.storage.dav.CaldavStorage',
|
||||||
from ..storage.singlefile import SingleFileStorage
|
carddav='vdirsyncer.storage.dav.CarddavStorage',
|
||||||
|
filesystem='vdirsyncer.storage.filesystem.FilesystemStorage',
|
||||||
|
http='vdirsyncer.storage.http.HttpStorage',
|
||||||
|
singlefile='vdirsyncer.storage.singlefile.SingleFileStorage',
|
||||||
|
)
|
||||||
|
|
||||||
classes = (
|
def __getitem__(self, name):
|
||||||
CaldavStorage,
|
item = self._storages[name]
|
||||||
CarddavStorage,
|
if not isinstance(item, str):
|
||||||
FilesystemStorage,
|
return item
|
||||||
HttpStorage,
|
|
||||||
SingleFileStorage
|
|
||||||
)
|
|
||||||
|
|
||||||
rv = {}
|
modname, clsname = item.rsplit('.', 1)
|
||||||
for cls in classes:
|
mod = importlib.import_module(modname)
|
||||||
key = cls.storage_name
|
self._storages[name] = rv = getattr(mod, clsname)
|
||||||
assert key
|
assert rv.storage_name == name
|
||||||
assert isinstance(key, str)
|
return rv
|
||||||
assert key not in rv
|
|
||||||
rv[key] = cls
|
|
||||||
return rv
|
|
||||||
|
|
||||||
storage_names = _generate_storage_dict()
|
|
||||||
del _generate_storage_dict
|
storage_names = _StorageIndex()
|
||||||
|
del _StorageIndex
|
||||||
|
|
||||||
|
|
||||||
cli_logger = log.get(__name__)
|
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):
|
def storage_class_from_config(config):
|
||||||
config = dict(config)
|
config = dict(config)
|
||||||
storage_name = config.pop('type')
|
storage_name = config.pop('type')
|
||||||
cls = storage_names.get(storage_name, None)
|
try:
|
||||||
if cls is None:
|
cls = storage_names[storage_name]
|
||||||
|
except KeyError:
|
||||||
raise CliError('Unknown storage type: {}'.format(storage_name))
|
raise CliError('Unknown storage type: {}'.format(storage_name))
|
||||||
return cls, config
|
return cls, config
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from .compat import iteritems
|
from .compat import iteritems
|
||||||
from .. import exceptions, log
|
from .. import exceptions
|
||||||
|
|
||||||
|
|
||||||
_missing = object()
|
_missing = object()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue