Skip to content

Commit 4e24a3e

Browse files
authored
Add ruff and MultipartState(IntEnum) (#96)
* Several improvements * Add ruff to the pipeline * Use single quotes * Use double quotes * nitpicks * Add ruff to dev optional
1 parent 7f99806 commit 4e24a3e

File tree

10 files changed

+637
-697
lines changed

10 files changed

+637
-697
lines changed

.github/workflows/test.yaml renamed to .github/workflows/main.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ jobs:
2626
run: |
2727
python -m pip install --upgrade pip
2828
pip install .[dev]
29+
- name: Lint
30+
if: matrix.python-version == '3.8'
31+
run: |
32+
ruff multipart tests
2933
- name: Test with pytest
3034
run: |
3135
inv test

.github/workflows/publish.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,10 @@ permissions:
1818

1919
jobs:
2020
deploy:
21-
2221
runs-on: ubuntu-latest
2322

2423
steps:
2524
- uses: actions/checkout@v4
26-
2725
- name: Set up Python 3.10
2826
uses: actions/setup-python@v5
2927
with:

multipart/__init__.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
__copyright__ = "Copyright (c) 2012-2013, Andrew Dunham"
55
__version__ = "0.0.8"
66

7+
from .multipart import FormParser, MultipartParser, OctetStreamParser, QuerystringParser, create_form_parser, parse_form
78

8-
from .multipart import (
9-
FormParser,
10-
MultipartParser,
11-
OctetStreamParser,
12-
QuerystringParser,
13-
create_form_parser,
14-
parse_form,
9+
__all__ = (
10+
"FormParser",
11+
"MultipartParser",
12+
"OctetStreamParser",
13+
"QuerystringParser",
14+
"create_form_parser",
15+
"parse_form",
1516
)

multipart/decoders.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ def write(self, data):
5959
try:
6060
decoded = base64.b64decode(val)
6161
except binascii.Error:
62-
raise DecodeError('There was an error raised while decoding '
63-
'base64-encoded data.')
62+
raise DecodeError("There was an error raised while decoding base64-encoded data.")
6463

6564
self.underlying.write(decoded)
6665

@@ -69,7 +68,7 @@ def write(self, data):
6968
if remaining_len > 0:
7069
self.cache = data[-remaining_len:]
7170
else:
72-
self.cache = b''
71+
self.cache = b""
7372

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

8483
def finalize(self):
@@ -91,11 +90,11 @@ def finalize(self):
9190
call it.
9291
"""
9392
if len(self.cache) > 0:
94-
raise DecodeError('There are %d bytes remaining in the '
95-
'Base64Decoder cache when finalize() is called'
96-
% len(self.cache))
93+
raise DecodeError(
94+
"There are %d bytes remaining in the Base64Decoder cache when finalize() is called" % len(self.cache)
95+
)
9796

98-
if hasattr(self.underlying, 'finalize'):
97+
if hasattr(self.underlying, "finalize"):
9998
self.underlying.finalize()
10099

101100
def __repr__(self):
@@ -111,8 +110,9 @@ class QuotedPrintableDecoder:
111110
112111
:param underlying: the underlying object to pass writes to
113112
"""
113+
114114
def __init__(self, underlying):
115-
self.cache = b''
115+
self.cache = b""
116116
self.underlying = underlying
117117

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

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

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

166166
# Finalize our underlying stream.
167-
if hasattr(self.underlying, 'finalize'):
167+
if hasattr(self.underlying, "finalize"):
168168
self.underlying.finalize()
169169

170170
def __repr__(self):

multipart/exceptions.py

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
class FormParserError(ValueError):
22
"""Base error class for our form parser."""
3-
pass
43

54

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

2220

2321
class QuerystringParseError(ParseError):
2422
"""This is a specific error that is raised when the QuerystringParser
2523
detects an error while parsing.
2624
"""
27-
pass
2825

2926

3027
class DecodeError(ParseError):
3128
"""This exception is raised when there is a decoding error - for example
3229
with the Base64Decoder or QuotedPrintableDecoder.
3330
"""
34-
pass
35-
36-
37-
# On Python 3.3, IOError is the same as OSError, so we don't want to inherit
38-
# from both of them. We handle this case below.
39-
if IOError is not OSError: # pragma: no cover
40-
class FileError(FormParserError, IOError, OSError):
41-
"""Exception class for problems with the File class."""
42-
pass
43-
else: # pragma: no cover
44-
class FileError(FormParserError, OSError):
45-
"""Exception class for problems with the File class."""
46-
pass
31+
32+
33+
class FileError(FormParserError, OSError):
34+
"""Exception class for problems with the File class."""

0 commit comments

Comments
 (0)