cms.utils.django_load: 36 total statements, 0.0% covered

Generated: Wed 2013-03-13 10:33 CET

Source file: /media/Envs/Envs/filer-gallery/lib/python2.7/site-packages/cms/utils/django_load.py

Stats: 0 executed, 31 missed, 5 excluded, 52 ignored

  1. # -*- coding: utf-8 -*-
  2. """
  3. This is revision from 3058ab9d9d4875589638cc45e84b59e7e1d7c9c3 of
  4. https://github.com/ojii/django-load.
  5. ANY changes to this file, be it upstream fixes or changes for the cms *must* be
  6. documentet clearly within this file with comments.
  7. For documentation on how to use the functions described in this file, please
  8. refer to http://django-load.readthedocs.org/en/latest/index.html.
  9. """
  10. from django.conf import settings
  11. from django.utils.importlib import import_module
  12. def get_module(app, modname, verbose, failfast):
  13. """
  14. Internal function to load a module from a single app.
  15. """
  16. module_name = '%s.%s' % (app, modname)
  17. try:
  18. module = import_module(module_name)
  19. except ImportError, e:
  20. if failfast:
  21. raise e
  22. elif verbose:
  23. print "Could not load %r from %r: %s" % (modname, app, e)
  24. return None
  25. if verbose:
  26. print "Loaded %r from %r" % (modname, app)
  27. return module
  28. def load(modname, verbose=False, failfast=False):
  29. """
  30. Loads all modules with name 'modname' from all installed apps.
  31. If verbose is True, debug information will be printed to stdout.
  32. If failfast is True, import errors will not be surpressed.
  33. """
  34. for app in settings.INSTALLED_APPS:
  35. get_module(app, modname, verbose, failfast)
  36. def iterload(modname, verbose=False, failfast=False):
  37. """
  38. Loads all modules with name 'modname' from all installed apps and returns
  39. and iterator of those modules.
  40. If verbose is True, debug information will be printed to stdout.
  41. If failfast is True, import errors will not be surpressed.
  42. """
  43. for app in settings.INSTALLED_APPS:
  44. module = get_module(app, modname, verbose, failfast)
  45. if module:
  46. yield module
  47. def load_object(import_path):
  48. """
  49. Loads an object from an 'import_path', like in MIDDLEWARE_CLASSES and the
  50. likes.
  51. Import paths should be: "mypackage.mymodule.MyObject". It then imports the
  52. module up until the last dot and tries to get the attribute after that dot
  53. from the imported module.
  54. If the import path does not contain any dots, a TypeError is raised.
  55. If the module cannot be imported, an ImportError is raised.
  56. If the attribute does not exist in the module, a AttributeError is raised.
  57. """
  58. if '.' not in import_path:
  59. raise TypeError(
  60. "'import_path' argument to 'django_load.core.load_object' must "
  61. "contain at least one dot."
  62. )
  63. module_name, object_name = import_path.rsplit('.', 1)
  64. module = import_module(module_name)
  65. return getattr(module, object_name)
  66. def iterload_objects(import_paths):
  67. """
  68. Load a list of objects.
  69. """
  70. for import_path in import_paths:
  71. yield load_object(import_path)