From 4f8bcf1fff74c3c5b27054264936f2bedc9714bb Mon Sep 17 00:00:00 2001 From: Ari Sosnovsky Date: Sat, 17 Oct 2020 22:11:51 -0400 Subject: [PATCH] - using a common generic type for PDFObject as oppose to Union (needed a common data contract) - parsing arrays --- pdfmajor/lexer/token.py | 4 ++ pdfmajor/parser_v2/__init__.py | 85 ++++++++++++++++++++-------------- pdfmajor/parser_v2/context.py | 2 +- pdfmajor/parser_v2/objects.py | 57 ++++++++++++++++------- pdfmajor/parser_v2/util.py | 0 tests/test_parser.py | 9 ++++ 6 files changed, 104 insertions(+), 53 deletions(-) create mode 100644 pdfmajor/parser_v2/util.py diff --git a/pdfmajor/lexer/token.py b/pdfmajor/lexer/token.py index 5f3cd1f..17106ae 100644 --- a/pdfmajor/lexer/token.py +++ b/pdfmajor/lexer/token.py @@ -178,3 +178,7 @@ class TokenArray(Token[TArrayValue]): """ pass + + +TokenComplexType = Union[TokenDictionary, TokenArray] +TokenComplexTypeVal = Union[TDictValue, TArrayValue] diff --git a/pdfmajor/parser_v2/__init__.py b/pdfmajor/parser_v2/__init__.py index 33180fc..b2e8d82 100644 --- a/pdfmajor/parser_v2/__init__.py +++ b/pdfmajor/parser_v2/__init__.py @@ -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" + ) diff --git a/pdfmajor/parser_v2/context.py b/pdfmajor/parser_v2/context.py index b346984..456e469 100644 --- a/pdfmajor/parser_v2/context.py +++ b/pdfmajor/parser_v2/context.py @@ -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): diff --git a/pdfmajor/parser_v2/objects.py b/pdfmajor/parser_v2/objects.py index 60a7af8..ec11dfd 100644 --- a/pdfmajor/parser_v2/objects.py +++ b/pdfmajor/parser_v2/objects.py @@ -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] diff --git a/pdfmajor/parser_v2/util.py b/pdfmajor/parser_v2/util.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_parser.py b/tests/test_parser.py index 15500a1..36d19ce 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -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")] + )