PageRenderTime 9ms CodeModel.GetById 1ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/hyde/loader.py

http://github.com/hyde/hyde
Python | 49 lines | 37 code | 5 blank | 7 comment | 1 complexity | e539ad9af11d8a373a7452d3650b8eb2 MD5 | raw file
 1# -*- coding: utf-8 -*-
 2"""
 3Generic loader of extensions (plugins & templates)
 4"""
 5import sys
 6
 7from hyde.exceptions import HydeException
 8
 9from hyde.util import getLoggerWithNullHandler
10logger = getLoggerWithNullHandler('hyde.engine')
11
12plugins = {}
13templates = {}
14
15
16def load_python_object(name):
17    """
18    Loads a python module from string
19    """
20    (module_name, _, object_name) = name.rpartition(".")
21    if module_name == '':
22        (module_name, object_name) = (object_name, module_name)
23    try:
24        logger.debug('Loading module [%s]' % module_name)
25        module = __import__(module_name)
26    except ImportError:
27        raise HydeException("The given module name [%s] is invalid." %
28                            module_name)
29
30    if object_name == '':
31        return module
32
33    try:
34        module = sys.modules[module_name]
35    except KeyError:
36        raise HydeException("Error occured when loading module [%s]" %
37                            module_name)
38
39    try:
40        logger.debug('Getting object [%s] from module [%s]' %
41                    (object_name, module_name))
42        return getattr(module, object_name)
43    except AttributeError:
44        raise HydeException("Cannot load the specified plugin [%s]. "
45                            "The given module [%s] does not contain the "
46                            "desired object [%s]. Please fix the "
47                            "configuration or ensure that the module is "
48                            "installed properly" %
49                            (name, module_name, object_name))