Skip to content

Commit

Permalink
Use ruff and absolute imports
Browse files Browse the repository at this point in the history
  • Loading branch information
pietermarsman committed Jul 9, 2024
1 parent f5cb734 commit 9b5ec7e
Show file tree
Hide file tree
Showing 45 changed files with 3,763 additions and 3,672 deletions.
59 changes: 2 additions & 57 deletions .github/workflows/actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,45 +14,8 @@ env:
default-python: "3.10"

jobs:

check-code-imports:
name: Format import coding style
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Python ${{ env.default-python }}
uses: actions/setup-python@v4
with:
python-version: ${{ env.default-python }}
- name: Upgrade pip, Install nox
run: |
python -m pip install --upgrade pip
python -m pip install nox
- name: Check coding style
run: |
nox --error-on-missing-interpreters --non-interactive --session format_isort
check-code-autoflake:
name: Format coding style with autoflake
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Python ${{ env.default-python }}
uses: actions/setup-python@v4
with:
python-version: ${{ env.default-python }}
- name: Upgrade pip, Install nox
run: |
python -m pip install --upgrade pip
python -m pip install nox
- name: Check coding style
run: |
nox --error-on-missing-interpreters --non-interactive --session format_autoflake
check-code-formatting:
name: Format coding style with black
code-formatting:
name: Format and check code style
runs-on: ubuntu-latest
steps:
- name: Checkout code
Expand All @@ -69,24 +32,6 @@ jobs:
run: |
nox --error-on-missing-interpreters --non-interactive --session format
check-coding-style:
name: Lint coding style
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Python ${{ env.default-python }}
uses: actions/setup-python@v4
with:
python-version: ${{ env.default-python }}
- name: Upgrade pip, Install nox
run: |
python -m pip install --upgrade pip
python -m pip install nox
- name: Check coding style
run: |
nox --error-on-missing-interpreters --non-interactive --session lint
check-static-types:
name: Check static types
runs-on: ubuntu-latest
Expand Down
4 changes: 2 additions & 2 deletions fuzzing/extract_text_fuzzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

with atheris.instrument_imports():
from fuzzing.utils import (
prepare_pdfminer_fuzzing,
is_valid_byte_stream,
generate_layout_parameters,
is_valid_byte_stream,
prepare_pdfminer_fuzzing,
)
from pdfminer.high_level import extract_text

Expand Down
5 changes: 4 additions & 1 deletion fuzzing/fuzzed_data_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ def ConsumeMemoryFile(self, all_data: bool = False) -> io.BytesIO:
return io.BytesIO(self.ConsumeRandomBytes())

