diff options
author | James R. Barlow <jim@purplerock.ca> | 2019-06-19 01:35:11 -0700 |
---|---|---|
committer | James R. Barlow <jim@purplerock.ca> | 2019-06-19 01:35:11 -0700 |
commit | 8f8c4f1d2f4612d9a324572b3b64577ea6c6abb1 (patch) | |
tree | 636aec083d6a0907b033f1932a013582f4631097 | |
parent | dea146cec9e7473026a385dc65400a3ed46848b6 (diff) |
Revise PdfPermissions - shouldn't be a tuple when it's not ordered
-rw-r--r-- | src/pikepdf/_methods.py | 2 | ||||
-rw-r--r-- | src/pikepdf/models/__init__.py | 59 | ||||
-rw-r--r-- | tests/test_pdf.py | 2 |
3 files changed, 44 insertions, 19 deletions
diff --git a/src/pikepdf/_methods.py b/src/pikepdf/_methods.py index 3024a99..726c276 100644 --- a/src/pikepdf/_methods.py +++ b/src/pikepdf/_methods.py @@ -311,7 +311,7 @@ class Extend_Pdf: """ results = {} - for field in PdfPermissions._fields: + for field in PdfPermissions.fields(): results[field] = getattr(self, '_allow_' + field) return PdfPermissions(**results) diff --git a/src/pikepdf/models/__init__.py b/src/pikepdf/models/__init__.py index 1e266ad..1605bf5 100644 --- a/src/pikepdf/models/__init__.py +++ b/src/pikepdf/models/__init__.py @@ -4,29 +4,54 @@ # # Copyright (C) 2017, James R. Barlow (https://github.com/jbarlow83/) -from collections import namedtuple - from .. import Object, ObjectType, PdfError +import types + from .matrix import PdfMatrix from .image import PdfImage, PdfInlineImage, UnsupportedImageTypeError from .metadata import PdfMetadata -PdfPermissions = namedtuple( - 'PdfPermissions', - [ - "accessibility", - "extract", - "print_lowres", - "print_highres", - "modify_assembly", - "modify_form", - "modify_annotation", - "modify_other", - "modify_all", - ], - defaults=[True] * 9, -) + +class PdfPermissions(types.SimpleNamespace): + """ + Stores the permissions for an encrypted PDF. + + Unencrypted PDFs implicitly have all permissions allowed. + pikepdf does not enforce the restrictions in any way. + """ + + def __init__( + self, + accessibility=True, + extract=True, + modify_all=True, + modify_annotation=True, + modify_assembly=False, + modify_form=True, + modify_other=True, + print_lowres=True, + print_highres=True, + ): + kvs = locals() + del kvs['self'] + super().__init__(**kvs) + + def __setattr__(self, k, v): + raise TypeError("object is read-only") + + def __delattr__(self, k): + raise TypeError("object is read-only") + + def keys(self): + yield from (k for k in self.__dict__ if not k.startswith('_')) + + def values(self): + yield from (v for k, v in self.__dict__.items() if not k.startswith('_')) + + @classmethod + def fields(cls): + yield from (k for k in cls().__dict__ if not k.startswith('_')) def parse_content_stream(page_or_stream, operators=''): diff --git a/tests/test_pdf.py b/tests/test_pdf.py index 9e63893..7c993eb 100644 --- a/tests/test_pdf.py +++ b/tests/test_pdf.py @@ -95,7 +95,7 @@ class TestPermissions: ) def test_permissions_all_true_not_encrypted(self, trivial): - assert all(trivial.allow) + assert all(trivial.allow.values()) class TestStreams: |