Skip to content

BUG/ENH: make attachements compatible with kids, and allow list in RF #2197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 18 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fix include mypy
  • Loading branch information
pubpub-zz committed Nov 2, 2023
commit ab963313e2d54d5b757f1f4c6e07f12eea5311bc
30 changes: 19 additions & 11 deletions pypdf/generic/_data_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
Any,
Callable,
Dict,
Generator,
Iterable,
Iterator,
List,
Mapping,
Optional,
Expand Down Expand Up @@ -1848,7 +1848,7 @@ def get_from_file_specification(_a: DictionaryObject) -> PdfObject:
)


class AttachmentBytesDictionary(dict):
class AttachmentBytesDictionary(Mapping[str, AttachmentBytes]):
"""
Dict[str, AttachmentBytes]
Ease access to Dictionary of Object
Expand All @@ -1858,24 +1858,28 @@ class AttachmentBytesDictionary(dict):
names: List[str]

def __init__(
self, root: Optional[Union[NameTree, DictionaryObject, IndirectObject]]
):
dict.__init__(self)
self, root: Optional[Union[NameTree, DictionaryObject]] = None
) -> None:
# super().__init__(self)
if isinstance(root, IndirectObject):
root = cast(DictionaryObject, root.get_object())
if root is not None:
self.root = (
root if isinstance(root, NameTree) else NameTree(root)
)
self.root = root if isinstance(root, NameTree) else NameTree(root)
self.names = list(self.root.list_keys())
else:
self.root = None
self.names = []

def keys(self) -> List[str]:
def keys(self) -> List[str]: # type: ignore[override]
return self.names

def items(self) -> Generator[str, AttachmentBytes]:
def __len__(self) -> int:
return len(self.names)

def __iter__(self) -> Iterator[str]: # type: ignore
yield from self.names

def items(self) -> Iterable[Tuple[str, AttachmentBytes]]: # type: ignore[override]
if self.root is None:
return []
else:
Expand All @@ -1891,8 +1895,12 @@ def items(self) -> Generator[str, AttachmentBytes]:

def __getitem__(self, k: str) -> AttachmentBytes:
if k not in self.names:
raise KeyError("KeyError: k")
raise KeyError(f"KeyError: {k}")
if self.root is None:
raise ValueError("Empty Object")
v = self.root.list_get(k)
if v is None:
raise KeyError(f"KeyError: {k}")
return AttachmentBytes(cast(DictionaryObject, v.get_object()))

def __repr__(self) -> str:
Expand Down