- reorginized repo
This commit is contained in:
1 parent
4d1e0f2389
commit
f587a4389f
106 files changed
+163
-148
No files matched your search
@@ -0,0 +1,2 @@
|
||||
class PDFMajorException(Exception):
|
||||
pass
|
||||
@@ -1,4 +1,4 @@
|
||||
from pdfmajor.execptions import PDFMajorException
|
||||
from pdfmajor.exceptions import PDFMajorException
|
||||
|
||||
|
||||
class LexerError(PDFMajorException):
|
||||
|
||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -0,0 +1,126 @@
|
||||
import io
|
||||
from pdfmajor.parser.objects.stream import PDFStream
|
||||
from typing import Iterator, Optional
|
||||
|
||||
from pdfmajor.lexer import PDFLexer
|
||||
from pdfmajor.lexer.token import (
|
||||
TArrayValue,
|
||||
TDictValue,
|
||||
TokenArray,
|
||||
TokenComment,
|
||||
TokenDictionary,
|
||||
TokenKeyword,
|
||||
)
|
||||
|
||||
from .state import ParsingState
|
||||
from .parsers import attempt_parse_prim, deal_with_collection_object
|
||||
from .exceptions import ParserError
|
||||
|
||||
from .objects.base import PDFObject
|
||||
from .objects.collections import PDFArray, PDFDictionary
|
||||
from .objects.comment import PDFComment
|
||||
from .objects.indirect import IndirectObject
|
||||
|
||||
|
||||
class PDFParser:
|
||||
"""PDF Parser"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
fp: io.BufferedIOBase,
|
||||
buffer_size: int = 4096,
|
||||
state: Optional[ParsingState] = None,
|
||||
strict: bool = True,
|
||||
) -> None:
|
||||
self.lexer = PDFLexer(fp, buffer_size)
|
||||
self.strict = strict
|
||||
self.state = ParsingState([], []) if state is None else state
|
||||
|
||||
def seek(self, offset: int, state: Optional[ParsingState] = None) -> int:
|
||||
"""moves the offset of the reader to a certain spot, and returns the new offset
|
||||
|
||||
Args:
|
||||
offset (int)
|
||||
|
||||
Returns:
|
||||
int: new offset
|
||||
"""
|
||||
self.state = ParsingState([], []) if state is None else state
|
||||
return self.lexer.seek(offset)
|
||||
|
||||
def iter_objects(self) -> Iterator[PDFObject]:
|
||||
"""Iterates over the objects in the current byte stream using level 1 parsing
|
||||
|
||||
Yields:
|
||||
PDFObject
|
||||
"""
|
||||
for token in self.lexer.iter_tokens():
|
||||
attempt_prim = attempt_parse_prim(token, self.state)
|
||||
if attempt_prim.success:
|
||||
cur_ctx = self.state.current_context
|
||||
for obj in attempt_prim.next_objs:
|
||||
if cur_ctx is not None:
|
||||
cur_ctx.pass_item(obj)
|
||||
else:
|
||||
yield obj
|
||||
elif isinstance(token, TokenComment):
|
||||
yield self.state.set_last_obj(
|
||||
PDFComment(
|
||||
token.value,
|
||||
(token.start_loc, token.end_loc),
|
||||
)
|
||||
)
|
||||
elif isinstance(token, TokenKeyword):
|
||||
if token.value == b"R":
|
||||
obj = self.state.get_indobjects()
|
||||
cur_ctx = self.state.current_context
|
||||
if cur_ctx is not None:
|
||||
cur_ctx.pass_item(obj)
|
||||
else:
|
||||
yield obj
|
||||
elif token.value == b"obj":
|
||||
self.state.initialize_indirect_obj()
|
||||
elif token.value == b"endobj":
|
||||
last_ctx = self.state.context_stack.pop()
|
||||
if isinstance(last_ctx, IndirectObject):
|
||||
yield last_ctx.clone()
|
||||
elif self.strict:
|
||||
raise ParserError(f"Invalid position for 'endobj' {token}")
|
||||
elif token.value == b"stream":
|
||||
last_ctx = self.state.current_context
|
||||
if last_ctx is not None and isinstance(last_ctx, IndirectObject):
|
||||
stream = PDFStream(token.end_loc, 0)
|
||||
stream.pass_item(last_ctx.get_object())
|
||||
last_ctx.save_object(PDFStream(token.end_loc, 0))
|
||||
self.state.context_stack.append(stream)
|
||||
self.lexer.seek(stream.offset + stream.length)
|
||||
else:
|
||||
raise ParserError(
|
||||
f"PDFStream is missing initilization dictionary"
|
||||
)
|
||||
elif token.value == b"endstream":
|
||||
stream = self.state.context_stack.pop()
|
||||
if not isinstance(stream, PDFStream):
|
||||
raise ParserError(f"Invalid position for 'endstream' {token}")
|
||||
else:
|
||||
raise ParserError(f"Invalud Keyword {token}")
|
||||
elif isinstance(token, TokenArray):
|
||||
for obj in self.state.flush_int_collections():
|
||||
yield obj
|
||||
obj = deal_with_collection_object(
|
||||
self.state, token, PDFArray, TArrayValue.OPEN, strict=self.strict
|
||||
)
|
||||
if obj is not None:
|
||||
yield obj
|
||||
elif isinstance(token, TokenDictionary):
|
||||
for obj in self.state.flush_int_collections():
|
||||
yield obj
|
||||
obj = deal_with_collection_object(
|
||||
self.state,
|
||||
token=token,
|
||||
cls_const=PDFDictionary,
|
||||
open_t=TDictValue.OPEN,
|
||||
strict=self.strict,
|
||||
)
|
||||
if obj is not None:
|
||||
yield obj
|
||||
@@ -1,4 +1,4 @@
|
||||
from pdfmajor.execptions import PDFMajorException
|
||||
from pdfmajor.exceptions import PDFMajorException
|
||||
|
||||
|
||||
class ParserError(PDFMajorException):
|
||||
File renamed without changes.
@@ -9,12 +9,25 @@
|
||||
# "FACSIMILE CODING SCHEMES AND CODING CONTROL FUNCTIONS FOR GROUP 4 FACSIMILE APPARATUS"
|
||||
|
||||
|
||||
import sys
|
||||
import array
|
||||
|
||||
from typing import Optional
|
||||
from pdfmajor.execptions import EOFB, InvalidData, ByteSkip
|
||||
from abc import abstractmethod
|
||||
|
||||
from ..exceptions import DecodeFailed
|
||||
|
||||
|
||||
class CCITEOFB(DecodeFailed):
|
||||
pass
|
||||
|
||||
|
||||
class CCITInvalidData(DecodeFailed):
|
||||
pass
|
||||
|
||||
|
||||
class CCITByteSkip(DecodeFailed):
|
||||
pass
|
||||
|
||||
|
||||
def get_bytes(data):
|
||||
for byte in data:
|
||||
@@ -332,10 +345,10 @@ class CCITTG4Parser(BitParser):
|
||||
try:
|
||||
for m in (128, 64, 32, 16, 8, 4, 2, 1):
|
||||
self._parse_bit(byte & m)
|
||||
except ByteSkip:
|
||||
except CCITByteSkip:
|
||||
self._accept = self._parse_mode
|
||||
self._state = self.MODE
|
||||
except EOFB:
|
||||
except CCITEOFB:
|
||||
break
|
||||
return
|
||||
|
||||
@@ -355,17 +368,17 @@ class CCITTG4Parser(BitParser):
|
||||
self._accept = self._parse_uncompressed
|
||||
return self.UNCOMPRESSED
|
||||
elif mode == "e":
|
||||
raise EOFB
|
||||
raise CCITEOFB
|
||||
elif isinstance(mode, int):
|
||||
self._do_vertical(mode)
|
||||
self._flush_line()
|
||||
return self.MODE
|
||||
else:
|
||||
raise InvalidData(mode)
|
||||
raise CCITInvalidData(mode)
|
||||
|
||||
def _parse_horiz1(self, n):
|
||||
if n is None:
|
||||
raise InvalidData
|
||||
raise CCITInvalidData
|
||||
self._n1 += n
|
||||
if n < 64:
|
||||
self._n2 = 0
|
||||
@@ -378,7 +391,7 @@ class CCITTG4Parser(BitParser):
|
||||
|
||||
def _parse_horiz2(self, n):
|
||||
if n is None:
|
||||
raise InvalidData
|
||||
raise CCITInvalidData
|
||||
self._n2 += n
|
||||
if n < 64:
|
||||
self._color = 1 - self._color
|
||||
@@ -393,7 +406,7 @@ class CCITTG4Parser(BitParser):
|
||||
|
||||
def _parse_uncompressed(self, bits):
|
||||
if not bits:
|
||||
raise InvalidData
|
||||
raise CCITInvalidData
|
||||
if bits.startswith("T"):
|
||||
self._accept = self._parse_mode
|
||||
self._color = int(bits[1])
|
||||
@@ -445,7 +458,7 @@ class CCITTG4Parser(BitParser):
|
||||
self._y += 1
|
||||
self._reset_line()
|
||||
if self.bytealign:
|
||||
raise ByteSkip
|
||||
raise CCITByteSkip
|
||||
return
|
||||
|
||||
def _do_vertical(self, dx):
|
||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
Whitespace-only changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -1,4 +1,4 @@
|
||||
from pdfmajor.parser_v3.exceptions import ParserError
|
||||
from pdfmajor.parser.exceptions import ParserError
|
||||
from typing import Any, Dict, List, Optional
|
||||
from .base import PDFContextualObject, PDFObject
|
||||
from .primitives import PDFName
|
||||
Loaded 100 of 106 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user