GithubHelp home page GithubHelp logo

headerparser's Introduction

Project Status: Active โ€” The project has reached a stable, usable
state and is being actively developed.

CI Status

image

image

MIT License

GitHub | PyPI | Documentation | Issues | Changelog

headerparser parses key-value pairs in the style of RFC 822 (e-mail) headers and converts them into case-insensitive dictionaries with the trailing message body (if any) attached. Fields can be converted to other types, marked required, or given default values using an API based on the standard library's argparse module. (Everyone loves argparse, right?) Low-level functions for just scanning header fields (breaking them into sequences of key-value pairs without any further processing) are also included.

The Format

RFC 822-style headers are header fields that follow the general format of e-mail headers as specified by RFC 822 and friends: each field is a line of the form "Name: Value", with long values continued onto multiple lines ("folded") by indenting the extra lines. A blank line marks the end of the header section and the beginning of the message body.

This basic grammar has been used by numerous textual formats besides e-mail, including but not limited to:

  • HTTP request & response headers
  • Usenet messages
  • most Python packaging metadata files
  • Debian packaging control files
  • META-INF/MANIFEST.MF files in Java JARs
  • a subset of the YAML serialization format

โ€” all of which this package can parse.

Installation

headerparser requires Python 3.6 or higher. Just use pip for Python 3 (You have pip, right?) to install headerparser:

python3 -m pip install headerparser

Examples

Define a parser:

>>> 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')

Parse some headers and inspect the results:

>>> msg = parser.parse_string('''... 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'

Fail to parse headers that don't meet your requirements:

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

Allow fields you didn't even think of:

>>> parser.add_additional() >>> msg = parser.parse_string('Name: unknown fieldnField: Value') >>> msg['Field'] 'Value'

Just split some headers into names & values and worry about validity later:

>>> for field in headerparser.scan_string('''... 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's People

Contributors

jwodder avatar pombredanne avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.