def ConsumeOptionalIntList(
self, max_count: int, min: int, max: int
self,
max_count: int,
min: int,
max: int,
) -> Optional[List[int]]:
if self.ConsumeBool():
count = self.ConsumeIntInRange(0, max_count)
Expand Down
2 changes: 1 addition & 1 deletion fuzzing/page_extraction_fuzzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def fuzz_one_input(data: bytes) -> None:
maxpages=fdp.ConsumeIntInRange(0, 10),
page_numbers=fdp.ConsumeOptionalIntList(10, 0, 10),
laparams=generate_layout_parameters(fdp),
)
),
)
except (AssertionError, PSException):
return
Expand Down
9 changes: 3 additions & 6 deletions fuzzing/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""
Utilities shared across the various PDF fuzzing harnesses
"""
"""Utilities shared across the various PDF fuzzing harnesses"""

import logging
from typing import Optional

Expand All @@ -12,9 +11,7 @@


def prepare_pdfminer_fuzzing() -> None:
"""
Used to disable logging of the pdfminer module
"""
"""Used to disable logging of the pdfminer module"""
logging.getLogger("pdfminer").setLevel(logging.CRITICAL)


Expand Down
48 changes: 19 additions & 29 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,16 @@
PYTHON_MODULES = ["fuzzing", "pdfminer", "tools", "tests", "noxfile.py", "setup.py"]


@nox.session
def format_isort(session):
session.install("isort")
if "CI" in os.environ:
session.run("isort", "--check", *PYTHON_MODULES)
else:
session.run("isort", *PYTHON_MODULES)


@nox.session
def format_autoflake(session):
session.install("autoflake")
if "CI" in os.environ:
session.run("autoflake", "--check-diff", *PYTHON_MODULES)
else:
session.run("autoflake", "--in-place", *PYTHON_MODULES)


@nox.session
def format(session):
session.install("black<23")
session.install("ruff==0.5.1")
# Format files locally with black, but only check in cicd
if "CI" in os.environ:
session.run("black", "--check", *PYTHON_MODULES)
session.run("ruff", "check")
session.run("ruff", "format", "--check")
else:
session.run("black", *PYTHON_MODULES)


@nox.session
def lint(session):
session.install("flake8")
session.run("flake8", *PYTHON_MODULES, "--count", "--statistics")
session.run("ruff", "check", "--fix")
session.run("ruff", "format")


@nox.session
Expand Down Expand Up @@ -66,8 +44,20 @@ def docs(session):
session.install("setuptools")
session.install("-e", ".[docs]")
session.run(
"python", "-m", "sphinx", "-b", "html", "docs/source", "docs/build/html"
"python",
"-m",
"sphinx",
"-b",
"html",
"docs/source",
"docs/build/html",
)
session.run(
"python", "-m", "sphinx", "-b", "doctest", "docs/source", "docs/build/doctest"
"python",
"-m",
"sphinx",
"-b",
"doctest",
"docs/source",
"docs/build/doctest",
)
8 changes: 6 additions & 2 deletions pdfminer/_saslprep.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import unicodedata
from typing import Callable, Tuple

from .pdfexceptions import PDFValueError
from pdfminer.pdfexceptions import PDFValueError

# RFC4013 section 2.3 prohibited output.
_PROHIBITED: Tuple[Callable[[str], bool], ...] = (
Expand Down Expand Up @@ -66,7 +66,11 @@ def saslprep(data: str, prohibit_unassigned_code_points: bool = True) -> str:
in_table_c12 = stringprep.in_table_c12
in_table_b1 = stringprep.in_table_b1
data = "".join(
["\u0020" if in_table_c12(elt) else elt for elt in data if not in_table_b1(elt)]
[
"\u0020" if in_table_c12(elt) else elt
for elt in data
if not in_table_b1(elt)
],
)

# RFC3454 section 2, step 2 - Normalize
Expand Down
3 changes: 1 addition & 2 deletions pdfminer/arcfour.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
""" Python implementation of Arcfour encryption algorithm.
"""Python implementation of Arcfour encryption algorithm.
See https://en.wikipedia.org/wiki/RC4
This code is in the public domain.
"""


from typing import Sequence


Expand Down
10 changes: 4 additions & 6 deletions pdfminer/ascii85.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
""" Python implementation of ASCII85/ASCIIHex decoder (Adobe version).
"""Python implementation of ASCII85/ASCIIHex decoder (Adobe version).
This code is in the public domain.
Expand All @@ -10,8 +10,7 @@

# ascii85decode(data)
def ascii85decode(data: bytes) -> bytes:
"""
In ASCII85 encoding, every four bytes are encoded with five ASCII
"""In ASCII85 encoding, every four bytes are encoded with five ASCII
letters, using 85 different types of characters (as 256**4 < 85**5).
When the length of the original bytes is not a multiple of 4, a special
rule is used for round up.
Expand All @@ -24,7 +23,7 @@ def ascii85decode(data: bytes) -> bytes:
out = b""
for i in iter(data):
c = bytes((i,))
if b"!" <= c and c <= b"u":
if c >= b"!" and c <= b"u":
n += 1
b = b * 85 + (ord(c) - 33)
if n == 5:
Expand All @@ -48,8 +47,7 @@ def ascii85decode(data: bytes) -> bytes:


