def adapter_for_class(class_, adapter_interface):
gsm = getGlobalSiteManager()
adapter = gsm.adapters.registered((implementedBy(class_),), adapter_interface)
if adapter:
return adapter
# recurse up the inheritence tree to find an adapter, if we don't have one
# registered for this class directly.
for base_class in class_.__bases__:
return adapter_for_class(base_class, adapter_interface)
python类implementedBy()的实例源码
def __call__(self):
result = {'type': self.field_type}
for schema in implementedBy(self.field.__class__).flattened():
self.field_attributes.update(getFields(schema))
for attribute_name in sorted(self.field_attributes.keys()):
attribute_field = self.field_attributes[attribute_name]
if attribute_name in self.filtered_attributes:
continue
element_name = attribute_field.__name__
attribute_field = attribute_field.bind(self.field)
force = (element_name in self.forced_fields)
value = attribute_field.get(self.field)
# For 'default', 'missing_value' etc, we want to validate against
# the imported field type itself, not the field type of the
# attribute
if element_name in self.field_type_attributes or \
element_name in self.non_validated_field_type_attributes:
attribute_field = self.field
text = None
if isinstance(value, bytes):
text = value.decode('utf-8')
elif isinstance(value, str):
text = value
elif IField.providedBy(value):
serializer = getMultiAdapter(
(value, self.field, self.request),
ISchemaFieldSerializeToJson)
text = serializer()
elif value is not None and (force or value != self.field.missing_value):
text = IValueToJson(value)
# handle i18n
# if isinstance(value, Message):
# child.set(ns('domain', I18N_NAMESPACE), value.domain)
# if not value.default:
# child.set(ns('translate', I18N_NAMESPACE), '')
# else:
# child.set(ns('translate', I18N_NAMESPACE), child.text)
# child.text = converter.toUnicode(value.default)
if text:
if attribute_name == 'value_type':
attribute_name = 'items'
result[attribute_name] = text
if result['type'] == 'object':
if isinstance(self.field.schema, dict):
result['properties'] = self.field.schema
else:
schema_serializer = getMultiAdapter((self.field.schema, self.request),
ISchemaSerializeToJson)
result['properties'] = schema_serializer()
return result
def adapter(_context, factory, provides=None, for_=None, name=''):
if for_ is None:
if len(factory) == 1:
for_ = adaptedBy(factory[0])
if for_ is None:
raise TypeError("No for attribute was provided and can't "
"determine what the factory adapts.")
for_ = tuple(for_)
if provides is None:
if len(factory) == 1:
p = list(implementedBy(factory[0]))
if len(p) == 1:
provides = p[0]
if provides is None:
raise TypeError("Missing 'provides' attribute")
if name == '':
if len(factory) == 1:
name = getName(factory[0])
# Generate a single factory from multiple factories:
factories = factory
if len(factories) == 1:
factory = factories[0]
elif len(factories) < 1:
raise ComponentConfigurationError("No factory specified")
elif len(factories) > 1 and len(for_) != 1:
raise ComponentConfigurationError(
"Can't use multiple factories and multiple for")
else:
factory = _rolledUpFactory(factories)
_context.action(
discriminator=('adapter', for_, provides, name),
callable=handler,
args=('registerAdapter', factory, for_, provides, name))
_context.action(
discriminator=None,
callable=provide_interface,
args=('', provides))
if for_:
for iface in for_:
if iface is not None:
_context.action(
discriminator=None,
callable=provide_interface,
args=('', iface))
def test_classImplements_multiple_w_explict_implements(self):
from zope.interface import Interface
from zope.interface import implementedBy
from zope.interface import providedBy
class ILeft(Interface):
def method():
pass
class IRight(ILeft):
pass
class IOther(Interface):
pass
class Left():
__implemented__ = ILeft
def method(self):
pass
class Right(object):
__implemented__ = IRight
class Other(object):
__implemented__ = IOther
class Mixed(Left, Right):
__implemented__ = Left.__implemented__, Other.__implemented__
mixed = Mixed()
self.assertTrue(ILeft.implementedBy(Mixed))
self.assertFalse(IRight.implementedBy(Mixed))
self.assertTrue(IOther.implementedBy(Mixed))
self.assertTrue(ILeft in implementedBy(Mixed))
self.assertFalse(IRight in implementedBy(Mixed))
self.assertTrue(IOther in implementedBy(Mixed))
self.assertTrue(ILeft in providedBy(mixed))
self.assertFalse(IRight in providedBy(mixed))
self.assertTrue(IOther in providedBy(mixed))
def test_classImplements_multiple_w_explict_implements(self):
from zope.interface import Interface
from zope.interface import implementedBy
from zope.interface import providedBy
class ILeft(Interface):
def method():
pass
class IRight(ILeft):
pass
class IOther(Interface):
pass
class Left():
__implemented__ = ILeft
def method(self):
pass
class Right(object):
__implemented__ = IRight
class Other(object):
__implemented__ = IOther
class Mixed(Left, Right):
__implemented__ = Left.__implemented__, Other.__implemented__
mixed = Mixed()
self.assertTrue(ILeft.implementedBy(Mixed))
self.assertFalse(IRight.implementedBy(Mixed))
self.assertTrue(IOther.implementedBy(Mixed))
self.assertTrue(ILeft in implementedBy(Mixed))
self.assertFalse(IRight in implementedBy(Mixed))
self.assertTrue(IOther in implementedBy(Mixed))
self.assertTrue(ILeft in providedBy(mixed))
self.assertFalse(IRight in providedBy(mixed))
self.assertTrue(IOther in providedBy(mixed))