Skip to content

Commit

Permalink
Replace more os.path usages with pathlib
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinThoma committed Jul 31, 2022
1 parent 8e4e3a7 commit 11d8340
Show file tree
Hide file tree
Showing 12 changed files with 33 additions and 35 deletions.
21 changes: 11 additions & 10 deletions tests/bench.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import os
from pathlib import Path

import PyPDF2
from PyPDF2 import PdfReader, Transformation
from PyPDF2.generic import Destination

TESTS_ROOT = os.path.abspath(os.path.dirname(__file__))
PROJECT_ROOT = os.path.dirname(TESTS_ROOT)
RESOURCE_ROOT = os.path.join(PROJECT_ROOT, "resources")
SAMPLE_ROOT = os.path.join(PROJECT_ROOT, "sample-files")
TESTS_ROOT = Path(__file__).parent.resolve()
PROJECT_ROOT = TESTS_ROOT.parent
RESOURCE_ROOT = PROJECT_ROOT / "resources"
SAMPLE_ROOT = PROJECT_ROOT / "sample-files"


def page_ops(pdf_path, password):
pdf_path = os.path.join(RESOURCE_ROOT, pdf_path)
pdf_path = RESOURCE_ROOT / pdf_path

reader = PdfReader(pdf_path)

Expand Down Expand Up @@ -50,10 +51,10 @@ def test_page_operations(benchmark):


def merge():
pdf_path = os.path.join(RESOURCE_ROOT, "crazyones.pdf")
outline = os.path.join(RESOURCE_ROOT, "pdflatex-outline.pdf")
pdf_forms = os.path.join(RESOURCE_ROOT, "pdflatex-forms.pdf")
pdf_pw = os.path.join(RESOURCE_ROOT, "libreoffice-writer-password.pdf")
pdf_path = RESOURCE_ROOT / "crazyones.pdf"
outline = RESOURCE_ROOT / "pdflatex-outline.pdf"
pdf_forms = RESOURCE_ROOT / "pdflatex-forms.pdf"
pdf_pw = RESOURCE_ROOT / "libreoffice-writer-password.pdf"

file_merger = PyPDF2.PdfMerger()

Expand Down Expand Up @@ -126,5 +127,5 @@ def text_extraction(pdf_path):


def test_text_extraction(benchmark):
file_path = os.path.join(SAMPLE_ROOT, "009-pdflatex-geotopo/GeoTopo.pdf")
file_path = SAMPLE_ROOT / "009-pdflatex-geotopo/GeoTopo.pdf"
benchmark(text_extraction, file_path)
4 changes: 2 additions & 2 deletions tests/test_basic_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

from PyPDF2 import PdfReader, PdfWriter

TESTS_ROOT = os.path.abspath(os.path.dirname(__file__))
PROJECT_ROOT = Path(os.path.dirname(TESTS_ROOT))
TESTS_ROOT = Path(__file__).parent.resolve()
PROJECT_ROOT = TESTS_ROOT.parent
RESOURCE_ROOT = PROJECT_ROOT / "resources"


Expand Down
5 changes: 2 additions & 3 deletions tests/test_encryption.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
from pathlib import Path

import pytest
Expand All @@ -15,8 +14,8 @@
except ImportError:
HAS_PYCRYPTODOME = False

TESTS_ROOT = os.path.abspath(os.path.dirname(__file__))
PROJECT_ROOT = Path(os.path.dirname(TESTS_ROOT))
TESTS_ROOT = Path(__file__).parent.resolve()
PROJECT_ROOT = TESTS_ROOT.parent
RESOURCE_ROOT = PROJECT_ROOT / "resources"


Expand Down
4 changes: 2 additions & 2 deletions tests/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@

from . import get_pdf_from_url

TESTS_ROOT = os.path.abspath(os.path.dirname(__file__))
PROJECT_ROOT = Path(os.path.dirname(TESTS_ROOT))
TESTS_ROOT = Path(__file__).parent.resolve()
PROJECT_ROOT = TESTS_ROOT.parent
RESOURCE_ROOT = PROJECT_ROOT / "resources"


Expand Down
5 changes: 2 additions & 3 deletions tests/test_javascript.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import os
from pathlib import Path

import pytest

