__init__.py 1.0 KB

12345678910111213141516171819202122232425262728
  1. # Use of this source code is governed by a BSD-style
  2. # license that can be found in the LICENSE file.
  3. # Copyright 2019 The OSArchiver Authors. All rights reserved.
  4. """
  5. Common functions of osarchiver
  6. """
  7. from importlib import import_module
  8. def backend_factory(*args, backend='db', module=None, subclass=None, **kwargs):
  9. """
  10. This factory function rule is to return the backend instances
  11. It raises an exception on import or attribute error or unavailable backend
  12. """
  13. try:
  14. class_name = backend.capitalize()
  15. backend_module = import_module(module + '.' + backend)
  16. backend_class = getattr(backend_module, class_name)
  17. instance = backend_class(*args, **kwargs)
  18. except (AttributeError, ImportError):
  19. raise ImportError("{} is not part of our backend"
  20. " collection!".format(backend))
  21. else:
  22. if not issubclass(backend_class, subclass):
  23. raise ImportError("Unsupported '{}' destination"
  24. " backend".format(backend))
  25. return instance