Browse Source

Added metaclass to get a list of child Class from Plugin Base class

m.orban 8 years ago
parent
commit
22eae9d5ec
1 changed files with 30 additions and 1 deletions
  1. 30
    1
      lodel/plugin/plugins.py

+ 30
- 1
lodel/plugin/plugins.py View File

@@ -148,6 +148,35 @@ to generic PluginVersion comparison function : '%s'" % cmp_fun_name)
148 148
         return {'major': self.major, 'minor': self.minor,
149 149
             'revision': self.revision}
150 150
 
151
+##@brief Stores plugin class registered
152
+__all_ptypes = list()
153
+
154
+##@brief Plugin metaclass that allows to "catch" child class
155
+#declaration
156
+#
157
+#Automatic script registration on child class declaration
158
+class MetaPlugType(type):
159
+    
160
+    def __init__(self, name, bases, attrs):
161
+        #Here we can store all child classes of Plugin
162
+        super().__init__(name, bases, attrs)
163
+        if len(bases) == 1 and bases[0] == object:
164
+            print("Dropped : ", name, bases)
165
+            return
166
+        list_name= [cls._name__ for cls in plugin_types(self)] 
167
+        if self.name is in list_name:
168
+            return
169
+        else:
170
+            plug_type_register(self)
171
+
172
+def plug_type_register(cls):
173
+    __all__ptypes.append(cls)
174
+    logger.info("New child class registered : %s" % cls.__name__)
175
+
176
+##@brief Return a list of child Class Plugin
177
+@classmethod
178
+def plugin_types(cls):
179
+    return cls.__all_ptypes
151 180
 
152 181
 ##@brief Handle plugins
153 182
 #
@@ -158,7 +187,7 @@ to generic PluginVersion comparison function : '%s'" % cmp_fun_name)
158 187
 # 1. Settings call start method to instanciate all plugins found in confs
159 188
 # 2. Settings fetch all confspecs
160 189
 # 3. the loader call load_all to register hooks etc
161
-class Plugin(object):
190
+class Plugin(object, metaclass=MetaPlugType):
162 191
     
163 192
     ##@brief Stores plugin directories paths
164 193
     _plugin_directories = None

Loading…
Cancel
Save