易于构建cubicweb客户端的python库
cwclientlib的Python项目详细描述
摘要
易于构建CubicWeb客户机的python库:
- 远程执行RQL查询(使用rqlcontroller),
- 访问需要身份验证的实例(使用signedrequest)。
它还提供了一个简单的命令行工具(cwrql)来执行简单的请求。
配置
cwclientlib实现一个cwproxy_for(instance)函数 将为给定实例生成一个CWProxy,读取 来自配置文件的身份验证凭据(可以是ini 文件、json或yaml)。默认配置文件名为 ~/.config/cwclientlibrc(使用ini文件格式),但这可以是 已使用CWCLCONF环境变量更改。例如:
david@perseus:~$ cat ~/.config/cwclientlibrc [cwo]url= https://www.cubicweb.org/ token-id = my_cwo_token secret= <my-secret> [elo]url= https://www.logilab.org token-id = my_elo_token secret= <my-secret> [activites]url= https://my.intranet/activites auth-mech = kerberos
使写入成为可能:
david@perseus:~$ cwrql cwo "Any N,S WHERE P eid 1251664, P name N, P summary S" projman a project management tool david@perseus:~$ cwrql -v ejsonexport -j cwo "Any P WHERE P eid 1251664"[{"description": "It reads project descriptions [...]", "modification_date": "2015/02/13 18:12:40", "icon_format": null, "description_format": "text/rest", "summary": "a project management tool", "downloadurl": "http://download.logilab.org/pub/projman", "cwuri": "http://www.logilab.org/873", "__cwetype__": "Project", "eid": 1251664, "creation_date": "2006/09/28 17:44:38", "homepage": null, "debian_source_package": null, "name": "projman"}]
或:
fromcwclientlibimportcwproxy_forclient=cwproxy_for('cwo')# or client = cwproxy_for('https://www.cubicweb.org/')query='Any X WHERE X is Ticket, X concerns P, P name "cwclientlib"'resp=client.rql(query)data=resp.json()
请注意,配置文件可能包含凭据,因此其权限 必须只能由用户读取(仅在POSIX平台上检查)。
使用kerberos
只要确保Python-Kerberos和Requests-Kerberos是 安装。cubicweb服务器必须支持基于kerberos的 身份验证。
配置
您可以为常用的cubicweb定义url和凭据 配置文件中的终结点。默认情况下,在Linux上,它将是一个ini 文件位于$HOME/.config/cwclientlibrc,但您可以定义 CWCLCONFenvironmentvariable来指定它。此配置文件可以 也可以是yaml(文件名必须以.yaml结尾)或json文件(.json)。
文件将如下所示:
[cwo]url=https://www.cubicweb.org/token-id=my token idsecret=<my secret>[intra]url=https://my.intranetauth-mech=kerberosserver-ca=/path/to/ca-bundle.pem
命令行工具
cwclientlib附带3个简单的命令行工具,可以轻松地 从shell请求cubicweb应用程序:
cwrql进行rql查询:
david@perseus:~$ cwrql -h Usage: cwrql [options](url|instance_id) rqlquery [rqlquery2] ... Options: -h, --help show this help message and exit -j, --json produce JSON data -v VID, --vid=VID vid to use (default is jsonexport) -S, --no-ssl do NOT verify ssl server certificate; ignored if --ca is given -c CA, --ca=CA Bundle CA to use to verify server certificate -w, --rqlio use rqlio david@perseus:~$ cwrql cwo "Any VN, VS WHERE V version_of P, > P name 'cwclientlib', V num VN, V in_state S, S name VS"0.2.1 published 0.3.0 dev 0.2.0 published 0.1.0 published
cwget发出任何“获取之王”请求(即调用特定的cubicweb控制器):
david@perseus:~$ cwget cwo /testconfig/1251730 \ vid=apycot.get_configuration environment=4209277[{"pylint_threshold": "7", "install": "python_setup", "pycoverage_threshold": "70"}]
cwshell连接到cubicweb内点并启动交互式 python shell还有一些额外的内置项rql和 client。此shell还提供rql自动完成功能:
david@perseus:~$ cwshell cwo You are connected to https://www.cubicweb.org >>> client.execute('Any X WHERE X is P Patch Plan Project ProjectEnvironment >>> rql('Any P, N WHERE X is Project, X name P ,V version_of X, V in_state S, V num N, S name "ready"') [[u'cubicweb-pyramid', u'0.2.0'], [u'cubicweb-simplefacet', u'0.3.2']] >>>
提供额外的内置设备:
client: | is the CWProxy instance connected to the cubicweb endpoint. |
---|---|
rql: | shortcut for ^{tt11}$. |
python示例
简单的只读查询:
fromcwclientlibimportcwproxyclient=cwproxy.CWProxy('http://www.cubicweb.org/')query='Any X WHERE X is Ticket, X concerns P, P name "cwclientlib"'resp=client.rql(query)data=resp.json()
创建实体,使用signedrequest进行身份验证 从配置文件读取的凭据:
fromcwclientlibimportcwproxy_forclient=cwproxy_for('cwo')queries=[('INSERT CWUser U: U login %(l)s, U upassword %(p)s',{'l':'Babar','p':'cubicweb rulez & 42'}),]resp=client.rqlio(queries)data=resp.json()
创建实体,使用signedrequest生成 亲笔签名:
fromcwclientlibimportcwproxyauth=cwproxy.SignedRequestAuth('my token','6ed44d82172211e49d9777269ec78bae')client=cwproxy.CWProxy('https://www.cubicweb.org/',auth)queries=[('INSERT CWUser U: U login %(l)s, U upassword %(p)s',{'l':'Babar','p':'cubicweb rulez & 42'}),]resp=client.rqlio(queries)data=resp.json()
创建文件实体,使用signedrequest:
fromioimportBytesIOfromcwclientlibimportcwproxy_forclient=cwproxy_for('cwo')queries=[('INSERT File F: F data %(content)s, F data_name %(fname)s',{'content':BytesIO('some binary data'),'fname':'toto.bin'})]resp=client.rqlio(queries)data=resp.json()
使用buildershelpers,使用 kerberos身份验证程序:
fromcwclientlibimportcwproxy,buildersfromrequests_kerberosimportHTTPKerberosAuth,OPTIONALauth=HTTPKerberosAuth(mutual_authentication=OPTIONAL)client=cwproxy.CWProxy('https://www.cubicweb.org/',auth)queries=[builders.create_entity('CWUser',login='Babar',upassword='secret'),('SET U in_group G WHERE U eid %(eid)s, G name "users"',{'eid':'__r0'}),]resp=client.rqlio(queries)data=resp.json()