Skip to main content

Parser for HTTP Structured Field Values

Project description

StruFi: HTTP Structured Field Values

This library implements a parser and a serializer for RFC 9651 (HTTP Structured Field Values).

Installation

Install strufi by running pip install strufi.
strufi package has no dependency.

See also package page on PyPI.

Usage

Parser

strufi module exposes 3 functions to parse structured field values:

  • load_item to parse a single item, returning the value with the parameters.
    >>> import strufi
    >>> strufi.load_item('text/html; charset=utf-8')
    ('text/html', {'charset': 'utf-8'})
    
  • load_list to parse a list of items, returning a list of pairs (value, parameters).
    >>> strufi.load_list('text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')
    [('text/html', {}), ('application/xhtml+xml', {}), ('application/xml', {'q': 0.9}), ('*/*', {'q': 0.8})]
    
  • load_dict to parse a map of keys:items, returning values with their parameters.
    >>> strufi.load_dict('u=1, i')
    {'u': (1, {}), 'i': (True, {})}
    

Serializer

The module also exposes functions for the serializing operations, which are the reverse operations of the parsing ones.

  • dump_item takes a value with parameters and returns the structured field value representation of a single item as a string
    >>> strufi.dump_item(('text/html', {'charset': 'utf-8'}))
    '"text/html";charset="utf-8"'
    
  • dump_list takes a list of items and return the structured list representation
    >>> strufi.dump_list([('text/html', {}), ('application/xhtml+xml', {}), ('application/xml', {'q': 0.9}), ('*/*', {'q': 0.8})])
    '"text/html", "application/xhtml+xml", "application/xml";q=0.9, "*/*";q=0.8'
    
  • dump_dict takes a dictionnary of key:item and return the structured dict representation
    >>> strufi.dump_dict({'u': (1, {}), 'i': (True, {})})
    'u=1, i'
    

Development

Environment

Use pip install -e '.[dev]' to install strufi with development dependencies (tests & lint) in your local (virtual) environment.

Here are the tools you can use to reproduce the checks made by the continuous integration workflow:

  • ruff format --diff to check file format (you can run ruff format to apply changes)
  • ruff check to check for specific linting rules
  • mypy . to check for type annotations consistency
  • pytest -vv to run tests suit

All these commands are also available through a Makefile that also takes care of the virtual environment: make lint, make mypy and make tests. You could also run make all or simply make to execute the three tasks.

Contributing

Feel free to open issues to report bugs or ask for features and to open pull-requests to work on existing issues.

The code of the project is published under MIT license.

Packaging

Build dependencies can be installed with pip install -e '.[build]'.

Then the package can be built with python -m build (or make build) and uploaded on PyPI with twine upload dist/* (or make upload that builds & uploads the package).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

strufi-1.0.1.tar.gz (15.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

strufi-1.0.1-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file strufi-1.0.1.tar.gz.

File metadata

  • Download URL: strufi-1.0.1.tar.gz
  • Upload date:
  • Size: 15.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for strufi-1.0.1.tar.gz
Algorithm Hash digest
SHA256 d13fcb0fc84c64a630af7c7847febd6da4479005f2a209d9a01a8faf48296a7e
MD5 dd77051937a53fe22e1a96c9893e8edb
BLAKE2b-256 146e54072116d589f1ce5275be3805823ee14cdcfad9e71dff109c8a97aef495

See more details on using hashes here.

File details

Details for the file strufi-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: strufi-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for strufi-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0559daeed00a084349d718ba7ba279086d2d80e577a0587c53961898663a7d55
MD5 f0727d7f7cb6f408dec57ae415692e05
BLAKE2b-256 ea35c9f98021009216ed30224299e6cde399491be6ab5545a30d0d57f98f6543

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page