abstract-utilities 0.2.2.492__py3-none-any.whl → 0.2.2.583__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.
Files changed (201) hide show
  1. abstract_utilities/__init__.py +6 -10
  2. abstract_utilities/circular_import_finder.py +222 -0
  3. abstract_utilities/circular_import_finder2.py +118 -0
  4. abstract_utilities/class_utils/__init__.py +7 -0
  5. abstract_utilities/class_utils/abstract_classes.py +74 -0
  6. abstract_utilities/class_utils/caller_utils.py +53 -0
  7. abstract_utilities/class_utils/class_utils.py +109 -0
  8. abstract_utilities/class_utils/function_utils.py +153 -0
  9. abstract_utilities/class_utils/global_utils.py +57 -0
  10. abstract_utilities/class_utils/imports/__init__.py +2 -0
  11. abstract_utilities/class_utils/imports/imports.py +2 -0
  12. abstract_utilities/class_utils/imports/utils.py +40 -0
  13. abstract_utilities/class_utils/module_utils.py +63 -0
  14. abstract_utilities/directory_utils/__init__.py +2 -0
  15. abstract_utilities/directory_utils/directory_utils.py +94 -0
  16. abstract_utilities/directory_utils/imports/__init__.py +2 -0
  17. abstract_utilities/directory_utils/imports/imports.py +1 -0
  18. abstract_utilities/directory_utils/imports/module_imports.py +2 -0
  19. abstract_utilities/directory_utils/name_utils.py +43 -0
  20. abstract_utilities/directory_utils/size_utils.py +57 -0
  21. abstract_utilities/directory_utils/src/__init__.py +4 -0
  22. abstract_utilities/directory_utils/src/directory_utils.py +108 -0
  23. abstract_utilities/directory_utils/src/name_utils.py +43 -0
  24. abstract_utilities/directory_utils/src/size_utils.py +57 -0
  25. abstract_utilities/directory_utils/src/utils.py +116 -0
  26. abstract_utilities/directory_utils/utils.py +116 -0
  27. abstract_utilities/env_utils/imports/imports.py +5 -3
  28. abstract_utilities/error_utils/__init__.py +2 -0
  29. abstract_utilities/error_utils/error_utils.py +25 -0
  30. abstract_utilities/error_utils/imports/__init__.py +2 -0
  31. abstract_utilities/error_utils/imports/imports.py +1 -0
  32. abstract_utilities/error_utils/imports/module_imports.py +1 -0
  33. abstract_utilities/file_utils/__init__.py +1 -2
  34. abstract_utilities/file_utils/imports/constants.py +84 -4
  35. abstract_utilities/file_utils/imports/imports.py +2 -21
  36. abstract_utilities/file_utils/imports/module_imports.py +2 -7
  37. abstract_utilities/file_utils/module_imports.py +12 -0
  38. abstract_utilities/file_utils/src/__init__.py +7 -0
  39. abstract_utilities/file_utils/src/file_filters/__init__.py +4 -0
  40. abstract_utilities/file_utils/src/file_filters/ensure_utils.py +116 -0
  41. abstract_utilities/file_utils/src/file_filters/filter_params.py +86 -0
  42. abstract_utilities/file_utils/src/file_filters/filter_utils.py +78 -0
  43. abstract_utilities/file_utils/src/file_filters/predicate_utils.py +114 -0
  44. abstract_utilities/file_utils/src/file_filters.py +177 -0
  45. abstract_utilities/file_utils/src/file_reader.py +543 -0
  46. abstract_utilities/file_utils/src/file_utils.py +156 -0
  47. abstract_utilities/file_utils/src/filter_params.py +197 -0
  48. abstract_utilities/file_utils/src/find_collect.py +190 -0
  49. abstract_utilities/file_utils/src/find_content.py +210 -0
  50. abstract_utilities/file_utils/src/initFunctionsGen.py +280 -0
  51. abstract_utilities/file_utils/src/map_utils.py +29 -0
  52. abstract_utilities/file_utils/src/pdf_utils.py +300 -0
  53. abstract_utilities/file_utils/src/reader_utils/__init__.py +4 -0
  54. abstract_utilities/file_utils/src/reader_utils/directory_reader.py +53 -0
  55. abstract_utilities/file_utils/src/reader_utils/file_reader.py +543 -0
  56. abstract_utilities/file_utils/src/reader_utils/file_readers.py +376 -0
  57. abstract_utilities/file_utils/src/reader_utils/imports.py +18 -0
  58. abstract_utilities/file_utils/src/reader_utils/pdf_utils.py +300 -0
  59. abstract_utilities/file_utils/src/type_checks.py +91 -0
  60. abstract_utilities/file_utils (2)/__init__.py +2 -0
  61. abstract_utilities/file_utils (2)/imports/__init__.py +2 -0
  62. abstract_utilities/file_utils (2)/imports/constants.py +118 -0
  63. abstract_utilities/file_utils (2)/imports/imports/__init__.py +3 -0
  64. abstract_utilities/file_utils (2)/imports/imports/constants.py +119 -0
  65. abstract_utilities/file_utils (2)/imports/imports/imports.py +46 -0
  66. abstract_utilities/file_utils (2)/imports/imports/module_imports.py +8 -0
  67. abstract_utilities/file_utils (2)/imports/utils/__init__.py +3 -0
  68. abstract_utilities/file_utils (2)/imports/utils/classes.py +379 -0
  69. abstract_utilities/file_utils (2)/imports/utils/clean_imps.py +155 -0
  70. abstract_utilities/file_utils (2)/imports/utils/filter_utils.py +341 -0
  71. abstract_utilities/file_utils (2)/src/__init__.py +8 -0
  72. abstract_utilities/file_utils (2)/src/file_filters.py +155 -0
  73. abstract_utilities/file_utils (2)/src/file_reader.py +604 -0
  74. abstract_utilities/file_utils (2)/src/find_collect.py +258 -0
  75. abstract_utilities/file_utils (2)/src/initFunctionsGen.py +286 -0
  76. abstract_utilities/file_utils (2)/src/map_utils.py +28 -0
  77. abstract_utilities/file_utils (2)/src/pdf_utils.py +300 -0
  78. abstract_utilities/hash_utils/__init__.py +2 -0
  79. abstract_utilities/hash_utils/hash_utils.py +5 -0
  80. abstract_utilities/hash_utils/imports/__init__.py +2 -0
  81. abstract_utilities/hash_utils/imports/imports.py +1 -0
  82. abstract_utilities/hash_utils/imports/module_imports.py +0 -0
  83. abstract_utilities/history_utils/__init__.py +2 -0
  84. abstract_utilities/history_utils/history_utils.py +37 -0
  85. abstract_utilities/history_utils/imports/__init__.py +2 -0
  86. abstract_utilities/history_utils/imports/imports.py +1 -0
  87. abstract_utilities/history_utils/imports/module_imports.py +0 -0
  88. abstract_utilities/import_utils/__init__.py +2 -0
  89. abstract_utilities/import_utils/circular_import_finder.py +222 -0
  90. abstract_utilities/import_utils/circular_import_finder2.py +118 -0
  91. abstract_utilities/import_utils/imports/__init__.py +4 -0
  92. abstract_utilities/import_utils/imports/constants.py +2 -0
  93. abstract_utilities/import_utils/imports/imports.py +4 -0
  94. abstract_utilities/import_utils/imports/module_imports.py +8 -0
  95. abstract_utilities/import_utils/imports/utils.py +30 -0
  96. abstract_utilities/import_utils/src/__init__.py +7 -0
  97. abstract_utilities/import_utils/src/clean_imports.py +278 -0
  98. abstract_utilities/import_utils/src/dot_utils.py +80 -0
  99. abstract_utilities/import_utils/src/extract_utils.py +46 -0
  100. abstract_utilities/import_utils/src/import_functions.py +91 -0
  101. abstract_utilities/import_utils/src/import_utils.py +299 -0
  102. abstract_utilities/import_utils/src/package_utils/__init__.py +139 -0
  103. abstract_utilities/import_utils/src/package_utils/context_utils.py +27 -0
  104. abstract_utilities/import_utils/src/package_utils/import_collectors.py +53 -0
  105. abstract_utilities/import_utils/src/package_utils/path_utils.py +28 -0
  106. abstract_utilities/import_utils/src/package_utils/safe_import.py +27 -0
  107. abstract_utilities/import_utils/src/package_utils.py +140 -0
  108. abstract_utilities/import_utils/src/package_utilss/__init__.py +139 -0
  109. abstract_utilities/import_utils/src/package_utilss/context_utils.py +27 -0
  110. abstract_utilities/import_utils/src/package_utilss/import_collectors.py +53 -0
  111. abstract_utilities/import_utils/src/package_utilss/path_utils.py +28 -0
  112. abstract_utilities/import_utils/src/package_utilss/safe_import.py +27 -0
  113. abstract_utilities/import_utils/src/pkg_utils.py +194 -0
  114. abstract_utilities/import_utils/src/sysroot_utils.py +112 -0
  115. abstract_utilities/imports.py +18 -0
  116. abstract_utilities/json_utils/__init__.py +2 -0
  117. abstract_utilities/json_utils/imports/__init__.py +2 -0
  118. abstract_utilities/json_utils/imports/imports.py +2 -0
  119. abstract_utilities/json_utils/imports/module_imports.py +5 -0
  120. abstract_utilities/json_utils/json_utils.py +743 -0
  121. abstract_utilities/list_utils/__init__.py +2 -0
  122. abstract_utilities/list_utils/imports/__init__.py +2 -0
  123. abstract_utilities/list_utils/imports/imports.py +1 -0
  124. abstract_utilities/list_utils/imports/module_imports.py +0 -0
  125. abstract_utilities/list_utils/list_utils.py +199 -0
  126. abstract_utilities/log_utils/__init__.py +5 -0
  127. abstract_utilities/log_utils/abstractLogManager.py +64 -0
  128. abstract_utilities/log_utils/call_response.py +68 -0
  129. abstract_utilities/log_utils/imports/__init__.py +2 -0
  130. abstract_utilities/log_utils/imports/imports.py +7 -0
  131. abstract_utilities/log_utils/imports/module_imports.py +2 -0
  132. abstract_utilities/log_utils/log_file.py +59 -0
  133. abstract_utilities/log_utils/logger_callable.py +49 -0
  134. abstract_utilities/math_utils/__init__.py +2 -0
  135. abstract_utilities/math_utils/imports/__init__.py +2 -0
  136. abstract_utilities/math_utils/imports/imports.py +2 -0
  137. abstract_utilities/math_utils/imports/module_imports.py +1 -0
  138. abstract_utilities/math_utils/math_utils.py +208 -0
  139. abstract_utilities/parse_utils/__init__.py +2 -0
  140. abstract_utilities/parse_utils/imports/__init__.py +3 -0
  141. abstract_utilities/parse_utils/imports/constants.py +10 -0
  142. abstract_utilities/parse_utils/imports/imports.py +2 -0
  143. abstract_utilities/parse_utils/imports/module_imports.py +4 -0
  144. abstract_utilities/parse_utils/parse_utils.py +516 -0
  145. abstract_utilities/path_utils/__init__.py +2 -0
  146. abstract_utilities/path_utils/imports/__init__.py +3 -0
  147. abstract_utilities/path_utils/imports/imports.py +1 -0
  148. abstract_utilities/path_utils/imports/module_imports.py +8 -0
  149. abstract_utilities/path_utils/path_utils.py +253 -0
  150. abstract_utilities/path_utils.py +95 -14
  151. abstract_utilities/read_write_utils/__init__.py +1 -0
  152. abstract_utilities/read_write_utils/imports/__init__.py +2 -0
  153. abstract_utilities/read_write_utils/imports/imports.py +2 -0
  154. abstract_utilities/read_write_utils/imports/module_imports.py +5 -0
  155. abstract_utilities/read_write_utils/read_write_utils.py +338 -0
  156. abstract_utilities/read_write_utils.py +66 -34
  157. abstract_utilities/safe_utils/__init__.py +2 -0
  158. abstract_utilities/safe_utils/imports/__init__.py +3 -0
  159. abstract_utilities/safe_utils/imports/imports.py +2 -0
  160. abstract_utilities/safe_utils/imports/module_imports.py +2 -0
  161. abstract_utilities/safe_utils/safe_utils.py +166 -0
  162. abstract_utilities/ssh_utils/__init__.py +3 -1
  163. abstract_utilities/ssh_utils/classes.py +0 -1
  164. abstract_utilities/ssh_utils/cmd_utils.py +207 -0
  165. abstract_utilities/ssh_utils/imports/__init__.py +3 -0
  166. abstract_utilities/ssh_utils/imports/imports.py +5 -0
  167. abstract_utilities/ssh_utils/imports/module_imports.py +6 -0
  168. abstract_utilities/ssh_utils/imports/utils.py +189 -0
  169. abstract_utilities/ssh_utils/pexpect_utils.py +11 -18
  170. abstract_utilities/ssh_utils/type_checks.py +92 -0
  171. abstract_utilities/string_utils/__init__.py +4 -0
  172. abstract_utilities/string_utils/clean_utils.py +28 -0
  173. abstract_utilities/string_utils/eat_utils.py +103 -0
  174. abstract_utilities/string_utils/imports/__init__.py +3 -0
  175. abstract_utilities/string_utils/imports/imports.py +2 -0
  176. abstract_utilities/string_utils/imports/module_imports.py +2 -0
  177. abstract_utilities/string_utils/imports/utils.py +81 -0
  178. abstract_utilities/string_utils/replace_utils.py +27 -0
  179. abstract_utilities/thread_utils/__init__.py +2 -0
  180. abstract_utilities/thread_utils/imports/__init__.py +2 -0
  181. abstract_utilities/thread_utils/imports/imports.py +2 -0
  182. abstract_utilities/thread_utils/imports/module_imports.py +2 -0
  183. abstract_utilities/thread_utils/thread_utils.py +140 -0
  184. abstract_utilities/time_utils/__init__.py +2 -0
  185. abstract_utilities/time_utils/imports/__init__.py +2 -0
  186. abstract_utilities/time_utils/imports/imports.py +3 -0
  187. abstract_utilities/time_utils/imports/module_imports.py +1 -0
  188. abstract_utilities/time_utils/time_utils.py +392 -0
  189. abstract_utilities/type_utils/__init__.py +3 -0
  190. abstract_utilities/type_utils/alpha_utils.py +59 -0
  191. abstract_utilities/type_utils/imports/__init__.py +2 -0
  192. abstract_utilities/type_utils/imports/imports.py +4 -0
  193. abstract_utilities/type_utils/imports/module_imports.py +1 -0
  194. abstract_utilities/type_utils/num_utils.py +19 -0
  195. abstract_utilities/type_utils/type_utils.py +981 -0
  196. {abstract_utilities-0.2.2.492.dist-info → abstract_utilities-0.2.2.583.dist-info}/METADATA +1 -1
  197. abstract_utilities-0.2.2.583.dist-info/RECORD +277 -0
  198. imports/__init__.py +36 -0
  199. abstract_utilities-0.2.2.492.dist-info/RECORD +0 -92
  200. {abstract_utilities-0.2.2.492.dist-info → abstract_utilities-0.2.2.583.dist-info}/WHEEL +0 -0
  201. {abstract_utilities-0.2.2.492.dist-info → abstract_utilities-0.2.2.583.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,338 @@
1
+ """
2
+ read_write_utils.py
3
+ -------------------
4
+ Unified read/write utility for safe file operations.
5
+ Supports:
6
+ - Writing content to a file
7
+ - Reading content from a file
8
+ - Creating and reading if missing
9
+ - Detecting file/content params via positional args or kwargs
10
+
11
+ Usage:
12
+ from abstract_utilities.read_write_utils import *
13
+ """
14
+
15
+ from .imports import *
16
+ _FILE_PATH_KEYS = ['file', 'filepath', 'file_path', 'path', 'directory', 'f', 'dst', 'dest']
17
+ _CONTENTS_KEYS = ['cont', 'content', 'contents', 'data', 'datas', 'dat', 'src', 'source']
18
+
19
+
20
+ # --- Helper utilities --------------------------------------------------------
21
+ def string_in_keys(strings, kwargs):
22
+ """Find a matching keyword in kwargs that contains any of the given substrings."""
23
+ for key in kwargs:
24
+ for s in strings:
25
+ if s.lower() in key.lower():
26
+ return key
27
+ return None
28
+ def make_dirs(path, exist_ok=True, **kwargs):
29
+ remote = get_user_pass_host_key(**kwargs)
30
+
31
+ if remote:
32
+ kwargs['cmd'] = f"mkdir -p {path}"
33
+
34
+ resp = run_pruned_func(run_cmd, **kwargs)
35
+
36
+ else:
37
+ os.makedirs(path, exist_ok=exist_ok)
38
+ return path
39
+ def make_path(path, home_dir=None, file=None, **kwargs):
40
+ if not path:
41
+ return None
42
+
43
+ basename = os.path.basename(path)
44
+ parts = [p for p in path.split('/') if p]
45
+
46
+ # Detect whether this is a file or a folder
47
+ is_file = file if file is not None else ('.' in basename)
48
+ pieces = parts[:-1] if is_file else parts
49
+
50
+ full_dir = home_dir or '/'
51
+ for piece in pieces:
52
+ full_dir = os.path.join(full_dir, piece)
53
+ make_dirs(full_dir, exist_ok=True, **kwargs)
54
+
55
+ if is_file:
56
+ full_dir = os.path.join(full_dir, basename)
57
+
58
+ return full_dir
59
+ def get_rel_path(src,src_rel,dst,**kwargs):
60
+ if src.startswith(src_rel):
61
+ nu_src = src[len(src_rel):]
62
+ nu_src= eatAll(nu_src,'/')
63
+ directory= eatOuter(dst,'/')
64
+ rel_path = os.path.join(dst,nu_src)
65
+ return rel_path
66
+ def make_relative_path(src,src_rel,dst,**kwargs):
67
+
68
+ if src.startswith(src_rel):
69
+ rel_path = get_rel_path(src,src_rel,dst)
70
+
71
+ path = make_path(rel_path,**kwargs)
72
+
73
+ return path
74
+
75
+ def path_join(*args):
76
+ path = None
77
+ for i,arg in enumerate(args):
78
+ if arg:
79
+ if i == 0:
80
+ path = arg
81
+ else:
82
+ path = os.path.join(path,arg)
83
+ return path
84
+
85
+ def get_path(paths,**kwargs):
86
+ """Return the first valid path among given paths."""
87
+ for path in paths:
88
+ if isinstance(path, str):
89
+ if is_file(path,**kwargs):
90
+ return path
91
+ dirname = os.path.dirname(path)
92
+ if is_exists(dirname,**kwargs):
93
+ return path
94
+ return None
95
+
96
+
97
+ def break_down_find_existing(path,**kwargs):
98
+ """Return the first non-existent subpath within a path chain."""
99
+ test_path = ''
100
+ for part in path.split(os.sep):
101
+ test_path = os.path.join(test_path, part)
102
+ if not is_exists(test_path,**kwargs):
103
+ return test_path if test_path else None
104
+ return test_path
105
+
106
+
107
+ # --- Parameter parsing --------------------------------------------------------
108
+ def check_read_write_params(*args, **kwargs):
109
+ """
110
+ Determine file_path and contents from arguments.
111
+ Returns a tuple: (file_path, contents)
112
+ """
113
+ file_key = string_in_keys(_FILE_PATH_KEYS, kwargs)
114
+ content_key = string_in_keys(_CONTENTS_KEYS, kwargs)
115
+
116
+ file_path = kwargs.get(file_key) if file_key else None
117
+ contents = kwargs.get(content_key) if content_key else None
118
+
119
+ # Handle positional args (fallback)
120
+ if file_path is None and len(args) > 0:
121
+ file_path = args[0]
122
+ if contents is None and len(args) > 1:
123
+ contents = args[1]
124
+
125
+ if file_path is None:
126
+ raise ValueError("Missing file_path argument.")
127
+ return file_path, contents
128
+
129
+ def write_to_path(
130
+ file_path: str,
131
+ contents: str,
132
+ *,
133
+ user_at_host: str = None,
134
+ cwd: str | None = None,
135
+ password=None,
136
+ key=None,
137
+ env_path=None,
138
+ **kwargs
139
+ ) -> str:
140
+ """
141
+ Completely overwrite a file (locally or remotely).
142
+ Supports sudo and password-based remote execution.
143
+ """
144
+
145
+ # sanitize for shell safety
146
+ quoted_path = shlex.quote(file_path)
147
+ quoted_data = shlex.quote(str(contents))
148
+
149
+ # shell command that fully overwrites
150
+ # (no append, replaces contents entirely)
151
+ base_cmd = f'sudo sh -c "echo {quoted_data} > {quoted_path}"'
152
+
153
+ # optional sudo password injection
154
+ full_cmd = get_print_sudo_cmd(
155
+ cmd=base_cmd,
156
+ password=password,
157
+ key=key,
158
+ env_path=env_path
159
+ )
160
+
161
+ # local or remote dispatch
162
+ if user_at_host:
163
+ return run_remote_cmd(
164
+ user_at_host=user_at_host,
165
+ cmd=full_cmd,
166
+ cwd=cwd,
167
+ password=password,
168
+ key=key,
169
+ env_path=env_path,
170
+ **kwargs
171
+ )
172
+ else:
173
+ return run_local_cmd(
174
+ cmd=full_cmd,
175
+ cwd=cwd,
176
+ password=password,
177
+ key=key,
178
+ env_path=env_path,
179
+ **kwargs
180
+ )
181
+ ### --- Core functionality -------------------------------------------------------
182
+ ##def write_to_file(*args, **kwargs):
183
+ ## """
184
+ ## Write contents to a file (create if missing).
185
+ ##
186
+ ## Returns the file_path written.
187
+ ## """
188
+ ## file_path, contents = check_read_write_params(*args, **kwargs)
189
+ ## if contents is None:
190
+ ## raise ValueError("Missing contents to write.")
191
+ ##
192
+ ## os.makedirs(os.path.dirname(file_path) or ".", exist_ok=True)
193
+ ## with open(file_path, "w", encoding="utf-8") as f:
194
+ ## f.write(str(contents))
195
+ ## return file_path
196
+ # --- Core functionality -------------------------------------------------------
197
+ def write_to_file(*args, **kwargs):
198
+ """
199
+ Write contents to a file (create if missing).
200
+
201
+ Returns the file_path written.
202
+ """
203
+ file_path, contents = check_read_write_params(*args, **kwargs)
204
+ values,kwargs = get_from_kwargs(['file_path','contents'],del_kwarg=True,**kwargs)
205
+ dirname = os.path.dirname(file_path)
206
+
207
+ if contents is None:
208
+ raise ValueError("Missing contents to write.")
209
+ user_at_host = kwargs.get("user_at_host")
210
+ if get_user_pass_host_key(**kwargs):
211
+ make_dirs(dirname, exist_ok=True,**kwargs)
212
+ kwargs["cwd"] = kwargs.get('cwd') or os.path.dirname(file_path)
213
+ # sanitize for shell safety
214
+ quoted_path = shlex.quote(file_path)
215
+ quoted_data = shlex.quote(str(contents))
216
+ # shell command that fully overwrites
217
+ # (no append, replaces contents entirely)
218
+ kwargs["cmd"] = f'sh -c "echo {quoted_data} > {quoted_path}"'
219
+ if not kwargs.get('password') and not kwargs.get('key'):
220
+ kwargs["cmd"]=f'sudo {kwargs["cmd"]}'
221
+ result = run_pruned_func(run_cmd,**kwargs)
222
+ if 'file_path' in kwargs:
223
+ del kwargs['file_path']
224
+ if not is_file(file_path,**kwargs) or str(contents) != read_from_file(file_path,**kwargs):
225
+ kwargs["cmd"]=f'sudo {kwargs["cmd"]}'
226
+ result = run_pruned_func(run_cmd,**kwargs)
227
+ return result
228
+
229
+ make_dirs(dirname or ".", exist_ok=True)
230
+ with open(file_path, "w", encoding="utf-8") as f:
231
+ f.write(str(contents))
232
+ return file_path
233
+
234
+
235
+ def read_from_file(file_path,**kwargs):
236
+ if get_user_pass_host_key(**kwargs):
237
+ kwargs["cwd"] = kwargs.get('cwd') or os.path.dirname(file_path)
238
+ basename = os.path.basename(file_path)
239
+ kwargs["cmd"] = f'cat {basename}'
240
+ return run_pruned_func(run_cmd,**kwargs)
241
+ """Read text content from a file."""
242
+ with open(file_path, "r", encoding="utf-8") as f:
243
+ return f.read()
244
+
245
+
246
+ def copy_dirs(dirs, dst_root, src_rel=None, **kwargs):
247
+ """
248
+ Recursively copy directory structures (without files) from dirs → dst_root.
249
+ """
250
+ for src in dirs:
251
+ # build destination path preserving relative structure
252
+ dst_path = make_relative_path(src, src_rel, dst_root, **kwargs) if src_rel else dst_root
253
+ make_path(dst_path, **kwargs) # ensures directory exists
254
+
255
+
256
+
257
+ def copy_file(src, dst_root, src_rel=None, **kwargs):
258
+ """
259
+ Copy a single file to dst_root, preserving relative structure if src_rel provided.
260
+ Supports remote copy via read/write.
261
+ """
262
+ # derive destination file path
263
+ dst_path = make_relative_path(src, src_rel, dst_root, **kwargs) if src_rel else os.path.join(dst_root, os.path.basename(src))
264
+ make_path(dst_path, **kwargs)
265
+
266
+ if get_user_pass_host_key(**kwargs): # remote mode
267
+ contents = read_from_file(src, **kwargs)
268
+ write_to_file(contents=contents, file_path=dst_path, **kwargs)
269
+ else: # local
270
+ os.makedirs(os.path.dirname(dst_path), exist_ok=True)
271
+ shutil.copy2(src, dst_path)
272
+
273
+
274
+ return dst_path
275
+
276
+
277
+ def copy_files(files, dst_root, src_rel=None, **kwargs):
278
+ """
279
+ Copy a list of files to dst_root.
280
+ """
281
+ for src in files:
282
+ copy_file(src=src, dst_root=dst_root, src_rel=src_rel, **kwargs)
283
+
284
+ def create_and_read_file(*args, **kwargs):
285
+ """
286
+ Create the file (if missing) and read contents from it.
287
+ """
288
+ file_path, contents = check_read_write_params(*args, **kwargs)
289
+ if not os.path.isfile(file_path):
290
+ write_to_file(file_path, contents or "")
291
+ return read_from_file(file_path)
292
+
293
+
294
+ def is_file_extension(obj: str) -> bool:
295
+ """Return True if obj looks like a filename with extension."""
296
+ if not isinstance(obj, str):
297
+ return False
298
+ root, ext = os.path.splitext(obj)
299
+ return bool(root and ext)
300
+
301
+
302
+ def delete_file(file_path: str):
303
+ """Safely delete a file if it exists."""
304
+ if os.path.isfile(file_path):
305
+ os.remove(file_path)
306
+ return True
307
+ return False
308
+
309
+
310
+ def get_content_lines(*args, **kwargs):
311
+ """Return a list of lines from string or file path."""
312
+ file_path, contents = check_read_write_params(*args, **kwargs)
313
+ if os.path.isfile(file_path):
314
+ contents = read_from_file(filepath)
315
+
316
+ if isinstance(contents, str):
317
+ return contents.splitlines()
318
+ elif isinstance(contents, list):
319
+ return contents
320
+ return []
321
+ def collate_text_docs(directory=None):
322
+ return [read_from_file(item) for item in get_all_files(directory=directory)]
323
+ def get_content(*paths):
324
+ item_path = os.path.join(*paths)
325
+ if os.path.isfile(item_path):
326
+ try:
327
+ content = read_from_file(item_path)
328
+ return content
329
+ except:
330
+ pass
331
+ return None
332
+ def get_text_or_read(text=None,file_path=None):
333
+ text = text or ''
334
+ imports_js = {}
335
+ if not text and file_path and os.path.isfile(file_path):
336
+ text=read_from_file(file_path)
337
+ return text
338
+ ##
@@ -12,14 +12,12 @@ Usage:
12
12
  from abstract_utilities.read_write_utils import *
13
13
  """
14
14
 
15
- import os
16
- import shlex
15
+ import os,shlex
17
16
  from .string_clean import *
18
17
  from .ssh_utils.utils import run_cmd,get_print_sudo_cmd,run_local_cmd,run_remote_cmd
19
- from .file_utils.file_utils.type_checks import is_file,is_dir,get_user_pass_host_key,is_exists
20
18
  from .abstract_classes import run_pruned_func
21
19
  from .string_utils import get_from_kwargs
22
-
20
+ from .path_utils import get_all_files,is_file,is_dir,get_user_pass_host_key,is_exists
23
21
  _FILE_PATH_KEYS = ['file', 'filepath', 'file_path', 'path', 'directory', 'f', 'dst', 'dest']
24
22
  _CONTENTS_KEYS = ['cont', 'content', 'contents', 'data', 'datas', 'dat', 'src', 'source']
25
23
 
@@ -34,12 +32,12 @@ def string_in_keys(strings, kwargs):
34
32
  return None
35
33
  def make_dirs(path, exist_ok=True, **kwargs):
36
34
  remote = get_user_pass_host_key(**kwargs)
37
- print(remote)
35
+
38
36
  if remote:
39
37
  kwargs['cmd'] = f"mkdir -p {path}"
40
- print(kwargs)
38
+
41
39
  resp = run_pruned_func(run_cmd, **kwargs)
42
- print(resp)
40
+
43
41
  else:
44
42
  os.makedirs(path, exist_ok=exist_ok)
45
43
  return path
@@ -53,16 +51,15 @@ def make_path(path, home_dir=None, file=None, **kwargs):
53
51
  # Detect whether this is a file or a folder
54
52
  is_file = file if file is not None else ('.' in basename)
55
53
  pieces = parts[:-1] if is_file else parts
56
- print(pieces)
54
+
57
55
  full_dir = home_dir or '/'
58
56
  for piece in pieces:
59
57
  full_dir = os.path.join(full_dir, piece)
60
58
  make_dirs(full_dir, exist_ok=True, **kwargs)
61
- print(f"✅ full_dir == {full_dir}")
59
+
62
60
  if is_file:
63
61
  full_dir = os.path.join(full_dir, basename)
64
62
 
65
- print(f"✅ full_dir == {full_dir}")
66
63
  return full_dir
67
64
  def get_rel_path(src,src_rel,dst,**kwargs):
68
65
  if src.startswith(src_rel):
@@ -72,12 +69,12 @@ def get_rel_path(src,src_rel,dst,**kwargs):
72
69
  rel_path = os.path.join(dst,nu_src)
73
70
  return rel_path
74
71
  def make_relative_path(src,src_rel,dst,**kwargs):
75
- print(f"src == {src}\nsrc_rel == {src_rel}\dst == {dst}")
72
+
76
73
  if src.startswith(src_rel):
77
74
  rel_path = get_rel_path(src,src_rel,dst)
78
- print(rel_path)
75
+
79
76
  path = make_path(rel_path,**kwargs)
80
- print(f"path == {path}")
77
+
81
78
  return path
82
79
 
83
80
  def path_join(*args):
@@ -157,7 +154,7 @@ def write_to_path(
157
154
  # shell command that fully overwrites
158
155
  # (no append, replaces contents entirely)
159
156
  base_cmd = f'sudo sh -c "echo {quoted_data} > {quoted_path}"'
160
- input(base_cmd)
157
+
161
158
  # optional sudo password injection
162
159
  full_cmd = get_print_sudo_cmd(
163
160
  cmd=base_cmd,
@@ -250,27 +247,44 @@ def read_from_file(file_path,**kwargs):
250
247
  with open(file_path, "r", encoding="utf-8") as f:
251
248
  return f.read()
252
249
 
253
- def copy_dirs(dirs,dst,src_rel=None,**kwargs):
250
+
251
+ def copy_dirs(dirs, dst_root, src_rel=None, **kwargs):
252
+ """
253
+ Recursively copy directory structures (without files) from dirs → dst_root.
254
+ """
254
255
  for src in dirs:
255
- if rel_path:
256
- dst = make_relative_path(src,src_rel,dst,**kwargs)
257
- make_path(dst,**kwargs)
258
-
259
- def copy_file(src,dst,rel_path=None,**kwargs):
260
-
261
- if rel_path:
262
- dst = make_relative_path(src,rel_path,dst,**kwargs)
263
- print(dst)
264
- if get_user_pass_host_key(**kwargs):
265
- contents=read_from_file(src,**kwargs)
266
- write_to_file(contents=contents,file_path=dst,**kwargs)
267
- else:
268
- shutil.copy(src,dst)
269
- print(dst)
270
- return dst
271
- def copy_files(files,dst,rel_path=None,**kwargs):
272
- for file in files:
273
- copy_file(src=file,dst=dst,rel_path=rel_path,**kwargs)
256
+ # build destination path preserving relative structure
257
+ dst_path = make_relative_path(src, src_rel, dst_root, **kwargs) if src_rel else dst_root
258
+ make_path(dst_path, **kwargs) # ensures directory exists
259
+
260
+
261
+
262
+ def copy_file(src, dst_root, src_rel=None, **kwargs):
263
+ """
264
+ Copy a single file to dst_root, preserving relative structure if src_rel provided.
265
+ Supports remote copy via read/write.
266
+ """
267
+ # derive destination file path
268
+ dst_path = make_relative_path(src, src_rel, dst_root, **kwargs) if src_rel else os.path.join(dst_root, os.path.basename(src))
269
+ make_path(dst_path, **kwargs)
270
+
271
+ if get_user_pass_host_key(**kwargs): # remote mode
272
+ contents = read_from_file(src, **kwargs)
273
+ write_to_file(contents=contents, file_path=dst_path, **kwargs)
274
+ else: # local
275
+ os.makedirs(os.path.dirname(dst_path), exist_ok=True)
276
+ shutil.copy2(src, dst_path)
277
+
278
+
279
+ return dst_path
280
+
281
+
282
+ def copy_files(files, dst_root, src_rel=None, **kwargs):
283
+ """
284
+ Copy a list of files to dst_root.
285
+ """
286
+ for src in files:
287
+ copy_file(src=src, dst_root=dst_root, src_rel=src_rel, **kwargs)
274
288
 
275
289
  def create_and_read_file(*args, **kwargs):
276
290
  """
@@ -309,3 +323,21 @@ def get_content_lines(*args, **kwargs):
309
323
  elif isinstance(contents, list):
310
324
  return contents
311
325
  return []
326
+ def collate_text_docs(directory=None):
327
+ return [read_from_file(item) for item in get_all_files(directory=directory)]
328
+ def get_content(*paths):
329
+ item_path = os.path.join(*paths)
330
+ if os.path.isfile(item_path):
331
+ try:
332
+ content = read_from_file(item_path)
333
+ return content
334
+ except:
335
+ pass
336
+ return None
337
+ def get_text_or_read(text=None,file_path=None):
338
+ text = text or ''
339
+ imports_js = {}
340
+ if not text and file_path and os.path.isfile(file_path):
341
+ text=read_from_file(file_path)
342
+ return text
343
+ ##
@@ -0,0 +1,2 @@
1
+ from .imports import *
2
+ from .safe_utils import *
@@ -0,0 +1,3 @@
1
+ from .imports import *
2
+
3
+ from .module_imports import *
@@ -0,0 +1,2 @@
1
+ from ...imports import *
2
+ from typing import *
@@ -0,0 +1,2 @@
1
+ from ...type_utils import is_number
2
+ from ...class_utils import get_caller_dir
@@ -0,0 +1,166 @@
1
+ """
2
+ abstract_safeops.py
3
+ -------------------
4
+ Utility functions for safely splitting, slicing, and retrieving elements from iterable or string objects
5
+ without raising exceptions on invalid input or out-of-range indices.
6
+
7
+ Designed for compatibility with the abstract_ ecosystem (e.g. abstract_utilities, abstract_math, etc.).
8
+ """
9
+
10
+ from .imports import *
11
+ _BASE_DIR = get_caller_dir()
12
+
13
+ class PathOutsideBase(Exception):
14
+ pass
15
+
16
+ def safe_join_base(base: Union[str, Path], *parts: Union[str, Path], must_exist: bool = False) -> Path:
17
+ """
18
+ Join base with parts, normalize, and ensure the result lives under base.
19
+ Prevents '../' traversal and ignores leading slashes in parts.
20
+ """
21
+ base = Path(base).resolve(strict=True)
22
+ # Disallow absolute/drive-anchored parts by stripping their anchors before joining.
23
+ cleaned = []
24
+ for p in parts:
25
+ p = Path(p)
26
+ # Convert absolute to relative (security: we won't allow escaping base anyway)
27
+ if p.is_absolute():
28
+ p = Path(*p.parts[1:]) # drop leading '/'
29
+ cleaned.append(p)
30
+
31
+ # Build and resolve (non-strict so missing files are allowed unless must_exist=True)
32
+ target = (base.joinpath(*cleaned)).resolve(strict=False)
33
+
34
+ # Containment check (works even if target doesn't exist)
35
+ try:
36
+ target.relative_to(base)
37
+ except ValueError:
38
+ raise PathOutsideBase(f"{target} escapes base {base}")
39
+
40
+ if must_exist and not target.exists():
41
+ raise FileNotFoundError(target)
42
+
43
+ return target
44
+ def safe_split(
45
+ string: Any,
46
+ char: Any,
47
+ i: Optional[int] = None,
48
+ default: Union[bool, Any] = False
49
+ ) -> Union[str, List[str], Any, None]:
50
+ """
51
+ Safely split a string by a character and optionally return index i.
52
+
53
+ Args:
54
+ string: Input string (or any object convertible to string).
55
+ char: Delimiter to split on.
56
+ i: Optional index to retrieve from the split result.
57
+ default: If True, return the original string on error.
58
+ If any other value, return that instead of raising.
59
+
60
+ Returns:
61
+ The split list, or the element at index i, or default behavior on error.
62
+ """
63
+ if string is None or char is None:
64
+ return string
65
+
66
+ s, c = str(string), str(char)
67
+ if c not in s:
68
+ return string
69
+
70
+ parts = s.split(c)
71
+
72
+ if i is None:
73
+ return parts
74
+
75
+ if is_number(i):
76
+ idx = int(i)
77
+ if 0 <= idx < len(parts):
78
+ return parts[idx]
79
+
80
+ if default:
81
+ return string if default is True else default
82
+
83
+ return None
84
+
85
+
86
+ def safe_slice(
87
+ obj: Any,
88
+ i: Optional[int] = None,
89
+ k: Optional[int] = None,
90
+ default: Union[bool, Any] = False
91
+ ) -> Any:
92
+ """
93
+ Safely slice an iterable object or string, with fallback behavior on invalid indices.
94
+
95
+ Args:
96
+ obj: Iterable or string-like object.
97
+ i: Start index (can be negative).
98
+ k: End index (can be negative).
99
+ default: If True, returns the original object on error.
100
+ If any other value, return that value on error.
101
+
102
+ Returns:
103
+ The sliced object, or default behavior on error.
104
+ """
105
+ # Null or invalid base case
106
+ if obj is None or isinstance(obj, bool):
107
+ return obj if default is True else default if default else None
108
+
109
+ # Non-iterable guard
110
+ if not hasattr(obj, "__getitem__"):
111
+ return obj if default is True else default if default else None
112
+
113
+ obj_len = len(obj)
114
+
115
+ # Normalize negative indices
116
+ if isinstance(i, int) and i < 0:
117
+ i = obj_len + i
118
+ if isinstance(k, int) and k < 0:
119
+ k = obj_len + k
120
+
121
+ # Bound indices
122
+ if i is not None:
123
+ i = max(0, min(i, obj_len))
124
+ if k is not None:
125
+ k = max(0, min(k, obj_len))
126
+
127
+ try:
128
+ return obj[i:k]
129
+ except Exception:
130
+ return obj if default is True else default if default else None
131
+
132
+ def safe_join(*paths):
133
+ paths = list(paths)
134
+ paths = [path for path in paths if path]
135
+ return os.path.join(*paths)
136
+ def safe_get(
137
+ obj: Any,
138
+ key: Union[int, str, None] = None,
139
+ default: Union[bool, Any] = False
140
+ ) -> Any:
141
+ """
142
+ Generalized safe getter for both indexable and mapping types.
143
+
144
+ Args:
145
+ obj: The object to access (list, dict, string, etc.).
146
+ key: Index or key to retrieve.
147
+ default: Fallback value or True for "return obj".
148
+
149
+ Returns:
150
+ Retrieved element, or default value on failure.
151
+ """
152
+ if obj is None or key is None:
153
+ return obj if default is True else default if default else None
154
+
155
+ try:
156
+ if isinstance(obj, dict):
157
+ return obj.get(key, obj if default is True else default if default else None)
158
+ return obj[key]
159
+ except Exception:
160
+ return obj if default is True else default if default else None
161
+ def get_slash(path):
162
+ if '/' in path:
163
+ return '/'
164
+ else:
165
+ return '//'
166
+ join_path=safe_join
@@ -1,3 +1,5 @@
1
+ from .imports import *
1
2
  from .classes import *
2
- from .utils import *
3
+ from .cmd_utils import *
3
4
  from .pexpect_utils import *
5
+ from .type_checks import *
@@ -1,5 +1,4 @@
1
1
  from .imports import *
2
- from .utils import run_local_cmd, run_ssh_cmd,run_any_cmd,run_cmd
3
2
 
4
3
  class PathBackend(Protocol):
5
4
  def join(self, *parts: str) -> str: ...