Skip to content

Commit

Permalink
STY: Use IntFlag for permissions_flag / update_page_form_field_values (
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinThoma authored Jul 12, 2022
1 parent d376d0e commit af5a0c3
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
- Apply black
- Typo in Changelog

Full Changelog: https://github.com/py-pdf/PyPDF2/compare/2.4.2...2.4.3
Full Changelog: https://github.com/py-pdf/PyPDF2/compare/2.4.2...2.5.0

## Version 2.4.2, 2022-07-05

Expand Down
19 changes: 15 additions & 4 deletions PyPDF2/_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
from .constants import EncryptionDictAttributes as ED
from .constants import (
FieldDictionaryAttributes,
FieldFlag,
FileSpecificationDictionaryEntries,
GoToActionArguments,
InteractiveFormDictEntries,
Expand All @@ -75,7 +76,7 @@
from .constants import PagesAttributes as PA
from .constants import StreamAttributes as SA
from .constants import TrailerKeys as TK
from .constants import TypFitArguments
from .constants import TypFitArguments, UserAccessPermissions
from .generic import (
ArrayObject,
BooleanObject,
Expand Down Expand Up @@ -110,6 +111,10 @@
logger = logging.getLogger(__name__)


OPTIONAL_READ_WRITE_FIELD = FieldFlag(0)
ALL_DOCUMENT_PERMISSIONS = UserAccessPermissions((2**31 - 1) - 3)


class PdfWriter:
"""
This class supports writing PDF files out, given pages produced by another
Expand Down Expand Up @@ -576,7 +581,10 @@ def appendPagesFromReader(
self.append_pages_from_reader(reader, after_page_append)

def update_page_form_field_values(
self, page: PageObject, fields: Dict[str, Any], flags: int = 0
self,
page: PageObject,
fields: Dict[str, Any],
flags: FieldFlag = OPTIONAL_READ_WRITE_FIELD,
) -> None:
"""
Update the form field values for a given page from a fields dictionary.
Expand Down Expand Up @@ -627,7 +635,10 @@ def update_page_form_field_values(
)

def updatePageFormFieldValues(
self, page: PageObject, fields: Dict[str, Any], flags: int = 0
self,
page: PageObject,
fields: Dict[str, Any],
flags: FieldFlag = OPTIONAL_READ_WRITE_FIELD,
) -> None: # pragma: no cover
"""
.. deprecated:: 1.28.0
Expand Down Expand Up @@ -699,7 +710,7 @@ def encrypt(
user_pwd: str,
owner_pwd: Optional[str] = None,
use_128bit: bool = True,
permissions_flag: int = -1,
permissions_flag: UserAccessPermissions = ALL_DOCUMENT_PERMISSIONS,
) -> None:
"""
Encrypt this PDF file with the PDF Standard encryption handler.
Expand Down
46 changes: 46 additions & 0 deletions PyPDF2/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
PDF Reference, sixth edition, Version 1.7, 2006.
"""

from enum import IntFlag
from typing import Dict, Tuple


Expand Down Expand Up @@ -47,6 +48,43 @@ class EncryptionDictAttributes:
ENCRYPT_METADATA = "/EncryptMetadata" # boolean flag, optional


class UserAccessPermissions(IntFlag):
"""TABLE 3.20 User access permissions"""

R1 = 1
R2 = 2
PRINT = 4
MODIFY = 8
EXTRACT = 16
ADD_OR_MODIFY = 32
R7 = 64
R8 = 128
FILL_FORM_FIELDS = 256
EXTRACT_TEXT_AND_GRAPHICS = 512
ASSEMBLE_DOC = 1024
PRINT_TO_REPRESENTATION = 2048
R13 = 2**12
R14 = 2**13
R15 = 2**14
R16 = 2**15
R17 = 2**16
R18 = 2**17
R19 = 2**18
R20 = 2**19
R21 = 2**20
R22 = 2**21
R23 = 2**22
R24 = 2**23
R25 = 2**24
R26 = 2**25
R27 = 2**26
R28 = 2**27
R29 = 2**28
R30 = 2**29
R31 = 2**30
R32 = 2**31


class Ressources:
"""TABLE 3.30 Entries in a resource dictionary."""

Expand Down Expand Up @@ -294,6 +332,14 @@ def attributes_dict(cls) -> Dict[str, str]:
}


class FieldFlag(IntFlag):
"""TABLE 8.70 Field flags common to all field types"""

READ_ONLY = 1
REQUIRED = 2
NO_EXPORT = 4


class DocumentInformationAttributes:
"""TABLE 10.2 Entries in the document information dictionary."""

Expand Down

0 comments on commit af5a0c3

Please sign in to comment.