Zope注释的Grok-like配置
项目描述
本包提供对简化Zope中注解使用的支持。
设置grokcore.annotation
本包的设置本质上与grokcore.component包类似,请参阅其文档以获取详细信息。您需要的唯一附加ZCML行是
<include package="grokcore.annotation" />
请将此行放置在根ZCML文件的顶部附近,但在包含grokcore.component配置的行下方。
示例
以下是一个使用注解的简单示例
import grokcore.annotation
from zope import interface
# Create a model and an interface you want to adapt it to
# and an annotation class to implement the persistent adapter.
class Mammoth(grokcore.annotation.Model):
pass
class ISerialBrand(interface.Interface):
unique = interface.Attribute("Brands")
class Branding(grokcore.annotation.Annotation):
grokcore.annotation.implements(ISerialBrand)
unique = 0
# Grok the above code, then create some mammoths
manfred = Mammoth()
mumbles = Mammoth()
# creating Annotations work just like Adapters
livestock1 = ISerialBrand(manfred)
livestock2 = ISerialBrand(mumbles)
# except you can store data in them, this data will transparently persist
# in the database for as long as the object exists
livestock1.unique = 101
livestock2.unique = 102
# attributes not listed in the interface will also be persisted
# on the annotation
livestock2.foo = "something"
API概览
基类
- 注解
注解的基类。继承自持久化类Persistent。
- 模型
您希望使用注解的模型的基类。
- queryAnnotation(model, interface)
查询给定模型上的给定接口的注解。如果找到则返回注解,否则返回None。这不会进行任何写操作。
- deleteAnnotation(model, interface)
查找给定的注解并从模型中删除它。
- LazyAnnotation
注解的基类。仅在显式设置懒属性值时才写入数据库对象。
- LazyAnnotationProperty
与LazyAnnotation一起工作的属性实现。
此外,grokcore.annotation包公开了grokcore.component API。
变更
4.0 (2023-07-12)
添加对Python 3.10和3.11的支持。
取消对Python 2.7、3.5和3.6的支持。
取消对已弃用的python setup.py test的支持。
3.2 (2021-08-31)
grokcore.annotation.testing.warn已被删除,因为它在内部未使用。如果仍然需要它,可以在grokcore.view.testing中找到副本。
向LazyAnnotation对象添加_p_changed属性,将其代理到实际的存储对象。这样,“显式标记对象为已更改”的“API”在“正常”注解对象和懒注解对象之间都是相同的。
添加对Python 3.7、3.8和3.9的支持。
取消对Python 3.4的支持。
3.1 (2020-10-27)
在LazyPropertyAnnotation中添加对FieldUpdatedEvent的支持,以反映zope.schema的行为。
3.0.1 (2018-01-17)
在整个文档中用@grok.implementer()指令替换使用grok.implements()。
3.0.0 (2018-01-12)
重新排列测试,以便Travis CI可以抓取所有功能测试。
1.6 (2017-05-30)
添加LazyAnnotation和LazyAnnotationProperty。
取消对Python 2.6的支持,并声明支持Python 3.4、3.5、3.6和PyPy。
1.5.1 (2016-01-29)
更新测试。
1.5 (2014-10-20)
更新MANIFEST.in,修复了一个棕色纸袋版本。
1.4 (2014-10-17)
将queryAnnotation()添加到返回注解。如果不存在则返回None。此辅助函数永远不会在数据库中执行任何写操作。
添加deleteAnnotation()以删除注解(如果存在)。
1.3 (2012-05-01)
使用来自grokcore.component.util的provideAdapter()。
使包符合zope.org存储库策略。
1.2 (2009-12-13)
使用zope.container而不是zope.app.container。
1.1 (2009-09-18)
注解对象实际上成为一个容器对象,以便意识到其上下文和名称。
在Grok的发布信息中使用1.0b1版本的versions.cfg,而不是本地副本;所有grokcore包的本地副本都太难维护。
1.0.1 (2009-06-30)
使用没有distutils错误的正确版本的Python重新上传到pypi。
1.0 (2009-06-29)
通过从Grok中提取注解组件、grokkers和指令创建了grokcore.Annotation。