包装器,使使用ZEO更轻松。
项目描述
简介
包装器,使使用ZEO更轻松。
默认情况下,您需要做很多事情,比如连接数据库、维护它、同步它(或运行asyncore循环)、处理重连等。本项目定义的类在后台为您完成所有这些工作。
文档
此模块定义了三个类
ZEOWrapperPrototype
ZEOConfWrapper
ZEOWrapper
ZEOWrapperPrototype
ZEOWrapperPrototype 包含方法和共享属性,这些属性可用于派生类。
除非您想创建自己的连接器,否则可以忽略此类。
ZEOConfWrapper
ZEOConfWrapper 可用于从 XML配置文件 创建到ZEO的连接。
假设您有一个文件 /tests/data/zeo_client.conf
<zeoclient>
server localhost:60985
</zeoclient>
现在您可以创建 ZEOConfWrapper 对象
from zeo_connector import ZEOConfWrapper
db_obj = ZEOConfWrapper(
conf_path="/tests/data/zeo_client.conf",
project_key="Some project key",
)
并将数据保存到数据库中
import transaction
with transaction.manager:
db_obj["data"] = "some data"
字符串 "some data" 现在已保存到 db._connection.root()[project_key]["data"] 路径下。
ZEOWrapper
ZEOWrapper 不使用XML配置文件,而是直接指定服务器/端口
from zeo_connector import ZEOWrapper
different_db_obj = ZEOWrapper(
server="localhost",
port=60985,
project_key="Some project key",
)
因此您可以检索存储到数据库中的数据
import transaction
with transaction.manager:
print different_db_obj["data"]
运行ZEO服务器
示例期望ZEO服务器正在运行。要运行ZEO,请查看ZEO捆绑包中runzeo脚本的帮助页面。此脚本需要命令行或XML配置。
您可以使用提供的zeo_connector_gen_defaults.py脚本生成配置文件,该脚本包含在<https://github.com/Bystroushaak/zeo_connector_defaults>包中。
$ zeo_connector_gen_defaults.py --help usage: zeo_connector_gen_defaults.py [-h] [-s SERVER] [-p PORT] [-C] [-S] [PATH] This program will create the default ZEO XML configuration files. positional arguments: PATH Path to the database on the server (used in server configuration only. optional arguments: -h, --help show this help message and exit -s SERVER, --server SERVER Server url. Default: localhost -p PORT, --port PORT Port of the server. Default: 60985 -C, --only-client Create only CLIENT configuration. -S, --only-server Create only SERVER configuration
例如
$ zeo_connector_gen_defaults.py /tmp
将创建包含以下内容的zeo.conf文件
<zeo>
address localhost:60985
</zeo>
<filestorage>
path /tmp/storage.fs
</filestorage>
<eventlog>
level INFO
<logfile>
path /tmp/zeo.log
format %(asctime)s %(message)s
</logfile>
</eventlog>
以及包含以下内容的zeo_client.conf
<zeoclient>
server localhost:60985
</zeoclient>
您可以使用--server或--port参数更改服务器的端口和地址。
要使用服务器配置文件运行ZEO,请运行以下命令
runzeo -C zeo.conf
要运行客户端,您可以使用如上所示ZEOConfWrapper
from zeo_connector import ZEOConfWrapper
db_obj = ZEOConfWrapper(
conf_path="./zeo_client.conf",
project_key="Some project key",
)
安装
该模块托管在<https://pypi.python.org/pypi/zeo_connector>,并可以使用PIP轻松安装
sudo pip install zeo_connector
源代码
项目采用MIT许可证发布。源代码可在GitHub上找到
单元测试
您可以使用位于项目根目录中的提供的run_tests.sh脚本来运行测试
如果您遇到任何问题,只需在run_tests.sh命令末尾添加--pdb选项,如下所示:./run_tests.sh --pdb。这会将您带到PDB外壳。
需求
此脚本期望已安装pytest包。如果您还没有安装它,可以很容易地使用以下命令安装它
pip install --user pytest
或对所有用户
sudo pip install pytest
示例
$ ./run_tests.sh ============================= test session starts ============================== platform linux2 -- Python 2.7.6 -- py-1.4.30 -- pytest-2.7.2 rootdir: /home/bystrousak/Plocha/Dropbox/c0d3z/python/libs/zeo_connector, inifile: plugins: cov collected 7 items tests/test_zeo_connector.py ....... =========================== 7 passed in 7.08 seconds ===========================
更改日志
0.4.8
ZEO版本未固定。将继续在https://github.com/zopefoundation/ZEO/issues/77中
0.4.7
固定了较旧的ZEO版本。
0.4.6
清理元数据文件。
0.4.0 - 0.4.5
为所有内部dict方法调用添加了@retry_and_reset装饰器。
项目密钥现在是可选的,因此可以使用此对象访问数据库的根。
属性ASYNCORE_RUNNING重命名为_ASYNCORE_RUNNING。
实现了.pack()。
添加了@transaction_manager。
添加了examples/database_handler.py和测试。
将@wraps(fn)添加到装饰器中。
添加了对zope.interface的要求。
0.3.0
环境生成器和其它共享部分移动到了https://github.com/Bystroushaak/zeo_connector_defaults
README.rst改进,增加了更多文档和附加部分。
0.2.0
添加了标准字典方法,如.__contains__()、.__delitem__()、.__iter__()等等。
0.1.0
项目创建。