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_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