Move variables in check_job_status to prevent undeclared error
[ganeti_webmgr.git] / muddle / core / apps / plugins.py
blobd2e86a06606da4092f6d23c9b0619fabc61f1dd4
1 import inspect
3 from django.conf import settings
5 from muddle.util import path_to_class
8 def load_app_plugin(module_name, Klass=None, method=None):
9 """
10 Helper method for loading an app plugin. An app plugin is defined as some
11 form of plugin that uses a form of registration logic that other apps must
12 use to register their usage of the plugin.
14 Some examples built into django:
15 * models found within models.py
16 * admin classes within admin.py
18 @param module_name: module name to load. This is a string,
19 @param klass: class type(s) to load. This may be a single class or a list
20 of classes.
21 @param method: method to run passing in each class that is found, or the
22 module if no classes are specified
23 """
25 for app in settings.INSTALLED_APPS:
26 try:
27 module = path_to_class('.'.join([app, module_name]))
29 if Klass is not None:
30 assert(callable(method))
32 for name in dir(module):
33 # run the method for each member
34 obj = getattr(module, name)
35 if inspect.isclass(obj) and issubclass(obj, (Klass,)):
36 method(obj)
38 elif callable(method):
39 # no classes specified, but there was a method. execute the method
40 # with the module
41 method(module)
43 else:
44 # no class or method, importing the module is enough
45 pass
46 except ImportError:
47 # not all apps will have the module we are looking for
48 pass