working xref db
This commit is contained in:
1 parent
cfc2dfca47
commit
e2a92517cd
12 files changed
+517
-227
No files matched your search
+1
-1
@@ -222,7 +222,7 @@ class Basics(TestCase):
|
||||
|
||||
class Lexer(TestCase):
|
||||
def run_test(self, raw: bytes, expected: List[Token]):
|
||||
tokenizer = PDFLexer(io.BytesIO(raw))
|
||||
tokenizer = PDFLexer.from_bytes(raw)
|
||||
for i, (etoken, token) in enumerate(zip(expected, tokenizer.iter_tokens()), 1):
|
||||
self.assertEqual(token, etoken, f"Failed at example #{i}")
|
||||
try:
|
||||
|
||||
+41
-51
@@ -1,12 +1,13 @@
|
||||
import io
|
||||
from pathlib import Path
|
||||
from pdfmajor.parser.objects.collections import PDFDictionary
|
||||
from pdfmajor.parser.objects.primitives import PDFHexString, PDFInteger, PDFString
|
||||
|
||||
from typing import Callable, List
|
||||
from typing import List
|
||||
from decimal import Decimal
|
||||
from unittest import TestCase
|
||||
|
||||
from pdfmajor.streambuffer import StreamEOF
|
||||
from pdfmajor.parser.objects.indirect import IndirectObject, IndirectObjectCollection
|
||||
from pdfmajor.parser.objects.indirect import IndirectObject
|
||||
from pdfmajor.parser.objects.base import PDFObject
|
||||
from pdfmajor.parser.objects.comment import PDFComment
|
||||
from pdfmajor.parser.objects.stream import PDFStream
|
||||
@@ -17,9 +18,8 @@ CURRENT_FOLDER = Path(__file__).parent
|
||||
|
||||
class Collections(TestCase):
|
||||
def test_parse_dict(self):
|
||||
parser = PDFParser(
|
||||
io.BytesIO(
|
||||
br"""<< /Type /Example
|
||||
parser = PDFParser.from_bytes(
|
||||
br"""<< /Type /Example
|
||||
/Subtype /DictionaryExample
|
||||
/Version 0.01
|
||||
/IntegerItem 12
|
||||
@@ -29,8 +29,7 @@ class Collections(TestCase):
|
||||
/Item2 true
|
||||
/LastItem ( not! )
|
||||
/VeryLastItem ( OK ) >>
|
||||
>>"""
|
||||
),
|
||||
>>""",
|
||||
strict=True,
|
||||
)
|
||||
obj = next(parser.iter_objects())
|
||||
@@ -52,8 +51,8 @@ class Collections(TestCase):
|
||||
)
|
||||
|
||||
def test_parse_array(self):
|
||||
parser = PDFParser(
|
||||
io.BytesIO(br"""[ 549 3.14 false ( Ralph ) /SomeName ]"""),
|
||||
parser = PDFParser.from_bytes(
|
||||
br"""[ 549 3.14 false ( Ralph ) /SomeName ]""",
|
||||
strict=True,
|
||||
)
|
||||
obj = next(parser.iter_objects())
|
||||
@@ -63,13 +62,11 @@ class Collections(TestCase):
|
||||
)
|
||||
|
||||
def test_parse_nested_array(self):
|
||||
parser = PDFParser(
|
||||
io.BytesIO(
|
||||
br"""[ [10 30] [22 33] [
|
||||
parser = PDFParser.from_bytes(
|
||||
br"""[ [10 30] [22 33] [
|
||||
<< /x 0 /y 5 >>
|
||||
<< /x 10 /y 5 >>
|
||||
] ]"""
|
||||
),
|
||||
] ]""",
|
||||
strict=True,
|
||||
)
|
||||
obj = next(parser.iter_objects())
|
||||
@@ -88,9 +85,8 @@ class Collections(TestCase):
|
||||
|
||||
class IndirectObjects(TestCase):
|
||||
def test_parse_simple_indobj(self):
|
||||
parser = PDFParser(
|
||||
io.BytesIO(
|
||||
br"""
|
||||
parser = PDFParser.from_bytes(
|
||||
br"""
|
||||
12 1 obj (Bring) endobj
|
||||
100 0 obj
|
||||
<33>
|
||||
@@ -98,47 +94,45 @@ class IndirectObjects(TestCase):
|
||||
13 8
|
||||
obj
|
||||
<< /x 1 /y 3>> endobj
|
||||
"""
|
||||
),
|
||||
""",
|
||||
strict=True,
|
||||
)
|
||||
expected = [
|
||||
(12, 1, "Bring"),
|
||||
(100, 0, b"3"),
|
||||
(13, 8, {"x": 1, "y": 3}),
|
||||
IndirectObject(12, 1, 18, PDFString("Bring", 0, 0)),
|
||||
IndirectObject(100, 0, 25, PDFHexString(b"3", 0, 0)),
|
||||
IndirectObject(
|
||||
13,
|
||||
8,
|
||||
144,
|
||||
PDFDictionary.from_dict(
|
||||
{"x": PDFInteger(1, 0, 0), "y": PDFInteger(3, 0, 0)}
|
||||
),
|
||||
),
|
||||
]
|
||||
for obj, eobj in zip(parser.iter_objects(), expected):
|
||||
self.assertIsInstance(obj, IndirectObject)
|
||||
self.assertEqual(
|
||||
obj,
|
||||
parser.state.indirect_object_collection.get_indobject(eobj[0], eobj[1]),
|
||||
)
|
||||
self.assertEqual(obj.to_python(), eobj[2])
|
||||
self.assertEqual(obj, eobj)
|
||||
with self.assertRaises(StreamEOF):
|
||||
next(parser.iter_objects())
|
||||
|
||||
def test_parse_obj_with_references(self):
|
||||
parser = PDFParser(
|
||||
io.BytesIO(
|
||||
br"""
|
||||
parser = PDFParser.from_bytes(
|
||||
br"""
|
||||
12 1 obj (Bring) endobj
|
||||
10 2 obj << /lastone 12 1 R >> endobj
|
||||
|
||||
"""
|
||||
),
|
||||
""",
|
||||
strict=True,
|
||||
)
|
||||
expected = [
|
||||
parser.state.indirect_object_collection.get_indobject(12, 1),
|
||||
parser.state.indirect_object_collection.get_indobject(10, 2),
|
||||
"Bring",
|
||||
{"lastone": "Bring"},
|
||||
]
|
||||
for obj, exp in zip(parser.iter_objects(), expected):
|
||||
self.assertEqual(obj, exp)
|
||||
self.assertEqual(obj.to_python(), exp)
|
||||
with self.assertRaises(StreamEOF):
|
||||
next(parser.iter_objects())
|
||||
sec_obj = parser.state.indirect_object_collection.get_indobject(
|
||||
10, 2
|
||||
).get_object()
|
||||
sec_obj = parser.db.get_object(10, 2)
|
||||
self.assertEqual(
|
||||
sec_obj.to_python(),
|
||||
{"lastone": "Bring"},
|
||||
@@ -147,13 +141,11 @@ class IndirectObjects(TestCase):
|
||||
def run_test(
|
||||
self,
|
||||
raw: bytes,
|
||||
expected: Callable[[IndirectObjectCollection], List[PDFObject]],
|
||||
expected: List[PDFObject],
|
||||
):
|
||||
parser = PDFParser(io.BytesIO(raw), strict=True)
|
||||
parser = PDFParser.from_bytes(raw, strict=True)
|
||||
for i, (eobj, obj) in enumerate(
|
||||
zip(
|
||||
expected(parser.state.indirect_object_collection), parser.iter_objects()
|
||||
),
|
||||
zip(expected, parser.iter_objects()),
|
||||
1,
|
||||
):
|
||||
self.assertEqual(obj, eobj, f"Failed at example #{i}")
|
||||
@@ -172,20 +164,19 @@ class IndirectObjects(TestCase):
|
||||
endobj %closing the object
|
||||
11 1 obj 2 endobj
|
||||
""",
|
||||
expected=lambda indobjc: [
|
||||
expected=[
|
||||
PDFComment(b" object references", (22, 41)),
|
||||
PDFComment(b" start of object", (62, 79)),
|
||||
PDFComment(b" object content", (110, 126)),
|
||||
indobjc.get_indobject(10, 0),
|
||||
IndirectObject(10, 0, 58, PDFString("testing", 0, 0)),
|
||||
PDFComment(b"closing the object", (150, 169)),
|
||||
indobjc.get_indobject(11, 1),
|
||||
IndirectObject(11, 1, 191, PDFInteger(2, 0, 0)),
|
||||
],
|
||||
)
|
||||
|
||||
def test_parse_stream(self):
|
||||
parser = PDFParser(
|
||||
io.BytesIO(
|
||||
br"""
|
||||
parser = PDFParser.from_bytes(
|
||||
br"""
|
||||
5 0 obj
|
||||
<< /Type /XRef /Length 124 /Filter /FlateDecode >>
|
||||
stream
|
||||
@@ -193,7 +184,6 @@ stream
|
||||
<80>endstream
|
||||
endobj
|
||||
"""
|
||||
)
|
||||
)
|
||||
for obj in parser.iter_objects():
|
||||
self.assertIsInstance(obj, IndirectObject)
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
from pathlib import Path
|
||||
from unittest import TestCase
|
||||
|
||||
from pdfmajor.streambuffer import BufferStream
|
||||
from pdfmajor.parser.xref import iter_over_xref
|
||||
|
||||
CURRENT_FOLDER = Path(__file__).parent
|
||||
|
||||
|
||||
class Standard(TestCase):
|
||||
def test_char_doc(self):
|
||||
with (CURRENT_FOLDER / "samples" / "pdf" / "chars.pdf").open("rb") as fp:
|
||||
buffer = BufferStream(fp)
|
||||
for xref in iter_over_xref(buffer):
|
||||
print(xref)
|
||||
Reference in new issue
Block a user