diff --git a/pdfmajor/exceptions.py b/pdfmajor/exceptions.py new file mode 100644 index 0000000..c1d56f4 --- /dev/null +++ b/pdfmajor/exceptions.py @@ -0,0 +1,2 @@ +class PDFMajorException(Exception): + pass diff --git a/pdfmajor/lexer/exceptions.py b/pdfmajor/lexer/exceptions.py index 0ea058f..000a6bd 100644 --- a/pdfmajor/lexer/exceptions.py +++ b/pdfmajor/lexer/exceptions.py @@ -1,4 +1,4 @@ -from pdfmajor.execptions import PDFMajorException +from pdfmajor.exceptions import PDFMajorException class LexerError(PDFMajorException): diff --git a/pdfmajor/BMPWriter/__init__.py b/pdfmajor/old/BMPWriter/__init__.py similarity index 100% rename from pdfmajor/BMPWriter/__init__.py rename to pdfmajor/old/BMPWriter/__init__.py diff --git a/pdfmajor/converters/__init__.py b/pdfmajor/old/converters/__init__.py similarity index 100% rename from pdfmajor/converters/__init__.py rename to pdfmajor/old/converters/__init__.py diff --git a/pdfmajor/converters/html.py b/pdfmajor/old/converters/html.py similarity index 100% rename from pdfmajor/converters/html.py rename to pdfmajor/old/converters/html.py diff --git a/pdfmajor/converters/json.py b/pdfmajor/old/converters/json.py similarity index 100% rename from pdfmajor/converters/json.py rename to pdfmajor/old/converters/json.py diff --git a/pdfmajor/converters/text.py b/pdfmajor/old/converters/text.py similarity index 100% rename from pdfmajor/converters/text.py rename to pdfmajor/old/converters/text.py diff --git a/pdfmajor/converters/writers/__init__.py b/pdfmajor/old/converters/writers/__init__.py similarity index 100% rename from pdfmajor/converters/writers/__init__.py rename to pdfmajor/old/converters/writers/__init__.py diff --git a/pdfmajor/converters/writers/html.py b/pdfmajor/old/converters/writers/html.py similarity index 100% rename from pdfmajor/converters/writers/html.py rename to pdfmajor/old/converters/writers/html.py diff --git a/pdfmajor/converters/writers/json.py b/pdfmajor/old/converters/writers/json.py similarity index 100% rename from pdfmajor/converters/writers/json.py rename to pdfmajor/old/converters/writers/json.py diff --git a/pdfmajor/converters/writers/xml.py b/pdfmajor/old/converters/writers/xml.py similarity index 100% rename from pdfmajor/converters/writers/xml.py rename to pdfmajor/old/converters/writers/xml.py diff --git a/pdfmajor/converters/writers/yaml.py b/pdfmajor/old/converters/writers/yaml.py similarity index 100% rename from pdfmajor/converters/writers/yaml.py rename to pdfmajor/old/converters/writers/yaml.py diff --git a/pdfmajor/converters/xml.py b/pdfmajor/old/converters/xml.py similarity index 100% rename from pdfmajor/converters/xml.py rename to pdfmajor/old/converters/xml.py diff --git a/pdfmajor/converters/yaml.py b/pdfmajor/old/converters/yaml.py similarity index 100% rename from pdfmajor/converters/yaml.py rename to pdfmajor/old/converters/yaml.py diff --git a/pdfmajor/execptions.py b/pdfmajor/old/execptions.py similarity index 100% rename from pdfmajor/execptions.py rename to pdfmajor/old/execptions.py diff --git a/pdfmajor/interpreter/PageInterpreter.py b/pdfmajor/old/interpreter/PageInterpreter.py similarity index 100% rename from pdfmajor/interpreter/PageInterpreter.py rename to pdfmajor/old/interpreter/PageInterpreter.py diff --git a/pdfmajor/interpreter/__init__.py b/pdfmajor/old/interpreter/__init__.py similarity index 100% rename from pdfmajor/interpreter/__init__.py rename to pdfmajor/old/interpreter/__init__.py diff --git a/pdfmajor/interpreter/commands/PDFCommands.py b/pdfmajor/old/interpreter/commands/PDFCommands.py similarity index 100% rename from pdfmajor/interpreter/commands/PDFCommands.py rename to pdfmajor/old/interpreter/commands/PDFCommands.py diff --git a/pdfmajor/interpreter/commands/__init__.py b/pdfmajor/old/interpreter/commands/__init__.py similarity index 100% rename from pdfmajor/interpreter/commands/__init__.py rename to pdfmajor/old/interpreter/commands/__init__.py diff --git a/pdfmajor/interpreter/commands/commands.py b/pdfmajor/old/interpreter/commands/commands.py similarity index 100% rename from pdfmajor/interpreter/commands/commands.py rename to pdfmajor/old/interpreter/commands/commands.py diff --git a/pdfmajor/interpreter/commands/state/Curves.py b/pdfmajor/old/interpreter/commands/state/Curves.py similarity index 100% rename from pdfmajor/interpreter/commands/state/Curves.py rename to pdfmajor/old/interpreter/commands/state/Curves.py diff --git a/pdfmajor/interpreter/commands/state/PDFGraphicState/PDFColor.py b/pdfmajor/old/interpreter/commands/state/PDFGraphicState/PDFColor.py similarity index 100% rename from pdfmajor/interpreter/commands/state/PDFGraphicState/PDFColor.py rename to pdfmajor/old/interpreter/commands/state/PDFGraphicState/PDFColor.py diff --git a/pdfmajor/interpreter/commands/state/PDFGraphicState/PDFColorSpace.py b/pdfmajor/old/interpreter/commands/state/PDFGraphicState/PDFColorSpace.py similarity index 100% rename from pdfmajor/interpreter/commands/state/PDFGraphicState/PDFColorSpace.py rename to pdfmajor/old/interpreter/commands/state/PDFGraphicState/PDFColorSpace.py diff --git a/pdfmajor/interpreter/commands/state/PDFGraphicState/__init__.py b/pdfmajor/old/interpreter/commands/state/PDFGraphicState/__init__.py similarity index 100% rename from pdfmajor/interpreter/commands/state/PDFGraphicState/__init__.py rename to pdfmajor/old/interpreter/commands/state/PDFGraphicState/__init__.py diff --git a/pdfmajor/interpreter/commands/state/PDFItem/PDFImage.py b/pdfmajor/old/interpreter/commands/state/PDFItem/PDFImage.py similarity index 100% rename from pdfmajor/interpreter/commands/state/PDFItem/PDFImage.py rename to pdfmajor/old/interpreter/commands/state/PDFItem/PDFImage.py diff --git a/pdfmajor/interpreter/commands/state/PDFItem/PDFShape.py b/pdfmajor/old/interpreter/commands/state/PDFItem/PDFShape.py similarity index 100% rename from pdfmajor/interpreter/commands/state/PDFItem/PDFShape.py rename to pdfmajor/old/interpreter/commands/state/PDFItem/PDFShape.py diff --git a/pdfmajor/interpreter/commands/state/PDFItem/PDFText.py b/pdfmajor/old/interpreter/commands/state/PDFItem/PDFText.py similarity index 100% rename from pdfmajor/interpreter/commands/state/PDFItem/PDFText.py rename to pdfmajor/old/interpreter/commands/state/PDFItem/PDFText.py diff --git a/pdfmajor/interpreter/commands/state/PDFItem/PDFXObject.py b/pdfmajor/old/interpreter/commands/state/PDFItem/PDFXObject.py similarity index 100% rename from pdfmajor/interpreter/commands/state/PDFItem/PDFXObject.py rename to pdfmajor/old/interpreter/commands/state/PDFItem/PDFXObject.py diff --git a/pdfmajor/interpreter/commands/state/PDFItem/TextDecoder.py b/pdfmajor/old/interpreter/commands/state/PDFItem/TextDecoder.py similarity index 100% rename from pdfmajor/interpreter/commands/state/PDFItem/TextDecoder.py rename to pdfmajor/old/interpreter/commands/state/PDFItem/TextDecoder.py diff --git a/pdfmajor/interpreter/commands/state/PDFItem/__init__.py b/pdfmajor/old/interpreter/commands/state/PDFItem/__init__.py similarity index 100% rename from pdfmajor/interpreter/commands/state/PDFItem/__init__.py rename to pdfmajor/old/interpreter/commands/state/PDFItem/__init__.py diff --git a/pdfmajor/interpreter/commands/state/PDFItem/_base.py b/pdfmajor/old/interpreter/commands/state/PDFItem/_base.py similarity index 100% rename from pdfmajor/interpreter/commands/state/PDFItem/_base.py rename to pdfmajor/old/interpreter/commands/state/PDFItem/_base.py diff --git a/pdfmajor/interpreter/commands/state/PDFTextState/PDFFont/CFFFont.py b/pdfmajor/old/interpreter/commands/state/PDFTextState/PDFFont/CFFFont.py similarity index 100% rename from pdfmajor/interpreter/commands/state/PDFTextState/PDFFont/CFFFont.py rename to pdfmajor/old/interpreter/commands/state/PDFTextState/PDFFont/CFFFont.py diff --git a/pdfmajor/interpreter/commands/state/PDFTextState/PDFFont/PDFFont.py b/pdfmajor/old/interpreter/commands/state/PDFTextState/PDFFont/PDFFont.py similarity index 100% rename from pdfmajor/interpreter/commands/state/PDFTextState/PDFFont/PDFFont.py rename to pdfmajor/old/interpreter/commands/state/PDFTextState/PDFFont/PDFFont.py diff --git a/pdfmajor/interpreter/commands/state/PDFTextState/PDFFont/TrueTypeFont.py b/pdfmajor/old/interpreter/commands/state/PDFTextState/PDFFont/TrueTypeFont.py similarity index 100% rename from pdfmajor/interpreter/commands/state/PDFTextState/PDFFont/TrueTypeFont.py rename to pdfmajor/old/interpreter/commands/state/PDFTextState/PDFFont/TrueTypeFont.py diff --git a/pdfmajor/interpreter/commands/state/PDFTextState/PDFFont/Type1FontHeaderParser.py b/pdfmajor/old/interpreter/commands/state/PDFTextState/PDFFont/Type1FontHeaderParser.py similarity index 100% rename from pdfmajor/interpreter/commands/state/PDFTextState/PDFFont/Type1FontHeaderParser.py rename to pdfmajor/old/interpreter/commands/state/PDFTextState/PDFFont/Type1FontHeaderParser.py diff --git a/pdfmajor/interpreter/commands/state/PDFTextState/PDFFont/__init__.py b/pdfmajor/old/interpreter/commands/state/PDFTextState/PDFFont/__init__.py similarity index 100% rename from pdfmajor/interpreter/commands/state/PDFTextState/PDFFont/__init__.py rename to pdfmajor/old/interpreter/commands/state/PDFTextState/PDFFont/__init__.py diff --git a/pdfmajor/interpreter/commands/state/PDFTextState/PDFFont/fonts.py b/pdfmajor/old/interpreter/commands/state/PDFTextState/PDFFont/fonts.py similarity index 100% rename from pdfmajor/interpreter/commands/state/PDFTextState/PDFFont/fonts.py rename to pdfmajor/old/interpreter/commands/state/PDFTextState/PDFFont/fonts.py diff --git a/pdfmajor/interpreter/commands/state/PDFTextState/PDFFont/util.py b/pdfmajor/old/interpreter/commands/state/PDFTextState/PDFFont/util.py similarity index 100% rename from pdfmajor/interpreter/commands/state/PDFTextState/PDFFont/util.py rename to pdfmajor/old/interpreter/commands/state/PDFTextState/PDFFont/util.py diff --git a/pdfmajor/interpreter/commands/state/PDFTextState/__init__.py b/pdfmajor/old/interpreter/commands/state/PDFTextState/__init__.py similarity index 100% rename from pdfmajor/interpreter/commands/state/PDFTextState/__init__.py rename to pdfmajor/old/interpreter/commands/state/PDFTextState/__init__.py diff --git a/pdfmajor/interpreter/commands/state/__init__.py b/pdfmajor/old/interpreter/commands/state/__init__.py similarity index 100% rename from pdfmajor/interpreter/commands/state/__init__.py rename to pdfmajor/old/interpreter/commands/state/__init__.py diff --git a/pdfmajor/interpreter/commands/state/layout/LTChar.py b/pdfmajor/old/interpreter/commands/state/layout/LTChar.py similarity index 100% rename from pdfmajor/interpreter/commands/state/layout/LTChar.py rename to pdfmajor/old/interpreter/commands/state/layout/LTChar.py diff --git a/pdfmajor/interpreter/commands/state/layout/LTCharBlock.py b/pdfmajor/old/interpreter/commands/state/layout/LTCharBlock.py similarity index 100% rename from pdfmajor/interpreter/commands/state/layout/LTCharBlock.py rename to pdfmajor/old/interpreter/commands/state/layout/LTCharBlock.py diff --git a/pdfmajor/interpreter/commands/state/layout/LTCurves.py b/pdfmajor/old/interpreter/commands/state/layout/LTCurves.py similarity index 100% rename from pdfmajor/interpreter/commands/state/layout/LTCurves.py rename to pdfmajor/old/interpreter/commands/state/layout/LTCurves.py diff --git a/pdfmajor/interpreter/commands/state/layout/LTImage.py b/pdfmajor/old/interpreter/commands/state/layout/LTImage.py similarity index 100% rename from pdfmajor/interpreter/commands/state/layout/LTImage.py rename to pdfmajor/old/interpreter/commands/state/layout/LTImage.py diff --git a/pdfmajor/interpreter/commands/state/layout/LTTextBlock.py b/pdfmajor/old/interpreter/commands/state/layout/LTTextBlock.py similarity index 100% rename from pdfmajor/interpreter/commands/state/layout/LTTextBlock.py rename to pdfmajor/old/interpreter/commands/state/layout/LTTextBlock.py diff --git a/pdfmajor/interpreter/commands/state/layout/LTXObject.py b/pdfmajor/old/interpreter/commands/state/layout/LTXObject.py similarity index 100% rename from pdfmajor/interpreter/commands/state/layout/LTXObject.py rename to pdfmajor/old/interpreter/commands/state/layout/LTXObject.py diff --git a/pdfmajor/interpreter/commands/state/layout/__init__.py b/pdfmajor/old/interpreter/commands/state/layout/__init__.py similarity index 100% rename from pdfmajor/interpreter/commands/state/layout/__init__.py rename to pdfmajor/old/interpreter/commands/state/layout/__init__.py diff --git a/pdfmajor/interpreter/commands/state/layout/_base.py b/pdfmajor/old/interpreter/commands/state/layout/_base.py similarity index 100% rename from pdfmajor/interpreter/commands/state/layout/_base.py rename to pdfmajor/old/interpreter/commands/state/layout/_base.py diff --git a/pdfmajor/interpreter/commands/state/layout/utils/__init__.py b/pdfmajor/old/interpreter/commands/state/layout/utils/__init__.py similarity index 100% rename from pdfmajor/interpreter/commands/state/layout/utils/__init__.py rename to pdfmajor/old/interpreter/commands/state/layout/utils/__init__.py diff --git a/pdfmajor/interpreter/commands/state/layout/utils/textdecoder.py b/pdfmajor/old/interpreter/commands/state/layout/utils/textdecoder.py similarity index 100% rename from pdfmajor/interpreter/commands/state/layout/utils/textdecoder.py rename to pdfmajor/old/interpreter/commands/state/layout/utils/textdecoder.py diff --git a/pdfmajor/interpreter/commands/utils.py b/pdfmajor/old/interpreter/commands/utils.py similarity index 100% rename from pdfmajor/interpreter/commands/utils.py rename to pdfmajor/old/interpreter/commands/utils.py diff --git a/pdfmajor/parser/EncodingDB.py b/pdfmajor/old/parser/EncodingDB.py similarity index 100% rename from pdfmajor/parser/EncodingDB.py rename to pdfmajor/old/parser/EncodingDB.py diff --git a/pdfmajor/parser/PDFContentParser.py b/pdfmajor/old/parser/PDFContentParser.py similarity index 100% rename from pdfmajor/parser/PDFContentParser.py rename to pdfmajor/old/parser/PDFContentParser.py diff --git a/pdfmajor/parser/PDFDocument/PDFSecurityHandler.py b/pdfmajor/old/parser/PDFDocument/PDFSecurityHandler.py similarity index 100% rename from pdfmajor/parser/PDFDocument/PDFSecurityHandler.py rename to pdfmajor/old/parser/PDFDocument/PDFSecurityHandler.py diff --git a/pdfmajor/parser/PDFDocument/PDFXRef.py b/pdfmajor/old/parser/PDFDocument/PDFXRef.py similarity index 100% rename from pdfmajor/parser/PDFDocument/PDFXRef.py rename to pdfmajor/old/parser/PDFDocument/PDFXRef.py diff --git a/pdfmajor/parser/PDFDocument/__init__.py b/pdfmajor/old/parser/PDFDocument/__init__.py similarity index 100% rename from pdfmajor/parser/PDFDocument/__init__.py rename to pdfmajor/old/parser/PDFDocument/__init__.py diff --git a/pdfmajor/parser/PDFDocument/constants.py b/pdfmajor/old/parser/PDFDocument/constants.py similarity index 100% rename from pdfmajor/parser/PDFDocument/constants.py rename to pdfmajor/old/parser/PDFDocument/constants.py diff --git a/pdfmajor/parser/PDFPage.py b/pdfmajor/old/parser/PDFPage.py similarity index 100% rename from pdfmajor/parser/PDFPage.py rename to pdfmajor/old/parser/PDFPage.py diff --git a/pdfmajor/parser/PDFParser.py b/pdfmajor/old/parser/PDFParser.py similarity index 100% rename from pdfmajor/parser/PDFParser.py rename to pdfmajor/old/parser/PDFParser.py diff --git a/pdfmajor/parser/PDFStream/PDFObjRef.py b/pdfmajor/old/parser/PDFStream/PDFObjRef.py similarity index 100% rename from pdfmajor/parser/PDFStream/PDFObjRef.py rename to pdfmajor/old/parser/PDFStream/PDFObjRef.py diff --git a/pdfmajor/parser/PDFStream/PDFStream.py b/pdfmajor/old/parser/PDFStream/PDFStream.py similarity index 100% rename from pdfmajor/parser/PDFStream/PDFStream.py rename to pdfmajor/old/parser/PDFStream/PDFStream.py diff --git a/pdfmajor/parser/PDFStream/__init__.py b/pdfmajor/old/parser/PDFStream/__init__.py similarity index 100% rename from pdfmajor/parser/PDFStream/__init__.py rename to pdfmajor/old/parser/PDFStream/__init__.py diff --git a/pdfmajor/parser/PDFStream/ccitt.py b/pdfmajor/old/parser/PDFStream/ccitt.py similarity index 100% rename from pdfmajor/parser/PDFStream/ccitt.py rename to pdfmajor/old/parser/PDFStream/ccitt.py diff --git a/pdfmajor/parser/PDFStream/constants.py b/pdfmajor/old/parser/PDFStream/constants.py similarity index 100% rename from pdfmajor/parser/PDFStream/constants.py rename to pdfmajor/old/parser/PDFStream/constants.py diff --git a/pdfmajor/parser/PDFStream/types.py b/pdfmajor/old/parser/PDFStream/types.py similarity index 100% rename from pdfmajor/parser/PDFStream/types.py rename to pdfmajor/old/parser/PDFStream/types.py diff --git a/pdfmajor/parser/PDFStream/util.py b/pdfmajor/old/parser/PDFStream/util.py similarity index 100% rename from pdfmajor/parser/PDFStream/util.py rename to pdfmajor/old/parser/PDFStream/util.py diff --git a/pdfmajor/parser/PSStackParser/PSBaseParser.py b/pdfmajor/old/parser/PSStackParser/PSBaseParser.py similarity index 100% rename from pdfmajor/parser/PSStackParser/PSBaseParser.py rename to pdfmajor/old/parser/PSStackParser/PSBaseParser.py diff --git a/pdfmajor/parser/PSStackParser/__init__.py b/pdfmajor/old/parser/PSStackParser/__init__.py similarity index 100% rename from pdfmajor/parser/PSStackParser/__init__.py rename to pdfmajor/old/parser/PSStackParser/__init__.py diff --git a/pdfmajor/parser/PSStackParser/constants.py b/pdfmajor/old/parser/PSStackParser/constants.py similarity index 100% rename from pdfmajor/parser/PSStackParser/constants.py rename to pdfmajor/old/parser/PSStackParser/constants.py diff --git a/pdfmajor/parser/PSStackParser/types.py b/pdfmajor/old/parser/PSStackParser/types.py similarity index 100% rename from pdfmajor/parser/PSStackParser/types.py rename to pdfmajor/old/parser/PSStackParser/types.py diff --git a/pdfmajor/parser_v3/objects/__init__.py b/pdfmajor/old/parser/__init__.py similarity index 100% rename from pdfmajor/parser_v3/objects/__init__.py rename to pdfmajor/old/parser/__init__.py diff --git a/pdfmajor/parser/cmapdb/__init__.py b/pdfmajor/old/parser/cmapdb/__init__.py similarity index 100% rename from pdfmajor/parser/cmapdb/__init__.py rename to pdfmajor/old/parser/cmapdb/__init__.py diff --git a/pdfmajor/parser/constants.py b/pdfmajor/old/parser/constants.py similarity index 100% rename from pdfmajor/parser/constants.py rename to pdfmajor/old/parser/constants.py diff --git a/pdfmajor/parser/types.py b/pdfmajor/old/parser/types.py similarity index 100% rename from pdfmajor/parser/types.py rename to pdfmajor/old/parser/types.py diff --git a/pdfmajor/utils/__init__.py b/pdfmajor/old/utils/__init__.py similarity index 100% rename from pdfmajor/utils/__init__.py rename to pdfmajor/old/utils/__init__.py diff --git a/pdfmajor/utils/ascii85.py b/pdfmajor/old/utils/ascii85.py similarity index 100% rename from pdfmajor/utils/ascii85.py rename to pdfmajor/old/utils/ascii85.py diff --git a/pdfmajor/utils/encoding/__init__.py b/pdfmajor/old/utils/encoding/__init__.py similarity index 100% rename from pdfmajor/utils/encoding/__init__.py rename to pdfmajor/old/utils/encoding/__init__.py diff --git a/pdfmajor/utils/encoding/glyphlist.py b/pdfmajor/old/utils/encoding/glyphlist.py similarity index 100% rename from pdfmajor/utils/encoding/glyphlist.py rename to pdfmajor/old/utils/encoding/glyphlist.py diff --git a/pdfmajor/utils/encoding/latin_enc.py b/pdfmajor/old/utils/encoding/latin_enc.py similarity index 100% rename from pdfmajor/utils/encoding/latin_enc.py rename to pdfmajor/old/utils/encoding/latin_enc.py diff --git a/pdfmajor/utils/fontmetrics.py b/pdfmajor/old/utils/fontmetrics.py similarity index 100% rename from pdfmajor/utils/fontmetrics.py rename to pdfmajor/old/utils/fontmetrics.py diff --git a/pdfmajor/utils/generic.py b/pdfmajor/old/utils/generic.py similarity index 100% rename from pdfmajor/utils/generic.py rename to pdfmajor/old/utils/generic.py diff --git a/pdfmajor/utils/log.py b/pdfmajor/old/utils/log.py similarity index 100% rename from pdfmajor/utils/log.py rename to pdfmajor/old/utils/log.py diff --git a/pdfmajor/utils/lzw.py b/pdfmajor/old/utils/lzw.py similarity index 100% rename from pdfmajor/utils/lzw.py rename to pdfmajor/old/utils/lzw.py diff --git a/pdfmajor/utils/matrix.py b/pdfmajor/old/utils/matrix.py similarity index 100% rename from pdfmajor/utils/matrix.py rename to pdfmajor/old/utils/matrix.py diff --git a/pdfmajor/utils/text.py b/pdfmajor/old/utils/text.py similarity index 100% rename from pdfmajor/utils/text.py rename to pdfmajor/old/utils/text.py diff --git a/pdfmajor/utils/types.py b/pdfmajor/old/utils/types.py similarity index 100% rename from pdfmajor/utils/types.py rename to pdfmajor/old/utils/types.py diff --git a/pdfmajor/parser/__init__.py b/pdfmajor/parser/__init__.py index e69de29..6eae44b 100644 --- a/pdfmajor/parser/__init__.py +++ b/pdfmajor/parser/__init__.py @@ -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 diff --git a/pdfmajor/parser_v3/exceptions.py b/pdfmajor/parser/exceptions.py similarity index 77% rename from pdfmajor/parser_v3/exceptions.py rename to pdfmajor/parser/exceptions.py index 082c58b..e572271 100644 --- a/pdfmajor/parser_v3/exceptions.py +++ b/pdfmajor/parser/exceptions.py @@ -1,4 +1,4 @@ -from pdfmajor.execptions import PDFMajorException +from pdfmajor.exceptions import PDFMajorException class ParserError(PDFMajorException): diff --git a/pdfmajor/parser_v3/fliters/__init__.py b/pdfmajor/parser/fliters/__init__.py similarity index 100% rename from pdfmajor/parser_v3/fliters/__init__.py rename to pdfmajor/parser/fliters/__init__.py diff --git a/pdfmajor/parser_v3/fliters/ccit.py b/pdfmajor/parser/fliters/ccit.py similarity index 97% rename from pdfmajor/parser_v3/fliters/ccit.py rename to pdfmajor/parser/fliters/ccit.py index 45e1f65..ffd54af 100644 --- a/pdfmajor/parser_v3/fliters/ccit.py +++ b/pdfmajor/parser/fliters/ccit.py @@ -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): diff --git a/pdfmajor/parser_v3/fliters/lzw.py b/pdfmajor/parser/fliters/lzw.py similarity index 100% rename from pdfmajor/parser_v3/fliters/lzw.py rename to pdfmajor/parser/fliters/lzw.py diff --git a/pdfmajor/parser_v3/fliters/rld.py b/pdfmajor/parser/fliters/rld.py similarity index 100% rename from pdfmajor/parser_v3/fliters/rld.py rename to pdfmajor/parser/fliters/rld.py diff --git a/pdfmajor/parser_v3/fliters/types.py b/pdfmajor/parser/fliters/types.py similarity index 100% rename from pdfmajor/parser_v3/fliters/types.py rename to pdfmajor/parser/fliters/types.py diff --git a/pdfmajor/parser/objects/__init__.py b/pdfmajor/parser/objects/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pdfmajor/parser_v3/objects/base.py b/pdfmajor/parser/objects/base.py similarity index 100% rename from pdfmajor/parser_v3/objects/base.py rename to pdfmajor/parser/objects/base.py diff --git a/pdfmajor/parser_v3/objects/collections.py b/pdfmajor/parser/objects/collections.py similarity index 100% rename from pdfmajor/parser_v3/objects/collections.py rename to pdfmajor/parser/objects/collections.py diff --git a/pdfmajor/parser_v3/objects/comment.py b/pdfmajor/parser/objects/comment.py similarity index 100% rename from pdfmajor/parser_v3/objects/comment.py rename to pdfmajor/parser/objects/comment.py diff --git a/pdfmajor/parser_v3/objects/indirect.py b/pdfmajor/parser/objects/indirect.py similarity index 100% rename from pdfmajor/parser_v3/objects/indirect.py rename to pdfmajor/parser/objects/indirect.py diff --git a/pdfmajor/parser_v3/objects/primitives.py b/pdfmajor/parser/objects/primitives.py similarity index 100% rename from pdfmajor/parser_v3/objects/primitives.py rename to pdfmajor/parser/objects/primitives.py diff --git a/pdfmajor/parser_v3/objects/stream.py b/pdfmajor/parser/objects/stream.py similarity index 98% rename from pdfmajor/parser_v3/objects/stream.py rename to pdfmajor/parser/objects/stream.py index 210929c..2dbe0c2 100644 --- a/pdfmajor/parser_v3/objects/stream.py +++ b/pdfmajor/parser/objects/stream.py @@ -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 diff --git a/pdfmajor/parser_v3/parsers.py b/pdfmajor/parser/parsers.py similarity index 100% rename from pdfmajor/parser_v3/parsers.py rename to pdfmajor/parser/parsers.py diff --git a/pdfmajor/parser_v3/state.py b/pdfmajor/parser/state.py similarity index 100% rename from pdfmajor/parser_v3/state.py rename to pdfmajor/parser/state.py diff --git a/pdfmajor/parser_v3/__init__.py b/pdfmajor/parser_v3/__init__.py deleted file mode 100644 index 0073289..0000000 --- a/pdfmajor/parser_v3/__init__.py +++ /dev/null @@ -1,126 +0,0 @@ -import io -from pdfmajor.parser_v3.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 diff --git a/pdfmajor/streambuffer.py b/pdfmajor/streambuffer.py index 1c5127d..49315d1 100644 --- a/pdfmajor/streambuffer.py +++ b/pdfmajor/streambuffer.py @@ -1,6 +1,6 @@ import io from typing import NamedTuple -from pdfmajor.execptions import PDFMajorException +from pdfmajor.exceptions import PDFMajorException class StreamEOF(PDFMajorException, EOFError): diff --git a/tests/test_parser.py b/tests/test_parser.py index c0ed9bd..0c74773 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -5,11 +5,11 @@ from decimal import Decimal from unittest import TestCase from pdfmajor.streambuffer import StreamEOF -from pdfmajor.parser_v3.objects.indirect import IndirectObject, IndirectObjectCollection -from pdfmajor.parser_v3.objects.base import PDFObject -from pdfmajor.parser_v3.objects.comment import PDFComment -from pdfmajor.parser_v3.objects.stream import PDFStream -from pdfmajor.parser_v3 import PDFParser +from pdfmajor.parser.objects.indirect import IndirectObject, IndirectObjectCollection +from pdfmajor.parser.objects.base import PDFObject +from pdfmajor.parser.objects.comment import PDFComment +from pdfmajor.parser.objects.stream import PDFStream +from pdfmajor.parser import PDFParser class Collections(TestCase): diff --git a/tests/test_parser_filters.py b/tests/test_parser_filters.py index 9b689a1..97f849a 100644 --- a/tests/test_parser_filters.py +++ b/tests/test_parser_filters.py @@ -1,8 +1,8 @@ from unittest import TestCase -from pdfmajor.parser_v3.fliters.ccit import CCITTG4Parser -from pdfmajor.parser_v3.fliters.lzw import lzwdecode -from pdfmajor.parser_v3.fliters.rld import rldecode +from pdfmajor.parser.fliters.ccit import CCITTG4Parser +from pdfmajor.parser.fliters.lzw import lzwdecode +from pdfmajor.parser.fliters.rld import rldecode class Filters(TestCase):