abstract-utilities 0.2.2.387__py3-none-any.whl → 0.2.2.480__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.

Potentially problematic release.


This version of abstract-utilities might be problematic. Click here for more details.

Files changed (63) hide show
  1. abstract_utilities/__init__.py +14 -43
  2. abstract_utilities/abstract_classes.py +49 -0
  3. abstract_utilities/class_utils.py +38 -3
  4. abstract_utilities/cmd_utils/imports/__init__.py +1 -0
  5. abstract_utilities/cmd_utils/imports/imports.py +10 -0
  6. abstract_utilities/cmd_utils/pexpect_utils.py +310 -0
  7. abstract_utilities/cmd_utils/user_utils.py +1 -1
  8. abstract_utilities/dynimport.py +7 -15
  9. abstract_utilities/env_utils/__init__.py +3 -0
  10. abstract_utilities/env_utils/abstractEnv.py +129 -0
  11. abstract_utilities/env_utils/envy_it.py +33 -0
  12. abstract_utilities/env_utils/imports/__init__.py +2 -0
  13. abstract_utilities/env_utils/imports/imports.py +8 -0
  14. abstract_utilities/env_utils/imports/utils.py +122 -0
  15. abstract_utilities/file_utils/__init__.py +3 -0
  16. abstract_utilities/file_utils/file_utils/__init__.py +8 -0
  17. abstract_utilities/file_utils/file_utils/file_filters.py +104 -0
  18. abstract_utilities/{robust_reader → file_utils/file_utils}/file_reader.py +5 -19
  19. abstract_utilities/{robust_readers/file_filters.py → file_utils/file_utils/file_utils.py} +5 -4
  20. abstract_utilities/{robust_readers → file_utils/file_utils}/filter_params.py +1 -38
  21. abstract_utilities/file_utils/file_utils/find_collect.py +154 -0
  22. abstract_utilities/file_utils/file_utils/imports/__init__.py +3 -0
  23. abstract_utilities/file_utils/file_utils/imports/constants.py +39 -0
  24. abstract_utilities/file_utils/file_utils/imports/file_functions.py +10 -0
  25. abstract_utilities/file_utils/file_utils/imports/imports.py +39 -0
  26. abstract_utilities/file_utils/file_utils/imports/module_imports.py +14 -0
  27. abstract_utilities/file_utils/file_utils/imports.py +10 -0
  28. abstract_utilities/file_utils/file_utils/map_utils.py +29 -0
  29. abstract_utilities/{robust_reader → file_utils/file_utils}/pdf_utils.py +1 -9
  30. abstract_utilities/file_utils/file_utils/type_checks.py +91 -0
  31. abstract_utilities/file_utils/imports/__init__.py +4 -0
  32. abstract_utilities/file_utils/imports/classes.py +381 -0
  33. abstract_utilities/file_utils/imports/clean_imps.py +158 -0
  34. abstract_utilities/file_utils/imports/constants.py +39 -0
  35. abstract_utilities/file_utils/imports/file_functions.py +10 -0
  36. abstract_utilities/file_utils/imports/imports.py +65 -0
  37. abstract_utilities/file_utils/imports/module_imports.py +13 -0
  38. abstract_utilities/file_utils/req.py +329 -0
  39. abstract_utilities/log_utils.py +1 -1
  40. abstract_utilities/path_utils.py +90 -6
  41. abstract_utilities/read_write_utils.py +250 -157
  42. abstract_utilities/robust_reader/__init__.py +1 -1
  43. abstract_utilities/robust_reader/imports/__init__.py +1 -0
  44. abstract_utilities/robust_reader/imports/imports.py +3 -0
  45. abstract_utilities/robust_readers/__init__.py +0 -1
  46. abstract_utilities/robust_readers/import_utils/__init__.py +1 -0
  47. abstract_utilities/robust_readers/import_utils/clean_imports.py +175 -0
  48. abstract_utilities/robust_readers/imports.py +8 -0
  49. abstract_utilities/robust_readers/initFuncGen.py +92 -76
  50. abstract_utilities/safe_utils.py +133 -0
  51. abstract_utilities/ssh_utils/__init__.py +3 -0
  52. abstract_utilities/ssh_utils/classes.py +127 -0
  53. abstract_utilities/ssh_utils/imports.py +10 -0
  54. abstract_utilities/ssh_utils/pexpect_utils.py +315 -0
  55. abstract_utilities/ssh_utils/utils.py +188 -0
  56. abstract_utilities/string_clean.py +40 -1
  57. abstract_utilities/string_utils.py +51 -0
  58. abstract_utilities/type_utils.py +25 -2
  59. {abstract_utilities-0.2.2.387.dist-info → abstract_utilities-0.2.2.480.dist-info}/METADATA +1 -1
  60. abstract_utilities-0.2.2.480.dist-info/RECORD +92 -0
  61. abstract_utilities-0.2.2.387.dist-info/RECORD +0 -52
  62. {abstract_utilities-0.2.2.387.dist-info → abstract_utilities-0.2.2.480.dist-info}/WHEEL +0 -0
  63. {abstract_utilities-0.2.2.387.dist-info → abstract_utilities-0.2.2.480.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,29 @@
1
+ from .imports import MIME_TYPES,make_list,os
2
+ from .file_filters import get_globs
3
+ def get_file_type(file_path,types=None,default=None):
4
+ mime_types = {}
5
+ if types:
6
+ types = make_list(types)
7
+ for typ in types:
8
+ mime_types[typ] = MIME_TYPES.get(typ)
9
+ else:
10
+ mime_types = MIME_TYPES
11
+
12
+ if os.path.isfile(file_path):
13
+ basename = os.path.basename(file_path)
14
+ filename,ext = os.path.splitext(basename)
15
+ for file_type,ext_values in mime_types.items():
16
+ if ext in ext_values:
17
+ return file_type
18
+ def get_file_map(directory,types=None,default=None):
19
+ if directory and os.path.isfile(directory):
20
+ directory = os.path.dirname(directory)
21
+ all_types = {}
22
+ files = get_globs(directory)
23
+ for file in files:
24
+ file_type = get_file_type(file,types=types,default=default)
25
+ if file_type:
26
+ if file_type not in all_types:
27
+ all_types[file_type] = []
28
+ all_types[file_type].append(file)
29
+ return all_types
@@ -1,12 +1,4 @@
1
- import PyPDF2
2
- from typing import *
3
- from pdf2image import convert_from_path
4
- from abstract_utilities.path_utils import (is_file, mkdirs, get_directory,
5
- get_base_name, split_text,
6
- get_ext, get_file_name)
7
- from abstract_utilities.type_utils import is_str
8
- from abstract_utilities.cmd_utils import cmd_input
9
- from abstract_utilities.read_write_utils import write_to_file
1
+ from .imports import *
10
2
  def if_none_return(obj: object, obj_2: object) -> object:
11
3
  """
12
4
  Return obj if obj_2 is None, otherwise return obj_2.
@@ -0,0 +1,91 @@
1
+ from .imports import *
2
+
3
+ def get_user_pass_host_key(**kwargs):
4
+ args = ['password','user_at_host','host','key','user']
5
+ values,kwargs = get_from_kwargs(*args,**kwargs,del_kwarg=False)
6
+ return values
7
+
8
+ # --- Base remote checker -----------------------------------------------------
9
+ def _remote_test(path: str, test_flag: str, timeout: int = 5,*args, **kwargs) -> bool:
10
+ """
11
+ Run a remote shell test (e.g. -f, -d) via SSH.
12
+ Returns True if test succeeds, False otherwise.
13
+ """
14
+ try:
15
+ kwargs['cmd']=f"[ {test_flag} {shlex.quote(path)} ] && echo 1 || echo 0"
16
+ kwargs['text']=True
17
+ kwargs['timeout']=timeout
18
+ kwargs['stderr']=subprocess.DEVNULL
19
+ result = run_pruned_func(run_cmd,**kwargs)
20
+ return result.strip() == "1"
21
+ except Exception:
22
+ return False
23
+
24
+
25
+ # --- Individual path checks --------------------------------------------------
26
+ def is_remote_file(path: str,*args, **kwargs) -> bool:
27
+ """True if remote path is a file."""
28
+ return _remote_test(path, "-f", **kwargs)
29
+
30
+
31
+ def is_remote_dir(path: str,*args, **kwargs) -> bool:
32
+ """True if remote path is a directory."""
33
+ return _remote_test(path, "-d", **kwargs)
34
+
35
+
36
+ def is_local_file(path: str) -> bool:
37
+ """True if local path is a file."""
38
+ return os.path.isfile(path)
39
+
40
+
41
+ def is_local_dir(path: str) -> bool:
42
+ """True if local path is a directory."""
43
+ return os.path.isdir(path)
44
+
45
+
46
+ # --- Unified interface -------------------------------------------------------
47
+
48
+ def is_file(path: str,*args,**kwargs) -> bool:
49
+ """Determine if path is a file (works local or remote)."""
50
+ if get_user_pass_host_key(**kwargs):
51
+ return is_remote_file(path, **kwargs)
52
+ return is_local_file(path)
53
+
54
+
55
+ def is_dir(path: str, *args,**kwargs) -> bool:
56
+ """Determine if path is a directory (works local or remote)."""
57
+ if get_user_pass_host_key(**kwargs):
58
+ return is_remote_dir(path, **kwargs)
59
+ return is_local_dir(path)
60
+
61
+ def is_exists(path: str, *args,**kwargs) -> bool:
62
+ if is_file(path,**kwargs):
63
+ return True
64
+ if is_dir(path,**kwargs):
65
+ return True
66
+ return False
67
+ # --- Optional: keep your original all-in-one wrapper ------------------------
68
+ def check_path_type(
69
+ path: str,
70
+ *args,
71
+ **kwargs
72
+ ) -> str:
73
+ """
74
+ Return 'file', 'directory', 'missing', or 'unknown'.
75
+ Uses isolated is_file/is_dir functions.
76
+ """
77
+ if get_user_pass_host_key(**kwargs):
78
+ if is_remote_file(path,**kwargs):
79
+ return "file"
80
+ elif is_remote_dir(path,**kwargs):
81
+ return "directory"
82
+ else:
83
+ return "missing"
84
+ else:
85
+ if os.path.isfile(path):
86
+ return "file"
87
+ elif os.path.isdir(path):
88
+ return "directory"
89
+ elif not os.path.exists(path):
90
+ return "missing"
91
+ return "unknown"
@@ -0,0 +1,4 @@
1
+ from .constants import *
2
+ from .module_imports import *
3
+ from .classes import *
4
+ from .imports import *
@@ -0,0 +1,381 @@
1
+ from .imports import *
2
+ from .module_imports import *
3
+ from .constants import *
4
+ def get_item_check_cmd(path, file=True, directory=False, exists=False):
5
+ if (directory and file) or exists:
6
+ typ = "e"
7
+ elif file:
8
+ typ = "f"
9
+ elif directory:
10
+ typ = "d"
11
+ elif isinstance(file, str):
12
+ if "f" in file:
13
+ typ = "f"
14
+ elif "d" in file:
15
+ typ = "d"
16
+ else:
17
+ typ = "e"
18
+ else:
19
+ typ = "e"
20
+ return f"test -{typ} {shlex.quote(path)} && echo __OK__ || true"
21
+
22
+
23
+ def get_all_item_check_cmd(path, file=True, directory=True, exists=True):
24
+ collects = []
25
+ out_js = {}
26
+
27
+ if file:
28
+ collects.append("file")
29
+ if directory:
30
+ collects.append("dir")
31
+ if exists:
32
+ collects.append("exists")
33
+
34
+ if not collects:
35
+ return out_js
36
+
37
+ path = shlex.quote(path)
38
+ for typ in collects:
39
+ t = typ[0] # f, d, or e
40
+ out_js[typ] = f"test -{t} {path} && echo __OK__ || true"
41
+
42
+ return out_js
43
+
44
+
45
+ def is_file(
46
+ path,
47
+ user_at_host=None,
48
+ password=None,
49
+ key=None,
50
+ env_path=None,
51
+ **kwargs
52
+ ):
53
+ contingencies = list(set([user_at_host,password,key,env_path]))
54
+ len_contingencies = len(contingencies)
55
+ is_potential = (len_contingencies >1 or (None not in contingencies))
56
+ if not is_potential:
57
+ return os.path.isfile(path)
58
+ cmd = get_item_check_cmd(path,file=True)
59
+ return run_cmd(cmd=cmd,
60
+ user_at_host=user_at_host,
61
+ password=password,
62
+ key=key,
63
+ env_path=env_path,
64
+ **kwargs
65
+ )
66
+ def is_dir(
67
+ path,
68
+ user_at_host=None,
69
+ password=None,
70
+ key=None,
71
+ env_path=None,
72
+ **kwargs
73
+ ):
74
+ contingencies = list(set([user_at_host,password,key,env_path]))
75
+ len_contingencies = len(contingencies)
76
+ is_potential = (len_contingencies >1 or (None not in contingencies))
77
+ if not is_potential:
78
+ return os.path.isdir(path)
79
+ cmd = get_item_check_cmd(path,file=False,directory=True)
80
+ return run_cmd(cmd=cmd,
81
+ user_at_host=user_at_host,
82
+ password=password,
83
+ key=key,
84
+ env_path=env_path,
85
+ **kwargs
86
+ )
87
+ def is_exists(
88
+ path,
89
+ user_at_host=None,
90
+ password=None,
91
+ key=None,
92
+ env_path=None,
93
+ **kwargs
94
+ ):
95
+ contingencies = list(set([user_at_host,password,key,env_path]))
96
+ len_contingencies = len(contingencies)
97
+ is_potential = (len_contingencies >1 or (None not in contingencies))
98
+ if not is_potential:
99
+ return os.path.exists(path)
100
+ if is_potential == True:
101
+ cmd = get_item_check_cmd(path,exists=True)
102
+ return run_cmd(cmd=cmd,
103
+ user_at_host=user_at_host,
104
+ password=password,
105
+ key=key,
106
+ env_path=env_path,
107
+ **kwargs
108
+ )
109
+ def is_any(
110
+ path,
111
+ user_at_host=None,
112
+ password=None,
113
+ key=None,
114
+ env_path=None,
115
+ **kwargs
116
+ ):
117
+ contingencies = list(set([user_at_host,password,key,env_path]))
118
+ len_contingencies = len(contingencies)
119
+ is_potential = (len_contingencies >1 or (None not in contingencies))
120
+ if not is_potential:
121
+ return os.path.exists(path)
122
+ if is_potential == True:
123
+ out_js = get_all_item_check_cmd(path,file=True,directory=True,exists=True)
124
+ for typ,cmd in out_js.items():
125
+ response = run_cmd(cmd=cmd,
126
+ user_at_host=user_at_host,
127
+ password=password,
128
+ key=key,
129
+ env_path=env_path,
130
+ **kwargs
131
+ )
132
+ result = "__OK__" in (response or "")
133
+ if result:
134
+ return typ
135
+ return None
136
+ class PathBackend(Protocol):
137
+ def join(self, *parts: str) -> str: ...
138
+ def isfile(self, path: str) -> bool: ...
139
+ def isdir(self, path: str) -> bool: ...
140
+ def glob_recursive(self, base: str, **opts) -> List[str]: ...
141
+ def listdir(self, base: str) -> List[str]: ...
142
+
143
+ class LocalFS:
144
+ def __init__(self, get_type=False, get_is_dir=False, get_is_file=False, get_is_exists=False, **kwargs):
145
+ self.get_type = get_type
146
+ self.get_is_dir = get_is_dir
147
+ self.get_is_file = get_is_file
148
+ self.get_is_exists = get_is_exists
149
+
150
+ def join(self, *parts: str) -> str:
151
+ return os.path.join(*parts)
152
+
153
+ def isfile(self, path: str) -> bool:
154
+ return os.path.isfile(path)
155
+
156
+ def isdir(self, path: str) -> bool:
157
+ return os.path.isdir(path)
158
+
159
+ def isexists(self, path: str) -> bool:
160
+ return os.path.exists(path)
161
+
162
+ def istype(self, path: str) -> str | None:
163
+ funcs_js = {"file": os.path.isfile, "dir": os.path.isdir, "exists": os.path.exists}
164
+ for key, func in funcs_js.items():
165
+ if func(path):
166
+ return key
167
+ return None
168
+
169
+ def is_included(self, path, **kwargs):
170
+ include_js = {}
171
+ if self.get_type:
172
+ include_js["typ"] = self.istype(path)
173
+ if self.get_is_dir:
174
+ include_js["dir"] = self.isdir(path)
175
+ if self.get_is_file:
176
+ include_js["file"] = self.isfile(path)
177
+ if self.get_is_exists:
178
+ include_js["exists"] = self.isexists(path)
179
+ return include_js
180
+ def glob_recursive(self, base: str, **opts) -> List[str]:
181
+ """
182
+ opts:
183
+ - maxdepth: int | None
184
+ - mindepth: int (default 1)
185
+ - follow_symlinks: bool
186
+ - include_dirs: bool
187
+ - include_files: bool
188
+ - exclude_hidden: bool
189
+ """
190
+ maxdepth = opts.get("maxdepth")
191
+ mindepth = opts.get("mindepth", 1)
192
+ follow = opts.get("follow_symlinks", False)
193
+ want_d = opts.get("include_dirs", True)
194
+ want_f = opts.get("include_files", True)
195
+ hide = opts.get("exclude_hidden", False)
196
+
197
+ results: List[str] = []
198
+ base_depth = os.path.normpath(base).count(os.sep)
199
+
200
+ for root, dirs, files in os.walk(base, followlinks=follow):
201
+ depth = os.path.normpath(root).count(os.sep) - base_depth
202
+ if maxdepth is not None and depth > maxdepth:
203
+ dirs[:] = []
204
+ continue
205
+ if want_d and depth >= mindepth:
206
+ for d in dirs:
207
+ if hide and d.startswith("."): continue
208
+ results.append(os.path.join(root, d))
209
+ if want_f and depth >= mindepth:
210
+ for f in files:
211
+ if hide and f.startswith("."): continue
212
+ results.append(os.path.join(root, f))
213
+ return results
214
+
215
+ def listdir(self, base: str) -> List[str]:
216
+ try:
217
+ return [os.path.join(base, name) for name in os.listdir(base)]
218
+ except Exception:
219
+ return []
220
+ def get_spec_kwargs(
221
+ user_at_host=None,
222
+ password=None,
223
+ key=None,
224
+ env_path=None,
225
+ kwargs=None
226
+ ):
227
+ kwargs = kwargs or {}
228
+ kwargs["user_at_host"] = kwargs.get("user_at_host") or user_at_host
229
+ kwargs["password"] = kwargs.get("password") or password
230
+ kwargs["key"] = kwargs.get("key") or key
231
+ kwargs["env_path"] = kwargs.get("env_path") or env_path
232
+ return kwargs
233
+ class SSHFS:
234
+ """Remote POSIX backend via run_remote_cmd."""
235
+ def __init__(self, password=None, key=None, env_path=None,
236
+ get_type=False, get_is_dir=False, get_is_file=False, get_is_exists=False, **kwargs):
237
+ self.user_at_host = kwargs.get('user_at_host') or kwargs.get('user') or kwargs.get('host')
238
+ self.password = password
239
+ self.key = key
240
+ self.env_path = env_path
241
+ self.get_type = get_type
242
+ self.get_is_dir = get_is_dir
243
+ self.get_is_file = get_is_file
244
+ self.get_is_exists = get_is_exists
245
+
246
+ def cell_spec_kwargs(self, func, path, **kwargs):
247
+ kwargs = get_spec_kwargs(
248
+ user_at_host=self.user_at_host,
249
+ password=self.password,
250
+ key=self.key,
251
+ env_path=self.env_path,
252
+ kwargs=kwargs
253
+ )
254
+ return func(path, **kwargs)
255
+
256
+ def is_included(self, path, **kwargs):
257
+ include_js = {}
258
+ if self.get_type:
259
+ include_js["typ"] = self.istype(path, **kwargs)
260
+ if self.get_is_dir:
261
+ include_js["dir"] = self.isdir(path, **kwargs)
262
+ if self.get_is_file:
263
+ include_js["file"] = self.isfile(path, **kwargs)
264
+ if self.get_is_exists:
265
+ include_js["exists"] = self.isexists(path, **kwargs)
266
+ return include_js
267
+
268
+ def join(self, *parts: str) -> str:
269
+ return posixpath.join(*parts)
270
+
271
+ def isfile(self, path: str, **kwargs) -> bool:
272
+ out = self.cell_spec_kwargs(is_file, path, **kwargs)
273
+ return "__OK__" in (out or "")
274
+
275
+ def isdir(self, path: str, **kwargs) -> bool:
276
+ out = self.cell_spec_kwargs(is_dir, path, **kwargs)
277
+ return "__OK__" in (out or "")
278
+
279
+ def isexists(self, path: str, **kwargs) -> bool:
280
+ out = self.cell_spec_kwargs(is_exists, path, **kwargs)
281
+ return "__OK__" in (out or "")
282
+
283
+ def istype(self, path: str, **kwargs) -> str | None:
284
+ out = self.cell_spec_kwargs(is_any, path, **kwargs)
285
+ return out
286
+
287
+ def glob_recursive(self, base: str, **opts) -> List[str]:
288
+ maxdepth = opts.get("maxdepth")
289
+ mindepth = opts.get("mindepth", 1)
290
+ follow = opts.get("follow_symlinks", False)
291
+ want_d = opts.get("include_dirs", True)
292
+ want_f = opts.get("include_files", True)
293
+ hide = opts.get("exclude_hidden", False)
294
+
295
+ parts = []
296
+ if follow:
297
+ parts.append("-L")
298
+ parts += ["find", shlex.quote(base)]
299
+ if mindepth is not None:
300
+ parts += ["-mindepth", str(mindepth)]
301
+ if maxdepth is not None:
302
+ parts += ["-maxdepth", str(maxdepth)]
303
+
304
+ type_filters = []
305
+ if want_d and not want_f:
306
+ type_filters = ["-type", "d"]
307
+ elif want_f and not want_d:
308
+ type_filters = ["-type", "f"]
309
+
310
+ hidden_filter = []
311
+ if hide:
312
+ hidden_filter = ["!", "-regex", r".*/\..*"]
313
+
314
+ cmd = " ".join(parts + type_filters + hidden_filter + ["-printf", r"'%p\n'"]) + " 2>/dev/null"
315
+ out = run_remote_cmd(self.user_at_host, cmd)
316
+ return [line.strip().strip("'") for line in (out or "").splitlines() if line.strip()]
317
+
318
+ def listdir(self, base: str) -> List[str]:
319
+ cmd = f"find {shlex.quote(base)} -maxdepth 1 -mindepth 1 -printf '%p\\n' 2>/dev/null"
320
+ out = run_remote_cmd(self.user_at_host, cmd)
321
+ return [line.strip() for line in (out or "").splitlines() if line.strip()]
322
+
323
+
324
+
325
+ def try_group(pre,item,strings):
326
+
327
+ try:
328
+ m = pre.match(item)
329
+ for i,string in enumerate(strings):
330
+ strings[i] = m.group(string)
331
+
332
+ except:
333
+ return None
334
+ return strings
335
+ def normalize_items(
336
+ paths: Iterable[str],
337
+ user_at_host=None,
338
+ get_type=True,
339
+ get_is_dir=False,
340
+ get_is_file=False,
341
+ get_is_exists=False,
342
+ **kwargs
343
+ ) -> List[tuple[PathBackend, str, dict]]:
344
+ pairs: List[tuple[PathBackend, str, dict]] = []
345
+ host = user_at_host or kwargs.get("host") or kwargs.get("user")
346
+ paths = make_list(paths)
347
+ for item in paths:
348
+ if not item:
349
+ continue
350
+
351
+ strings = try_group(REMOTE_RE, item, ["host", "path"])
352
+ fs_host = None
353
+ nuhost = None
354
+
355
+ if (strings and None not in strings) or host:
356
+ if strings and None not in strings:
357
+ nuhost = strings[0]
358
+ item = strings[1] or item
359
+ nuhost = nuhost or host
360
+ fs_host = SSHFS(
361
+ nuhost,
362
+ user_at_host=user_at_host,
363
+ get_type=get_type,
364
+ get_is_dir=get_is_dir,
365
+ get_is_file=get_is_file,
366
+ get_is_exists=get_is_exists,
367
+ **kwargs
368
+ )
369
+ else:
370
+ fs_host = LocalFS(
371
+ get_type=get_type,
372
+ get_is_dir=get_is_dir,
373
+ get_is_file=get_is_file,
374
+ get_is_exists=get_is_exists
375
+ )
376
+
377
+ includes = fs_host.is_included(item)
378
+ pairs.append((fs_host, item, includes))
379
+ return pairs
380
+
381
+
@@ -0,0 +1,158 @@
1
+ from abstract_utilities import read_from_file, eatAll
2
+ import os, sys, re, inspect
3
+ from typing import *
4
+
5
+ # ============================================================
6
+ # Constants
7
+ # ============================================================
8
+ import_tag = 'import '
9
+ from_tag = 'from '
10
+
11
+ # ============================================================
12
+ # Helpers
13
+ # ============================================================
14
+ def get_caller_path(i=None):
15
+ i = i or 1
16
+ frame = inspect.stack()[i]
17
+ return os.path.abspath(frame.filename)
18
+
19
+ def make_list(obj: any) -> list:
20
+ if isinstance(obj, str) and ',' in obj:
21
+ obj = obj.split(',')
22
+ if isinstance(obj, (set, tuple)):
23
+ return list(obj)
24
+ if isinstance(obj, list):
25
+ return obj
26
+ return [obj]
27
+
28
+ def eatElse(stringObj, chars=None):
29
+ chars = make_list(chars or []) + list('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_')
30
+ while stringObj:
31
+ if stringObj and stringObj[0] not in chars:
32
+ stringObj = stringObj[1:]
33
+ continue
34
+ if stringObj and stringObj[-1] not in chars:
35
+ stringObj = stringObj[:-1]
36
+ continue
37
+ break
38
+ return stringObj
39
+
40
+ def clean_line(line):
41
+ return eatAll(line, [' ', '', '\t', '\n'])
42
+
43
+ def is_line_import(line):
44
+ return bool(line and line.startswith(import_tag) and 'from ' not in line)
45
+
46
+ def is_line_from_import(line):
47
+ return bool(line and line.startswith(from_tag) and ' import ' in line)
48
+
49
+ def is_from_group_start(line):
50
+ return bool(line and line.startswith(from_tag) and 'import' in line and '(' in line and not line.rstrip().endswith(')'))
51
+
52
+ def is_from_group_end(line):
53
+ return bool(line and ')' in line)
54
+
55
+ def clean_imports(imports):
56
+ if isinstance(imports, str):
57
+ imports = imports.split(',')
58
+ return [eatElse(imp.strip()) for imp in imports if imp.strip()]
59
+
60
+ # ============================================================
61
+ # Combine lone import statements
62
+ # ============================================================
63
+ def combine_lone_imports(text=None, file_path=None):
64
+ text = text or ''
65
+ if file_path and os.path.isfile(file_path):
66
+ text += read_from_file(file_path)
67
+ lines = text.split('\n')
68
+
69
+ cleaned_import_list = []
70
+ nu_lines = []
71
+ j = None
72
+
73
+ for i, line in enumerate(lines):
74
+ if is_line_import(line):
75
+ if j is None:
76
+ nu_lines.append(import_tag)
77
+ j = i
78
+ cleaned_import_list += clean_imports(line.split(import_tag)[1])
79
+ else:
80
+ nu_lines.append(line)
81
+
82
+ if j is None:
83
+ return '\n'.join(nu_lines)
84
+ cleaned_import_list = sorted(set(cleaned_import_list))
85
+ nu_lines[j] += ', '.join(cleaned_import_list)
86
+ return '\n'.join(nu_lines)
87
+
88
+ # ============================================================
89
+ # Merge repeated 'from pkg import ...' (1-line only)
90
+ # Preserve multi-line grouped imports
91
+ # ============================================================
92
+ def merge_from_import_groups(text=None, file_path=None):
93
+ if file_path and os.path.isfile(file_path):
94
+ text = read_from_file(file_path)
95
+ text = text or ''
96
+ lines = text.split('\n')
97
+
98
+ pkg_to_imports: Dict[str, Set[str]] = {}
99
+ pkg_to_line_index: Dict[str, int] = {}
100
+ nu_lines: List[str] = []
101
+
102
+ in_group = False
103
+ for i, line in enumerate(lines):
104
+ stripped = line.strip()
105
+
106
+ # preserve multi-line grouped blocks intact
107
+ if in_group:
108
+ nu_lines.append(line)
109
+ if is_from_group_end(line):
110
+ in_group = False
111
+ continue
112
+
113
+ if is_from_group_start(line):
114
+ in_group = True
115
+ nu_lines.append(line)
116
+ continue
117
+
118
+ if is_line_from_import(line):
119
+ try:
120
+ pkg_part, imps_part = line.split(' import ', 1)
121
+ pkg_name = pkg_part.replace('from ', '').strip()
122
+ imps = clean_imports(imps_part)
123
+ except Exception:
124
+ nu_lines.append(line)
125
+ continue
126
+
127
+ if pkg_name not in pkg_to_imports:
128
+ pkg_to_imports[pkg_name] = set(imps)
129
+ pkg_to_line_index[pkg_name] = len(nu_lines)
130
+ nu_lines.append(line)
131
+ else:
132
+ pkg_to_imports[pkg_name].update(imps)
133
+ else:
134
+ nu_lines.append(line)
135
+
136
+ # Rewrite first occurrences
137
+ for pkg, idx in pkg_to_line_index.items():
138
+ all_imps = sorted(pkg_to_imports[pkg])
139
+ nu_lines[idx] = f"from {pkg} import {', '.join(all_imps)}"
140
+
141
+ return '\n'.join(nu_lines)
142
+
143
+ # ============================================================
144
+ # Pipeline
145
+ # ============================================================
146
+ def clean_imports_pipeline(path: str):
147
+ raw = read_from_file(path)
148
+ step1 = combine_lone_imports(text=raw)
149
+ step2 = merge_from_import_groups(text=step1)
150
+ return step2
151
+
152
+ # ============================================================
153
+ # Standalone Run
154
+ # ============================================================
155
+ if __name__ == "__main__":
156
+ abs_path = "/home/flerb/Documents/pythonTools/modules/src/modules/abstract_utilities/src/abstract_utilities/file_utils/imports/imports.py"
157
+ cleaned = clean_imports_pipeline(abs_path)
158
+ print(cleaned)