# Multifruits
Tasty multipart form data parser built with cython.
## Install
pip install multifruits
## Usage
`multifruits` has one `Parser` class and two helpers: `extract_filename` and
`parse_content_disposition`.
#### `Parser`
`Parser` needs the `Content-Type` header value and a handler, which could
define one or more of these methods:
```python
on_body_begin()
on_part_begin()
on_header(name: bytes, value: bytes)
on_headers_complete()
on_data(data: bytes)
on_part_complete()
on_body_complete()
```
Example:
```python
from multifruits import Parser
class MyHandler:
def on_part_begin(self):
self.part = MyPart()
def on_header(self, name, value):
self.part.headers[name] = value
def on_data(self, data):
self.part.write(data)
handler = MyHandler()
parser = Parser(handler, request.headers['Content-Type'])
parser.feed_data(request.body) # You can pass chunks
```
#### Helpers
##### `parse_content_disposition`
Takes raw `Content-Disposition` header value and returns the disposition type
(`attachment`, `form-data`, `inline` and so on) and the parameters parsed as a
dictionary.
Example:
```python
dtype, params = parse_content_disposition(b'inline; filename="foo.html"')
assert dtype == b'inline'
assert params == {b'filename': b'foo.html'}
```
##### `extract_filename`
Takes parameters from `parse_content_disposition` as a dict and tries to
return the appropriated `str` filename (like `filename*`).
Example:
```python
assert extract_filename({
b'filename*': "UTF-8''foo-ä-€.html".encode()
}) == 'foo-ä-€.html'
```
## Build from source
You need a virtualenv with cython installed, then:
git clone https://github.com/pyrates/multifruits
cd multifruits
make compile
python setup.py develop
## Tests
To run tests:
make test
## Acknowledgements
- https://github.com/iafonov/multipart-parser-c/
- https://github.com/francoiscolas/multipart-parser/
- https://github.com/felixge/node-formidable/
Raw data
{
"_id": null,
"home_page": "https://github.com/pyrates/multifruits",
"name": "multifruits",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "",
"author": "Pyrates",
"author_email": "yohan.boniface@data.gouv.fr",
"download_url": "https://files.pythonhosted.org/packages/8c/81/5236fd520d50a5ae8fad51e063302e0a4a002b47fc5a9a015bc0047be931/multifruits-0.1.7.tar.gz",
"platform": "POSIX",
"description": "# Multifruits\n\nTasty multipart form data parser built with cython.\n\n\n## Install\n\n pip install multifruits\n\n\n## Usage\n\n`multifruits` has one `Parser` class and two helpers: `extract_filename` and\n`parse_content_disposition`.\n\n\n#### `Parser`\n\n`Parser` needs the `Content-Type` header value and a handler, which could\ndefine one or more of these methods:\n\n```python\non_body_begin()\non_part_begin()\non_header(name: bytes, value: bytes)\non_headers_complete()\non_data(data: bytes)\non_part_complete()\non_body_complete()\n```\n\nExample:\n\n```python\nfrom multifruits import Parser\n\nclass MyHandler:\n\n def on_part_begin(self):\n self.part = MyPart()\n\n def on_header(self, name, value):\n self.part.headers[name] = value\n\n def on_data(self, data):\n self.part.write(data)\n\nhandler = MyHandler()\nparser = Parser(handler, request.headers['Content-Type'])\nparser.feed_data(request.body) # You can pass chunks\n```\n\n#### Helpers\n\n##### `parse_content_disposition`\n\nTakes raw `Content-Disposition` header value and returns the disposition type\n(`attachment`, `form-data`, `inline` and so on) and the parameters parsed as a\ndictionary.\n\nExample:\n\n```python\ndtype, params = parse_content_disposition(b'inline; filename=\"foo.html\"')\nassert dtype == b'inline'\nassert params == {b'filename': b'foo.html'}\n```\n\n\n##### `extract_filename`\n\nTakes parameters from `parse_content_disposition` as a dict and tries to\nreturn the appropriated `str` filename (like `filename*`).\n\nExample:\n\n```python\nassert extract_filename({\n b'filename*': \"UTF-8''foo-\u00e4-\u20ac.html\".encode()\n}) == 'foo-\u00e4-\u20ac.html'\n```\n\n\n## Build from source\n\nYou need a virtualenv with cython installed, then:\n\n git clone https://github.com/pyrates/multifruits\n cd multifruits\n make compile\n python setup.py develop\n\n## Tests\n\nTo run tests:\n\n make test\n\n\n## Acknowledgements\n\n- https://github.com/iafonov/multipart-parser-c/\n- https://github.com/francoiscolas/multipart-parser/\n- https://github.com/felixge/node-formidable/\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Tasty multipart form data parser built with cython.",
"version": "0.1.7",
"project_urls": {
"Homepage": "https://github.com/pyrates/multifruits"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "8c815236fd520d50a5ae8fad51e063302e0a4a002b47fc5a9a015bc0047be931",
"md5": "b19ac92e2b344326418b25a6f131bbf7",
"sha256": "8985bb7b73001525f92cad2e0efa353c42a3ae67a7510d67f19143b09be41019"
},
"downloads": -1,
"filename": "multifruits-0.1.7.tar.gz",
"has_sig": false,
"md5_digest": "b19ac92e2b344326418b25a6f131bbf7",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 94093,
"upload_time": "2024-02-21T10:23:00",
"upload_time_iso_8601": "2024-02-21T10:23:00.581188Z",
"url": "https://files.pythonhosted.org/packages/8c/81/5236fd520d50a5ae8fad51e063302e0a4a002b47fc5a9a015bc0047be931/multifruits-0.1.7.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-02-21 10:23:00",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "pyrates",
"github_project": "multifruits",
"travis_ci": true,
"coveralls": false,
"github_actions": false,
"lcname": "multifruits"
}