ClassRegistry.py 6.93 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#MDANSE : Molecular Dynamics Analysis for Neutron Scattering Experiments
#------------------------------------------------------------------------------------------
#Copyright (C)
#2015- Eric C. Pellegrini Institut Laue-Langevin
#BP 156
#6, rue Jules Horowitz
#38042 Grenoble Cedex 9
#France
#pellegrini[at]ill.fr
#goret[at]ill.fr
#aoun[at]ill.fr
#
#This library is free software; you can redistribute it and/or
#modify it under the terms of the GNU Lesser General Public
#License as published by the Free Software Foundation; either
#version 2.1 of the License, or (at your option) any later version.
#
#This library is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#Lesser General Public License for more details.
#
#You should have received a copy of the GNU Lesser General Public
#License along with this library; if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

''' 
Created on Mar 30, 2015

@author: pellegrini
'''

eric pellegrini's avatar
test    
eric pellegrini committed
33
import abc
34
import glob
35
36
import inspect
import os
37
import sys
eric pellegrini's avatar
test    
eric pellegrini committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

class _Meta(type):
    '''
    Metaclass that allows to use the __getitem__ method at a class level for the class that has been built.
     
    The class that uses this metaclass must define a class attribute named _registry that will be used
    by the __getitem__ method.
    '''
         
    def __getitem__(self, item):
        """
        Returns a given item stored in the class registry
        """
         
        return self._registry[item]
    
class ClassRegistry(abc.ABCMeta):
    '''
56
    Metaclass that registers the subclasses of all the base classes used in MDANSE framework.
eric pellegrini's avatar
test    
eric pellegrini committed
57

58
59
60
    The subclasses are stored internally in a nested dictionary whose primary key 
    is the 'type' class attribute of the base class they are inheriting from and secondary key is 
    their own 'type' class attribute.
eric pellegrini's avatar
test    
eric pellegrini committed
61
62
63
64
65
66
67
68
69
70
71
72
73
    '''
    
    __metaclass__ = _Meta

    __interfaces = []

    _registry = {}
                
    def __init__(self, name, bases, namespace):
        '''
        Constructor of a class metaclassed by ClassFactory
        
        :param name: the name of the class to be built by this metaclass
74
        :type name: str
eric pellegrini's avatar
test    
eric pellegrini committed
75
        :param bases: the base classes of the class to be built by this metaclass
76
        :type bases: tuple 
eric pellegrini's avatar
test    
eric pellegrini committed
77
        :param namespace: the attributes and methods of the class to be built by this metaclass
78
        :type namespace: dict
eric pellegrini's avatar
test    
eric pellegrini committed
79
80
81
82
83
84
85
86
87
88
89
        '''
        
        super(ClassRegistry, self).__init__(name, bases, namespace)
        
        # Get the typ of the class
        typ = getattr(self, 'type', None)
                
        if typ is None:
            return

        metaClass = namespace.get("__metaclass__", None)
90
                              
eric pellegrini's avatar
test    
eric pellegrini committed
91
        if metaClass is ClassRegistry:
92
            ClassRegistry.__interfaces.append(self)
eric pellegrini's avatar
eric pellegrini committed
93
94
            if (ClassRegistry._registry.has_key(typ)):
                return
eric pellegrini's avatar
test    
eric pellegrini committed
95
96
97
98
99
100
101
102
            ClassRegistry._registry[typ] = {}

        else:
                                
            for interface in ClassRegistry.__interfaces:
                if issubclass(self, interface):
                    ClassRegistry._registry[interface.type][typ] = self
                    break
103
104
105
106
107
108
109
110
111
112
113
114
115
          
    @classmethod      
    def update_registry(cls,packageDir):
        '''
        Update the classes registry by importing all the modules contained in a given package.
        
        Only the classes that are metaclassed by ClassRegistry will be registered.
        
        :param cls: the ClassRegistry instance
        :type: ClassRegistry
        :param packageDir: the package for which all modules should be imported
        :type packageDir: str
        '''
116
                
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
        for module in glob.glob(os.path.join(packageDir,'*.py')):
                             
            moduleDir, moduleFile = os.path.split(module)
     
            if moduleFile == '__init__.py':
                continue
     
            moduleName, moduleExt = os.path.splitext(moduleFile)
            
            print moduleName
    
            if moduleDir not in sys.path:        
                sys.path.append(moduleDir)
                      
            # Any error that may occur here has to be caught. In such case the module is skipped.    
            try:
                __import__(moduleName, locals(), globals())
            except:
                continue
    
eric pellegrini's avatar
test    
eric pellegrini committed
137
138
139
140
141
    @classmethod
    def info(cls, interface):
        '''
        Returns informations about the subclasses of a given base class  stored in the registry.
        
142
        :param cls: the ClassRegistry instance
143
        :type cls: ClassRegistry
eric pellegrini's avatar
test    
eric pellegrini committed
144
        :param interface: the name of base class of whom information about its subclasses is requested
145
        :type interface: str
eric pellegrini's avatar
test    
eric pellegrini committed
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
        '''
                
        if not cls._registry.has_key(interface):
            return "The interface " + interface + " is not registered"

        # Dictionnay whose keys are the package names and values and list of (job name, job path) stored in the corresponding package. 
        packages = {}

        # Loop over the registry items.
        for k, v in cls._registry[interface].items():
            
            # Get the module corresponding to the job class.
            mod = inspect.getmodule(v)

            # The package hosting the module.
            modPackage = mod.__package__
        
            # The module file.
            modFilename = mod.__file__
        
            # If no package could be found, guess a name using the directory name of the module file.
            if modPackage is None:
                modPackage = os.path.split(os.path.dirname(modFilename))[1]
169
170
                
            modPackage = modPackage.split(".")[-1]
eric pellegrini's avatar
test    
eric pellegrini committed
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
        
            # Update the packages dictionary.
            if packages.has_key(modPackage):
                packages[modPackage].append([k, v.__name__])
            else:
                packages[modPackage] = [[k, v.__name__]]
    
        contents = []
        
        # Print the contents of the packages dictionary.
        contents.append("="*130)
        contents.append("%-50s %-40s %-s" % ("Package", "Name", "Class"))
        contents.append("="*130)
        for k, v in sorted(packages.items()):
            for vv in sorted(v): 
                contents.append("%-50s %-40s %-s" % (k, vv[0], vv[1]))
            contents.append('-' * 130)
        
        contents = "\n".join(contents)
    
        return contents

193
194
195
196
197
198
199
200
201
202
    @classmethod
    def get_interfaces(cls):
        '''
        Returns the interfaces that are currently registered.
        
        :param cls: the ClassRegsitry instance
        :type cls: ClassRegistry
        '''
        
        return sorted(cls._registry.keys())