Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ruff to autosort imports #995

Merged
merged 8 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 2 additions & 22 deletions .github/workflows/actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,8 @@ env:
default-python: "3.10"

jobs:

check-code-formatting:
name: Check 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

check-coding-style:
name: Check coding style
name: Format and check coding style
runs-on: ubuntu-latest
steps:
- name: Checkout code
Expand All @@ -49,7 +30,7 @@ jobs:
python -m pip install nox
- name: Check coding style
run: |
nox --error-on-missing-interpreters --non-interactive --session lint
nox --error-on-missing-interpreters --non-interactive --session format

check-static-types:
name: Check static types
Expand Down Expand Up @@ -122,7 +103,6 @@ jobs:
name: Publish to PyPi
runs-on: ubuntu-latest
needs:
- check-code-formatting
- check-coding-style
- check-static-types
- tests
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,5 @@ Pipfile
Pipfile.lock
.noseids
.vscode/
pyproject.toml
poetry.lock
.eggs
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [Unreleased]

### Changed

- Using absolute instead of relative imports ([[#995](https://github.com/pdfminer/pdfminer.six/pull/995)])

### Deprecated

- The third argument (generation number) to `PDFObjRef` ([#972](https://github.com/pdfminer/pdfminer.six/pull/972))
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
4 changes: 2 additions & 2 deletions fuzzing/extract_text_to_fp_fuzzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,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_to_fp
from pdfminer.psexceptions import PSException
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
9 changes: 5 additions & 4 deletions fuzzing/page_extraction_fuzzer.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#!/usr/bin/env python3
import atheris
import sys

import atheris

from fuzzing.fuzzed_data_provider import PdfminerFuzzedDataProvider

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_pages
from pdfminer.psexceptions import PSException
Expand All @@ -29,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
31 changes: 19 additions & 12 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,20 @@

import nox


PYTHON_ALL_VERSIONS = ["3.8", "3.9", "3.10", "3.11", "3.12"]
PYTHON_MODULES = ["fuzzing", "pdfminer", "tools", "tests", "noxfile.py", "setup.py"]


@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 @@ -49,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",
)
2 changes: 1 addition & 1 deletion pdfminer/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from importlib.metadata import version, PackageNotFoundError
from importlib.metadata import PackageNotFoundError, version

try:
__version__ = version("pdfminer.six")
Expand Down
10 changes: 7 additions & 3 deletions pdfminer/_saslprep.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
__all__ = ["saslprep"]

import stringprep
from typing import Callable, Tuple
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
2 changes: 1 addition & 1 deletion pdfminer/casting.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Optional, Any
from typing import Any, Optional


def safe_int(o: Any) -> Optional[int]:
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
Loading