added parsing for object types
This commit is contained in:
1 parent
78175d3a64
commit
4e3b3e10b7
8 files changed
+324
-135
No files matched your search
@@ -1,8 +1,8 @@
|
||||
from pdfmajor.parser.exceptions import ParserError
|
||||
from typing import Any, Dict, List, Optional, Union
|
||||
from pdfmajor.parser.objects.base import PDFObject
|
||||
from typing import Any, List, Optional, TypeVar, Union
|
||||
|
||||
from ..objects.base import PDFObject
|
||||
from ..objects.collections import PDFDictionary
|
||||
from ..objects.primitives import PDFNull
|
||||
|
||||
|
||||
class PDFStream:
|
||||
@@ -11,18 +11,18 @@ class PDFStream:
|
||||
def __init__(
|
||||
self,
|
||||
offset: int = 0,
|
||||
length: int = 0,
|
||||
filter: Optional[List[str]] = None,
|
||||
decode_parms: Optional[List[Dict[str, Any]]] = None,
|
||||
f: Optional[Any] = None, # file-specifications
|
||||
ffilter: Optional[List[str]] = None,
|
||||
fdecode_parms: Optional[List[Dict[str, Any]]] = None,
|
||||
dl: Optional[int] = None,
|
||||
length: PDFObject = PDFNull(),
|
||||
filter: Optional[List[PDFObject]] = None,
|
||||
decode_parms: Optional[List[PDFObject]] = None,
|
||||
f: Optional[Any] = None, # TODO: file-specifications
|
||||
ffilter: Optional[List[PDFObject]] = None,
|
||||
fdecode_parms: Optional[List[PDFObject]] = None,
|
||||
dl: Optional[PDFObject] = None,
|
||||
) -> None:
|
||||
"""
|
||||
Args:
|
||||
offset (int): the offset where the stream itself starts
|
||||
length (int): the length of the stream
|
||||
length (PotentialInt): the length of the stream
|
||||
filter (Optional[List[str]], optional): The names of a filter that shall be applied in processing the stream data. Defaults to None.
|
||||
decode_parms (Optional[List[Dict[str, Any]]], optional): Parameter dictionaries used by the filters specified by filter. Defaults to None.
|
||||
f (Optional[Any], optional): The file containing the stream data. If this entry is present, the bytes between stream and endstream shall be ignored. However, the Length entry should still specify the number of those bytes (usually, there are no bytes and Length is 0).. Defaults to None.
|
||||
@@ -32,49 +32,59 @@ class PDFStream:
|
||||
"""
|
||||
self.offset = offset
|
||||
self.length = length
|
||||
self.filter: Optional[List[str]] = filter
|
||||
self.decode_parms: Optional[List[Dict[str, Any]]] = decode_parms
|
||||
self.f: Optional[Any] = f
|
||||
self.ffilter: Optional[List[str]] = ffilter
|
||||
self.fdecode_parms: Optional[List[Dict[str, Any]]] = fdecode_parms
|
||||
self.dl: Optional[int] = dl
|
||||
self.filter = filter
|
||||
self.decode_parms = decode_parms
|
||||
self.f = f
|
||||
self.ffilter = ffilter
|
||||
self.fdecode_parms = fdecode_parms
|
||||
self.dl = dl
|
||||
|
||||
@classmethod
|
||||
def from_pdfdict(cls, offset: int, item: PDFDictionary) -> "PDFStream":
|
||||
data = item.to_python()
|
||||
stream = cls(offset, data["Length"])
|
||||
stream.filter = get_single_or_list(data.get("Filter", stream.filter))
|
||||
stream = cls(offset, item["Length"])
|
||||
stream.filter = get_single_or_list(item.get("Filter", stream.filter))
|
||||
stream.decode_parms = get_single_or_list(
|
||||
data.get("DecodeParms", stream.decode_parms)
|
||||
item.get("DecodeParms", stream.decode_parms)
|
||||
)
|
||||
stream.f = data.get("F", stream.f)
|
||||
stream.ffilter = get_single_or_list(data.get("FFilter", stream.ffilter))
|
||||
stream.f = item.get("F", stream.f)
|
||||
stream.ffilter = get_single_or_list(item.get("FFilter", stream.ffilter))
|
||||
stream.fdecode_parms = get_single_or_list(
|
||||
data.get("FDecodeParms", stream.fdecode_parms)
|
||||
item.get("FDecodeParms", stream.fdecode_parms)
|
||||
)
|
||||
return stream
|
||||
|
||||
def to_python(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],
|
||||
"length": self.length.to_python(),
|
||||
"filter": to_python_list(self.filter),
|
||||
"decode_parms": to_python_list(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,
|
||||
"ffilter": to_python_list(self.ffilter),
|
||||
"fdecode_parms": to_python_list(self.fdecode_parms),
|
||||
"dl": to_python(self.dl),
|
||||
}
|
||||
|
||||
|
||||
def get_single_or_list(obj: Optional[Union[List[Any], Any]]) -> List[Any]:
|
||||
T = TypeVar("T")
|
||||
|
||||
|
||||
def get_single_or_list(obj: Optional[Union[List[T], T]]) -> List[T]:
|
||||
if obj is None:
|
||||
return []
|
||||
elif isinstance(obj, list):
|
||||
return obj
|
||||
else:
|
||||
return [obj]
|
||||
|
||||
|
||||
def to_python(obj: Optional[PDFObject]) -> Any:
|
||||
if obj is None:
|
||||
return None
|
||||
return obj.to_python()
|
||||
|
||||
|
||||
def to_python_list(obj: Optional[List[PDFObject]]) -> List[Any]:
|
||||
if obj is None:
|
||||
return []
|
||||
return [x.to_python() for x in obj]
|
||||
Reference in new issue
Block a user