Skip to content

Commit

Permalink
TST: Xmp module
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinThoma committed Jun 9, 2022
1 parent 9118f03 commit 9211af1
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
4 changes: 2 additions & 2 deletions PyPDF2/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ def readFromStream(
return NumberObject.read_from_stream(stream)


def readHexStringFromStream(
def readHexStringFromStream( # TODO: PEP8
stream: StreamType,
) -> Union["TextStringObject", "ByteStringObject"]:
stream.read(1)
Expand All @@ -380,7 +380,7 @@ def readHexStringFromStream(
return createStringObject(b_(txt))


def readStringFromStream(
def readStringFromStream( # TODO: PEP8
stream: StreamType,
forced_encoding: Union[None, str, List[str], Dict[int, str]] = None,
) -> Union["TextStringObject", "ByteStringObject"]:
Expand Down
2 changes: 1 addition & 1 deletion PyPDF2/xmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ class XmpInformation(PdfObject):
def __init__(self, stream: ContentStream) -> None:
self.stream = stream
doc_root: Document = parseString(self.stream.get_data())
self.rdfRoot: XmlElement = doc_root.getElementsByTagNameNS(
self.rdfRoot: XmlElement = doc_root.getElementsByTagNameNS( # TODO: PEP8
RDF_NAMESPACE, "RDF"
)[0]
self.cache: Dict[Any, Any] = {}
Expand Down
44 changes: 44 additions & 0 deletions tests/test_xmp.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import os
from datetime import datetime
from tokenize import Number

import pytest

import PyPDF2.generic
import PyPDF2.xmp
from PyPDF2 import PdfReader

Expand Down Expand Up @@ -54,9 +56,51 @@ def test_regression_issue774():
assert date.minute == 23
assert date.second == 34
assert date.microsecond == 123000
with pytest.raises(ValueError) as exc:
PyPDF2.xmp._converter_date("today")
assert exc.value.args[0].startswith("Invalid date format")

date = PyPDF2.xmp._converter_date("2021-04-28T12:23:01-03:00")
assert date.year == 2021
assert date.month == 4
assert date.day == 28
assert date.hour == 15
assert date.minute == 23
assert date.second == 1
assert date.microsecond == 0


def test_regression_issue914():
path = os.path.join(RESOURCE_ROOT, "issue-914-xmp-data.pdf")
reader = PdfReader(path)
assert reader.xmp_metadata.xmp_modifyDate == datetime(2022, 4, 9, 15, 22, 43)


@pytest.mark.parametrize(
"x",
["a", 42, 3.141, False, True],
)
def test_identity(x):
assert PyPDF2.xmp._identity(x) == x


# def test_getter_bag():
# f = PyPDF2.xmp._getter_bag("namespace", "name")
# class Tst: # to replace pdf
# strict = False

# reader = PdfReader(os.path.join(RESOURCE_ROOT, "commented-xmp.pdf"))
# xmp_info = reader.xmp_metadata
# # <?xpacket begin='' id='W5M0MpCehiHzreSzNTczkc9d'?>
# # <x:xmpmeta xmlns:x='adobe:ns:meta/' x:xmptk='Image::ExifTool 11.88'>
# # <rdf:RDF xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'>

# # <rdf:Description rdf:about=''
# # xmlns:tiff='http://ns.adobe.com/tiff/1.0/'>
# # <tiff:Artist>me</tiff:Artist>
# # </rdf:Description>
# # </rdf:RDF>
# # </x:xmpmeta>

# assert xmp_info is not None
# f(xmp_info)

0 comments on commit 9211af1

Please sign in to comment.