From 336f5f1fba4a9b3c9b4c55340d38251507bb2f4b Mon Sep 17 00:00:00 2001 From: Ariel Sosnovsky Date: Sat, 7 Dec 2019 22:58:13 -0500 Subject: [PATCH] propograted exceptions --- pdfmajor/execptions.py | 2 ++ pdfmajor/interpreter/PageInterpreter.py | 1 - pdfmajor/interpreter/commands/PDFCommands.py | 7 ++++--- pdfmajor/interpreter/commands/commands.py | 20 +++++++++---------- .../state/PDFGraphicState/__init__.py | 1 - .../state/PDFTextState/PDFFont/PDFFont.py | 8 +++----- .../state/PDFTextState/PDFFont/__init__.py | 7 ++++--- .../state/PDFTextState/PDFFont/execptions.py | 4 ++++ .../state/PDFTextState/PDFFont/fonts.py | 13 ++++++------ pdfmajor/interpreter/exceptions.py | 3 +++ pdfmajor/parser/exceptions.py | 5 +++++ 11 files changed, 42 insertions(+), 29 deletions(-) create mode 100644 pdfmajor/execptions.py create mode 100644 pdfmajor/interpreter/commands/state/PDFTextState/PDFFont/execptions.py create mode 100644 pdfmajor/interpreter/exceptions.py create mode 100644 pdfmajor/parser/exceptions.py diff --git a/pdfmajor/execptions.py b/pdfmajor/execptions.py new file mode 100644 index 0000000..a9ecf0d --- /dev/null +++ b/pdfmajor/execptions.py @@ -0,0 +1,2 @@ +import pdfmajor.parser.exceptions as parser +import pdfmajor.interpreter.exceptions as interpreter \ No newline at end of file diff --git a/pdfmajor/interpreter/PageInterpreter.py b/pdfmajor/interpreter/PageInterpreter.py index af87c4d..55d1410 100644 --- a/pdfmajor/interpreter/PageInterpreter.py +++ b/pdfmajor/interpreter/PageInterpreter.py @@ -5,7 +5,6 @@ from .commands import PDFStateStack from .commands import process_command_stream, prep_state class PageInterpreter: - class PDFInterpreterError(Exception): pass def __init__(self, page: PDFPage, page_num: int, font_cache: dict = {}): (x0, y0, x1, y1) = page.mediabox diff --git a/pdfmajor/interpreter/commands/PDFCommands.py b/pdfmajor/interpreter/commands/PDFCommands.py index 3e5f147..c7260b2 100644 --- a/pdfmajor/interpreter/commands/PDFCommands.py +++ b/pdfmajor/interpreter/commands/PDFCommands.py @@ -7,17 +7,18 @@ from pdfmajor.parser.constants import LITERAL_FORM, LITERAL_IMAGE from .state import PDFStateStack, PDFColorSpace from .state.Curves import CurveMethod, CurvePath, CurvePoint +class PDFCommandsInvalidOperation(Exception): pass +class PDFCommandsRepeatedCommand(Exception): pass + class PDFCommands: commands = {} - class InvalidOperation(Exception): pass - class RepeatedCommand(Exception): pass @classmethod def __add(cls, cmd_name: str, cmd: callable): if not (cmd_name in cls.commands.keys()): cls.commands[cmd_name] = cmd return cmd - raise cls.RepeatedCommand(cmd_name) + raise PDFCommandsRepeatedCommand(cmd_name) @classmethod def add(cls, *cmd_names: List[str]): diff --git a/pdfmajor/interpreter/commands/commands.py b/pdfmajor/interpreter/commands/commands.py index 7925eea..5468198 100644 --- a/pdfmajor/interpreter/commands/commands.py +++ b/pdfmajor/interpreter/commands/commands.py @@ -3,7 +3,7 @@ from pdfmajor.parser.PSStackParser import literal_name from pdfmajor.parser.PDFStream import PDFStream, list_value, dict_value from pdfmajor.parser.constants import LITERAL_FORM, LITERAL_IMAGE -from .PDFCommands import PDFCommands +from .PDFCommands import PDFCommands, PDFCommandsInvalidOperation, PDFCommandsRepeatedCommand from .state import PDFStateStack, PDFGraphicState, LTTextBlock from .state.Curves import CurveMethod, CurvePath, CurvePoint # from .state.PDFItem import PDFImage, PDFShape, PDFText, PDFXObject @@ -253,7 +253,7 @@ def set_stroke_colorspace(stack: PDFStateStack, name: str) -> PDFStateStack: stack.graphics.scolspace = stack.colorspace_map[literal_name(name)] return stack except KeyError: - raise PDFCommands.InvalidOperation('Undefined ColorSpace: %r' % name) + raise PDFCommandsInvalidOperation('Undefined ColorSpace: %r' % name) # setcolorspace-non-strokine @PDFCommands.add('cs') @@ -262,7 +262,7 @@ def set_nonstroke_colorspace(stack: PDFStateStack, name: str) -> PDFStateStack: stack.graphics.ncolspace = stack.colorspace_map[literal_name(name)] return stack except KeyError: - raise PDFCommands.InvalidOperation('Undefined ColorSpace: %r' % name) + raise PDFCommandsInvalidOperation('Undefined ColorSpace: %r' % name) # setgray-stroking @PDFCommands.add('G') @@ -326,7 +326,7 @@ def set_nonstroke_cmyk(stack: PDFStateStack, c, m, y, k) -> PDFStateStack: @PDFCommands.add('SCN','SC') def set_stroke_colorspace_custom(stack: PDFStateStack) -> PDFStateStack: if stack.graphics.scolspace is None: - raise PDFCommands.InvalidOperation('No colorspace specified!') + raise PDFCommandsInvalidOperation('No colorspace specified!') components = stack.pop(stack.graphics.scolspace.ncomponents) stack.graphics.set_stroke_color( stack.graphics.scolspace, @@ -337,7 +337,7 @@ def set_stroke_colorspace_custom(stack: PDFStateStack) -> PDFStateStack: @PDFCommands.add('scn','sc') def set_nonstroke_colorspace_custom(stack: PDFStateStack) -> PDFStateStack: if stack.graphics.ncolspace is None: - raise PDFCommands.InvalidOperation('No colorspace specified!') + raise PDFCommandsInvalidOperation('No colorspace specified!') components = stack.pop(stack.graphics.ncolspace.ncomponents) stack.graphics.set_nostroke_color( stack.graphics.ncolspace, @@ -353,7 +353,7 @@ def begin_text(stack: PDFStateStack) -> PDFStateStack: if stack.current_textblock is None: stack.current_textblock = LTTextBlock() else: - raise PDFCommands.InvalidOperation("Last TextBlock did not clear") + raise PDFCommandsInvalidOperation("Last TextBlock did not clear") return stack @PDFCommands.add('ET') @@ -394,7 +394,7 @@ def set_font(stack: PDFStateStack, fontid, fontsize) -> PDFStateStack: stack.text.font = stack.fontmap[literal_name(fontid)] return stack except KeyError: - raise PDFCommands.InvalidOperation('Undefined Font id: %r' % fontid) + raise PDFCommandsInvalidOperation('Undefined Font id: %r' % fontid) # setrendering @PDFCommands.add('Tr') @@ -444,9 +444,9 @@ def nextline(stack: PDFStateStack) -> PDFStateStack: @PDFCommands.add('TJ') def add_char_blocks(stack: PDFStateStack, seq: bytearray) -> PDFStateStack: if stack.text.font is None: - raise PDFCommands.InvalidOperation("No Font Specified") + raise PDFCommandsInvalidOperation("No Font Specified") if stack.current_textblock is None: - raise PDFCommands.InvalidOperation("No TextBlock initilized") + raise PDFCommandsInvalidOperation("No TextBlock initilized") stack.current_textblock.add_char_block( seq, stack.t_matrix, @@ -487,7 +487,7 @@ def insert_xobject(stack: PDFStateStack, xobjid) -> PDFStateStack: try: xobj = PDFStream.validated_stream(stack.xobjmap[xobjid]) except KeyError: - raise PDFCommands.InvalidOperation('Undefined xobject id: %r' % xobjid) + raise PDFCommandsInvalidOperation('Undefined xobject id: %r' % xobjid) # log.info('Processing xobj: %r', xobj) subtype = xobj.get('Subtype') if subtype is LITERAL_FORM and 'BBox' in xobj: diff --git a/pdfmajor/interpreter/commands/state/PDFGraphicState/__init__.py b/pdfmajor/interpreter/commands/state/PDFGraphicState/__init__.py index e789ba4..4a24fbe 100644 --- a/pdfmajor/interpreter/commands/state/PDFGraphicState/__init__.py +++ b/pdfmajor/interpreter/commands/state/PDFGraphicState/__init__.py @@ -2,7 +2,6 @@ from .PDFColor import PDFColor, PDFColorSpace from .PDFColorSpace import PREDEFINED_COLORSPACE class PDFGraphicState(object): - class InvalidOperation(Exception): pass def __init__(self): self.linewidth = 0 diff --git a/pdfmajor/interpreter/commands/state/PDFTextState/PDFFont/PDFFont.py b/pdfmajor/interpreter/commands/state/PDFTextState/PDFFont/PDFFont.py index 8c35565..65d1717 100644 --- a/pdfmajor/interpreter/commands/state/PDFTextState/PDFFont/PDFFont.py +++ b/pdfmajor/interpreter/commands/state/PDFTextState/PDFFont/PDFFont.py @@ -10,10 +10,10 @@ from pdfmajor.parser.PDFStream import resolve1 from pdfmajor.parser.PDFStream import int_value from pdfmajor.parser.PDFStream import num_value from pdfmajor.parser.PDFStream import list_value -from pdfmajor.parser.PDFStream import PDFException from pdfmajor.parser.PDFStream import PDFStream from pdfmajor.parser.cmapdb import FileUnicodeMap from pdfmajor.parser.cmapdb import CMapParser +from .execptions import PDFFontError, PDFUnicodeNotDefined ## Literals ## @@ -22,8 +22,6 @@ LITERAL_TYPE1C = LIT('Type1C') class PDFFont(object): - class PDFFontError(PDFException): pass - class PDFUnicodeNotDefined(PDFFontError): pass def __init__(self, descriptor, widths, default_width=None): self.descriptor = descriptor @@ -77,7 +75,7 @@ class PDFFont(object): except KeyError: try: return self.widths[self.to_unichr(cid)] * self.hscale - except (KeyError, self.PDFUnicodeNotDefined): + except (KeyError, PDFUnicodeNotDefined): return self.default_width * self.hscale def char_disp(self, cid): @@ -137,4 +135,4 @@ class PDFSimpleFont(PDFFont): try: return self.cid2unicode[cid] except KeyError: - raise self.PDFUnicodeNotDefined(None, cid) + raise PDFUnicodeNotDefined(None, cid) diff --git a/pdfmajor/interpreter/commands/state/PDFTextState/PDFFont/__init__.py b/pdfmajor/interpreter/commands/state/PDFTextState/PDFFont/__init__.py index 749032b..e530401 100644 --- a/pdfmajor/interpreter/commands/state/PDFTextState/PDFFont/__init__.py +++ b/pdfmajor/interpreter/commands/state/PDFTextState/PDFFont/__init__.py @@ -4,6 +4,7 @@ from pdfmajor.parser.PDFStream import list_value from pdfmajor.parser.PDFStream import dict_value from pdfmajor.parser.PDFStream import resolve1 from pdfmajor.parser.constants import LITERAL_FONT +from .execptions import PDFFontError from .fonts import PDFType1Font, PDFTrueTypeFont, PDFType3Font, PDFCIDFont, PDFFont @@ -16,13 +17,13 @@ def get_font(objid: int, spec: dict, cached_fonts: dict = {}): log.debug('get_font: create: objid=%r, spec=%r', objid, spec) if settings.STRICT: if spec['Type'] is not LITERAL_FONT: - raise PDFFont.PDFFontError('Type is not /Font') + raise PDFFontError('Type is not /Font') # Create a Font object. if 'Subtype' in spec: subtype = literal_name(spec['Subtype']) else: if settings.STRICT: - raise PDFFont.PDFFontError('Font Subtype is not specified.') + raise PDFFontError('Font Subtype is not specified.') subtype = 'Type1' if subtype in ('Type1', 'MMType1'): # Type1 Font @@ -47,7 +48,7 @@ def get_font(objid: int, spec: dict, cached_fonts: dict = {}): font = get_font(None, subspec, cached_fonts) else: if settings.STRICT: - raise PDFFont.PDFFontError('Invalid Font spec: %r' % spec) + raise PDFFontError('Invalid Font spec: %r' % spec) font = PDFType1Font(spec) # this is so wrong! if objid: cached_fonts[objid] = font diff --git a/pdfmajor/interpreter/commands/state/PDFTextState/PDFFont/execptions.py b/pdfmajor/interpreter/commands/state/PDFTextState/PDFFont/execptions.py new file mode 100644 index 0000000..d4c4a67 --- /dev/null +++ b/pdfmajor/interpreter/commands/state/PDFTextState/PDFFont/execptions.py @@ -0,0 +1,4 @@ +from pdfmajor.parser.PDFStream import PDFException + +class PDFFontError(PDFException): pass +class PDFUnicodeNotDefined(PDFFontError): pass diff --git a/pdfmajor/interpreter/commands/state/PDFTextState/PDFFont/fonts.py b/pdfmajor/interpreter/commands/state/PDFTextState/PDFFont/fonts.py index 73d8f9b..dbd94f1 100644 --- a/pdfmajor/interpreter/commands/state/PDFTextState/PDFFont/fonts.py +++ b/pdfmajor/interpreter/commands/state/PDFTextState/PDFFont/fonts.py @@ -15,6 +15,7 @@ from .PDFFont import PDFFont, PDFSimpleFont from .util import FontMetricsDB, get_widths, get_widths2 from .Type1FontHeaderParser import Type1FontHeaderParser from .TrueTypeFont import TrueTypeFont +from .execptions import PDFFontError, PDFUnicodeNotDefined # PDFType1Font class PDFType1Font(PDFSimpleFont): @@ -24,7 +25,7 @@ class PDFType1Font(PDFSimpleFont): self.basefont = literal_name(spec['BaseFont']) except KeyError: if settings.STRICT: - raise self.PDFFontError('BaseFont is missing') + raise PDFFontError('BaseFont is missing') self.basefont = 'unknown' try: (descriptor, widths) = FontMetricsDB.get_metrics(self.basefont) @@ -86,7 +87,7 @@ class PDFCIDFont(PDFFont): self.basefont = literal_name(spec['BaseFont']) except KeyError: if strict: - raise self.PDFFontError('BaseFont is missing') + raise PDFFontError('BaseFont is missing') self.basefont = 'unknown' self.cidsysteminfo = dict_value(spec.get('CIDSystemInfo', {})) self.cidcoding = '%s-%s' % (resolve1(self.cidsysteminfo.get('Registry', b'unknown')).decode("latin1"), @@ -95,19 +96,19 @@ class PDFCIDFont(PDFFont): name = literal_name(spec['Encoding']) except KeyError: if strict: - raise self.PDFFontError('Encoding is unspecified') + raise PDFFontError('Encoding is unspecified') name = 'unknown' try: self.cmap = CMapDB.get_cmap(name) except CMapDB.CMapNotFound as e: if strict: - raise self.PDFFontError(e) + raise PDFFontError(e) self.cmap = CMap() try: descriptor = dict_value(spec['FontDescriptor']) except KeyError: if strict: - raise self.PDFFontError('FontDescriptor is missing') + raise PDFFontError('FontDescriptor is missing') descriptor = {} ttf = None if 'FontFile2' in descriptor: @@ -171,4 +172,4 @@ class PDFCIDFont(PDFFont): raise KeyError(cid) return self.unicode_map.get_unichr(cid) except KeyError: - raise self.PDFUnicodeNotDefined(self.cidcoding, cid) + raise PDFUnicodeNotDefined(self.cidcoding, cid) diff --git a/pdfmajor/interpreter/exceptions.py b/pdfmajor/interpreter/exceptions.py new file mode 100644 index 0000000..252f13b --- /dev/null +++ b/pdfmajor/interpreter/exceptions.py @@ -0,0 +1,3 @@ +from .commands import CommandProcessorError +from .commands.PDFCommands import PDFCommandsRepeatedCommand, PDFCommandsInvalidOperation +from .commands.state.PDFTextState.PDFFont.execptions import * \ No newline at end of file diff --git a/pdfmajor/parser/exceptions.py b/pdfmajor/parser/exceptions.py new file mode 100644 index 0000000..87ad644 --- /dev/null +++ b/pdfmajor/parser/exceptions.py @@ -0,0 +1,5 @@ +from .cmapdb import CMapError +from .PDFStream.exceptions import * +from .PDFDocument.exceptions import * +from .PSStackParser.exceptions import * +from .PDFParser import PDFSyntaxError \ No newline at end of file