plugable: simplify API plugin initialization code

Use a flat dictionary to track plugins in API rather than nested
dictionaries.

https://fedorahosted.org/freeipa/ticket/4739

Reviewed-By: David Kupka <dkupka@redhat.com>
This commit is contained in:
Jan Cholasta 2016-05-25 12:50:10 +02:00
parent ed4c2d9252
commit 19cf6e9509

View File

@ -574,16 +574,18 @@ class API(ReadOnly):
raise TypeError('plugin must be a class; got %r' % klass) raise TypeError('plugin must be a class; got %r' % klass)
# Find the base class or raise SubclassError: # Find the base class or raise SubclassError:
found = False
for base in self.bases: for base in self.bases:
if not issubclass(klass, base): if issubclass(klass, self.bases):
continue break
else:
sub_d = self.__plugins.setdefault(base, {}) raise errors.PluginSubclassError(
found = True plugin=klass,
bases=self.bases,
)
# Check override: # Check override:
if klass.__name__ in sub_d: prev = self.__plugins.get(klass.__name__)
if prev:
if not override: if not override:
# Must use override=True to override: # Must use override=True to override:
raise errors.PluginOverrideError( raise errors.PluginOverrideError(
@ -601,13 +603,7 @@ class API(ReadOnly):
) )
# The plugin is okay, add to sub_d: # The plugin is okay, add to sub_d:
sub_d[klass.__name__] = klass self.__plugins[klass.__name__] = klass
if not found:
raise errors.PluginSubclassError(
plugin=klass,
bases=self.bases,
)
def finalize(self): def finalize(self):
""" """
@ -625,10 +621,11 @@ class API(ReadOnly):
for base in self.bases: for base in self.bases:
name = base.__name__ name = base.__name__
sub_d = self.__plugins.get(base, {})
members = [] members = []
for klass in sub_d.values():
for klass in self.__plugins.values():
if not issubclass(klass, base):
continue
try: try:
instance = plugins[klass] instance = plugins[klass]
except KeyError: except KeyError: