sunholo 0.138.1__py3-none-any.whl → 0.139.0__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.
@@ -0,0 +1,132 @@
1
+ import os
2
+ import json
3
+
4
+ from ..custom_logging import log
5
+ from ..utils.mime import get_mime_type_gemini
6
+ from .download_url import get_bytes_from_gcs
7
+
8
+ def download_gcs_source_to_string(source:str) -> str:
9
+ """
10
+ source: str The Google Cloud Storage URI of the file to download (e.g., 'gs://bucket_name/file_name').
11
+ """
12
+ mime_type = get_mime_type_gemini(source)
13
+ if mime_type == "":
14
+ log.warning(f"Can not download to string file source {source}")
15
+ return ""
16
+ """
17
+ mime_types = {
18
+
19
+ # Images
20
+ 'png': 'image/png',
21
+ 'jpg': 'image/jpeg',
22
+ 'jpeg': 'image/jpeg',
23
+ 'gif': 'image/gif',
24
+ 'webp': 'image/webp',
25
+
26
+ # Document formats
27
+ 'pdf': 'application/pdf',
28
+
29
+ # Programming languages
30
+ 'js': 'text/javascript',
31
+ 'py': 'text/x-python',
32
+
33
+ # Web formats
34
+ 'html': 'text/html',
35
+ 'htm': 'text/html',
36
+ 'css': 'text/css',
37
+
38
+ # Text formats
39
+ 'txt': 'text/plain',
40
+ 'md': 'text/md',
41
+ 'csv': 'text/csv',
42
+ 'xml': 'text/xml',
43
+ 'rtf': 'text/rtf',
44
+
45
+ # Special case: JSON files are treated as plain text
46
+ 'json': 'text/plain'
47
+ }
48
+ """
49
+ if mime_type.startswith("image/") or mime_type == "application/pdf":
50
+ log.warning(f"Can not download to string file source {source} of type {mime_type}")
51
+ return ""
52
+
53
+ try:
54
+ log.info(f"Extracting text for {source}")
55
+ bytes = get_bytes_from_gcs(source)
56
+ string = bytes.decode('utf-8', errors='replace')
57
+ log.info(f"Extracted {len(string)} characters from {source}: {string[:100]}")
58
+
59
+ except Exception as err:
60
+ log.error(f"Could not extract string text for {source}: {str(err)}")
61
+
62
+ return ""
63
+
64
+ if not string:
65
+ raise ValueError(f"No string text for {source}")
66
+
67
+ file_ext = os.path.splitext(source)[1].lower().lstrip('.')
68
+ if file_ext == "json":
69
+ try:
70
+ extracted_data = json.loads(string)
71
+ log.debug("Turning json text into markdown format so as not to confuse structured output", log_struct=extracted_data)
72
+ string = json_data_to_markdown(extracted_data)
73
+ except json.JSONDecodeError:
74
+ log.warning(f"Could not get valid json from .json file: {source}")
75
+
76
+ return string
77
+
78
+ def json_data_to_markdown(data, indent_level: int = 0) -> str:
79
+ """
80
+ Recursively converts a Python object (from parsed JSON) into a Markdown string.
81
+ """
82
+ indent = " " * indent_level # Use 2 spaces for indentation
83
+ markdown_parts = []
84
+
85
+ if isinstance(data, dict):
86
+ if not data:
87
+ return f"{indent}(empty object)"
88
+ for key, value in data.items():
89
+ # Process the value recursively
90
+ value_md = json_data_to_markdown(value, indent_level + 1)
91
+ # Determine if the rendered value is complex (multi-line or was list/dict)
92
+ is_complex_render = "\n" in value_md.strip() or (isinstance(value, (dict, list)) and value)
93
+
94
+ if is_complex_render:
95
+ markdown_parts.append(f"{indent}**{key}**:")
96
+ markdown_parts.append(value_md)
97
+ else:
98
+ # Simple value rendering, strip its own indent before adding key
99
+ markdown_parts.append(f"{indent}**{key}**: {value_md.strip()}")
100
+ return "\n".join(markdown_parts)
101
+
102
+ elif isinstance(data, list):
103
+ if not data:
104
+ return f"{indent}(empty list)"
105
+ for item in data:
106
+ # Process item recursively
107
+ item_md = json_data_to_markdown(item, indent_level + 1)
108
+ # Remove leading indent from the recursive call before processing lines
109
+ lines = item_md.lstrip(' ').split('\n')
110
+ # Add bullet point to the first line
111
+ first_line = f"{indent}- {lines[0]}"
112
+ # Ensure subsequent lines are indented correctly relative to the bullet
113
+ rest_lines = [f"{indent} {line}" for line in lines[1:]]
114
+ markdown_parts.append(first_line)
115
+ markdown_parts.extend(rest_lines)
116
+ return "\n".join(markdown_parts)
117
+
118
+ elif isinstance(data, str):
119
+ # Handle multi-line strings: indent subsequent lines
120
+ lines = data.split('\n')
121
+ if len(lines) <= 1:
122
+ return f"{indent}{data}" # Single line string
123
+ else:
124
+ indented_lines = [f"{indent}{lines[0]}"] + [f"{indent} {line}" for line in lines[1:]]
125
+ return "\n".join(indented_lines)
126
+
127
+ elif data is None:
128
+ return f"{indent}*null*" # Represent None distinctly
129
+ elif isinstance(data, bool):
130
+ return f"{indent}{str(data).lower()}" # true / false
131
+ else: # Numbers (int, float)
132
+ return f"{indent}{str(data)}"
sunholo/utils/mime.py CHANGED
@@ -66,3 +66,53 @@ def guess_mime_type(file_path: str) -> str:
66
66
 
67
67
  return mime
68
68
 
69
+
70
+ def get_mime_type_gemini(file_path:str) -> str:
71
+ """
72
+ Determine the MIME type based on file extension.
73
+ Only returns valid Gemini formats, or None if they are not supported.
74
+
75
+ Args:
76
+ file_path (str): Path to the file
77
+
78
+ Returns:
79
+ str: The appropriate MIME type for the file
80
+ """
81
+ # Extract the file extension (lowercase)
82
+ ext = os.path.splitext(file_path)[1].lower().lstrip('.')
83
+
84
+ # Define the mapping of extensions to MIME types
85
+ mime_types = {
86
+
87
+ # Images
88
+ 'png': 'image/png',
89
+ 'jpg': 'image/jpeg',
90
+ 'jpeg': 'image/jpeg',
91
+ 'gif': 'image/gif',
92
+ 'webp': 'image/webp',
93
+
94
+ # Document formats
95
+ 'pdf': 'application/pdf',
96
+
97
+ # Programming languages
98
+ 'js': 'text/javascript',
99
+ 'py': 'text/x-python',
100
+
101
+ # Web formats
102
+ 'html': 'text/html',
103
+ 'htm': 'text/html',
104
+ 'css': 'text/css',
105
+
106
+ # Text formats
107
+ 'txt': 'text/plain',
108
+ 'md': 'text/md',
109
+ 'csv': 'text/csv',
110
+ 'xml': 'text/xml',
111
+ 'rtf': 'text/rtf',
112
+
113
+ # Special case: JSON files are treated as plain text
114
+ 'json': 'text/plain'
115
+ }
116
+
117
+ # Return the appropriate MIME type, defaulting to None if unknown
118
+ return mime_types.get(ext, "")
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sunholo
3
- Version: 0.138.1
3
+ Version: 0.139.0
4
4
  Summary: AI DevOps - a package to help deploy GenAI to the Cloud.
5
5
  Author-email: Holosun ApS <multivac@sunholo.com>
6
6
  License: Apache License, Version 2.0
@@ -85,6 +85,7 @@ sunholo/excel/plugin.py,sha256=TJJdcKWyqEIce1agCJImvqvNp2CvLhzi4wUmLYHcLc8,4032
85
85
  sunholo/gcs/__init__.py,sha256=SZvbsMFDko40sIRHTHppA37IijvJTae54vrhooEF5-4,90
86
86
  sunholo/gcs/add_file.py,sha256=Pd5Zc1a3gqbuBgSI-UDC2mQnYGLJbAh_-IUzkDN5s9k,8273
87
87
  sunholo/gcs/download_folder.py,sha256=ijJTnS595JqZhBH8iHFErQilMbkuKgL-bnTCMLGuvlA,1614
88
+ sunholo/gcs/download_gcs_text.py,sha256=odgmuYimf1Jiy67Q06jNlb3FAKQmIq3KcakxsCnoMtY,4784
88
89
  sunholo/gcs/download_url.py,sha256=9QMEtZhrN-y1VAqvi-7Tw2GI9iRG_uuZzCg6Qhq8_yw,6421
89
90
  sunholo/gcs/extract_and_sign.py,sha256=paRrTCvCN5vkQwCB7OSkxWi-pfOgOtZ0bwdXE08c3Ps,1546
