pyaws-s3 1.0.6__tar.gz → 1.0.8__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.
- {pyaws_s3-1.0.6 → pyaws_s3-1.0.8}/PKG-INFO +1 -1
- {pyaws_s3-1.0.6 → pyaws_s3-1.0.8}/pyaws_s3/s3.py +78 -14
- {pyaws_s3-1.0.6 → pyaws_s3-1.0.8}/pyaws_s3.egg-info/PKG-INFO +1 -1
- {pyaws_s3-1.0.6 → pyaws_s3-1.0.8}/pyaws_s3.egg-info/SOURCES.txt +0 -1
- {pyaws_s3-1.0.6 → pyaws_s3-1.0.8}/pyproject.toml +1 -1
- pyaws_s3-1.0.6/pyaws_s3/util.py +0 -47
- {pyaws_s3-1.0.6 → pyaws_s3-1.0.8}/LICENSE.md +0 -0
- {pyaws_s3-1.0.6 → pyaws_s3-1.0.8}/README.md +0 -0
- {pyaws_s3-1.0.6 → pyaws_s3-1.0.8}/pyaws_s3/__init__.py +0 -0
- {pyaws_s3-1.0.6 → pyaws_s3-1.0.8}/pyaws_s3.egg-info/dependency_links.txt +0 -0
- {pyaws_s3-1.0.6 → pyaws_s3-1.0.8}/pyaws_s3.egg-info/requires.txt +0 -0
- {pyaws_s3-1.0.6 → pyaws_s3-1.0.8}/pyaws_s3.egg-info/top_level.txt +0 -0
- {pyaws_s3-1.0.6 → pyaws_s3-1.0.8}/setup.cfg +0 -0
@@ -5,9 +5,9 @@ import aioboto3
|
|
5
5
|
import pandas as pd
|
6
6
|
import io
|
7
7
|
import matplotlib.pyplot as plt
|
8
|
+
from plotly.graph_objs import Figure
|
8
9
|
from pandas.plotting import table
|
9
10
|
from typing import Any, Literal
|
10
|
-
from util import bytes_from_figure, html_from_figure
|
11
11
|
from fpdf import FPDF
|
12
12
|
|
13
13
|
logger = logging.getLogger(__name__)
|
@@ -39,6 +39,48 @@ class S3Client:
|
|
39
39
|
self.region_name = kwargs.get("region_name", os.getenv("AWS_REGION"))
|
40
40
|
self.bucket_name = kwargs.get("bucket_name", os.getenv("AWS_BUCKET_NAME"))
|
41
41
|
|
42
|
+
def _bytes_from_figure(self, f: Figure, **kwargs) -> bytes:
|
43
|
+
"""
|
44
|
+
Convert a Plotly Figure to a PNG image as bytes.
|
45
|
+
|
46
|
+
Args:
|
47
|
+
f (Figure): The Plotly Figure object to be converted.
|
48
|
+
|
49
|
+
Returns:
|
50
|
+
bytes: The PNG image data as bytes.
|
51
|
+
:param f: The Plotly Figure object to be converted into a PNG image.
|
52
|
+
"""
|
53
|
+
|
54
|
+
format_file = kwargs.get("format_file", "png") # The format of the image to be converted to
|
55
|
+
width = kwargs.get("width", 640) # The width of the image in pixels
|
56
|
+
height = kwargs.get("height", 480) # The height of the image in pixels
|
57
|
+
|
58
|
+
with io.BytesIO() as bytes_buffer:
|
59
|
+
f.write_image(bytes_buffer,
|
60
|
+
format=format_file,
|
61
|
+
width = width,
|
62
|
+
height = height) # Write the figure to the bytes buffer as a PNG image
|
63
|
+
bytes_buffer.seek(0) # Reset the buffer position to the beginning
|
64
|
+
return bytes_buffer.getvalue() # Return the bytes data
|
65
|
+
|
66
|
+
def _html_from_figure(self, f: Figure) -> str:
|
67
|
+
"""
|
68
|
+
Convert a Plotly Figure to an HTML string.
|
69
|
+
|
70
|
+
Args:
|
71
|
+
f (Figure): The Plotly Figure object to be converted.
|
72
|
+
|
73
|
+
Returns:
|
74
|
+
str: The HTML representation of the figure as a string.
|
75
|
+
"""
|
76
|
+
with io.BytesIO() as bytes_buffer:
|
77
|
+
# Wrap the BytesIO with a TextIOWrapper to handle strings
|
78
|
+
with io.TextIOWrapper(bytes_buffer, encoding='utf-8') as text_buffer:
|
79
|
+
f.write_html(text_buffer) # Write the figure to the text buffer
|
80
|
+
text_buffer.flush() # Ensure all data is written
|
81
|
+
bytes_buffer.seek(0) # Reset the buffer position to the beginning
|
82
|
+
return bytes_buffer.getvalue().decode('utf-8') # Decode bytes to string and return
|
83
|
+
|
42
84
|
async def _get_s3_client_async(self) -> Any:
|
43
85
|
"""
|
44
86
|
Get an asynchronous S3 client using the provided AWS credentials and region.
|
@@ -148,11 +190,16 @@ class S3Client:
|
|
148
190
|
"""
|
149
191
|
try:
|
150
192
|
|
151
|
-
|
193
|
+
if args:
|
194
|
+
fig = args[0] if len(args) > 0 else None
|
195
|
+
object_name = args[1] if len(args) > 1 else None
|
196
|
+
else:
|
197
|
+
fig = kwargs.get("fig", None)
|
198
|
+
object_name = kwargs.get("object_name", None)
|
199
|
+
|
152
200
|
if fig is None:
|
153
201
|
raise Exception("Figure is None")
|
154
202
|
|
155
|
-
object_name = args[1] if len(args) > 1 else None
|
156
203
|
if object_name is None:
|
157
204
|
raise Exception("Object name is None")
|
158
205
|
|
@@ -178,12 +225,12 @@ class S3Client:
|
|
178
225
|
|
179
226
|
if format_file == "html":
|
180
227
|
# Convert the figure to SVG
|
181
|
-
file_text =
|
228
|
+
file_text = self._html_from_figure(fig)
|
182
229
|
# Upload the html text to s3
|
183
230
|
s3_resource.Bucket(self.bucket_name).Object(object_name).put(Body=file_text, ContentType=mimetypes)
|
184
231
|
else:
|
185
232
|
# Convert the figure to bytes
|
186
|
-
file_buffer =
|
233
|
+
file_buffer = self._bytes_from_figure(fig, format_file=format_file)
|
187
234
|
# Upload the image bytes to S3
|
188
235
|
s3_resource.Bucket(self.bucket_name).Object(object_name).put(Body=file_buffer, ContentType=mimetypes)
|
189
236
|
|
@@ -212,11 +259,18 @@ class S3Client:
|
|
212
259
|
"""
|
213
260
|
try:
|
214
261
|
|
215
|
-
|
262
|
+
if args:
|
263
|
+
# Get the DataFrame and object name from the arguments
|
264
|
+
df = args[0] if len(args) > 0 else None
|
265
|
+
object_name = args[1] if len(args) > 1 else None
|
266
|
+
else:
|
267
|
+
# Get the DataFrame and object name from the keyword arguments
|
268
|
+
df = kwargs.get("df", None)
|
269
|
+
object_name = kwargs.get("object_name", None)
|
270
|
+
|
216
271
|
if df is None:
|
217
272
|
raise Exception("Figure is None")
|
218
273
|
|
219
|
-
object_name = args[1] if len(args) > 1 else None
|
220
274
|
if object_name is None:
|
221
275
|
raise Exception("Object name is None")
|
222
276
|
|
@@ -297,7 +351,7 @@ class S3Client:
|
|
297
351
|
logger.error(f"Error deleting files: {str(e)}")
|
298
352
|
raise Exception(f"Error deleting files: {str(e)}")
|
299
353
|
|
300
|
-
def upload_to_pdf(self, *args : Any) -> str:
|
354
|
+
def upload_to_pdf(self, *args : Any, **kwargs : Any) -> str:
|
301
355
|
"""
|
302
356
|
Export the given text as a PDF and upload it to the S3 bucket.
|
303
357
|
|
@@ -310,13 +364,18 @@ class S3Client:
|
|
310
364
|
str: Pre-signed URL for the uploaded PDF.
|
311
365
|
"""
|
312
366
|
try:
|
313
|
-
|
314
|
-
|
315
|
-
|
367
|
+
if args:
|
368
|
+
text = args[0] if len(args) > 0 else None
|
369
|
+
object_name = args[1] if len(args) > 1 else None
|
370
|
+
else:
|
371
|
+
text = kwargs.get("text", None)
|
372
|
+
object_name = kwargs.get("object_name", None)
|
316
373
|
|
317
|
-
|
374
|
+
if text is None:
|
375
|
+
raise Exception("Text is None")
|
376
|
+
|
318
377
|
if object_name is None:
|
319
|
-
|
378
|
+
raise Exception("Object name is None")
|
320
379
|
|
321
380
|
mimetypes = "application/pdf"
|
322
381
|
s3_client = self._get_s3_client()
|
@@ -359,7 +418,12 @@ class S3Client:
|
|
359
418
|
str: The local path of the downloaded file.
|
360
419
|
"""
|
361
420
|
try:
|
362
|
-
|
421
|
+
|
422
|
+
if args:
|
423
|
+
object_name = args[0] if len(args) > 0 else None
|
424
|
+
else:
|
425
|
+
object_name = kwargs.get("object_name", None)
|
426
|
+
|
363
427
|
if object_name is None:
|
364
428
|
raise Exception("Object name is None")
|
365
429
|
|
pyaws_s3-1.0.6/pyaws_s3/util.py
DELETED
@@ -1,47 +0,0 @@
|
|
1
|
-
import io
|
2
|
-
import logging
|
3
|
-
from plotly.graph_objs import Figure
|
4
|
-
|
5
|
-
logger = logging.getLogger(__name__)
|
6
|
-
|
7
|
-
def bytes_from_figure(f: Figure, **kwargs) -> bytes:
|
8
|
-
"""
|
9
|
-
Convert a Plotly Figure to a PNG image as bytes.
|
10
|
-
|
11
|
-
Args:
|
12
|
-
f (Figure): The Plotly Figure object to be converted.
|
13
|
-
|
14
|
-
Returns:
|
15
|
-
bytes: The PNG image data as bytes.
|
16
|
-
:param f: The Plotly Figure object to be converted into a PNG image.
|
17
|
-
"""
|
18
|
-
|
19
|
-
format_file = kwargs.get("format_file", "png") # The format of the image to be converted to
|
20
|
-
width = kwargs.get("width", 640) # The width of the image in pixels
|
21
|
-
height = kwargs.get("height", 480) # The height of the image in pixels
|
22
|
-
|
23
|
-
with io.BytesIO() as bytes_buffer:
|
24
|
-
f.write_image(bytes_buffer,
|
25
|
-
format=format_file,
|
26
|
-
width = width,
|
27
|
-
height = height) # Write the figure to the bytes buffer as a PNG image
|
28
|
-
bytes_buffer.seek(0) # Reset the buffer position to the beginning
|
29
|
-
return bytes_buffer.getvalue() # Return the bytes data
|
30
|
-
|
31
|
-
def html_from_figure(f: Figure) -> str:
|
32
|
-
"""
|
33
|
-
Convert a Plotly Figure to an HTML string.
|
34
|
-
|
35
|
-
Args:
|
36
|
-
f (Figure): The Plotly Figure object to be converted.
|
37
|
-
|
38
|
-
Returns:
|
39
|
-
str: The HTML representation of the figure as a string.
|
40
|
-
"""
|
41
|
-
with io.BytesIO() as bytes_buffer:
|
42
|
-
# Wrap the BytesIO with a TextIOWrapper to handle strings
|
43
|
-
with io.TextIOWrapper(bytes_buffer, encoding='utf-8') as text_buffer:
|
44
|
-
f.write_html(text_buffer) # Write the figure to the text buffer
|
45
|
-
text_buffer.flush() # Ensure all data is written
|
46
|
-
bytes_buffer.seek(0) # Reset the buffer position to the beginning
|
47
|
-
return bytes_buffer.getvalue().decode('utf-8') # Decode bytes to string and return
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|