- using a common generic type for PDFObject as oppose to Union (needed a common data contract)
- parsing arrays
This commit is contained in:
1 parent
6d9a57c722
commit
4f8bcf1fff
6 files changed
+104
-53
No files matched your search
@@ -178,3 +178,7 @@ class TokenArray(Token[TArrayValue]):
|
||||
"""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
TokenComplexType = Union[TokenDictionary, TokenArray]
|
||||
TokenComplexTypeVal = Union[TDictValue, TArrayValue]
|
||||
@@ -1,12 +1,20 @@
|
||||
import io
|
||||
from pdfmajor.parser_v2.exceptions import ParserError
|
||||
from pdfmajor.parser_v2.objects import PDFArray, PDFDictionary, PDFPrimivite, PDFObject
|
||||
from typing import Iterator, List, Optional
|
||||
from pdfmajor.parser_v2.objects import (
|
||||
PDFArray,
|
||||
PDFDictionary,
|
||||
PDFPrimivite,
|
||||
PDFObject,
|
||||
PDFComplexType,
|
||||
)
|
||||
from typing import Any, Callable, Iterator, List, Optional
|
||||
from pdfmajor.lexer.token import (
|
||||
TArrayValue,
|
||||
TDictValue,
|
||||
TokenArray,
|
||||
TokenDictionary,
|
||||
TokenComplexType,
|
||||
TokenComplexTypeVal,
|
||||
is_primitive,
|
||||
)
|
||||
from pdfmajor.lexer import PDFLexer
|
||||
@@ -59,40 +67,45 @@ class PDFParser:
|
||||
else:
|
||||
cur_ctx.push(obj)
|
||||
elif isinstance(token, TokenArray):
|
||||
if token.value == TArrayValue.OPEN:
|
||||
self.context_stack.append(PDFParseContext(PDFArray()))
|
||||
elif len(self.context_stack) > 0:
|
||||
last_ctx = self.context_stack.pop()
|
||||
if isinstance(last_ctx.pdfobject, PDFArray):
|
||||
cur_ctx = self.current_context
|
||||
if cur_ctx is None:
|
||||
yield last_ctx.pdfobject
|
||||
else:
|
||||
cur_ctx.push(last_ctx.pdfobject)
|
||||
elif self.strict:
|
||||
raise ParserError(f"Invalid end of array specified at {token}")
|
||||
elif self.strict:
|
||||
raise ParserError(
|
||||
f"Invalid end of array specified at {token} with no start"
|
||||
)
|
||||
obj = self.__deal_with_obj(token, PDFArray, TArrayValue.OPEN)
|
||||
if obj is not None:
|
||||
yield obj
|
||||
elif isinstance(token, TokenDictionary):
|
||||
if token.value == TDictValue.OPEN:
|
||||
self.context_stack.append(PDFParseContext(PDFDictionary()))
|
||||
elif len(self.context_stack) > 0:
|
||||
last_ctx = self.context_stack.pop()
|
||||
if isinstance(last_ctx.pdfobject, PDFDictionary):
|
||||
cur_ctx = self.current_context
|
||||
if cur_ctx is None:
|
||||
yield last_ctx.pdfobject
|
||||
else:
|
||||
cur_ctx.push(last_ctx.pdfobject)
|
||||
elif self.strict:
|
||||
raise ParserError(
|
||||
f"Invalid end of dictionary specified at {token}"
|
||||
)
|
||||
elif self.strict:
|
||||
raise ParserError(
|
||||
f"Invalid end of dictionary specified at {token} with no start"
|
||||
)
|
||||
obj = self.__deal_with_obj(token, PDFDictionary, TDictValue.OPEN)
|
||||
if obj is not None:
|
||||
yield obj
|
||||
else:
|
||||
raise ParserError(f"Invalid token provided {token}")
|
||||
|
||||
def __deal_with_obj(
|
||||
self,
|
||||
token: TokenComplexType,
|
||||
cls_const: Any,
|
||||
open_t: TokenComplexTypeVal,
|
||||
) -> Optional[PDFObject]:
|
||||
"""generic method for processing things that have a start and end token
|
||||
|
||||
Args:
|
||||
token (TokenComplexType)
|
||||
cls_const (Any): the class constructor for the object
|
||||
open_t (TokenComplexTypeVal): the value represnting 'open' for the object
|
||||
|
||||
Returns:
|
||||
Optional[PDFObject]
|
||||
"""
|
||||
if token.value == open_t:
|
||||
self.context_stack.append(PDFParseContext(cls_const()))
|
||||
elif len(self.context_stack) > 0:
|
||||
last_ctx = self.context_stack.pop()
|
||||
if isinstance(last_ctx.pdfobject, cls_const):
|
||||
cur_ctx = self.current_context
|
||||
if cur_ctx is None:
|
||||
return last_ctx.pdfobject
|
||||
else:
|
||||
cur_ctx.push(last_ctx.pdfobject)
|
||||
elif self.strict:
|
||||
raise ParserError(f"Invalid end of {cls_const} specified at {token}")
|
||||
elif self.strict:
|
||||
raise ParserError(
|
||||
f"Invalid end of {cls_const} specified at {token} with no start"
|
||||
)
|
||||
@@ -32,7 +32,7 @@ class PDFParseContext:
|
||||
ParserError: raises if an invalid complex object is used
|
||||
"""
|
||||
if isinstance(self.pdfobject, PDFArray):
|
||||
self.pdfobject.append(item)
|
||||
self.pdfobject.pass_item(item)
|
||||
elif isinstance(self.pdfobject, PDFDictionary):
|
||||
self.pdfobject.pass_item(item)
|
||||
elif isinstance(self.pdfobject, PDFStream):
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
# (Boolean values, Integer and Real numbers, Strings, Names), Arrays, Dictionaries, Streams, and the null object
|
||||
|
||||
|
||||
from abc import ABCMeta, abstractmethod
|
||||
from pdfmajor.execptions import ParserError
|
||||
from pdfmajor.lexer.token import PDFName, TokenName, TokenPrimitive
|
||||
from typing import Dict, Generic, List, NamedTuple, Optional, TypeVar, Union
|
||||
@@ -8,47 +9,71 @@ from typing import Dict, Generic, List, NamedTuple, Optional, TypeVar, Union
|
||||
TToken = TypeVar("TToken", bound=TokenPrimitive)
|
||||
|
||||
|
||||
class PDFPrimivite(Generic[TToken]):
|
||||
class PDFObject(metaclass=ABCMeta):
|
||||
"""A common abstract class that other PDF Objects will extend"""
|
||||
|
||||
@abstractmethod
|
||||
def get_value(self):
|
||||
"""return the base value of this object"""
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
def to_python(self):
|
||||
"""returns the python variation of this object"""
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
class PDFPrimivite(Generic[TToken], PDFObject):
|
||||
def __init__(self, token: TToken) -> None:
|
||||
self.token: TToken = token
|
||||
|
||||
@property
|
||||
def value(self):
|
||||
def get_value(self):
|
||||
return self.token.value
|
||||
|
||||
def to_python(self):
|
||||
return self.value
|
||||
|
||||
class PDFDictionary(Dict[PDFName, "PDFObject"]):
|
||||
|
||||
class PDFDictionary(Dict[PDFName, PDFObject], PDFObject):
|
||||
def __init__(self, strict: bool = False) -> None:
|
||||
super().__init__()
|
||||
self.name: Optional[PDFName] = None
|
||||
self.strict = strict
|
||||
|
||||
def pass_item(self, item: "PDFObject"):
|
||||
def pass_item(self, item: PDFObject):
|
||||
if self.name is None:
|
||||
if isinstance(item, PDFPrimivite) and isinstance(item.token, TokenName):
|
||||
self.name = item.value
|
||||
self.name = item.get_value()
|
||||
if self.strict:
|
||||
raise ParserError("Invalid token provided to dictionary")
|
||||
else:
|
||||
self[self.name] = item.value
|
||||
self[self.name] = item.get_value()
|
||||
self.name = None
|
||||
|
||||
@property
|
||||
def value(self):
|
||||
def get_value(self):
|
||||
return self
|
||||
|
||||
def to_python(self):
|
||||
return dict(self)
|
||||
|
||||
|
||||
class PDFArray(List["PDFObject"]):
|
||||
@property
|
||||
def value(self):
|
||||
class PDFArray(List[PDFObject], PDFObject):
|
||||
def get_value(self):
|
||||
return self
|
||||
|
||||
def pass_item(self, item: PDFObject):
|
||||
self.append(item.get_value())
|
||||
|
||||
class PDFStream(object):
|
||||
@property
|
||||
def value(self):
|
||||
def to_python(self):
|
||||
return list(self)
|
||||
|
||||
|
||||
class PDFStream(PDFObject):
|
||||
def get_value(self):
|
||||
return self
|
||||
|
||||
def to_python(self):
|
||||
return self
|
||||
|
||||
|
||||
PDFComplexType = Union[PDFDictionary, PDFArray, PDFStream]
|
||||
PDFObject = Union[PDFComplexType, PDFPrimivite]
|
||||
Whitespace-only changes.
@@ -39,3 +39,12 @@ class Basic(TestCase):
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
def test_parse_array(self):
|
||||
parser = PDFParser(
|
||||
io.BytesIO(br"""[ 549 3.14 false ( Ralph ) /SomeName ]""")
|
||||
)
|
||||
obj = next(parser.iter_objects())
|
||||
self.assertListEqual(
|
||||
obj, [549, Decimal("3.14"), False, " Ralph ", PDFName("SomeName")]
|
||||
)
|
||||
Reference in new issue
Block a user