90
91
  sunholo/gcs/metadata.py,sha256=oQLcXi4brsZ74aegWyC1JZmhlaEV270HS5_UWtAYYWE,898
@@ -155,7 +156,7 @@ sunholo/utils/config_class.py,sha256=uSRiJLj8t5UgWNxaq8W4KPnzxb4SkUJ1avXecDHuP-E
155
156
  sunholo/utils/config_schema.py,sha256=Wv-ncitzljOhgbDaq9qnFqH5LCuxNv59dTGDWgd1qdk,4189
156
157
  sunholo/utils/gcp.py,sha256=lus1HH8YhFInw6QRKwfvKZq-Lz-2KQg4ips9v1I_3zE,4783
157
158
  sunholo/utils/gcp_project.py,sha256=Fa0IhCX12bZ1ctF_PKN8PNYd7hihEUfb90kilBfUDjg,1411
158
- sunholo/utils/mime.py,sha256=7_J1PnWOlvAPRoHWKESAncdRVVldVwRdKvuDvi9sRfE,2020
159
+ sunholo/utils/mime.py,sha256=mELAiZcGa69PshBxV7y770E0K09YfX4Z4ZRBPL-7gXs,3352
159
160
  sunholo/utils/parsers.py,sha256=wES0fRn3GONoymRXOXt-z62HCoOiUvvFXa-MfKfjCls,6421
160
161
  sunholo/utils/timedelta.py,sha256=BbLabEx7_rbErj_YbNM0MBcaFN76DC4PTe4zD2ucezg,493
161
162
  sunholo/utils/user_ids.py,sha256=SQd5_H7FE7vcTZp9AQuQDWBXd4FEEd7TeVMQe1H4Ny8,292
@@ -168,9 +169,9 @@ sunholo/vertex/init.py,sha256=1OQwcPBKZYBTDPdyU7IM4X4OmiXLdsNV30C-fee2scQ,2875
168
169
  sunholo/vertex/memory_tools.py,sha256=tBZxqVZ4InTmdBvLlOYwoSEWu4-kGquc-gxDwZCC4FA,7667
169
170
  sunholo/vertex/safety.py,sha256=S9PgQT1O_BQAkcqauWncRJaydiP8Q_Jzmu9gxYfy1VA,2482
170
171
  sunholo/vertex/type_dict_to_json.py,sha256=uTzL4o9tJRao4u-gJOFcACgWGkBOtqACmb6ihvCErL8,4694
171
- sunholo-0.138.1.dist-info/licenses/LICENSE.txt,sha256=SdE3QjnD3GEmqqg9EX3TM9f7WmtOzqS1KJve8rhbYmU,11345
172
- sunholo-0.138.1.dist-info/METADATA,sha256=GC4bwGLlBT68d6uqc99tTFX7Y_WtWnombuk2fPrTzls,10067
173
- sunholo-0.138.1.dist-info/WHEEL,sha256=0CuiUZ_p9E4cD6NyLD6UG80LBXYyiSYZOKDm5lp32xk,91
174
- sunholo-0.138.1.dist-info/entry_points.txt,sha256=bZuN5AIHingMPt4Ro1b_T-FnQvZ3teBes-3OyO0asl4,49
175
- sunholo-0.138.1.dist-info/top_level.txt,sha256=wt5tadn5--5JrZsjJz2LceoUvcrIvxjHJe-RxuudxAk,8
176
- sunholo-0.138.1.dist-info/RECORD,,
172
+ sunholo-0.139.0.dist-info/licenses/LICENSE.txt,sha256=SdE3QjnD3GEmqqg9EX3TM9f7WmtOzqS1KJve8rhbYmU,11345
173
+ sunholo-0.139.0.dist-info/METADATA,sha256=ZMNryoyWlaoHvg9c_VBaKznsSlYhyYheSNCh0c3fZm4,10067
174
+ sunholo-0.139.0.dist-info/WHEEL,sha256=DnLRTWE75wApRYVsjgc6wsVswC54sMSJhAEd4xhDpBk,91
175
+ sunholo-0.139.0.dist-info/entry_points.txt,sha256=bZuN5AIHingMPt4Ro1b_T-FnQvZ3teBes-3OyO0asl4,49
176
+ sunholo-0.139.0.dist-info/top_level.txt,sha256=wt5tadn5--5JrZsjJz2LceoUvcrIvxjHJe-RxuudxAk,8
177
+ sunholo-0.139.0.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (80.3.1)
2
+ Generator: setuptools (80.4.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5