databutton 0.30.1__tar.gz → 0.31.1__tar.gz

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.
Files changed (27) hide show
  1. {databutton-0.30.1 → databutton-0.31.1}/PKG-INFO +2 -2
  2. databutton-0.31.1/databutton/notify/__init__.py +8 -0
  3. databutton-0.31.1/databutton/notify/email.py +285 -0
  4. {databutton-0.30.1 → databutton-0.31.1}/databutton/version.py +1 -1
  5. {databutton-0.30.1 → databutton-0.31.1}/pyproject.toml +6 -6
  6. databutton-0.30.1/databutton/notify/__init__.py +0 -3
  7. databutton-0.30.1/databutton/notify/email.py +0 -62
  8. {databutton-0.30.1 → databutton-0.31.1}/LICENSE +0 -0
  9. {databutton-0.30.1 → databutton-0.31.1}/README.md +0 -0
  10. {databutton-0.30.1 → databutton-0.31.1}/databutton/__init__.py +0 -0
  11. {databutton-0.30.1 → databutton-0.31.1}/databutton/cachetools.py +0 -0
  12. {databutton-0.30.1 → databutton-0.31.1}/databutton/internal/__init__.py +0 -0
  13. {databutton-0.30.1 → databutton-0.31.1}/databutton/internal/auth.py +0 -0
  14. {databutton-0.30.1 → databutton-0.31.1}/databutton/internal/byteutils.py +0 -0
  15. {databutton-0.30.1 → databutton-0.31.1}/databutton/internal/dbapiclient.py +0 -0
  16. {databutton-0.30.1 → databutton-0.31.1}/databutton/internal/headers.py +0 -0
  17. {databutton-0.30.1 → databutton-0.31.1}/databutton/internal/httpxclient.py +0 -0
  18. {databutton-0.30.1 → databutton-0.31.1}/databutton/internal/performedby.py +0 -0
  19. {databutton-0.30.1 → databutton-0.31.1}/databutton/internal/retries.py +0 -0
  20. {databutton-0.30.1 → databutton-0.31.1}/databutton/jobs/__init__.py +0 -0
  21. {databutton-0.30.1 → databutton-0.31.1}/databutton/jobs/run.py +0 -0
  22. {databutton-0.30.1 → databutton-0.31.1}/databutton/notify/send.py +0 -0
  23. {databutton-0.30.1 → databutton-0.31.1}/databutton/secrets/__init__.py +0 -0
  24. {databutton-0.30.1 → databutton-0.31.1}/databutton/secrets/secrets.py +0 -0
  25. {databutton-0.30.1 → databutton-0.31.1}/databutton/storage/__init__.py +0 -0
  26. {databutton-0.30.1 → databutton-0.31.1}/databutton/storage/storage.py +0 -0
  27. {databutton-0.30.1 → databutton-0.31.1}/databutton/user.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: databutton
3
- Version: 0.30.1
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.23.3,<0.24.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)
@@ -0,0 +1,8 @@
1
+ from .email import email
2
+
3
+ __all__ = [
4
+ "email",
5
+ "attachment_from_bytes",
6
+ "attachment_from_str",
7
+ "attachment_from_file",
8
+ ]
@@ -0,0 +1,285 @@
1
+ import base64
2
+ import io
3
+ import mimetypes
4
+ import re
5
+ from collections.abc import Sequence
6
+ from typing import List, Optional, Union
7
+
8
+ import pandas as pd
9
+ from pydantic import BaseModel
10
+
11
+ from .send import send
12
+
13
+
14
+ def valid_email(recipient: str) -> bool:
15
+ # Note: We could possibly use some email validation library but it's tricky
16
+ parts = recipient.split("@")
17
+ if len(parts) != 2:
18
+ return False
19
+ return bool(parts[0] and parts[1])
20
+
21
+
22
+ def validate_email_to_arg(to: Union[str, List[str]]) -> List[str]:
23
+ if isinstance(to, str):
24
+ to = [to]
25
+ if not isinstance(to, (list, tuple)) and len(to) > 0:
26
+ raise ValueError(
27
+ "Invalid recipient, expecting 'to' to be a string or list of strings."
28
+ )
29
+ invalid_emails = []
30
+ for recipient in to:
31
+ if not valid_email(recipient):
32
+ invalid_emails.append(recipient)
33
+ if invalid_emails:
34
+ raise ValueError("\n".join(["Invalid email address(es):"] + invalid_emails))
35
+ return to
36
+
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
+
259
+ def email(
260
+ to: Union[str, List[str]],
261
+ subject: str,
262
+ *,
263
+ content_text: Optional[str] = None,
264
+ content_html: Optional[str] = None,
265
+ attachments: Sequence[Attachment] = (),
266
+ ):
267
+ """Send email notification from databutton.
268
+
269
+ At least one of the content arguments must be present.
270
+
271
+ A link to the project will be added at the end of the email body.
272
+
273
+ If content_text is not provided it will be generated from
274
+ content_html for email clients without html support,
275
+ the result may be less pretty than handcrafted text.
276
+ """
277
+ send(
278
+ create_email(
279
+ to=to,
280
+ subject=subject,
281
+ content_text=content_text,
282
+ content_html=content_html,
283
+ attachments=attachments,
284
+ )
285
+ )
@@ -5,4 +5,4 @@ This module contains project version information.
5
5
  .. moduleauthor:: Databutton <support@databutton.com>
6
6
  """
7
7
 
8
- __version__ = "0.30.1"
8
+ __version__ = "0.31.1"
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "databutton"
3
- version = "0.30.1"
3
+ version = "0.31.1"
4
4
  description = "The CLI for databutton.com"
5
5
  authors = ["Databutton <hi@databutton.io>"]
6
6
  license = "Databutton"
@@ -9,8 +9,7 @@ packages = [{ include = "databutton" }]
9
9
 
10
10
  [tool.poetry.dependencies]
11
11
  python = "~3.10"
12
- httpx = "^0.23.3"
13
- # This is the same as streamlit, hopes it works
12
+ httpx = "^0.24.0"
14
13
  pandas = "^1.5.3"
15
14
  pydantic = "^1.10.7"
16
15
  PyJWT = "^2.6.0"
@@ -21,17 +20,18 @@ certifi = "^2022.12.7"
21
20
  streamlit = "^1.20.0"
22
21
 
23
22
  [tool.poetry.group.dev.dependencies]
24
- mypy = "^1.1.1"
23
+ mypy = "^1.2.0"
25
24
  types-pyyaml = "^6.0.12"
26
25
  types-requests = "^2.28.11.2"
27
- pytest = "^7.2.2"
28
- black = "^23.1.0"
26
+ pytest = "^7.3.0"
27
+ black = "^23.3.0"
29
28
  flake8 = "^6.0.0"
30
29
  isort = "^5.12.0"
31
30
  python-semantic-release = "7.28.1"
32
31
  pytest-mock = "^3.7.0"
33
32
  pandas-stubs = "^1.5.2.221124"
34
33
  ruff = "^0.0.261"
34
+ streamlit = "^1.21.0"
35
35
 
36
36
  [build-system]
37
37
  requires = ["poetry-core>=1.0.0"]
@@ -1,3 +0,0 @@
1
- from .email import email
2
-
3
- __all__ = ["email"]
@@ -1,62 +0,0 @@
1
- from typing import List, Optional, Union
2
-
3
- from pydantic import BaseModel
4
-
5
- from .send import send
6
-
7
-
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
- def valid_email(recipient: str) -> bool:
16
- # Note: We could possibly use some email validation library but it's tricky
17
- parts = recipient.split("@")
18
- if len(parts) != 2:
19
- return False
20
- return bool(parts[0] and parts[1])
21
-
22
-
23
- def validate_email_to_arg(to: Union[str, List[str]]) -> List[str]:
24
- if isinstance(to, str):
25
- to = [to]
26
- if not isinstance(to, (list, tuple)) and len(to) > 0:
27
- raise ValueError(
28
- "Invalid recipient, expecting 'to' to be a string or list of strings."
29
- )
30
- invalid_emails = []
31
- for recipient in to:
32
- if not valid_email(recipient):
33
- invalid_emails.append(recipient)
34
- if invalid_emails:
35
- raise ValueError("\n".join(["Invalid email address(es):"] + invalid_emails))
36
- return to
37
-
38
-
39
- def email(
40
- to: Union[str, List[str]],
41
- subject: str,
42
- content_text: Optional[str] = None,
43
- content_html: Optional[str] = None,
44
- ):
45
- """Send email notification from databutton.
46
-
47
- At least one of the content arguments must be present.
48
-
49
- A link to the project will be added at the end of the email body.
50
-
51
- If content_text is not provided it will be generated from
52
- content_html for email clients without html support,
53
- the result may be less pretty than handcrafted text.
54
- """
55
- send(
56
- Email(
57
- to=validate_email_to_arg(to),
58
- subject=subject,
59
- content_text=content_text,
60
- content_html=content_html,
61
- )
62
- )
File without changes
File without changes