databutton 0.30.1__py3-none-any.whl → 0.31.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- databutton/notify/__init__.py +6 -1
- databutton/notify/email.py +232 -9
- databutton/version.py +1 -1
- {databutton-0.30.1.dist-info → databutton-0.31.1.dist-info}/METADATA +2 -2
- {databutton-0.30.1.dist-info → databutton-0.31.1.dist-info}/RECORD +7 -7
- {databutton-0.30.1.dist-info → databutton-0.31.1.dist-info}/LICENSE +0 -0
- {databutton-0.30.1.dist-info → databutton-0.31.1.dist-info}/WHEEL +0 -0
databutton/notify/__init__.py
CHANGED
databutton/notify/email.py
CHANGED
@@ -1,17 +1,16 @@
|
|
1
|
+
import base64
|
2
|
+
import io
|
3
|
+
import mimetypes
|
4
|
+
import re
|
5
|
+
from collections.abc import Sequence
|
1
6
|
from typing import List, Optional, Union
|
2
7
|
|
8
|
+
import pandas as pd
|
3
9
|
from pydantic import BaseModel
|
4
10
|
|
5
11
|
from .send import send
|
6
12
|
|
7
13
|
|
8
|
-
class Email(BaseModel):
|
9
|
-
to: Union[str, List[str]]
|
10
|
-
subject: str
|
11
|
-
content_text: Optional[str]
|
12
|
-
content_html: Optional[str]
|
13
|
-
|
14
|
-
|
15
14
|
def valid_email(recipient: str) -> bool:
|
16
15
|
# Note: We could possibly use some email validation library but it's tricky
|
17
16
|
parts = recipient.split("@")
|
@@ -36,11 +35,234 @@ def validate_email_to_arg(to: Union[str, List[str]]) -> List[str]:
|
|
36
35
|
return to
|
37
36
|
|
38
37
|
|
38
|
+
# This is the type expected in the api
|
39
|
+
class Attachment(BaseModel):
|
40
|
+
"""An attachment to be included with an email."""
|
41
|
+
|
42
|
+
# Attachment file name
|
43
|
+
file_name: Optional[str] = None
|
44
|
+
|
45
|
+
# MIME type of the attachment
|
46
|
+
content_type: Optional[str] = None
|
47
|
+
|
48
|
+
# Content ID (CID) to use for inline attachments
|
49
|
+
content_id: Optional[str] = None
|
50
|
+
|
51
|
+
# Base64 encoded data
|
52
|
+
content_base64: str
|
53
|
+
|
54
|
+
|
55
|
+
# This is the type expected in the api
|
56
|
+
class Email(BaseModel):
|
57
|
+
to: Union[str, List[str]]
|
58
|
+
subject: str
|
59
|
+
content_text: Optional[str] = None
|
60
|
+
content_html: Optional[str] = None
|
61
|
+
attachments: list[Attachment] = []
|
62
|
+
|
63
|
+
|
64
|
+
def determine_type(type: Optional[str], name: Optional[str]) -> Optional[str]:
|
65
|
+
if type:
|
66
|
+
return type
|
67
|
+
if name:
|
68
|
+
type, encoding = mimetypes.guess_type(name)
|
69
|
+
# if encoding is not None:
|
70
|
+
# return "; ".join([type, encoding])
|
71
|
+
return type
|
72
|
+
return None
|
73
|
+
|
74
|
+
|
75
|
+
def encode_content(content: bytes | str) -> str:
|
76
|
+
if isinstance(content, str):
|
77
|
+
content = content.encode()
|
78
|
+
return base64.b64encode(content).decode()
|
79
|
+
|
80
|
+
|
81
|
+
def attachment_from_bytes(
|
82
|
+
content: bytes,
|
83
|
+
*,
|
84
|
+
file_name: Optional[str] = None,
|
85
|
+
content_type: Optional[str] = None,
|
86
|
+
cid: Optional[str] = None,
|
87
|
+
) -> Attachment:
|
88
|
+
"""Create attachment with content as raw bytes.
|
89
|
+
|
90
|
+
You can optionally provide a file name and/or content type.
|
91
|
+
|
92
|
+
If missing we will try to guess the content type from the file name.
|
93
|
+
|
94
|
+
To use an attachment as an inline image in the email,
|
95
|
+
set the `cid="my_image_id"` parameter,
|
96
|
+
and use `<img src="cid:my_image_id">` in the html content.
|
97
|
+
"""
|
98
|
+
return Attachment(
|
99
|
+
file_name=file_name,
|
100
|
+
content_type=determine_type(content_type, file_name),
|
101
|
+
content_base64=encode_content(content),
|
102
|
+
content_id=cid,
|
103
|
+
)
|
104
|
+
|
105
|
+
|
106
|
+
def attachment_from_str(
|
107
|
+
content: str,
|
108
|
+
*,
|
109
|
+
file_name: Optional[str] = None,
|
110
|
+
content_type: Optional[str] = None,
|
111
|
+
cid: Optional[str] = None,
|
112
|
+
) -> Attachment:
|
113
|
+
"""Create attachment with content as raw str."""
|
114
|
+
return attachment_from_bytes(
|
115
|
+
content.encode(),
|
116
|
+
file_name=file_name,
|
117
|
+
content_type=content_type,
|
118
|
+
cid=cid,
|
119
|
+
)
|
120
|
+
|
121
|
+
|
122
|
+
def attachment_from_file(
|
123
|
+
fp: Optional[io.IOBase] = None,
|
124
|
+
*,
|
125
|
+
file_name: Optional[str] = None,
|
126
|
+
content_type: Optional[str] = None,
|
127
|
+
cid: Optional[str] = None,
|
128
|
+
) -> Attachment:
|
129
|
+
"""Create attachment with content from a file.
|
130
|
+
|
131
|
+
fp can be anything with a .read() method returning bytes or str,
|
132
|
+
or omitted to read file_name from file system.
|
133
|
+
"""
|
134
|
+
if fp is None:
|
135
|
+
if file_name is None:
|
136
|
+
raise ValueError("Either `fp` or `file_name` must be provided.")
|
137
|
+
with open(file_name, "rb") as fp:
|
138
|
+
buf = fp.read()
|
139
|
+
else:
|
140
|
+
buf = fp.read()
|
141
|
+
if isinstance(buf, str):
|
142
|
+
buf = buf.encode()
|
143
|
+
return attachment_from_bytes(
|
144
|
+
buf,
|
145
|
+
file_name=file_name,
|
146
|
+
content_type=content_type,
|
147
|
+
cid=cid,
|
148
|
+
)
|
149
|
+
|
150
|
+
|
151
|
+
def attachment_from_pil_image_as_jpeg(
|
152
|
+
image, # PIL image
|
153
|
+
*,
|
154
|
+
image_format: str = "JPEG",
|
155
|
+
file_name: Optional[str] = None,
|
156
|
+
content_type: Optional[str] = None,
|
157
|
+
cid: Optional[str] = None,
|
158
|
+
) -> Attachment:
|
159
|
+
"""Create image attachment with content from a PIL compatible image (such as pillow).
|
160
|
+
|
161
|
+
This convenience function calls image.save(buffer, format=image_format),
|
162
|
+
to further customize image formats etc take a peek at the implementation
|
163
|
+
here and use attachment_from_bytes directly instead.
|
164
|
+
|
165
|
+
You can optionally provide a file name and/or content type.
|
166
|
+
|
167
|
+
If missing we will try to guess the content type from the file name.
|
168
|
+
|
169
|
+
To use an attachment as an inline image in the email,
|
170
|
+
set the `cid="my_image_id"` parameter,
|
171
|
+
and use `<img src="cid:my_image_id">` in the html content.
|
172
|
+
"""
|
173
|
+
buf = io.BytesIO()
|
174
|
+
image.save(buf, format=image_format)
|
175
|
+
return attachment_from_bytes(
|
176
|
+
buf.getvalue(),
|
177
|
+
file_name=file_name,
|
178
|
+
content_type=content_type,
|
179
|
+
cid=cid,
|
180
|
+
)
|
181
|
+
|
182
|
+
|
183
|
+
def attachment_from_dataframe_as_csv(
|
184
|
+
df: pd.DataFrame,
|
185
|
+
*,
|
186
|
+
file_name: Optional[str] = None,
|
187
|
+
) -> Attachment:
|
188
|
+
"""Create CSV attachment with content from a pandas dataframe."""
|
189
|
+
return attachment_from_str(
|
190
|
+
df.to_csv(),
|
191
|
+
file_name=file_name,
|
192
|
+
content_type="text/csv",
|
193
|
+
)
|
194
|
+
|
195
|
+
|
196
|
+
MIME_TYPE_XLSX = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
197
|
+
|
198
|
+
|
199
|
+
def attachment_from_dataframe_as_excel(
|
200
|
+
df: pd.DataFrame,
|
201
|
+
*,
|
202
|
+
file_name: Optional[str] = None,
|
203
|
+
) -> Attachment:
|
204
|
+
"""Create Excel (.xlsx) attachment with content from a pandas dataframe.
|
205
|
+
|
206
|
+
Requires the openpyxl package to be installed.
|
207
|
+
"""
|
208
|
+
buf = io.BytesIO()
|
209
|
+
df.to_excel(buf)
|
210
|
+
return attachment_from_bytes(
|
211
|
+
buf.getvalue(),
|
212
|
+
file_name=file_name,
|
213
|
+
content_type=MIME_TYPE_XLSX,
|
214
|
+
)
|
215
|
+
|
216
|
+
|
217
|
+
def validate_attachment(att: Attachment) -> Attachment:
|
218
|
+
assert isinstance(att, Attachment)
|
219
|
+
assert att.content_type
|
220
|
+
assert att.file_name
|
221
|
+
assert att.content_base64
|
222
|
+
assert isinstance(att.content_base64, str)
|
223
|
+
assert re.match(r"^[A-Za-z0-9+/=]+$", att.content_base64)
|
224
|
+
return att
|
225
|
+
|
226
|
+
|
227
|
+
def create_email(
|
228
|
+
*,
|
229
|
+
to: Union[str, List[str]],
|
230
|
+
subject: str,
|
231
|
+
content_text: Optional[str] = None,
|
232
|
+
content_html: Optional[str] = None,
|
233
|
+
attachments: Sequence[Attachment] = (),
|
234
|
+
) -> Email:
|
235
|
+
attachments = [validate_attachment(att) for att in attachments]
|
236
|
+
|
237
|
+
# Sendgrid has a 30 MB limit on everything, this estimate should be slightly stricter
|
238
|
+
size = (
|
239
|
+
len(content_html or "")
|
240
|
+
+ len(content_text or "")
|
241
|
+
+ sum([len(att.content_base64) for att in attachments])
|
242
|
+
)
|
243
|
+
max = 30 * 1024**2 # 30 MB
|
244
|
+
headroom = 100 * 1024 # leave some room for headers etc
|
245
|
+
if size > max - headroom:
|
246
|
+
raise ValueError(
|
247
|
+
"Email and attachment size exceeds 30MB, please reduce the size of the email."
|
248
|
+
)
|
249
|
+
|
250
|
+
return Email(
|
251
|
+
to=validate_email_to_arg(to),
|
252
|
+
subject=subject,
|
253
|
+
content_text=content_text,
|
254
|
+
content_html=content_html,
|
255
|
+
attachments=attachments,
|
256
|
+
)
|
257
|
+
|
258
|
+
|
39
259
|
def email(
|
40
260
|
to: Union[str, List[str]],
|
41
261
|
subject: str,
|
262
|
+
*,
|
42
263
|
content_text: Optional[str] = None,
|
43
264
|
content_html: Optional[str] = None,
|
265
|
+
attachments: Sequence[Attachment] = (),
|
44
266
|
):
|
45
267
|
"""Send email notification from databutton.
|
46
268
|
|
@@ -53,10 +275,11 @@ def email(
|
|
53
275
|
the result may be less pretty than handcrafted text.
|
54
276
|
"""
|
55
277
|
send(
|
56
|
-
|
57
|
-
to=
|
278
|
+
create_email(
|
279
|
+
to=to,
|
58
280
|
subject=subject,
|
59
281
|
content_text=content_text,
|
60
282
|
content_html=content_html,
|
283
|
+
attachments=attachments,
|
61
284
|
)
|
62
285
|
)
|
databutton/version.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: databutton
|
3
|
-
Version: 0.
|
3
|
+
Version: 0.31.1
|
4
4
|
Summary: The CLI for databutton.com
|
5
5
|
License: Databutton
|
6
6
|
Author: Databutton
|
@@ -13,7 +13,7 @@ Requires-Dist: PyJWT (>=2.6.0,<3.0.0)
|
|
13
13
|
Requires-Dist: PyYAML (>=6.0,<7.0)
|
14
14
|
Requires-Dist: certifi (>=2022.12.7,<2023.0.0)
|
15
15
|
Requires-Dist: cryptography (>=40.0.1,<41.0.0)
|
16
|
-
Requires-Dist: httpx (>=0.
|
16
|
+
Requires-Dist: httpx (>=0.24.0,<0.25.0)
|
17
17
|
Requires-Dist: pandas (>=1.5.3,<2.0.0)
|
18
18
|
Requires-Dist: pydantic (>=1.10.7,<2.0.0)
|
19
19
|
Requires-Dist: streamlit (>=1.20.0,<2.0.0)
|
@@ -10,16 +10,16 @@ databutton/internal/performedby.py,sha256=QZ4XtsZSbltYfmZesfqVW-v7t153_ziIbCwjWD
|
|
10
10
|
databutton/internal/retries.py,sha256=OMxnreq1Icaf8OrtFPPNEZ8tCZe6HUI9fJ4FQ6Uiw-M,2011
|
11
11
|
databutton/jobs/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
12
12
|
databutton/jobs/run.py,sha256=Td0BSiIrYKiymDKVf8w5MT3C5uiwzeulYNJUGcxQk2k,632
|
13
|
-
databutton/notify/__init__.py,sha256=
|
14
|
-
databutton/notify/email.py,sha256=
|
13
|
+
databutton/notify/__init__.py,sha256=PWuuSBzsRFd0-gc6PS2oAMl4gSMSrgLAwTX2GiOaGyE,137
|
14
|
+
databutton/notify/email.py,sha256=jfnDWtBTw8gosfEM5Oes2jeKkAKjgL3cD1NK_Usy3Cs,7932
|
15
15
|
databutton/notify/send.py,sha256=cj-TG0jwolEXwJJVxeBxwpfg_it03KoImUn30qAbbQE,749
|
16
16
|
databutton/secrets/__init__.py,sha256=sI0okrfRBs2l5tcLTrKc9uUfPVVRMM9D7WED8p226tI,128
|
17
17
|
databutton/secrets/secrets.py,sha256=pehXsD09iKTxHaNQJ6awvIHXgRXBJrf4Xhrc11ZUeOk,3427
|
18
18
|
databutton/storage/__init__.py,sha256=7ZNd4eQQ7lYYIe1MqCuFbFF1aAVCuj2c_zmJfaPVlDY,252
|
19
19
|
databutton/storage/storage.py,sha256=XBy6z005K0KqmiTkq9eju87PgTboV-vPN1RRXuC57KI,17240
|
20
20
|
databutton/user.py,sha256=QWyWV_G_bnfIJ9js2itOwBk4zqXgBUF9h8lf5mygef8,503
|
21
|
-
databutton/version.py,sha256=
|
22
|
-
databutton-0.
|
23
|
-
databutton-0.
|
24
|
-
databutton-0.
|
25
|
-
databutton-0.
|
21
|
+
databutton/version.py,sha256=eK4hE95pRdHCVWV4DPs-Hciq-3mDMMnVk8WH6hG2IwI,175
|
22
|
+
databutton-0.31.1.dist-info/LICENSE,sha256=c7h4pcVZapFEmqWoRTRv_S1P_aibrtDobqmlR-QtMUk,45
|
23
|
+
databutton-0.31.1.dist-info/METADATA,sha256=rlHUOLZkCxKTne3HNFxzauvKauuE_aqQftIVFiQm-Xw,2763
|
24
|
+
databutton-0.31.1.dist-info/WHEEL,sha256=7Z8_27uaHI_UZAc4Uox4PpBhQ9Y5_modZXWMxtUi4NU,88
|
25
|
+
databutton-0.31.1.dist-info/RECORD,,
|
File without changes
|
File without changes
|