def asciihexdecode(data: bytes) -> bytes:
"""
ASCIIHexDecode filter: PDFReference v1.4 section 3.3.1
"""ASCIIHexDecode filter: PDFReference v1.4 section 3.3.1
For each pair of ASCII hexadecimal digits (0-9 and A-F or a-f), the
ASCIIHexDecode filter produces one byte of binary data. All white-space
characters are ignored. A right angle bracket character (>) indicates
Expand Down
26 changes: 11 additions & 15 deletions pdfminer/ccitt.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
cast,
)

from .pdfexceptions import PDFException, PDFValueError
from pdfminer.pdfexceptions import PDFException, PDFValueError


def get_bytes(data: bytes) -> Iterator[int]:
Expand Down Expand Up @@ -53,7 +53,7 @@ def add(cls, root: BitParserState, v: Union[int, str], bits: str) -> None:
p: BitParserState = root
b = None
for i in range(len(bits)):
if 0 < i:
if i > 0:
assert b is not None
if p[b] is None:
p[b] = [None, None]
Expand Down Expand Up @@ -84,7 +84,6 @@ def _parse_bit(self, x: object) -> None:


class CCITTG4Parser(BitParser):

MODE = [None, None]
BitParser.add(MODE, 0, "1")
BitParser.add(MODE, +1, "011")
Expand Down Expand Up @@ -475,9 +474,7 @@ def _do_vertical(self, dx: int) -> None:
if x1 == 0:
if self._color == 1 and self._refline[x1] != self._color:
break
elif x1 == len(self._refline):
break
elif (
elif x1 == len(self._refline) or (
self._refline[x1 - 1] == self._color
and self._refline[x1] != self._color
):
Expand All @@ -501,9 +498,7 @@ def _do_pass(self) -> None:
if x1 == 0:
if self._color == 1 and self._refline[x1] != self._color:
break
elif x1 == len(self._refline):
break
elif (
elif x1 == len(self._refline) or (
self._refline[x1 - 1] == self._color
and self._refline[x1] != self._color
):
Expand All @@ -513,9 +508,7 @@ def _do_pass(self) -> None:
if x1 == 0:
if self._color == 0 and self._refline[x1] == self._color:
break
elif x1 == len(self._refline):
break
elif (
elif x1 == len(self._refline) or (
self._refline[x1 - 1] != self._color
and self._refline[x1] == self._color
):
Expand Down Expand Up @@ -550,7 +543,10 @@ def _do_uncompressed(self, bits: str) -> None:

class CCITTFaxDecoder(CCITTG4Parser):
def __init__(
self, width: int, bytealign: bool = False, reversed: bool = False
self,
width: int,
bytealign: bool = False,
reversed: bool = False,
) -> None:
CCITTG4Parser.__init__(self, width, bytealign=bytealign)
self.reversed = reversed
Expand All @@ -563,7 +559,7 @@ def output_line(self, y: int, bits: Sequence[int]) -> None:
arr = array.array("B", [0] * ((len(bits) + 7) // 8))
if self.reversed:
bits = [1 - b for b in bits]
for (i, b) in enumerate(bits):
for i, b in enumerate(bits):
if b:
arr[i // 8] += (128, 64, 32, 16, 8, 4, 2, 1)[i % 8]
self._buf += arr.tobytes()
Expand Down Expand Up @@ -598,7 +594,7 @@ def __init__(self, width: int, bytealign: bool = False) -> None:
self.img = pygame.Surface((self.width, 1000))

def output_line(self, y: int, bits: Sequence[int]) -> None:
for (x, b) in enumerate(bits):
for x, b in enumerate(bits):
if b:
self.img.set_at((x, y), (255, 255, 255))
else:
Expand Down
Loading

0 comments on commit 9b5ec7e

Please sign in to comment.