53: Changed plugable.Registar so the same plugin can be added to in the ns for more than one base (for cmd and mthd)

This commit is contained in:
Jason Gerard DeRose 2008-08-05 23:34:59 +00:00
parent 159207514f
commit f31f7813fe
3 changed files with 24 additions and 19 deletions

2
ipa
View File

@ -52,7 +52,7 @@ def print_api():
for i in ns: for i in ns:
print ' %s %r' % (i.name.ljust(m), i) print ' %s %r' % (i.name.ljust(m), i)
for n in ['cmd', 'obj', 'prop']: for n in ['cmd', 'obj', 'mthd', 'prop']:
print_ns(n) print_ns(n)
print '' print ''

View File

@ -236,9 +236,12 @@ class Registrar(object):
base; otherwise raises SubclassError. base; otherwise raises SubclassError.
""" """
assert inspect.isclass(cls) assert inspect.isclass(cls)
found = False
for base in self.__allowed: for base in self.__allowed:
if issubclass(cls, base): if issubclass(cls, base):
return base found = True
yield base
if not found:
raise errors.SubclassError(cls, self.__allowed) raise errors.SubclassError(cls, self.__allowed)
def __call__(self, cls, override=False): def __call__(self, cls, override=False):
@ -248,14 +251,14 @@ class Registrar(object):
if not inspect.isclass(cls): if not inspect.isclass(cls):
raise TypeError('plugin must be a class: %r' % cls) raise TypeError('plugin must be a class: %r' % cls)
# Find the base class or raise SubclassError:
base = self.__findbase(cls)
sub_d = self.__d[base.__name__]
# Raise DuplicateError if this exact class was already registered: # Raise DuplicateError if this exact class was already registered:
if cls in self.__registered: if cls in self.__registered:
raise errors.DuplicateError(cls) raise errors.DuplicateError(cls)
# Find the base class or raise SubclassError:
for base in self.__findbase(cls):
sub_d = self.__d[base.__name__]
# Check override: # Check override:
if cls.__name__ in sub_d: if cls.__name__ in sub_d:
# Must use override=True to override: # Must use override=True to override:
@ -266,10 +269,12 @@ class Registrar(object):
if override: if override:
raise errors.MissingOverrideError(base, cls) raise errors.MissingOverrideError(base, cls)
# The plugin is okay, add to __registered and sub_d: # The plugin is okay, add to sub_d:
self.__registered.add(cls)
sub_d[cls.__name__] = cls sub_d[cls.__name__] = cls
# The plugin is okay, add to __registered:
self.__registered.add(cls)
def __getitem__(self, item): def __getitem__(self, item):
""" """
Returns a copy of the namespace dict of the base class named `name`. Returns a copy of the namespace dict of the base class named `name`.

View File

@ -99,7 +99,7 @@ class PublicAPI(plugable.API):
__max_cmd_len = None __max_cmd_len = None
def __init__(self): def __init__(self):
super(PublicAPI, self).__init__(cmd, obj, prop) super(PublicAPI, self).__init__(cmd, obj, mthd, prop)
def __get_max_cmd_len(self): def __get_max_cmd_len(self):
if self.__max_cmd_len is None: if self.__max_cmd_len is None: