This library implements a parser and a serializer for RFC 9651 (HTTP Structured Field Values).
Install strufi by running pip install git+https://github.com/alma/strufi.git
strufi package has no dependency.
strufi module exposes 3 functions to parse structured field values:
load_itemto 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_listto 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_dictto parse a map of keys:items, returning values with their parameters.>>> strufi.load_dict('u=1, i') {'u': (1, {}), 'i': (True, {})}
The module also exposes functions for the serializing operations, which are the reverse operations of the parsing ones.
dump_itemtakes 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_listtakes 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_dicttakes a dictionnary of key:item and return the structured dict representation>>> strufi.dump_dict({'u': (1, {}), 'i': (True, {})}) 'u=1, i'
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 --diffto check file format (you can runruff formatto apply changes)ruff checkto check for specific linting rulesmypy .to check for type annotations consistencypytest -vvto 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.
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.
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).