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* *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
============= =============

View file

@ -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'

View file

@ -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

View file

@ -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',
classes = ( http='vdirsyncer.storage.http.HttpStorage',
CaldavStorage, singlefile='vdirsyncer.storage.singlefile.SingleFileStorage',
CarddavStorage,
FilesystemStorage,
HttpStorage,
SingleFileStorage
) )
rv = {} def __getitem__(self, name):
for cls in classes: item = self._storages[name]
key = cls.storage_name if not isinstance(item, str):
assert key return item
assert isinstance(key, str)
assert key not in rv modname, clsname = item.rsplit('.', 1)
rv[key] = cls mod = importlib.import_module(modname)
self._storages[name] = rv = getattr(mod, clsname)
assert rv.storage_name == name
return rv 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

View file

@ -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()