pyaws-s3 1.0.6__py3-none-any.whl → 1.0.8__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.
pyaws_s3/s3.py CHANGED
@@ -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
- fig = args[0] if len(args) > 0 else None
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 = html_from_figure(fig)
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 = bytes_from_figure(fig, format_file=format_file)
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
- df = args[0] if len(args) > 0 else None
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
- text = args[0] if len(args) > 0 else None
314
- if text is None:
315
- raise Exception("Text is None")
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
- object_name = args[1] if len(args) > 1 else None
374
+ if text is None:
375
+ raise Exception("Text is None")
376
+
318
377
  if object_name is None:
319
- raise Exception("Object name is None")
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
- object_name = args[0] if len(args) > 0 else None
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
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyaws_s3
3
- Version: 1.0.6
3
+ Version: 1.0.8
4
4
  Summary: A Python package for AWS S3 utilities
5
5
  Author-email: Giuseppe Zileni <giuseppe.zileni@gmail.com>
6
6
  Keywords: aws,s3,utilities
@@ -0,0 +1,7 @@
1
+ pyaws_s3/__init__.py,sha256=Tr7xJiCKOMWYydOJ4kxHlA7AR1X3pRsJ8MjxJev2wsw,24
2
+ pyaws_s3/s3.py,sha256=yC68TzOTeHP7cdDwyOriAhyLeQse5oUFbycS4sl_Iwo,18983
3
+ pyaws_s3-1.0.8.dist-info/licenses/LICENSE.md,sha256=7WXohDebeZpcVn_nH2aIaLhFZRvZBdcPSqWsO12lhwM,1074
4
+ pyaws_s3-1.0.8.dist-info/METADATA,sha256=JC1416mfukZieFRxrToJZ2DR6Fa5EOJXkqZ5rKA-Y00,5234
5
+ pyaws_s3-1.0.8.dist-info/WHEEL,sha256=Nw36Djuh_5VDukK0H78QzOX-_FQEo6V37m3nkm96gtU,91
6
+ pyaws_s3-1.0.8.dist-info/top_level.txt,sha256=MxSSC4Q8Vr32wKgrUAlwT4BTXwqUaG_CAWoBuPeXYjQ,9
7
+ pyaws_s3-1.0.8.dist-info/RECORD,,
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
@@ -1,8 +0,0 @@
1
- pyaws_s3/__init__.py,sha256=Tr7xJiCKOMWYydOJ4kxHlA7AR1X3pRsJ8MjxJev2wsw,24
2
- pyaws_s3/s3.py,sha256=c-sOzLxH3U0cQYpQMuIffOmE6T2oCS6ASQMBlvvLIeU,16326
3
- pyaws_s3/util.py,sha256=2DRhpZADoDpAr-bTwzXfZ_SNG3Bno5xVk1wfwmNxknU,1822
4
- pyaws_s3-1.0.6.dist-info/licenses/LICENSE.md,sha256=7WXohDebeZpcVn_nH2aIaLhFZRvZBdcPSqWsO12lhwM,1074
5
- pyaws_s3-1.0.6.dist-info/METADATA,sha256=ebrtOO6YPGdYL5roHCKKPowQO2eD1n-kln-caRL73c4,5234
6
- pyaws_s3-1.0.6.dist-info/WHEEL,sha256=Nw36Djuh_5VDukK0H78QzOX-_FQEo6V37m3nkm96gtU,91
7
- pyaws_s3-1.0.6.dist-info/top_level.txt,sha256=MxSSC4Q8Vr32wKgrUAlwT4BTXwqUaG_CAWoBuPeXYjQ,9
8
- pyaws_s3-1.0.6.dist-info/RECORD,,