Skip to content

Commit

Permalink
Merge pull request #136 from department-of-veterans-affairs/117-pypdf…
Browse files Browse the repository at this point in the history
…2-deprecation

#117 PyPDF2 deprecation
  • Loading branch information
babyjunior56 authored Aug 8, 2023
2 parents 6c29763 + ff5e477 commit 3738c8c
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 20 deletions.
2 changes: 1 addition & 1 deletion notifications_utils/field.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import re

from orderedset import OrderedSet
from flask import Markup
from markupsafe import Markup

from notifications_utils.columns import Columns
from notifications_utils.formatters import (
Expand Down
2 changes: 1 addition & 1 deletion notifications_utils/formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import mistune
import bleach
from itertools import count
from flask import Markup
from markupsafe import Markup
from . import email_with_smart_quotes_regex
from notifications_utils.sanitise_text import SanitiseSMS
import smartypants
Expand Down
11 changes: 5 additions & 6 deletions notifications_utils/pdf.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import PyPDF2
from PyPDF2 import PdfWriter
from PyPDF2.errors import PdfReadError
from pypdf import PdfWriter, PdfReader
from pypdf.errors import PdfReadError
import io


def pdf_page_count(src_pdf):
"""
Returns number of pages in a pdf file
:param PyPDF2.PdfReader src_pdf: A File object or an object that supports the standard read and seek methods
:param PdfReader src_pdf: A File object or an object that supports the standard read and seek methods
"""
try:
pdf = PyPDF2.PdfReader(src_pdf)
pdf = PdfReader(src_pdf)
except AttributeError as e:
raise PdfReadError("Could not open PDF file, stream is null", e)

Expand All @@ -25,7 +24,7 @@ def extract_page_from_pdf(src_pdf, page_number):
:param src_pdf: File object or an object that supports the standard read and seek methods similar to a File object.
:param page_number: The page number to retrieve (pages begin at zero)
"""
pdf = PyPDF2.PdfReader(src_pdf)
pdf = PdfReader(src_pdf)

if len(pdf.pages) < page_number:
raise PdfReadError("Page number requested: {} of {} does not exist in document".format(
Expand Down
2 changes: 1 addition & 1 deletion notifications_utils/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from datetime import datetime

from jinja2 import Environment, FileSystemLoader
from flask import Markup
from markupsafe import Markup
from html import unescape

from notifications_utils import SMS_CHAR_COUNT_LIMIT
Expand Down
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,17 @@
'Flask>=1.1.1',
'Flask-Redis>=0.4.0',
'Jinja2>=2.11.3',
'MarkupSafe>=2.1.3',
'mistune==0.8.4',
'monotonic>=1.6',
'orderedset>=2.0.3',
'phonenumbers~=8.12.12',
'pypdf2 ~= 3.0.1',
'pypdf >= 3.15.0',
'python-json-logger~=2.0.7',
'pytz>=2021.3',
'pyyaml==5.4.1',
'requests>=2.26.0',
'smartypants>=2.0.1',
'statsd>=3.3.0',
'statsd>=3.3.0'
]
)
2 changes: 1 addition & 1 deletion tests/test_formatters.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pytest
from flask import Markup
from markupsafe import Markup

from notifications_utils.formatters import (
unlink_govuk_escaped,
Expand Down
12 changes: 6 additions & 6 deletions tests/test_pdf.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import base64
from io import BytesIO

import PyPDF2
import pytest
from PyPDF2.errors import PdfReadError
from pypdf import PdfReader
from pypdf.errors import PdfReadError

from notifications_utils.pdf import pdf_page_count, extract_page_from_pdf
from tests.pdf_consts import one_page_pdf, multi_page_pdf, not_pdf
Expand Down Expand Up @@ -36,9 +36,9 @@ def test_extract_page_from_pdf_one_page_pdf():
file_data = base64.b64decode(one_page_pdf)
pdf_page = extract_page_from_pdf(BytesIO(file_data), 0)

pdf_original = PyPDF2.PdfReader(BytesIO(file_data))
pdf_original = PdfReader(BytesIO(file_data))

pdf_new = PyPDF2.PdfReader(BytesIO(pdf_page))
pdf_new = PdfReader(BytesIO(pdf_page))

assert pdf_original.pages[0].extract_text() == pdf_new.pages[0].extract_text()

Expand All @@ -47,9 +47,9 @@ def test_extract_page_from_pdf_multi_page_pdf():
file_data = base64.b64decode(multi_page_pdf)
pdf_page = extract_page_from_pdf(BytesIO(file_data), 4)

pdf_original = PyPDF2.PdfReader(BytesIO(file_data))
pdf_original = PdfReader(BytesIO(file_data))

pdf_new = PyPDF2.PdfReader(BytesIO(pdf_page))
pdf_new = PdfReader(BytesIO(pdf_page))

assert pdf_original.pages[4].extract_text() == pdf_new.pages[0].extract_text()
assert pdf_original.pages[3].extract_text() != pdf_new.pages[0].extract_text()
Expand Down
3 changes: 1 addition & 2 deletions tests/test_template_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
from time import process_time
import os
import pytest

from functools import partial
from unittest import mock
from flask import Markup
from markupsafe import Markup
from freezegun import freeze_time

from notifications_utils.formatters import unlink_govuk_escaped
Expand Down

0 comments on commit 3738c8c

Please sign in to comment.