您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

loading.py 3.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. from exceptions import Exception
  2. from imp import new_module
  3. from django.conf import settings
  4. class AppNotFoundError(Exception):
  5. pass
  6. def import_module(module_label, classes=[], namespace=None):
  7. u"""
  8. For dynamically importing classes from a module.
  9. Eg. calling import_module('product.models') will search INSTALLED_APPS for
  10. the relevant product app (default is 'oscar.product') and then import the
  11. classes from there. If the class can't be found in the overriding module,
  12. then we attempt to import it from within oscar.
  13. We search the INSTALLED_APPS list to find the appropriate app string and
  14. import that.
  15. This is very similar to django.db.models.get_model although that is only
  16. for loading models while this method will load any class.
  17. """
  18. # Classes must be specified in order for __import__ to work correctly. It's
  19. # also a good practice
  20. if not classes:
  21. raise ValueError("You must specify the classes to import")
  22. # Arguments will be things like 'product.models' and so we
  23. # we take the first component to find within the INSTALLED_APPS list.
  24. app_name = module_label.rsplit(".", 1)[0]
  25. for installed_app in settings.INSTALLED_APPS:
  26. base_package = installed_app.split(".")[0]
  27. module_name = installed_app.split(".", 2).pop() # strip oscar.apps
  28. try:
  29. # We search the second component of the installed apps
  30. if app_name == module_name:
  31. if base_package == 'oscar':
  32. # Using core module explicitly
  33. return _import_classes_from_module("oscar.apps.%s" % module_label, classes, namespace)
  34. else:
  35. # Using local override - check that requested module exists
  36. local_app = "%s.%s" % (base_package, module_label)
  37. try:
  38. imported_local_mod = __import__(local_app, fromlist=classes)
  39. except ImportError, e:
  40. # Module doesn't exist, fall back to oscar core. This can be tricky
  41. # as if the overriding module has an import error, it will get picked up
  42. # here.
  43. if str(e).startswith("No module named"):
  44. return _import_classes_from_module("oscar.apps.%s" % module_label, classes, namespace)
  45. raise e
  46. # Found overriding module, merging custom classes with core
  47. module = new_module(local_app)
  48. imported_oscar_mod = __import__("oscar.apps.%s" % module_label, fromlist=classes)
  49. for classname in classes:
  50. if hasattr(imported_local_mod, classname):
  51. if namespace:
  52. namespace[classname] = getattr(imported_local_mod, classname)
  53. else:
  54. module.__setattr__(classname, getattr(imported_local_mod, classname))
  55. else:
  56. if namespace:
  57. namespace[classname] = getattr(imported_oscar_mod, classname)
  58. else:
  59. module.__setattr__(classname, getattr(imported_oscar_mod, classname))
  60. return module
  61. except IndexError:
  62. pass
  63. raise AppNotFoundError("Unable to find an app matching %s in INSTALLED_APPS" % (app_name,))
  64. def _import_classes_from_module(module_name, classes, namespace):
  65. imported_module = __import__(module_name, fromlist=classes)
  66. if namespace:
  67. for classname in classes:
  68. namespace[classname] = getattr(imported_module, classname)
  69. return
  70. module = new_module(module_name)
  71. for classname in classes:
  72. setattr(module, classname, getattr(imported_module, classname))
  73. return module