arcp(archive and package)uri解析器和生成器
arcp的Python项目详细描述
创建/解析arcp(存档和包)uri。
简介
arcp提供创建arcpuri的函数, 可用于识别或解析超媒体 打包在存档或包中的文件,如zip文件。
arcp uri可用于使用或引用超媒体资源 捆绑在文件存档或应用程序包中,以及 在编程框架内解析存档资源的uri。
此uri方案提供了生成唯一基uri的机制 表示存档的根,以便相对uri引用 在绑定的资源中,可以在存档中解析,而无需提取本地文件系统上的存档内容。
arcp uri可用于隔离目的(例如,在使用 多个档案),安全约束(避免从 存档),或用于外部标识引用的子资源 超媒体格式。
- 示例:
- arcp://uuid,32a423d6-52ab-47e3-a9cd-54f418a48571/doc.html
- arcp://uuid,b7749d0b-0e47-5fc4-999d-f154abe68065/pics/
- arcp://ni,sha-256;F-34D4TUeOfG0selz7REKRDo4XePkewPeQYtjL3vQs0/
- arcp://name,gallery.example.org/
arcp uri中的uri authority的不同形式可以根据 在处理存档时应用的唯一性约束。 有关详细信息,请参阅arcp规范(draft soilandreyes arcp)。
注意,这个库只提供 generate和parsearcp uri,并且不与任何 特定的存档或url处理模块,如 zipfile或urllib.request。
许可证
版权所有©2018 Stian Soiland Reyes<;http://orcid.org/0000-0001-9842-9718>;,英国曼彻斯特大学
根据 apache许可证,版本2.0<;https://www.apache.org/licenses/LICENSE-2.0>;, 有关详细信息,请参见文件license.txt。
贡献
源代码:<;https://github.com/stain/arcp-py>;
请随时在<;https://github.com/stain/arcp-py/pulls>;处提出拉取请求。 或者在<;https://github.com/stain/arcp-py/issues>;出现问题。
提交的稿件假定包含在ApacheLicense2.0的第5节中。
安装
您将需要Python2.7、Python3.4或更高版本(推荐:3.6)。
如果您有pip,那么最简单的方法通常是使用
pip install arcp
如果要从此代码库手动安装,请尝试:
python setup.py install
用法
有关完整文档,请参见<;http://arcp.readthedocs.io/>;或使用help(arcp)
此模块提供创建arcpuri的函数, 可用于识别或解析超媒体 打包在存档或包中的文件,如zip文件::python
>>> from arcp import *>>> arcp_random() 'arcp://uuid,dcd6b1e8-b3a2-43c9-930b-0119cf0dc538/'>>> arcp_random("/foaf.ttl", fragment="me") 'arcp://uuid,dcd6b1e8-b3a2-43c9-930b-0119cf0dc538/foaf.ttl#me'>>> arcp_hash(b"Hello World!", "/folder/") 'arcp://ni,sha-256;f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk/folder/'>>> arcp_location("http://example.com/data.zip", "/file.txt") 'arcp://uuid,b7749d0b-0e47-5fc4-999d-f154abe68065/file.txt'
arcp url可以与urllib.parse一起使用, 例如,使用urljoin解析相对引用:
>>> css = arcp.arcp_name("app.example.com", "css/style.css") >>> urllib.parse.urljoin(css, "../fonts/foo.woff") 'arcp://name,app.example.com/fonts/foo.woff'
此外,此模块还提供可以使用的功能 将arcp uri解析为其组成字段::python
>>> is_arcp_uri("arcp://uuid,b7749d0b-0e47-5fc4-999d-f154abe68065/file.txt") True>>> is_arcp_uri("http://example.com/t") False>>> u = parse_arcp("arcp://uuid,b7749d0b-0e47-5fc4-999d-f154abe68065/file.txt") ARCPSplitResult(scheme='arcp',prefix='uuid',name='b7749d0b-0e47-5fc4-999d-f154abe68065', uuid='b7749d0b-0e47-5fc4-999d-f154abe68065',path='/file.txt',query='',fragment='')>>> u.path '/file.txt' >>> u.prefix 'uuid' >>> u.uuid UUID('b7749d0b-0e47-5fc4-999d-f154abe68065') >>> u.uuid.version 5>>> parse_arcp("arcp://ni,sha-256;f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk/folder/").hash ('sha-256', '7f83b1657ff1fc53b92dc18148a1d65dfc2d4b1fa3d677284addd200126d9069')
从parse_arcp返回的对象类似于 ParseResult来自urlparse,但包含其他属性 prefix,uuid,ni,hash和name, 其中一些将是None,具体取决于arcp前缀。
函数arcp.parse.urlparse可以作为替代项导入
到urllib.parse.urlparse。如果方案是arcp,则额外的
arcp字段如prefix
、uuid
、hash
和name
可用
从parse_arcp
开始,否则输出与从
常规的urlparse
::python
>>> from arcp.parse import urlparse >>> urlparse("arcp://ni,sha-256;f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk/folder/soup;sads") ARCPParseResult(scheme='arcp',prefix='ni', name='sha-256;f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk', ni='sha-256;f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk', hash=('sha-256', '7f83b1657ff1fc53b92dc18148a1d65dfc2d4b1fa3d677284addd200126d9069', path='/folder/soup;sads',query='',fragment='') >>> urlparse("http://example.com/help?q=a") ParseResult(scheme='http', netloc='example.com', path='/help', params='', query='q=a', fragment='')