Tasty multipart form data parser built with cython.
Project description
# 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/
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/
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
multifruits-0.1.0.tar.gz
(57.1 kB
view details)
Built Distribution
File details
Details for the file multifruits-0.1.0.tar.gz
.
File metadata
- Download URL: multifruits-0.1.0.tar.gz
- Upload date:
- Size: 57.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 982009cc6cf48ee119fcd0eff85064164c9a0fe78176c1e147102df0efc9598c |
|
MD5 | 2bdf8c9d67b87158543c88c6d9f78697 |
|
BLAKE2b-256 | 071d376e22deb79d95493bf58cb5a587db3f4215965d7b6561d18c650e9e4277 |
File details
Details for the file multifruits-0.1.0-cp36-cp36m-macosx_10_6_intel.whl
.
File metadata
- Download URL: multifruits-0.1.0-cp36-cp36m-macosx_10_6_intel.whl
- Upload date:
- Size: 71.6 kB
- Tags: CPython 3.6m, macOS 10.6+ intel
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5c4017a3f73b221cefaf1d67ec68184a882c48386d361ef881474d4d6c3b4d56 |
|
MD5 | 708200aa39788ca996470eaa4e78f527 |
|
BLAKE2b-256 | 78eaf426aaada9598ba31cef32ecaa9a42ebed9836468e612dcad4a9d5d55ad8 |