Automatically register all provider classes.
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 5 Jul 2014 22:05:24 +0000 (00:05 +0200)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 5 Jul 2014 22:05:24 +0000 (00:05 +0200)
src/ddns/__init__.py
src/ddns/providers.py

index d14097d..78009e3 100644 (file)
@@ -28,7 +28,8 @@ from i18n import _
 logger = logging.getLogger("ddns.core")
 logger.propagate = 1
 
-from .providers import *
+import providers
+
 from .system import DDNSSystem
 
 # Setup the logger.
@@ -61,8 +62,10 @@ class DDNSCore(object):
                self.settings = {}
 
                # Dict with all providers, that are supported.
-               self.providers = {}
-               self.register_all_providers()
+               self.providers = providers.get()
+
+               for handle, provider in sorted(self.providers.items()):
+                       logger.debug("Registered new provider: %s (%s)" % (provider.name, provider.handle))
 
                # List of configuration entries.
                self.entries = []
@@ -70,49 +73,6 @@ class DDNSCore(object):
                # Add the system class.
                self.system = DDNSSystem(self)
 
-       def register_provider(self, provider):
-               """
-                       Registers a new provider.
-               """
-               assert issubclass(provider, DDNSProvider)
-
-               if not all((provider.handle, provider.name, provider.website)):
-                       raise DDNSError(_("Provider is not properly configured"))
-
-               assert not self.providers.has_key(provider.handle), \
-                       "Provider '%s' has already been registered" % provider.handle
-
-               logger.debug("Registered new provider: %s (%s)" % (provider.name, provider.handle))
-               self.providers[provider.handle] = provider
-
-       def register_all_providers(self):
-               """
-                       Simply registers all providers.
-               """
-               for provider in (
-                       DDNSProviderAllInkl,
-                       DDNSProviderDHS,
-                       DDNSProviderDNSpark,
-                       DDNSProviderDtDNS,
-                       DDNSProviderDynDNS,
-                       DDNSProviderDynU,
-                       DDNSProviderEasyDNS,
-                       DDNSProviderFreeDNSAfraidOrg,
-                       DDNSProviderNamecheap,
-                       DDNSProviderNOIP,
-                       DDNSProviderLightningWireLabs,
-                       DDNSProviderOVH,
-                       DDNSProviderRegfish,
-                       DDNSProviderSelfhost,
-                       DDNSProviderSPDNS,
-                       DDNSProviderStrato,
-                       DDNSProviderTwoDNS,
-                       DDNSProviderUdmedia,
-                       DDNSProviderVariomedia,
-                       DDNSProviderZoneedit,
-               ):
-                       self.register_provider(provider)
-
        def get_provider_names(self):
                """
                        Returns a list of names of all registered providers.
index 91e0227..ed893a4 100644 (file)
@@ -31,6 +31,14 @@ from .errors import *
 logger = logging.getLogger("ddns.providers")
 logger.propagate = 1
 
+_providers = {}
+
+def get():
+       """
+               Returns a dict with all automatically registered providers.
+       """
+       return _providers.copy()
+
 class DDNSProvider(object):
        # A short string that uniquely identifies
        # this provider.
@@ -48,6 +56,24 @@ class DDNSProvider(object):
 
        DEFAULT_SETTINGS = {}
 
+       # Automatically register all providers.
+       class __metaclass__(type):
+               def __init__(provider, name, bases, dict):
+                       type.__init__(provider, name, bases, dict)
+
+                       # The main class from which is inherited is not registered
+                       # as a provider.
+                       if name == "DDNSProvider":
+                               return
+
+                       if not all((provider.handle, provider.name, provider.website)):
+                               raise DDNSError(_("Provider is not properly configured"))
+
+                       assert not _providers.has_key(provider.handle), \
+                               "Provider '%s' has already been registered" % provider.handle
+
+                       _providers[provider.handle] = provider
+
        def __init__(self, core, **settings):
                self.core = core