]> git.ipfire.org Git - collecty.git/blobdiff - src/collecty/plugins/base.py
Change DataSources to be called Plugins
[collecty.git] / src / collecty / plugins / base.py
index 8f67a3c7e9bc69c465bd8e8745f50ada065360b5..82b0982b521a9de371aae4dd53155306d36c976a 100644 (file)
@@ -31,6 +31,14 @@ import time
 from ..constants import *
 from ..i18n import _
 
+_plugins = {}
+
+def get():
+       """
+               Returns a list with all automatically registered plugins.
+       """
+       return _plugins.values()
+
 class Timer(object):
        def __init__(self, timeout, heartbeat=1):
                self.timeout = timeout
@@ -63,7 +71,7 @@ class Timer(object):
                return self.elapsed > self.timeout
 
 
-class DataSource(threading.Thread):
+class Plugin(threading.Thread):
        # The name of this plugin.
        name = None
 
@@ -85,6 +93,22 @@ class DataSource(threading.Thread):
        # The default interval of this plugin.
        default_interval = 60
 
+       # Automatically register all providers.
+       class __metaclass__(type):
+               def __init__(plugin, name, bases, dict):
+                       type.__init__(plugin, name, bases, dict)
+
+                       # The main class from which is inherited is not registered
+                       # as a plugin.
+                       if name == "Plugin":
+                               return
+
+                       if not all((plugin.name, plugin.description)):
+                               raise RuntimeError(_("Plugin is not properly configured: %s") \
+                                       % plugin)
+
+                       _plugins[plugin.name] = plugin
+
        def __init__(self, collecty, **kwargs):
                threading.Thread.__init__(self, name=self.description)
                self.daemon = True