Grouparchy zope.schema 扩展
项目描述
;--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
如果事件为 None,则不会触发事件
>>> 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>,)
各个组件提供了更多的灵活性。
字段
InterfacesField 描述了上下文提供的接口集合。
InterfacesField 必须有一个 InterfaceField 或 Choice value_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])
类似于任何 Set 字段,它接受一个具有词汇表或源的 Choice 字段,以缩小接口集合
>>> 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,))
Choice 字段不能绕过验证
>>> 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,但触发的事件提供更具体的IInterfacesPopulated、IInterfacesCleared、IInterfacesAdded、IInterfacesRemoved或IInterfacesChanged之一。
>>> 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>
IInterfacesPopulated和IInterfacesCleared分别扩展IInterfacesAdded和IInterfacesRemoved。IInterfacesChanged同时扩展两者。
>>> 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:
项目详情
grouparchy.schema-0.1.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 448f802fac8247c44e2d6a343d57795ce0fd2b465c53ae81780ae8f990136d45 |
|
MD5 | 99c138fe3c345cc5139f61ee96d33f2a |
|
BLAKE2b-256 | 36ab79d26d3d1b4bfa47950c1c3c4624c5b81f3a97e5469121e459e6e6294ad2 |