Skip to content

Commit

Permalink
Add ruff and MultipartState(IntEnum) (#96)
Browse files Browse the repository at this point in the history
* Several improvements

* Add ruff to the pipeline

* Use single quotes

* Use double quotes

* nitpicks

* Add ruff to dev optional
  • Loading branch information
Kludex authored Feb 10, 2024
1 parent 7f99806 commit 4e24a3e
Show file tree
Hide file tree
Showing 10 changed files with 637 additions and 697 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/test.yaml → .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ jobs:
run: |
python -m pip install --upgrade pip
pip install .[dev]
- name: Lint
if: matrix.python-version == '3.8'
run: |
ruff multipart tests
- name: Test with pytest
run: |
inv test
2 changes: 0 additions & 2 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@ permissions:

jobs:
deploy:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Set up Python 3.10
uses: actions/setup-python@v5
with:
Expand Down
15 changes: 8 additions & 7 deletions multipart/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
__copyright__ = "Copyright (c) 2012-2013, Andrew Dunham"
__version__ = "0.0.8"

from .multipart import FormParser, MultipartParser, OctetStreamParser, QuerystringParser, create_form_parser, parse_form

from .multipart import (
FormParser,
MultipartParser,
OctetStreamParser,
QuerystringParser,
create_form_parser,
parse_form,
__all__ = (
"FormParser",
"MultipartParser",
"OctetStreamParser",
"QuerystringParser",
"create_form_parser",
"parse_form",
)
28 changes: 14 additions & 14 deletions multipart/decoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ def write(self, data):
try:
decoded = base64.b64decode(val)
except binascii.Error:
raise DecodeError('There was an error raised while decoding '
'base64-encoded data.')
raise DecodeError("There was an error raised while decoding base64-encoded data.")

self.underlying.write(decoded)

Expand All @@ -69,7 +68,7 @@ def write(self, data):
if remaining_len > 0:
self.cache = data[-remaining_len:]
else:
self.cache = b''
self.cache = b""

# Return the length of the data to indicate no error.
return len(data)
Expand All @@ -78,7 +77,7 @@ def close(self):
"""Close this decoder. If the underlying object has a `close()`
method, this function will call it.
"""
if hasattr(self.underlying, 'close'):
if hasattr(self.underlying, "close"):
self.underlying.close()

def finalize(self):
Expand All @@ -91,11 +90,11 @@ def finalize(self):
call it.
"""
if len(self.cache) > 0:
raise DecodeError('There are %d bytes remaining in the '
'Base64Decoder cache when finalize() is called'
% len(self.cache))
raise DecodeError(
"There are %d bytes remaining in the Base64Decoder cache when finalize() is called" % len(self.cache)
)

if hasattr(self.underlying, 'finalize'):
if hasattr(self.underlying, "finalize"):
self.underlying.finalize()

def __repr__(self):
Expand All @@ -111,8 +110,9 @@ class QuotedPrintableDecoder:
:param underlying: the underlying object to pass writes to
"""

def __init__(self, underlying):
self.cache = b''
self.cache = b""
self.underlying = underlying

def write(self, data):
Expand All @@ -128,11 +128,11 @@ def write(self, data):
# If the last 2 characters have an '=' sign in it, then we won't be
# able to decode the encoded value and we'll need to save it for the
# next decoding step.
if data[-2:].find(b'=') != -1:
if data[-2:].find(b"=") != -1:
enc, rest = data[:-2], data[-2:]
else:
enc = data
rest = b''
rest = b""

# Encode and write, if we have data.
if len(enc) > 0:
Expand All @@ -146,7 +146,7 @@ def close(self):
"""Close this decoder. If the underlying object has a `close()`
method, this function will call it.
"""
if hasattr(self.underlying, 'close'):
if hasattr(self.underlying, "close"):
self.underlying.close()

def finalize(self):
Expand All @@ -161,10 +161,10 @@ def finalize(self):
# If we have a cache, write and then remove it.
if len(self.cache) > 0:
self.underlying.write(binascii.a2b_qp(self.cache))
self.cache = b''
self.cache = b""

# Finalize our underlying stream.
if hasattr(self.underlying, 'finalize'):
if hasattr(self.underlying, "finalize"):
self.underlying.finalize()

def __repr__(self):
Expand Down
20 changes: 4 additions & 16 deletions multipart/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
class FormParserError(ValueError):
"""Base error class for our form parser."""
pass


class ParseError(FormParserError):
Expand All @@ -17,30 +16,19 @@ class MultipartParseError(ParseError):
"""This is a specific error that is raised when the MultipartParser detects
an error while parsing.
"""
pass


class QuerystringParseError(ParseError):
"""This is a specific error that is raised when the QuerystringParser
detects an error while parsing.
"""
pass


class DecodeError(ParseError):
"""This exception is raised when there is a decoding error - for example
with the Base64Decoder or QuotedPrintableDecoder.
"""
pass


# On Python 3.3, IOError is the same as OSError, so we don't want to inherit
# from both of them. We handle this case below.
if IOError is not OSError: # pragma: no cover
class FileError(FormParserError, IOError, OSError):
"""Exception class for problems with the File class."""
pass
else: # pragma: no cover
class FileError(FormParserError, OSError):
"""Exception class for problems with the File class."""
pass


class FileError(FormParserError, OSError):
"""Exception class for problems with the File class."""
Loading

0 comments on commit 4e24a3e

Please sign in to comment.