source: TI12-security/trunk/NDGSecurity/python/ndg_security_common/ndg/security/common/utils/factory.py @ 7076

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg-security/TI12-security/trunk/NDGSecurity/python/ndg_security_common/ndg/security/common/utils/factory.py@7076
Revision 7076, 5.5 KB checked in by pjkersha, 10 years ago (diff)
  • Property svn:keywords set to Id
Line 
1"""
2Class Factory
3
4NERC DataGrid project
5"""
6__author__ = "Philip Kershaw"
7__date__ = "15/02/10"
8__copyright__ = "(C) 2010 Science and Technology Facilities Council"
9__license__ = "BSD - see LICENSE file in top-level directory"
10__contact__ = "Philip.Kershaw@stfc.ac.uk"
11__revision__ = '$Id$'
12import traceback
13import logging, os, sys
14log = logging.getLogger(__name__)
15
16
17def importModuleObject(moduleName, objectName=None, objectType=None):
18    '''Import from a string module name and object name.  Object can be
19    any entity contained in a module
20   
21    @param moduleName: Name of module containing the class
22    @type moduleName: str
23    @param objectName: Name of the class to import.  If none is given, the
24    class name will be assumed to be the last component of modulePath
25    @type objectName: str
26    @rtype: class object
27    @return: imported class'''
28    if objectName is None:
29        if ':' in moduleName:
30            # Support Paste style import syntax with rhs of colon denoting
31            # module content to import
32            _moduleName, objectName = moduleName.rsplit(':', 1)
33            if '.' in objectName:
34                objectName = objectName.split('.')
35        else: 
36            try:
37                _moduleName, objectName = moduleName.rsplit('.', 1)
38            except ValueError:
39                raise ValueError('Invalid module name %r set for import: %s' %
40                                 (moduleName, traceback.format_exc()))
41               
42            objectName = [objectName]
43    else:
44        _moduleName = moduleName
45        if isinstance(objectName, basestring):
46            objectName = [objectName]
47   
48    log.debug("Importing %r ..." % objectName) 
49     
50    module = __import__(_moduleName, globals(), locals(), [])
51    components = _moduleName.split('.')
52    try:
53        for component in components[1:]:
54            module = getattr(module, component)
55    except AttributeError, e:
56        raise AttributeError("Error importing %r: %s" %
57                             (objectName, traceback.format_exc()))
58
59    importedObject = module
60    for i in objectName:
61        importedObject = getattr(importedObject, i)
62
63    # Check class inherits from a base class
64    if objectType and not issubclass(importedObject, objectType):
65        raise TypeError("Specified class %r must be derived from %r; got %r" %
66                        (objectName, objectType, importedObject))
67   
68    log.info('Imported %r from module, %r', objectName, _moduleName)
69    return importedObject
70
71
72def callModuleObject(moduleName, objectName=None, moduleFilePath=None, 
73                     objectType=None, objectArgs=(), objectProperties={}):
74    '''
75    Create and return an instance of the specified class or invoke callable
76    @param moduleName: Name of module containing the class
77    @type moduleName: str
78    @param objectName: Name of the class to instantiate.  May be None in
79    which case, the class name is parsed from the moduleName last element
80    @type objectName: str
81    @param moduleFilePath: Path to the module - if unset, assume module on
82    system path already
83    @type moduleFilePath: str
84    @param objectProperties: dict of properties to use when instantiating the
85    class
86    @type objectProperties: dict
87    @param objectType: expected type for the object to instantiate - to
88    enforce use of specific interfaces
89    @type objectType: object
90    @return: object - instance of the class specified
91    '''
92
93   
94    # ensure that properties is a dict - NB, it may be passed in as a null
95    # value which can override the default val
96    if not objectProperties:
97        objectProperties = {}
98
99    # variable to store original state of the system path
100    sysPathBak = None
101    try:
102        try:
103            # Module file path may be None if the new module to be loaded
104            # can be found in the existing system path           
105            if moduleFilePath:
106                if not os.path.exists(moduleFilePath):
107                    raise IOError("Module file path '%s' doesn't exist" % 
108                                  moduleFilePath)
109                         
110                # Temporarily extend system path ready for import
111                sysPathBak = sys.path
112                         
113                sys.path.append(moduleFilePath)
114
115           
116            # Import module name specified in properties file
117            importedObject = importModuleObject(moduleName, 
118                                                objectName=objectName,
119                                                objectType=objectType)
120        finally:
121            # revert back to original sys path, if necessary
122            # NB, python requires the use of a try/finally OR a try/except
123            # block - not both combined
124            if sysPathBak:
125                sys.path = sysPathBak
126                           
127    except Exception, e:
128        log.error('%r module import raised %r type exception: %r' % 
129                  (moduleName, e.__class__, traceback.format_exc()))
130        raise 
131
132    # Instantiate class
133    log.debug('Instantiating object "%s"' % importedObject.__name__)
134    try:
135        if objectArgs:
136            object = importedObject(*objectArgs, **objectProperties)
137        else:
138            object = importedObject(**objectProperties)
139           
140        return object
141
142    except Exception, e:
143        log.error("Instantiating module object, %r: %r" % 
144                                                    (importedObject.__name__, 
145                                                     traceback.format_exc()))
146        raise
Note: See TracBrowser for help on using the repository browser.