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

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/NDGSecurity/python/ndg_security_common/ndg/security/common/utils/factory.py@6686
Revision 6686, 5.3 KB checked in by pjkersha, 10 years ago (diff)

Refactoring Attribute Authority to remove NDG Attribute Certificate and role mapping code.

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            _moduleName, objectName = moduleName.rsplit('.', 1)
37            objectName = [objectName]
38    else:
39        _moduleName = moduleName
40        if isinstance(objectName, basestring):
41            objectName = [objectName]
42   
43    log.debug("Importing %r ..." % objectName) 
44     
45    module = __import__(_moduleName, globals(), locals(), [])
46    components = _moduleName.split('.')
47    try:
48        for component in components[1:]:
49            module = getattr(module, component)
50    except AttributeError, e:
51        raise AttributeError("Error importing %r: %s" %
52                             (objectName, traceback.format_exc()))
53
54    importedObject = module
55    for i in objectName:
56        importedObject = getattr(importedObject, i)
57
58    # Check class inherits from a base class
59    if objectType and not issubclass(importedObject, objectType):
60        raise TypeError("Specified class %r must be derived from %r; got %r" %
61                        (objectName, objectType, importedObject))
62   
63    log.info('Imported %r from module, %r', objectName, _moduleName)
64    return importedObject
65
66
67def callModuleObject(moduleName, objectName=None, moduleFilePath=None, 
68                     objectType=None, objectArgs=(), objectProperties={}):
69    '''
70    Create and return an instance of the specified class or invoke callable
71    @param moduleName: Name of module containing the class
72    @type moduleName: str
73    @param objectName: Name of the class to instantiate.  May be None in
74    which case, the class name is parsed from the moduleName last element
75    @type objectName: str
76    @param moduleFilePath: Path to the module - if unset, assume module on
77    system path already
78    @type moduleFilePath: str
79    @param objectProperties: dict of properties to use when instantiating the
80    class
81    @type objectProperties: dict
82    @param objectType: expected type for the object to instantiate - to
83    enforce use of specific interfaces
84    @type objectType: object
85    @return: object - instance of the class specified
86    '''
87
88   
89    # ensure that properties is a dict - NB, it may be passed in as a null
90    # value which can override the default val
91    if not objectProperties:
92        objectProperties = {}
93
94    # variable to store original state of the system path
95    sysPathBak = None
96    try:
97        try:
98            # Module file path may be None if the new module to be loaded
99            # can be found in the existing system path           
100            if moduleFilePath:
101                if not os.path.exists(moduleFilePath):
102                    raise IOError("Module file path '%s' doesn't exist" % 
103                                  moduleFilePath)
104                         
105                # Temporarily extend system path ready for import
106                sysPathBak = sys.path
107                         
108                sys.path.append(moduleFilePath)
109
110           
111            # Import module name specified in properties file
112            importedObject = importModuleObject(moduleName, 
113                                                objectName=objectName,
114                                                objectType=objectType)
115        finally:
116            # revert back to original sys path, if necessary
117            # NB, python requires the use of a try/finally OR a try/except
118            # block - not both combined
119            if sysPathBak:
120                sys.path = sysPathBak
121                           
122    except Exception, e:
123        log.error('%r module import raised %r type exception: %r' % 
124                  (moduleName, e.__class__, traceback.format_exc()))
125        raise 
126
127    # Instantiate class
128    log.debug('Instantiating object "%s"' % importedObject.__name__)
129    try:
130        if objectArgs:
131            object = importedObject(*objectArgs, **objectProperties)
132        else:
133            object = importedObject(**objectProperties)
134           
135        return object
136
137    except Exception, e:
138        log.error("Instantiating module object, %r: %r" % 
139                                                    (importedObject.__name__, 
140                                                     traceback.format_exc()))
141        raise
Note: See TracBrowser for help on using the repository browser.