diff --git a/src/ankisyncd/collection/__init__.py b/src/ankisyncd/collection/__init__.py new file mode 100644 index 0000000..f72bef2 --- /dev/null +++ b/src/ankisyncd/collection/__init__.py @@ -0,0 +1,28 @@ +from ankisyncd import logging +from ankisyncd.collection.wrapper import CollectionWrapper +from ankisyncd.collection.manager import CollectionManager + +logger = logging.get_logger(__name__) + + +def get_collection_wrapper(config, path, setup_new_collection=None): + if "collection_wrapper" in config and config["collection_wrapper"]: + logger.info( + "Found collection_wrapper in config, using {} for " + "user data persistence".format(config["collection_wrapper"]) + ) + import importlib + import inspect + + module_name, class_name = config["collection_wrapper"].rsplit(".", 1) + module = importlib.import_module(module_name.strip()) + class_ = getattr(module, class_name.strip()) + + if not CollectionWrapper in inspect.getmro(class_): + raise TypeError( + """"collection_wrapper" found in the conf file but it doesn''t + inherit from CollectionWrapper""" + ) + return class_(config, path, setup_new_collection) + else: + return CollectionWrapper(config, path, setup_new_collection) diff --git a/src/ankisyncd/collection/manager.py b/src/ankisyncd/collection/manager.py new file mode 100644 index 0000000..1b417f0 --- /dev/null +++ b/src/ankisyncd/collection/manager.py @@ -0,0 +1,33 @@ +import os + +from ankisyncd.collection.wrapper import CollectionWrapper + + +class CollectionManager: + """Manages a set of CollectionWrapper objects.""" + + collection_wrapper = CollectionWrapper + + def __init__(self, config): + self.collections = {} + self.config = config + + def get_collection(self, path, setup_new_collection=None): + """Gets a CollectionWrapper for the given path.""" + + path = os.path.realpath(path) + + try: + col = self.collections[path] + except KeyError: + col = self.collections[path] = self.collection_wrapper( + self.config, path, setup_new_collection + ) + + return col + + def shutdown(self): + """Close all CollectionWrappers managed by this object.""" + for path, col in list(self.collections.items()): + del self.collections[path] + col.close() diff --git a/src/ankisyncd/collection.py b/src/ankisyncd/collection/wrapper.py similarity index 61% rename from src/ankisyncd/collection.py rename to src/ankisyncd/collection/wrapper.py index f93178a..60ba014 100644 --- a/src/ankisyncd/collection.py +++ b/src/ankisyncd/collection/wrapper.py @@ -1,12 +1,8 @@ +import os import anki.storage import ankisyncd.media -import os, errno -import logging - -logger = logging.getLogger("ankisyncd.collection") - class CollectionWrapper: """A simple wrapper around an anki.storage.Collection object. @@ -91,56 +87,3 @@ class CollectionWrapper: def opened(self): """Returns True if the collection is open, False otherwise.""" return self.__col is not None - - -class CollectionManager: - """Manages a set of CollectionWrapper objects.""" - - collection_wrapper = CollectionWrapper - - def __init__(self, config): - self.collections = {} - self.config = config - - def get_collection(self, path, setup_new_collection=None): - """Gets a CollectionWrapper for the given path.""" - - path = os.path.realpath(path) - - try: - col = self.collections[path] - except KeyError: - col = self.collections[path] = self.collection_wrapper( - self.config, path, setup_new_collection - ) - - return col - - def shutdown(self): - """Close all CollectionWrappers managed by this object.""" - for path, col in list(self.collections.items()): - del self.collections[path] - col.close() - - -def get_collection_wrapper(config, path, setup_new_collection=None): - if "collection_wrapper" in config and config["collection_wrapper"]: - logger.info( - "Found collection_wrapper in config, using {} for " - "user data persistence".format(config["collection_wrapper"]) - ) - import importlib - import inspect - - module_name, class_name = config["collection_wrapper"].rsplit(".", 1) - module = importlib.import_module(module_name.strip()) - class_ = getattr(module, class_name.strip()) - - if not CollectionWrapper in inspect.getmro(class_): - raise TypeError( - """"collection_wrapper" found in the conf file but it doesn''t - inherit from CollectionWrapper""" - ) - return class_(config, path, setup_new_collection) - else: - return CollectionWrapper(config, path, setup_new_collection)