abstract-utilities 0.2.2.449__py3-none-any.whl → 0.2.2.476__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.
- abstract_utilities/__init__.py +18 -44
- abstract_utilities/abstract_classes.py +49 -0
- abstract_utilities/class_utils.py +38 -3
- abstract_utilities/cmd_utils/imports/__init__.py +1 -0
- abstract_utilities/cmd_utils/imports/imports.py +10 -0
- abstract_utilities/cmd_utils/pexpect_utils.py +310 -0
- abstract_utilities/cmd_utils/user_utils.py +1 -1
- abstract_utilities/compare_utils/__init__.py +3 -0
- abstract_utilities/compare_utils/best_match.py +150 -0
- abstract_utilities/{compare_utils.py → compare_utils/compare_utils.py} +1 -1
- abstract_utilities/compare_utils/find_value.py +105 -0
- abstract_utilities/dynimport.py +7 -15
- abstract_utilities/env_utils/__init__.py +3 -0
- abstract_utilities/env_utils/abstractEnv.py +129 -0
- abstract_utilities/env_utils/envy_it.py +33 -0
- abstract_utilities/env_utils/imports/__init__.py +2 -0
- abstract_utilities/env_utils/imports/imports.py +8 -0
- abstract_utilities/env_utils/imports/utils.py +122 -0
- abstract_utilities/file_utils/__init__.py +3 -0
- abstract_utilities/file_utils/file_utils/__init__.py +8 -0
- abstract_utilities/file_utils/file_utils/file_filters.py +104 -0
- abstract_utilities/{robust_reader → file_utils/file_utils}/file_reader.py +5 -19
- abstract_utilities/{robust_readers/file_filters.py → file_utils/file_utils/file_utils.py} +5 -4
- abstract_utilities/{robust_readers → file_utils/file_utils}/filter_params.py +1 -38
- abstract_utilities/file_utils/file_utils/find_collect.py +154 -0
- abstract_utilities/file_utils/file_utils/imports/__init__.py +3 -0
- abstract_utilities/file_utils/file_utils/imports/constants.py +39 -0
- abstract_utilities/file_utils/file_utils/imports/file_functions.py +10 -0
- abstract_utilities/file_utils/file_utils/imports/imports.py +39 -0
- abstract_utilities/file_utils/file_utils/imports/module_imports.py +13 -0
- abstract_utilities/file_utils/file_utils/imports.py +10 -0
- abstract_utilities/file_utils/file_utils/map_utils.py +29 -0
- abstract_utilities/{robust_reader → file_utils/file_utils}/pdf_utils.py +1 -9
- abstract_utilities/file_utils/file_utils/type_checks.py +82 -0
- abstract_utilities/file_utils/imports/__init__.py +4 -0
- abstract_utilities/file_utils/imports/classes.py +381 -0
- abstract_utilities/file_utils/imports/clean_imps.py +158 -0
- abstract_utilities/file_utils/imports/constants.py +39 -0
- abstract_utilities/file_utils/imports/file_functions.py +10 -0
- abstract_utilities/file_utils/imports/imports.py +65 -0
- abstract_utilities/file_utils/imports/module_imports.py +13 -0
- abstract_utilities/file_utils/req.py +329 -0
- abstract_utilities/json_utils.py +35 -0
- abstract_utilities/log_utils.py +14 -3
- abstract_utilities/path_utils.py +90 -6
- abstract_utilities/read_write_utils.py +176 -154
- abstract_utilities/robust_reader/__init__.py +1 -1
- abstract_utilities/robust_reader/imports/__init__.py +1 -0
- abstract_utilities/robust_reader/imports/imports.py +3 -0
- abstract_utilities/robust_readers/__init__.py +0 -1
- abstract_utilities/robust_readers/import_utils/__init__.py +1 -0
- abstract_utilities/robust_readers/import_utils/clean_imports.py +175 -0
- abstract_utilities/robust_readers/imports.py +8 -0
- abstract_utilities/robust_readers/initFuncGen.py +92 -76
- abstract_utilities/safe_utils.py +133 -0
- abstract_utilities/ssh_utils/__init__.py +3 -0
- abstract_utilities/ssh_utils/classes.py +127 -0
- abstract_utilities/ssh_utils/imports.py +10 -0
- abstract_utilities/ssh_utils/pexpect_utils.py +315 -0
- abstract_utilities/ssh_utils/utils.py +188 -0
- abstract_utilities/string_clean.py +40 -1
- abstract_utilities/string_utils.py +48 -0
- abstract_utilities/type_utils.py +25 -2
- {abstract_utilities-0.2.2.449.dist-info → abstract_utilities-0.2.2.476.dist-info}/METADATA +15 -4
- abstract_utilities-0.2.2.476.dist-info/RECORD +92 -0
- {abstract_utilities-0.2.2.449.dist-info → abstract_utilities-0.2.2.476.dist-info}/WHEEL +1 -1
- abstract_utilities-0.2.2.449.dist-info/RECORD +0 -49
- {abstract_utilities-0.2.2.449.dist-info → abstract_utilities-0.2.2.476.dist-info}/top_level.txt +0 -0
|
@@ -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)
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
from .imports import *
|
|
2
|
+
from .module_imports import *
|
|
3
|
+
@dataclass
|
|
4
|
+
class ScanConfig:
|
|
5
|
+
allowed_exts: Set[str]
|
|
6
|
+
unallowed_exts: Set[str]
|
|
7
|
+
exclude_types: Set[str]
|
|
8
|
+
exclude_dirs: List[str] = field(default_factory=list)
|
|
9
|
+
exclude_patterns: List[str] = field(default_factory=list)
|
|
10
|
+
DEFAULT_ALLOWED_EXTS: Set[str] = {
|
|
11
|
+
".py", ".pyw", # python
|
|
12
|
+
".js", ".jsx", ".ts", ".tsx", ".mjs", # JS/TS
|
|
13
|
+
".html", ".htm", ".xml", # markup
|
|
14
|
+
".css", ".scss", ".sass", ".less", # styles
|
|
15
|
+
".json", ".yaml", ".yml", ".toml", ".ini", # configs
|
|
16
|
+
".cfg", ".md", ".markdown", ".rst", # docs
|
|
17
|
+
".sh", ".bash", ".env", # scripts/env
|
|
18
|
+
".txt" # plain text
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
DEFAULT_EXCLUDE_TYPES: Set[str] = {
|
|
22
|
+
"image", "video", "audio", "presentation",
|
|
23
|
+
"spreadsheet", "archive", "executable"
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
# never want these—even if they sneak into ALLOWED
|
|
27
|
+
_unallowed = set(get_media_exts(DEFAULT_EXCLUDE_TYPES)) | {'.bak', '.shp', '.cpg', '.dbf', '.shx','.geojson',".pyc",'.shx','.geojson','.prj','.sbn','.sbx'}
|
|
28
|
+
DEFAULT_UNALLOWED_EXTS = {e for e in _unallowed if e not in DEFAULT_ALLOWED_EXTS}
|
|
29
|
+
|
|
30
|
+
DEFAULT_EXCLUDE_DIRS: Set[str] = {
|
|
31
|
+
"node_modules", "old","__pycache__", "backups", "backup", "backs", "trash", "depriciated", "old", "__init__"
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
DEFAULT_EXCLUDE_PATTERNS: Set[str] = {
|
|
35
|
+
"__init__*", "*.tmp", "*.log", "*.lock", "*.zip","*~"
|
|
36
|
+
}
|
|
37
|
+
REMOTE_RE = re.compile(r"^(?P<host>[^:\s]+@[^:\s]+):(?P<path>/.*)$")
|
|
38
|
+
AllowedPredicate = Optional[Callable[[str], bool]]
|
|
39
|
+
DEFAULT_EXCLUDE_FILE_PATTERNS=DEFAULT_EXCLUDE_PATTERNS
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
from .imports import *
|
|
2
|
+
def get_caller_path(i=None):
|
|
3
|
+
i = i or 1
|
|
4
|
+
frame = inspect.stack()[i]
|
|
5
|
+
return os.path.abspath(frame.filename)
|
|
6
|
+
def get_caller_dir(i=None):
|
|
7
|
+
i = i or 1
|
|
8
|
+
frame = inspect.stack()[i]
|
|
9
|
+
abspath = os.path.abspath(frame.filename)
|
|
10
|
+
return os.path.dirname(abspath)
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# ============================================================
|
|
2
|
+
# abstract_utilities/imports/imports.py
|
|
3
|
+
# Global imports hub — everything imported here will be
|
|
4
|
+
# automatically available to any module that does:
|
|
5
|
+
# from ..imports import *
|
|
6
|
+
# ============================================================
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
import os
|
|
10
|
+
import sys, importlib,os
|
|
11
|
+
import sys, importlib, os, inspect
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
import os,sys
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
from typing import *
|
|
18
|
+
import re
|
|
19
|
+
|
|
20
|
+
from typing import *
|
|
21
|
+
from types import MethodType
|
|
22
|
+
import os,re, sys, importlib, inspect, os, importlib.util, hashlib
|
|
23
|
+
import os,tempfile,shutil,logging,ezodf,fnmatch,pytesseract,pdfplumber
|
|
24
|
+
import pandas as pd
|
|
25
|
+
import geopandas as gpd
|
|
26
|
+
from datetime import datetime
|
|
27
|
+
|
|
28
|
+
from typing import *
|
|
29
|
+
from werkzeug.utils import secure_filename
|
|
30
|
+
from werkzeug.datastructures import FileStorage
|
|
31
|
+
from pdf2image import convert_from_path # only used for OCR fallback
|
|
32
|
+
# ---- Core standard library modules -------------------------
|
|
33
|
+
import os, sys, re, shlex, glob, platform, textwrap, subprocess, inspect, json, time
|
|
34
|
+
import tempfile, shutil, logging, pathlib, fnmatch, importlib, importlib.util, types
|
|
35
|
+
|
|
36
|
+
from datetime import datetime
|
|
37
|
+
from types import ModuleType
|
|
38
|
+
|
|
39
|
+
# ---- Dataclasses and typing --------------------------------
|
|
40
|
+
from dataclasses import dataclass, field
|
|
41
|
+
from typing import (
|
|
42
|
+
Any, Optional, List, Dict, Set, Tuple,
|
|
43
|
+
Iterable, Callable, Literal, Union, TypeVar
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
# ---- Common 3rd-party dependencies --------------------------
|
|
47
|
+
import pandas as pd
|
|
48
|
+
import geopandas as gpd
|
|
49
|
+
import pytesseract
|
|
50
|
+
import pdfplumber
|
|
51
|
+
import PyPDF2
|
|
52
|
+
import ezodf
|
|
53
|
+
from pdf2image import convert_from_path
|
|
54
|
+
from werkzeug.utils import secure_filename
|
|
55
|
+
from werkzeug.datastructures import FileStorage
|
|
56
|
+
|
|
57
|
+
# ---- Helpers ------------------------------------------------
|
|
58
|
+
import textwrap as tw
|
|
59
|
+
from pprint import pprint
|
|
60
|
+
|
|
61
|
+
# ============================================================
|
|
62
|
+
# AUTO-EXPORT ALL NON-PRIVATE NAMES
|
|
63
|
+
# ============================================================
|
|
64
|
+
__all__ = [name for name in globals() if not name.startswith("_")]
|
|
65
|
+
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
from .imports import *
|
|
2
|
+
from ...string_clean import eatAll
|
|
3
|
+
from ...list_utils import make_list
|
|
4
|
+
from ...type_utils import get_media_exts, is_media_type, MIME_TYPES, is_str
|
|
5
|
+
from ...ssh_utils import *
|
|
6
|
+
from ...env_utils import *
|
|
7
|
+
from ...read_write_utils import *
|
|
8
|
+
from ...abstract_classes import SingletonMeta
|
|
9
|
+
from ...log_utils import get_logFile
|
|
10
|
+
from ...class_utils import get_caller, get_caller_path, get_caller_dir
|
|
11
|
+
from ...ssh_utils import run_cmd
|
|
12
|
+
|
|
13
|
+
__all__ = [name for name in globals() if not name.startswith("_")]
|