跳转到主要内容

用于邮件样式标题的argparse

项目描述

Project Status: Active — The project has reached a stable, usable state and is being actively developed. CI Status https://codecov.io/gh/jwodder/headerparser/branch/master/graph/badge.svg https://img.shields.io/pypi/pyversions/headerparser.svg MIT License

GitHub | PyPI | 文档 | 问题 | 变更日志

headerparser 解析类似RFC 822(电子邮件)头部的键值对,并将它们转换为不区分大小写的字典,并将尾部消息体(如果有)附加到字典中。可以使用基于标准库 argparse 模块的API将字段转换为其他类型、标记为必需或赋予默认值。(大家都喜欢 argparse,对吧?)还包含用于仅扫描头部字段(将它们分解为键值对序列而不进行任何进一步处理)的低级函数。

格式

RFC 822风格的头部是遵循RFC 822及其朋友规定的电子邮件头部通用格式的头部字段:每个字段是一行,形式为“名称: 值”,长值可以通过缩进额外行(“折叠”)延续到多行。空白行标记了头部部分的结束和消息体的开始。

这种基本语法已被许多除电子邮件以外的文本格式所使用,包括但不限于

  • HTTP请求和响应头部

  • Usenet消息

  • 大多数Python打包元数据文件

  • Debian打包控制文件

  • Java JAR中的 META-INF/MANIFEST.MF 文件

  • YAML序列化格式的一部分

——所有这些,本包都可以解析。

安装

headerparser 需要 Python 3.7 或更高版本。只需使用 Python 3 的 pip 安装 headerparser 即可。(您有 pip,对吧?)

python3 -m pip install headerparser

示例

定义一个解析器

>>> import headerparser
>>> parser = headerparser.HeaderParser()
>>> parser.add_field('Name', required=True)
>>> parser.add_field('Type', choices=['example', 'demonstration', 'prototype'], default='example')
>>> parser.add_field('Public', type=headerparser.BOOL, default=False)
>>> parser.add_field('Tag', multiple=True)
>>> parser.add_field('Data')

解析一些头部并检查结果

>>> msg = parser.parse('''\
... Name: Sample Input
... Public: yes
... tag: doctest, examples,
...   whatever
... TAG: README
...
... Wait, why I am using a body instead of the "Data" field?
... ''')
>>> sorted(msg.keys())
['Name', 'Public', 'Tag', 'Type']
>>> msg['Name']
'Sample Input'
>>> msg['Public']
True
>>> msg['Tag']
['doctest, examples,\n  whatever', 'README']
>>> msg['TYPE']
'example'
>>> msg['Data']
Traceback (most recent call last):
    ...
KeyError: 'data'
>>> msg.body
'Wait, why I am using a body instead of the "Data" field?\n'

无法解析不符合您要求的头部

>>> parser.parse('Type: demonstration')
Traceback (most recent call last):
    ...
headerparser.errors.MissingFieldError: Required header field 'Name' is not present
>>> parser.parse('Name: Bad type\nType: other')
Traceback (most recent call last):
    ...
headerparser.errors.InvalidChoiceError: 'other' is not a valid choice for 'Type'
>>> parser.parse('Name: unknown field\nField: Value')
Traceback (most recent call last):
    ...
headerparser.errors.UnknownFieldError: Unknown header field 'Field'

允许您甚至没有考虑到的字段

>>> parser.add_additional()
>>> msg = parser.parse('Name: unknown field\nField: Value')
>>> msg['Field']
'Value'

只需将一些头部拆分为名称和值,然后稍后再考虑其有效性

>>> for field in headerparser.scan('''\
... Name: Scanner Sample
... Unknown headers: no problem
... Unparsed-Boolean: yes
... CaSe-SeNsItIvE-rEsUlTs: true
... Whitespace around colons:optional
... Whitespace around colons  :  I already said it's optional.
...   That means you have the _option_ to use as much as you want!
...
... And there's a body, too, I guess.
... '''): print(field)
('Name', 'Scanner Sample')
('Unknown headers', 'no problem')
('Unparsed-Boolean', 'yes')
('CaSe-SeNsItIvE-rEsUlTs', 'true')
('Whitespace around colons', 'optional')
('Whitespace around colons', "I already said it's optional.\n  That means you have the _option_ to use as much as you want!")
(None, "And there's a body, too, I guess.\n")

项目详情


下载文件

下载您平台上的文件。如果您不确定该选择哪一个,请了解有关 安装包 的更多信息。

源分布

headerparser-0.5.1.tar.gz (34.3 kB 查看哈希值)

上传时间

构建分布

headerparser-0.5.1-py3-none-any.whl (18.3 kB 查看哈希值)

上传时间 Python 3

支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面