added indirect Objects and stream tokens
This commit is contained in:
1 parent
adf04aa50c
commit
3a5f8f44a3
4 files changed
+95
-20
No files matched your search
@@ -1,6 +1,15 @@
|
||||
from pdfmajor.streambuffer import BufferStream
|
||||
from pdfmajor.lexer.exceptions import LexerEOF
|
||||
from pdfmajor.lexer.token import Token, TokenBoolean, TokenKeyword, TokenNull
|
||||
from pdfmajor.lexer.token import (
|
||||
TObjValue,
|
||||
TStreamValue,
|
||||
Token,
|
||||
TokenBoolean,
|
||||
TokenKeyword,
|
||||
TokenNull,
|
||||
TokenObj,
|
||||
TokenStream,
|
||||
)
|
||||
from pdfmajor.lexer.regex import END_KEYWORD
|
||||
from typing import Optional, Union
|
||||
|
||||
@@ -31,6 +40,14 @@ def parse_keyword(buffer: BufferStream, initialpos: Optional[int] = None) -> Tok
|
||||
return TokenBoolean(initialpos, buffer.tell(), False)
|
||||
elif curtoken == b"null":
|
||||
return TokenNull(initialpos, buffer.tell(), None)
|
||||
elif curtoken == b"stream":
|
||||
return TokenStream(initialpos, buffer.tell(), TStreamValue.START)
|
||||
elif curtoken == b"endstream":
|
||||
return TokenStream(initialpos, buffer.tell(), TStreamValue.END)
|
||||
elif curtoken == b"obj":
|
||||
return TokenObj(initialpos, buffer.tell(), TObjValue.START)
|
||||
elif curtoken == b"endobj":
|
||||
return TokenObj(initialpos, buffer.tell(), TObjValue.END)
|
||||
else:
|
||||
return TokenKeyword(initialpos, buffer.tell(), curtoken)
|
||||
raise LexerEOF
|
||||
+24
-2
@@ -180,5 +180,27 @@ class TokenArray(Token[TArrayValue]):
|
||||
pass
|
||||
|
||||
|
||||
TokenComplexType = Union[TokenDictionary, TokenArray]
|
||||
TokenComplexTypeVal = Union[TDictValue, TArrayValue]
|
||||
class TStreamValue(Enum):
|
||||
START = "stream"
|
||||
END = "endstream"
|
||||
|
||||
|
||||
class TokenStream(Token[TStreamValue]):
|
||||
"""Tokens representing the starting and ending of a PDF stream as specified in PDF spec 1.7 section 7.3.8.1"""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
class TObjValue(Enum):
|
||||
START = "obj"
|
||||
END = "endobj"
|
||||
|
||||
|
||||
class TokenObj(Token[TObjValue]):
|
||||
"""Tokens representing the starting and ending of an indirect PDF object as specified in PDF spec 1.7 section 7.3.10"""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
TokenComplexType = Union[TokenDictionary, TokenArray, TokenStream, TokenObj]
|
||||
TokenComplexTypeVal = Union[TDictValue, TArrayValue, TStreamValue, TObjValue]
|
||||
@@ -4,7 +4,7 @@
|
||||
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
|
||||
from typing import Any, Dict, Generic, List, NamedTuple, Optional, TypeVar, Union
|
||||
|
||||
TToken = TypeVar("TToken", bound=TokenPrimitive)
|
||||
|
||||
@@ -15,7 +15,7 @@ class PDFObject(metaclass=ABCMeta):
|
||||
@abstractmethod
|
||||
def get_value(self):
|
||||
"""return the base value of this object"""
|
||||
raise NotImplementedError
|
||||
return self
|
||||
|
||||
@abstractmethod
|
||||
def to_python(self):
|
||||
@@ -24,17 +24,21 @@ class PDFObject(metaclass=ABCMeta):
|
||||
|
||||
|
||||
class PDFPrimivite(Generic[TToken], PDFObject):
|
||||
"""A class representing any simple primitive data type as specified in PDF spec 1.7 section 7.3"""
|
||||
|
||||
def __init__(self, token: TToken) -> None:
|
||||
self.token: TToken = token
|
||||
|
||||
def get_value(self):
|
||||
return self.token.value
|
||||
|
||||
def to_python(self):
|
||||
def to_python(self) -> Union[TToken.value]:
|
||||
return self.value
|
||||
|
||||
|
||||
class PDFDictionary(Dict[PDFName, PDFObject], PDFObject):
|
||||
"""A class representing a PDF Dictionary as specified in PDF spec 1.7 section 7.3.7"""
|
||||
|
||||
def __init__(self, strict: bool = False) -> None:
|
||||
super().__init__()
|
||||
self.name: Optional[PDFName] = None
|
||||
@@ -50,30 +54,58 @@ class PDFDictionary(Dict[PDFName, PDFObject], PDFObject):
|
||||
self[self.name] = item.get_value()
|
||||
self.name = None
|
||||
|
||||
def get_value(self):
|
||||
return self
|
||||
|
||||
def to_python(self):
|
||||
def to_python(self) -> Dict[PDFName, Any]:
|
||||
return dict(self)
|
||||
|
||||
|
||||
class PDFArray(List[PDFObject], PDFObject):
|
||||
def get_value(self):
|
||||
return self
|
||||
"""A class representing a PDF Array as specified in PDF spec 1.7 section 7.3.6"""
|
||||
|
||||
def pass_item(self, item: PDFObject):
|
||||
self.append(item.get_value())
|
||||
|
||||
def to_python(self):
|
||||
def to_python(self) -> List[Any]:
|
||||
return list(self)
|
||||
|
||||
|
||||
class PDFStream(PDFObject):
|
||||
def get_value(self):
|
||||
return self
|
||||
"""A class representing a PDF Stream as specified in PDF spec 1.7 section 7.3.8"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
offset: int,
|
||||
length: int,
|
||||
filter: Optional[List[PDFName]] = None,
|
||||
decode_parms: Optional[List[PDFDictionary]] = None,
|
||||
f: Optional[Any] = None, # file-specifications
|
||||
ffilter: Optional[List[PDFName]] = None,
|
||||
fdecode_parms: Optional[List[PDFDictionary]] = None,
|
||||
dl: Optional[int] = None,
|
||||
) -> None:
|
||||
self.offset = offset
|
||||
self.length = length
|
||||
self.filter = filter
|
||||
self.decode_parms = decode_parms
|
||||
self.f = f
|
||||
self.ffilter = ffilter
|
||||
self.fdecode_parms = fdecode_parms
|
||||
self.dl = dl
|
||||
|
||||
def to_python(self):
|
||||
return self
|
||||
return {
|
||||
"offset": self.offset,
|
||||
"length": self.length,
|
||||
"filter": self.filter,
|
||||
"decode_parms": None
|
||||
if self.decode_parms is None
|
||||
else [x.to_python() for x in self.decode_parms],
|
||||
"f": self.f,
|
||||
"ffilter": self.ffilter,
|
||||
"fdecode_parms": None
|
||||
if self.fdecode_parms is None
|
||||
else [x.to_python() for x in self.fdecode_parms],
|
||||
"dl": self.dl,
|
||||
}
|
||||
|
||||
|
||||
PDFComplexType = Union[PDFDictionary, PDFArray, PDFStream]
|
||||
+8
-4
@@ -16,17 +16,21 @@ from pdfmajor.lexer.token import (
|
||||
PDFName,
|
||||
TArrayValue,
|
||||
TDictValue,
|
||||
TObjValue,
|
||||
TStreamValue,
|
||||
Token,
|
||||
TokenArray,
|
||||
TokenBoolean,
|
||||
TokenComment,
|
||||
TokenNull,
|
||||
TokenObj,
|
||||
TokenReal,
|
||||
TokenDictionary,
|
||||
TokenHexString,
|
||||
TokenInteger,
|
||||
TokenKeyword,
|
||||
TokenName,
|
||||
TokenStream,
|
||||
TokenString,
|
||||
)
|
||||
from pdfmajor.lexer.comment import parse_comment
|
||||
@@ -262,13 +266,13 @@ endobj
|
||||
endobj
|
||||
""",
|
||||
expected=[
|
||||
TokenKeyword(0, 9, b"endstream"),
|
||||
TokenKeyword(10, 16, b"endobj"),
|
||||
TokenStream(0, 9, TStreamValue.END),
|
||||
TokenObj(10, 16, TObjValue.END),
|
||||
TokenInteger(17, 18, 4),
|
||||
TokenInteger(19, 20, 0),
|
||||
TokenKeyword(21, 24, b"obj"),
|
||||
TokenObj(21, 24, TObjValue.START),
|
||||
TokenInteger(28, 32, 2618),
|
||||
TokenKeyword(33, 39, b"endobj"),
|
||||
TokenObj(33, 39, TObjValue.END),
|
||||
],
|
||||
)
|
||||
|
||||
|
||||
Reference in new issue
Block a user