from PyPDF2 import PdfReader, PdfWriter

# Configure path environment
TESTS_ROOT = os.path.abspath(os.path.dirname(__file__))
PROJECT_ROOT = Path(os.path.dirname(TESTS_ROOT))
TESTS_ROOT = Path(__file__).parent.resolve()
PROJECT_ROOT = TESTS_ROOT.parent
RESOURCE_ROOT = PROJECT_ROOT / "resources"


Expand Down
4 changes: 2 additions & 2 deletions tests/test_merger.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

from . import get_pdf_from_url

TESTS_ROOT = os.path.abspath(os.path.dirname(__file__))
PROJECT_ROOT = Path(os.path.dirname(TESTS_ROOT))
TESTS_ROOT = Path(__file__).parent.resolve()
PROJECT_ROOT = TESTS_ROOT.parent
RESOURCE_ROOT = PROJECT_ROOT / "resources"

sys.path.append(str(PROJECT_ROOT))
Expand Down
4 changes: 2 additions & 2 deletions tests/test_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@

from . import get_pdf_from_url, normalize_warnings

TESTS_ROOT = os.path.abspath(os.path.dirname(__file__))
PROJECT_ROOT = Path(os.path.dirname(TESTS_ROOT))
TESTS_ROOT = Path(__file__).parent.resolve()
PROJECT_ROOT = TESTS_ROOT.parent
RESOURCE_ROOT = PROJECT_ROOT / "resources"
EXTERNAL_ROOT = PROJECT_ROOT / "sample-files"

Expand Down
4 changes: 2 additions & 2 deletions tests/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
except ImportError:
HAS_PYCRYPTODOME = False

TESTS_ROOT = os.path.abspath(os.path.dirname(__file__))
PROJECT_ROOT = Path(os.path.dirname(TESTS_ROOT))
TESTS_ROOT = Path(__file__).parent.resolve()
PROJECT_ROOT = TESTS_ROOT.parent
RESOURCE_ROOT = PROJECT_ROOT / "resources"
EXTERNAL_ROOT = PROJECT_ROOT / "sample-files"

Expand Down
4 changes: 2 additions & 2 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
)
from PyPDF2.errors import PdfStreamError

TESTS_ROOT = os.path.abspath(os.path.dirname(__file__))
PROJECT_ROOT = Path(os.path.dirname(TESTS_ROOT))
TESTS_ROOT = Path(__file__).parent.resolve()
PROJECT_ROOT = TESTS_ROOT.parent
RESOURCE_ROOT = PROJECT_ROOT / "resources"


Expand Down
4 changes: 2 additions & 2 deletions tests/test_workflows.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

from . import get_pdf_from_url, normalize_warnings

TESTS_ROOT = os.path.abspath(os.path.dirname(__file__))
PROJECT_ROOT = Path(os.path.dirname(TESTS_ROOT))
TESTS_ROOT = Path(__file__).parent.resolve()
PROJECT_ROOT = TESTS_ROOT.parent
RESOURCE_ROOT = PROJECT_ROOT / "resources"

sys.path.append(str(PROJECT_ROOT))
Expand Down
4 changes: 2 additions & 2 deletions tests/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

from . import get_pdf_from_url

TESTS_ROOT = os.path.abspath(os.path.dirname(__file__))
PROJECT_ROOT = Path(os.path.dirname(TESTS_ROOT))
TESTS_ROOT = Path(__file__).parent.resolve()
PROJECT_ROOT = TESTS_ROOT.parent
RESOURCE_ROOT = PROJECT_ROOT / "resources"


Expand Down
5 changes: 2 additions & 3 deletions tests/test_xmp.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
from datetime import datetime
from io import BytesIO
from pathlib import Path
Expand All @@ -12,8 +11,8 @@

from . import get_pdf_from_url

TESTS_ROOT = os.path.abspath(os.path.dirname(__file__))
PROJECT_ROOT = Path(os.path.dirname(TESTS_ROOT))
TESTS_ROOT = Path(__file__).parent.resolve()
PROJECT_ROOT = TESTS_ROOT.parent
RESOURCE_ROOT = PROJECT_ROOT / "resources"


Expand Down

0 comments on commit 11d8340

Please sign in to comment.