grouparchy zope.schema扩展
grouparchy.schema的Python项目详细描述
;--doctest--
现场事件
eventproperty是一个zope.schema fieldproperty,当 字段将被修改并将新旧值发送到事件。
>>> from zope import interface, schema >>> import grouparchy.schema.event >>> class IFoo(interface.Interface): ... field = schema.Field() >>> class Foo(object): ... interface.implements(IFoo) ... field = grouparchy.schema.event.EventProperty( ... IFoo['field']) >>> foo = Foo()
在为事件配置处理程序之前,不会发生任何事情:
>>> foo.field >>> foo.field = 'foo' >>> foo.field 'foo'
当我们为事件提供处理程序时,当我们 更改值:
>>> from zope import component >>> def handler(event): ... print 'event: %s' % event ... print 'object: %s' % event.object ... print 'event.old: %s' % event.old ... print 'event.new: %s' % event.new >>> component.provideHandler( ... handler, (grouparchy.schema.event.IFieldModifiedEvent,)) >>> foo.field = 'bar' event: <grouparchy.schema.event.FieldModifiedEvent object at ...> object: <Foo object at ...> event.old: foo event.new: bar如果新值与现有值相等,则事件不是 已触发:
>>> foo.field 'bar' >>> foo.field = 'bar'
另一个事件也可以传递给属性:
>>> class FooEvent(grouparchy.schema.event.FieldModifiedEvent): ... pass >>> Foo.field = grouparchy.schema.event.EventProperty( ... IFoo['field'], event=FooEvent) >>> foo.field = 'foo' event: <FooEvent object at ...> object: <Foo object at ...> event.old: bar event.new: foo
如果事件为“无”,则不会触发任何事件:
>>> Foo.field = grouparchy.schema.event.EventProperty( ... IFoo['field'], event=None) >>> foo.field = 'bar'
子类EventProperty可以重写Notify()的描述符 进一步控制的方法。例如,下面的描述符将 即使字段值不变,也会触发事件:
>>> from zope import event >>> class AlwaysEventProperty( ... grouparchy.schema.event.EventProperty): ... def notify(self, instance, new, old): ... event.notify(self.event(instance, new, old)) >>> Foo.field = AlwaysEventProperty(IFoo['field']) >>> foo.field 'bar' >>> foo.field = 'bar' event: <grouparchy.schema.event.FieldModifiedEvent object at ...> object: <Foo object at ...> event.old: bar event.new: bar
;--doctest--
接口字段
grouparchy.schema.interface包含的zope.schema字段用于 操作上下文提供的接口。
包含了一个用于直接管理接口的实现 由对象提供:
>>> from zope import interface >>> from grouparchy.schema.bbb import component_iface >>> class IFoo(interface.Interface): pass >>> component_iface.provideInterface('', IFoo) >>> class Context(object): ... interface.implements(interface.Interface) >>> from zope import component >>> import grouparchy.schema.interface >>> component.provideAdapter( ... factory=grouparchy.schema.interface.DirectlyProvided) >>> provider = Context() >>> directlyProvided = ( ... grouparchy.schema.interface.IDirectlyProvided( ... provider)) >>> tuple(directlyProvided.directlyProvided) () >>> directlyProvided.directlyProvided = (IFoo,) >>> tuple(directlyProvided.directlyProvided) (<InterfaceClass __builtin__.IFoo>,)个别组件提供了更大的灵活性。
字段
接口字段描述由 语境。
接口字段必须具有接口字段或选择值u type:
>>> from zope import schema >>> grouparchy.schema.interface.InterfacesField() Traceback (most recent call last): ... ValueError: 'value_type' must be an InterfaceField or a Choice. >>> grouparchy.schema.interface.InterfacesField( ... value_type=schema.Field()) Traceback (most recent call last): ... ValueError: 'value_type' must be an InterfaceField or a Choice. >>> field = grouparchy.schema.interface.InterfacesField( ... value_type=schema.InterfaceField())
有效值是接口序列:
>>> foo = object() >>> bound = field.bind(foo) >>> bound.validate(interface.Interface) Traceback (most recent call last): ... WrongType: (<InterfaceClass zope.interface.Interface>, (<type 'set'>, <type 'tuple'>, <type 'list'>)) >>> bound.validate((None,)) Traceback (most recent call last): ... WrongContainedType: [] >>> bound.validate((interface.Interface,)) >>> bound.validate([interface.Interface])
与任何集合字段一样,它接受带有词汇表或 缩小接口集的源:
>>> field = grouparchy.schema.interface.InterfacesField( ... value_type=schema.Choice(values=(IFoo,))) >>> bound = field.bind(foo) >>> bound.validate((interface.Interface,)) Traceback (most recent call last): ... WrongContainedType: [<InterfaceClass zope.interface.Interface>] >>> bound.validate((IFoo,))
选择字段不能绕过验证:
>>> field = grouparchy.schema.interface.InterfacesField( ... value_type=schema.Choice(values=(None,))) >>> bound = field.bind(foo) >>> bound.validate((None,)) Traceback (most recent call last): ... WrongContainedType: []
来源
还提供了一个可以与接口类型一起使用的源 用于确定字段的有效接口集:
>>> [i for i in grouparchy.schema.interface.InterfacesSource()] [<InterfaceClass __builtin__.IFoo>]
接口子集可以通过传递接口类型来指定:
>>> import zope.interface.interfaces >>> class IIBar(zope.interface.interfaces.IInterface): pass >>> class IBar(interface.Interface): pass >>> component_iface.provideInterface('', IBar) >>> component_iface.provideInterface('', IBar, IIBar) >>> [i for i in grouparchy.schema.interface.InterfacesSource()] [<InterfaceClass __builtin__.IFoo>, <InterfaceClass __builtin__.IBar>] >>> source = grouparchy.schema.interface.InterfacesSource(IIBar) >>> [i for i in source] [<InterfaceClass __builtin__.IBar>]
属性
还提供了两个属性来获取和设置字段 接口对象的值或接口的虚线名称:
>>> class IFoo(interface.Interface): ... all = grouparchy.schema.interface.InterfacesField( ... value_type=schema.InterfaceField()) ... bar = grouparchy.schema.interface.InterfacesField( ... value_type=schema.Choice(source=source)) ... dotted = grouparchy.schema.interface.InterfacesField( ... value_type=schema.InterfaceField()) >>> class Foo(object): ... all = grouparchy.schema.interface.InterfacesProperty( ... IFoo['all']) ... bar = grouparchy.schema.interface.InterfacesProperty( ... IFoo['bar']) ... dotted = ( ... grouparchy.schema.interface.InterfaceIdentsProperty( ... IFoo['dotted'])) >>> foo = Foo()
属性返回一个ideclaration:
>>> isinstance(foo.all, interface.Declaration) True
在对象提供任何内容之前,声明是空的:
>>> tuple(foo.all) () >>> tuple(foo.bar) () >>> tuple(foo.dotted) () >>> interface.alsoProvides(foo, interface.Interface) >>> tuple(foo.all) (<InterfaceClass zope.interface.Interface>,) >>> tuple(foo.dotted) ('zope.interface.Interface',) >>> interface.alsoProvides(foo, IBar) >>> tuple(foo.all) (<InterfaceClass zope.interface.Interface>, <InterfaceClass __builtin__.IBar>) >>> tuple(foo.bar) (<InterfaceClass __builtin__.IBar>,) >>> IBar.providedBy(foo) True >>> foo.bar = () >>> tuple(foo.bar) () >>> IBar.providedBy(foo) False >>> foo.bar = (IBar,) >>> tuple(foo.bar) (<InterfaceClass __builtin__.IBar>,) >>> IBar.providedBy(foo) True
属性需要知道如何获取提供 属性所在对象的接口。这是 使用对象的适配器完成。下面检查一些 在适配器上经常找到上下文并返回到的名称 对象本身:
>>> context = foo.context = Foo() >>> interface.alsoProvides(foo.context, interface.Interface) >>> tuple(foo.all) (<InterfaceClass zope.interface.Interface>, <InterfaceClass __builtin__.IBar>) >>> component.provideAdapter( ... grouparchy.schema.interface.getInterfacesContext) >>> tuple(foo.all) (<InterfaceClass zope.interface.Interface>,) >>> del foo.context >>> tuple(foo.all) (<InterfaceClass zope.interface.Interface>, <InterfaceClass __builtin__.IBar>) >>> foo.object = context >>> tuple(foo.all) (<InterfaceClass zope.interface.Interface>,) >>> del foo.object
事件
默认情况下,属性触发事件:
>>> import zope.interface.interfaces >>> def getIfacesStr(ifaces): ... return ', '.join((str(i) for i in sorted(ifaces))) >>> def printInterfacesModified(event): ... print 'Event: %s' % event ... print 'Object: %s' % event.object ... print 'New: ' + getIfacesStr(event.new) ... print 'Old: ' + getIfacesStr(event.old) ... print 'Added: ' + getIfacesStr(event.added) ... print 'Removed: ' + getIfacesStr(event.removed) >>> component.provideHandler( ... factory=printInterfacesModified, ... adapts=( ... grouparchy.schema.interface.IInterfacesModified,)) >>> class IBaz(interface.Interface): pass >>> component_iface.provideInterface('', IBaz) >>> component_iface.provideInterface('', IBaz, IIBar)
默认事件都提供了iInterfaceSmodified,但是 触发提供了一个更具体的接口, 界面搜索、界面添加、界面删除,或 i接口按适当顺序排列:
>>> foo.bar = () Event: <grouparchy.schema.interface.InterfacesCleared object at ...> Object: <Foo object at ...> New: Old: <InterfaceClass __builtin__.IBar> Added: Removed: <InterfaceClass __builtin__.IBar> >>> foo.bar = (IBar,) Event: <grouparchy.schema.interface.InterfacesPopulated object at ...> Object: <Foo object at ...> New: <InterfaceClass __builtin__.IBar> Old: Added: <InterfaceClass __builtin__.IBar> Removed: >>> foo.bar = (IBar, IBaz) Event: <grouparchy.schema.interface.InterfacesAdded object at ...> Object: <Foo object at ...> New: <InterfaceClass __builtin__.IBar>, <InterfaceClass __builtin__.IBaz> Old: <InterfaceClass __builtin__.IBar> Added: <InterfaceClass __builtin__.IBaz> Removed: >>> foo.bar = (IBar,) Event: <grouparchy.schema.interface.InterfacesRemoved object at ...> Object: <Foo object at ...> New: <InterfaceClass __builtin__.IBar> Old: <InterfaceClass __builtin__.IBar>, <InterfaceClass __builtin__.IBaz> Added: Removed: <InterfaceClass __builtin__.IBaz> >>> foo.bar = (IBaz,) Event: <grouparchy.schema.interface.InterfacesChanged object at ...> Object: <Foo object at ...> New: <InterfaceClass __builtin__.IBaz> Old: <InterfaceClass __builtin__.IBar> Added: <InterfaceClass __builtin__.IBaz> Removed: <InterfaceClass __builtin__.IBar>
接口填充和接口扩展接口添加 和接口分别被移除。界面扩展 两者:
>>> def printInterfacesAdded(event): print 'Interfaces Added' >>> component.provideHandler( ... factory=printInterfacesAdded, ... adapts=( ... grouparchy.schema.interface.IInterfacesAdded,)) >>> def printInterfacesRemoved(event): print 'Interfaces Removed' >>> component.provideHandler( ... factory=printInterfacesRemoved, ... adapts=( ... grouparchy.schema.interface.IInterfacesRemoved,)) >>> foo.bar = () Event: <grouparchy.schema.interface.InterfacesCleared object at ...> Object: <Foo object at ...> New: Old: <InterfaceClass __builtin__.IBaz> Added: Removed: <InterfaceClass __builtin__.IBaz> Interfaces Removed >>> foo.bar = (IBar,) Event: <grouparchy.schema.interface.InterfacesPopulated object at ...> Object: <Foo object at ...> New: <InterfaceClass __builtin__.IBar> Old: Added: <InterfaceClass __builtin__.IBar> Removed: Interfaces Added >>> foo.bar = (IBaz,) Event: <grouparchy.schema.interface.InterfacesChanged object at ...> Object: <Foo object at ...> New: <InterfaceClass __builtin__.IBaz> Old: <InterfaceClass __builtin__.IBar> Added: <InterfaceClass __builtin__.IBaz> Removed: <InterfaceClass __builtin__.IBar> Interfaces Removed Interfaces Added
事件类可以传递给属性,如下所示 grouparchy.schema.event.eventproperty:
>>> import grouparchy.schema.event >>> class IBarInterfacesModified( ... grouparchy.schema.event.IFieldModifiedEvent): pass >>> class BarInterfacesModified( ... grouparchy.schema.event.FieldModifiedEvent): ... interface.implements(IBarInterfacesModified) >>> def printBarInterfacesModified(event): ... print 'Event: %s' % event ... print 'Object: %s' % event.object ... print 'New: ' + getIfacesStr(event.new) ... print 'Old: ' + getIfacesStr(event.old) >>> component.provideHandler( ... factory=printBarInterfacesModified, ... adapts=(IBarInterfacesModified,)) >>> foo.bar = () Event: <grouparchy.schema.interface.InterfacesCleared object at ...> Object: <Foo object at ...> New: Old: <InterfaceClass __builtin__.IBaz> Added: Removed: <InterfaceClass __builtin__.IBaz> Interfaces Removed >>> foo.bar = (IBar,) Event: <grouparchy.schema.interface.InterfacesPopulated object at ...> Object: <Foo object at ...> New: <InterfaceClass __builtin__.IBar> Old: Added: <InterfaceClass __builtin__.IBar> Removed: Interfaces Added >>> Foo.bar = grouparchy.schema.interface.InterfacesProperty( ... IFoo['bar'], ... event=BarInterfacesModified) >>> foo.bar = () Event: <BarInterfacesModified object at ...> Object: <Foo object at ...> New: Old: <InterfaceClass __builtin__.IBar> >>> foo.bar = (IBar,) Event: <BarInterfacesModified object at ...> Object: <Foo object at ...> New: <InterfaceClass __builtin__.IBar> Old: