Coverage for src / kdbxtool / models / attachment.py: 82%

17 statements  

« prev     ^ index     » next       coverage.py v7.12.0, created at 2025-12-19 21:22 +0000

1"""Attachment model for KDBX binary attachments.""" 

2 

3from __future__ import annotations 

4 

5from dataclasses import dataclass 

6from typing import TYPE_CHECKING 

7 

8if TYPE_CHECKING: 

9 from .entry import Entry 

10 

11 

12@dataclass 

13class Attachment: 

14 """An attachment (binary file) associated with an entry. 

15 

16 Attachments represent files attached to entries in the database. The binary 

17 data is stored at the database level and referenced by ID. 

18 

19 Attributes: 

20 filename: Name of the attached file 

21 id: Reference ID to the binary data in the database 

22 entry: The entry this attachment belongs to 

23 """ 

24 

25 filename: str 

26 id: int 

27 entry: Entry 

28 

29 @property 

30 def data(self) -> bytes | None: 

31 """Get the binary data for this attachment. 

32 

33 Returns: 

34 Binary data if available, None if not found or entry has no database 

35 """ 

36 if self.entry.database is None: 

37 return None 

38 return self.entry.database.get_binary(self.id) 

39 

40 def __str__(self) -> str: 

41 return f"Attachment: '{self.filename}' -> {self.id}" 

42 

43 def __repr__(self) -> str: 

44 return f"Attachment(filename={self.filename!r}, id={self.id})"