Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/sentry_sdk/attachments.py: 26%
31 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
1import os
2import mimetypes
4from sentry_sdk._types import MYPY
5from sentry_sdk.envelope import Item, PayloadRef
7if MYPY: 7 ↛ 8line 7 didn't jump to line 8, because the condition on line 7 was never true
8 from typing import Optional, Union, Callable
11class Attachment(object):
12 def __init__(
13 self,
14 bytes=None, # type: Union[None, bytes, Callable[[], bytes]]
15 filename=None, # type: Optional[str]
16 path=None, # type: Optional[str]
17 content_type=None, # type: Optional[str]
18 add_to_transactions=False, # type: bool
19 ):
20 # type: (...) -> None
21 if bytes is None and path is None:
22 raise TypeError("path or raw bytes required for attachment")
23 if filename is None and path is not None:
24 filename = os.path.basename(path)
25 if filename is None:
26 raise TypeError("filename is required for attachment")
27 if content_type is None:
28 content_type = mimetypes.guess_type(filename)[0]
29 self.bytes = bytes
30 self.filename = filename
31 self.path = path
32 self.content_type = content_type
33 self.add_to_transactions = add_to_transactions
35 def to_envelope_item(self):
36 # type: () -> Item
37 """Returns an envelope item for this attachment."""
38 payload = None # type: Union[None, PayloadRef, bytes]
39 if self.bytes is not None:
40 if callable(self.bytes):
41 payload = self.bytes()
42 else:
43 payload = self.bytes
44 else:
45 payload = PayloadRef(path=self.path)
46 return Item(
47 payload=payload,
48 type="attachment",
49 content_type=self.content_type,
50 filename=self.filename,
51 )
53 def __repr__(self):
54 # type: () -> str
55 return "<Attachment %r>" % (self.filename,)