def test_adapter_registry(): info1 = adapter_registry.queryMultiAdapter([], IContactInfo) assert info1 assert zope.interface.verify.verifyObject(IContactInfo, info1) pt1 = adapter_registry.queryMultiAdapter([], IPoint) assert pt1 assert zope.interface.verify.verifyObject(IPoint, pt1) cls = adapter_registry.lookup([], IContactInfo) assert cls assert zope.interface.verify.verifyClass(IContactInfo, cls)
def serializer_for_key_tuple(key_prefix=None): '''Get a proper serializer for the tuple-typed keys of a dict. ''' serializer = adapter_registry.queryMultiAdapter([], IKeyTupleSerializer, 'serialize-key') if key_prefix is not None: serializer.prefix = key_prefix return serializer
def serializer_for_field(field): '''Get an XML serializer for a zope.schema.Field instance. ''' assert isinstance(field, zope.schema.Field) serializer = adapter_registry.queryMultiAdapter([field], IXmlSerializer, 'serialize-xml') return serializer
def formatter_for_field(field, name=''): '''Get a proper formatter for a zope.schema.Field instance. ''' assert isinstance(field, zope.schema.Field) # Build adaptee vector adaptee = build_adaptee(field, expand_collection=True) # Lookup candidates = ['format'] if name: candidates.insert(0, 'format:%s' % (name)) formatter = None while adaptee: for candidate in candidates: formatter = adapter_registry.queryMultiAdapter( adaptee, IFormatter, candidate) if formatter: break if formatter: break # Fallback to a more general version of this adaptee adaptee.pop() if formatter: formatter.requested_name = name return formatter
def widget_for_object(qualified_action, obj, errors={}): '''Find and instantiate a widget to adapt an object to a widget interface. ''' # Build an array with all candidate names q = qualified_action if isinstance(q, basestring): q = QualAction.from_string(q) candidates = list(reversed(q.parents() + [q])) # Lookup registry widget = None for candidate in candidates: name = candidate.to_string() widget = adapter_registry.queryMultiAdapter([obj], IObjectWidget, name) logger.debug('Trying to adapt [%s] to widget for action %s: %r', type(obj).__name__, name, widget if widget else None) if widget: break if widget: widget.context = LookupContext(requested=q, provided=candidate) widget.errors = errors else: raise WidgetNotFound(qualified_action, obj) # Found a widget to adapt to obj assert zope.interface.verify.verifyObject(IObjectWidget, widget) return widget
def widget_for_object(qualified_action, obj, errors={}): '''Find and instantiate a widget to adapt an object to a widget interface. ''' # Build an array with all candidate names q = qualified_action if isinstance(q, basestring): q = QualAction.from_string(q) candidates = list(reversed(q.parents() + [q])) # Lookup registry widget = None for candidate in candidates: name = candidate.to_string() widget = adapter_registry.queryMultiAdapter([obj], IObjectWidget, name) #logger.debug('Trying to adapt [%s] to widget for action %s: %r', # type(obj).__name__, name, # widget if widget else None) if widget: break if widget: widget.context = LookupContext(requested=q, provided=candidate) widget.errors = errors else: raise WidgetNotFound(qualified_action, obj) # Found a widget to adapt to obj assert zope.interface.verify.verifyObject(IObjectWidget, widget) return widget
def serializer_for_object(obj): '''Get an XML serializer for an IObject object. ''' assert IObject.providedBy(obj) serializer = adapter_registry.queryMultiAdapter([obj], IXmlSerializer, 'serialize-xml') return serializer
def serializer_for_object(obj, name='default'): '''Get a proper serializer for an Object instance. ''' assert name in serializers.supported_formats serializer = adapter_registry.queryMultiAdapter([obj], ISerializer, 'serialize:%s' % (name)) return serializer
def serializer_for_field(field, name='default'): '''Get a proper serializer for a zope.schema.Field instance. Normally, this will be used for leaf (non collection-based) fields. ''' assert isinstance(field, zope.schema.Field) assert name in supported_formats serializer = adapter_registry.queryMultiAdapter( [field], ISerializer, 'serialize:%s' %(name)) return serializer
def translator_for_field(field, source_language, field_translation): assert isinstance(field, Field) verifyObject(IFieldContext, field.context) if source_language != field_translation.source_language: raise ValueError( 'The field translation is built for a different source language') name = 'translate' translator = adapter_registry.queryMultiAdapter( [field, Language(source_language), field_translation], ITranslator, name) return translator
def formatter_for_object(obj, name=''): '''Get a proper formatter for an object instance. ''' candidates = ['format'] if name: candidates.insert(0, 'format:%s' % (name)) for candidate in candidates: formatter = adapter_registry.queryMultiAdapter([obj], IFormatter, candidate) if formatter: break if formatter: formatter.requested_name = name return formatter
def widget_for_field(qualified_action, field, errors={}): '''Find and instantiate a widget to adapt a field to a widget interface. The given field should be a bound instance of zope.schema.Field. ''' # Build a list with candidate names q = qualified_action if isinstance(q, basestring): q = QualAction.from_string(q) candidates = list(reversed(q.parents() + [q])) # Build adaptee vector adaptee = build_adaptee(field, expand_collection=True) # Lookup registry widget = None while len(adaptee): for candidate in candidates: name = candidate.to_string() widget = adapter_registry.queryMultiAdapter( adaptee, IFieldWidget, name) logger.debug('Trying to adapt [%s] to widget for action %s: %r', ', '.join([type(x).__name__ for x in adaptee]), name, widget if widget else None) if widget: break if widget: break # Fall back to a more general version of this adaptee adaptee.pop() if widget: widget.context = LookupContext(requested=q, provided=candidate) widget.errors = errors else: raise WidgetNotFound(qualified_action, field) # Found assert zope.interface.verify.verifyObject(IFieldWidget, widget) return widget
def widget_for_field(qualified_action, field, errors={}): '''Find and instantiate a widget to adapt a field to a widget interface. The given field should be a bound instance of zope.schema.Field. ''' # Build a list with candidate names q = qualified_action if isinstance(q, basestring): q = QualAction.from_string(q) candidates = list(reversed(q.parents() + [q])) # Build adaptee vector adaptee = build_adaptee(field, expand_collection=True) # Lookup registry widget = None while len(adaptee): for candidate in candidates: name = candidate.to_string() widget = adapter_registry.queryMultiAdapter(adaptee, IFieldWidget, name) logger.debug('Trying to adapt [%s] to widget for action %s: %r', ', '.join([type(x).__name__ for x in adaptee]), name, widget if widget else None) if widget: break if widget: break # Fall back to a more general version of this adaptee adaptee.pop() if widget: widget.context = LookupContext(requested=q, provided=candidate) widget.errors = errors else: raise WidgetNotFound(qualified_action, field) # Found assert zope.interface.verify.verifyObject(IFieldWidget, widget) return widget
def translator_for_metadata(md, source_language, name='default'): assert isinstance(md, Metadata) name = 'translate:%s' % (name) translator = adapter_registry.queryMultiAdapter( [md, Language(source_language)], IMetadataTranslator, name) return translator
def serializer_for_object(obj): """Get an XML serializer for an IObject object. """ assert IObject.providedBy(obj) serializer = adapter_registry.queryMultiAdapter([obj], IXmlSerializer, "serialize-xml") return serializer
def serializer_for_field(field): """Get an XML serializer for a zope.schema.Field instance. """ assert isinstance(field, zope.schema.Field) serializer = adapter_registry.queryMultiAdapter([field], IXmlSerializer, "serialize-xml") return serializer