abstract-utilities 0.2.2.504__py3-none-any.whl → 0.2.2.510__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.
- abstract_utilities/__init__.py +1 -0
- abstract_utilities/circular_import_finder.py +222 -0
- abstract_utilities/circular_import_finder2.py +118 -0
- abstract_utilities/directory_utils/__init__.py +4 -0
- abstract_utilities/directory_utils/directory_utils.py +94 -0
- abstract_utilities/directory_utils/name_utils.py +43 -0
- abstract_utilities/directory_utils/size_utils.py +57 -0
- abstract_utilities/directory_utils/utils.py +116 -0
- abstract_utilities/env_utils/imports/imports.py +2 -1
- abstract_utilities/file_utils/imports/module_imports.py +1 -2
- abstract_utilities/file_utils/src/__init__.py +0 -1
- abstract_utilities/file_utils/src/filter_params.py +6 -6
- abstract_utilities/import_utils/src/clean_imports.py +0 -1
- abstract_utilities/json_utils/imports/module_imports.py +1 -1
- abstract_utilities/json_utils/json_utils.py +1 -1
- abstract_utilities/log_utils/imports/module_imports.py +1 -1
- abstract_utilities/log_utils/log_file.py +15 -13
- abstract_utilities/path_utils/imports/__init__.py +1 -0
- abstract_utilities/path_utils/imports/module_imports.py +3 -2
- abstract_utilities/path_utils/path_utils.py +1 -465
- abstract_utilities/read_write_utils/imports/module_imports.py +2 -2
- abstract_utilities/safe_utils/imports/imports.py +1 -0
- abstract_utilities/safe_utils/safe_utils.py +6 -0
- abstract_utilities/ssh_utils/__init__.py +1 -0
- abstract_utilities/ssh_utils/imports/module_imports.py +4 -3
- abstract_utilities/ssh_utils/type_checks.py +92 -0
- abstract_utilities/string_utils/replace_utils.py +1 -1
- {abstract_utilities-0.2.2.504.dist-info → abstract_utilities-0.2.2.510.dist-info}/METADATA +1 -1
- {abstract_utilities-0.2.2.504.dist-info → abstract_utilities-0.2.2.510.dist-info}/RECORD +31 -23
- {abstract_utilities-0.2.2.504.dist-info → abstract_utilities-0.2.2.510.dist-info}/top_level.txt +0 -1
- {abstract_utilities-0.2.2.504.dist-info → abstract_utilities-0.2.2.510.dist-info}/WHEEL +0 -0
abstract_utilities/__init__.py
CHANGED
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
from abstract_utilities import *
|
|
2
|
+
from collections import defaultdict
|
|
3
|
+
def clean_line(line):
|
|
4
|
+
return eatAll(line,[' ','','\t','\n'])
|
|
5
|
+
def is_from_line_group(line):
|
|
6
|
+
if line and line.startswith(FROM_TAG) and IMPORT_TAG in line and '(' in line:
|
|
7
|
+
import_spl = line.split(IMPORT_TAG)[-1]
|
|
8
|
+
import_spl_clean = clean_line(line)
|
|
9
|
+
if not import_spl_clean.endswith(')'):
|
|
10
|
+
return True
|
|
11
|
+
return False
|
|
12
|
+
def clean_imports(text=None,file_path=None,import_pkg_js=None,fill_nulines=False):
|
|
13
|
+
if text and os.path.isfile(text):
|
|
14
|
+
file_path = text
|
|
15
|
+
input(file_path)
|
|
16
|
+
text = read_from_file(file_path)
|
|
17
|
+
if not import_pkg_js:
|
|
18
|
+
import_pkg_js = get_all_imports(text=text,file_path=file_path)
|
|
19
|
+
import_pkg_js = ensure_import_pkg_js(import_pkg_js,file_path=file_path)
|
|
20
|
+
nu_lines = import_pkg_js["context"]["nulines"]
|
|
21
|
+
for pkg,values in import_pkg_js.items():
|
|
22
|
+
comments = []
|
|
23
|
+
if pkg not in ["context"]:
|
|
24
|
+
|
|
25
|
+
imports = values.get('imports')
|
|
26
|
+
for i,imp in enumerate(imports):
|
|
27
|
+
if '#' in imp:
|
|
28
|
+
imp_spl = imp.split('#')
|
|
29
|
+
comments.append(imp_spl[-1])
|
|
30
|
+
imports[i] = clean_line(imp_spl[0])
|
|
31
|
+
imports = list(set(imports))
|
|
32
|
+
if '*' in imports:
|
|
33
|
+
imports="*"
|
|
34
|
+
else:
|
|
35
|
+
imports=','.join(imports)
|
|
36
|
+
if comments:
|
|
37
|
+
comments=','.join(comments)
|
|
38
|
+
imports+=f" #{comments}"
|
|
39
|
+
import_pkg_js[pkg]["imports"]=imports
|
|
40
|
+
if fill_nulines:
|
|
41
|
+
line = values.get('line')
|
|
42
|
+
if len(nu_lines) >= line:
|
|
43
|
+
nu_lines[line] += imports
|
|
44
|
+
return import_pkg_js
|
|
45
|
+
def get_all_imports(text=None,file_path=None,import_pkg_js=None):
|
|
46
|
+
if text and os.path.isfile(text):
|
|
47
|
+
|
|
48
|
+
try:
|
|
49
|
+
text = read_from_file(text)
|
|
50
|
+
except:
|
|
51
|
+
pass
|
|
52
|
+
file_path = text
|
|
53
|
+
text = get_text_or_read(text=text,file_path=file_path)
|
|
54
|
+
lines = text.split('\n')
|
|
55
|
+
cleaned_import_list=[]
|
|
56
|
+
nu_lines = []
|
|
57
|
+
is_from_group = False
|
|
58
|
+
import_pkg_js = ensure_import_pkg_js(import_pkg_js,file_path=file_path)
|
|
59
|
+
for line in lines:
|
|
60
|
+
if line.startswith(IMPORT_TAG) and ' from ' not in line:
|
|
61
|
+
cleaned_import_list = get_cleaned_import_list(line)
|
|
62
|
+
import_pkg_js = add_imports_to_import_pkg_js("import",cleaned_import_list,import_pkg_js=import_pkg_js)
|
|
63
|
+
else:
|
|
64
|
+
if is_from_group:
|
|
65
|
+
import_pkg=is_from_group
|
|
66
|
+
line = clean_line(line)
|
|
67
|
+
if line.endswith(')'):
|
|
68
|
+
is_from_group=False
|
|
69
|
+
line=line[:-1]
|
|
70
|
+
imports_from_import_pkg = clean_imports(line)
|
|
71
|
+
import_pkg_js = add_imports_to_import_pkg_js(import_pkg,imports_from_import_pkg,import_pkg_js=import_pkg_js)
|
|
72
|
+
|
|
73
|
+
else:
|
|
74
|
+
import_pkg_js=update_import_pkg_js(line,import_pkg_js=import_pkg_js)
|
|
75
|
+
if is_from_line_group(line) and is_from_group == False:
|
|
76
|
+
is_from_group=get_import_pkg(line)
|
|
77
|
+
return import_pkg_js
|
|
78
|
+
def get_path_or_init(pkg_info):
|
|
79
|
+
root_dirname = pkg_info.get("root_dirname")
|
|
80
|
+
pkg = pkg_info.get("pkg")
|
|
81
|
+
rel_path = pkg.replace('.','/')
|
|
82
|
+
dirname = os.path.dirname(root_dirname)
|
|
83
|
+
pkg_path = os.path.join(dirname,rel_path)
|
|
84
|
+
pkg_py_path = f"{pkg_path}.py"
|
|
85
|
+
if os.path.isfile(pkg_py_path):
|
|
86
|
+
return pkg_py_path
|
|
87
|
+
pkg_init_path = os.path.join(pkg_path,'__init__.py')
|
|
88
|
+
if os.path.isdir(pkg_path):
|
|
89
|
+
if os.path.isfile(pkg_init_path):
|
|
90
|
+
return pkg_init_path
|
|
91
|
+
#input(f"nnot found == {pkg_info}")
|
|
92
|
+
def get_dot_fro_line(line,dirname=None,file_path=None,get_info=False):
|
|
93
|
+
info_js = {"nuline":line,"og_line":line,"pkg":line,"dirname":dirname,"file_path":file_path,"root_dirname":None,"local":False}
|
|
94
|
+
if dirname and is_file(dirname):
|
|
95
|
+
file_path=dirname
|
|
96
|
+
dirname = os.path.dirname(dirname)
|
|
97
|
+
info_js["file_path"]=file_path
|
|
98
|
+
info_js["dirname"]=dirname
|
|
99
|
+
from_line = line.split(FROM_TAG)[-1]
|
|
100
|
+
dot_fro = ""
|
|
101
|
+
for char in from_line:
|
|
102
|
+
if char != '.':
|
|
103
|
+
pkg = f"{dot_fro}{eatAll(from_line,'.')}"
|
|
104
|
+
nuline=f"from {pkg}"
|
|
105
|
+
info_js["nuline"]=nuline
|
|
106
|
+
info_js["pkg"]=pkg
|
|
107
|
+
break
|
|
108
|
+
if dirname:
|
|
109
|
+
info_js["root_dirname"]=dirname
|
|
110
|
+
dirbase = os.path.basename(dirname)
|
|
111
|
+
dirname = os.path.dirname(dirname)
|
|
112
|
+
|
|
113
|
+
dot_fro = f"{dirbase}.{dot_fro}"
|
|
114
|
+
if get_info:
|
|
115
|
+
if dot_fro and os.path.isdir(info_js["root_dirname"]):
|
|
116
|
+
info_js["local"]=True
|
|
117
|
+
info_js["pkg_path"]=get_path_or_init(info_js)
|
|
118
|
+
return info_js
|
|
119
|
+
return line
|
|
120
|
+
def get_top_level_imp(line,dirname=None):
|
|
121
|
+
imp = get_dot_fro_line(line,dirname)
|
|
122
|
+
return imp.split('.')[0]
|
|
123
|
+
def return_local_imps(file_path):
|
|
124
|
+
local_imps = []
|
|
125
|
+
dirname = os.path.dirname(file_path)
|
|
126
|
+
imports_js = get_all_imports(file_path)
|
|
127
|
+
for pkg,imps in imports_js.items():
|
|
128
|
+
if pkg not in ['context','nulines']:
|
|
129
|
+
full_imp_info = get_dot_fro_line(pkg,dirname,file_path=file_path,get_info=True)
|
|
130
|
+
if full_imp_info.get("local") == True:
|
|
131
|
+
local_imps.append(full_imp_info)
|
|
132
|
+
return local_imps
|
|
133
|
+
def get_all_pkg_paths(file_path):
|
|
134
|
+
pkg_paths = []
|
|
135
|
+
local_imps = return_local_imps(file_path)
|
|
136
|
+
for local_imp in local_imps:
|
|
137
|
+
curr_file_path = local_imp.get('file_path')
|
|
138
|
+
pkg_path = local_imp.get('pkg_path')
|
|
139
|
+
if pkg_path != None:
|
|
140
|
+
pkg_paths.append(pkg_path)
|
|
141
|
+
return pkg_paths
|
|
142
|
+
def get_cir_dir(pkg_path):
|
|
143
|
+
dirname = os.path.dirname(pkg_path)
|
|
144
|
+
dirbase = os.path.basename(dirname)
|
|
145
|
+
while True:
|
|
146
|
+
if dirname == "/home/flerb/Documents/pythonTools/modules/src/modules/abstract_utilities/src/abstract_utilities":
|
|
147
|
+
break
|
|
148
|
+
dirbase = os.path.basename(dirname)
|
|
149
|
+
dirname = os.path.dirname(dirname)
|
|
150
|
+
#input(f"{dirbase} is circular")
|
|
151
|
+
return dirbase
|
|
152
|
+
def is_circular(pkg_path):
|
|
153
|
+
pkg_paths = get_all_pkg_paths(pkg_path)
|
|
154
|
+
if pkg_path in pkg_paths:
|
|
155
|
+
return pkg_path
|
|
156
|
+
def are_circular(pkg_path,cir_dirs = None):
|
|
157
|
+
cir_dirs = cir_dirs or []
|
|
158
|
+
pkg_path = is_circular(pkg_path)
|
|
159
|
+
if pkg_path:
|
|
160
|
+
if pkg_path not in cir_dirs:
|
|
161
|
+
cir_dirs.append(pkg_path)
|
|
162
|
+
return cir_dirs
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def build_dependency_graph(main_directory):
|
|
166
|
+
"""Map each file to all local imports (by resolved pkg_path)."""
|
|
167
|
+
graph = defaultdict(list)
|
|
168
|
+
dirs, all_local_scripts = get_files_and_dirs(
|
|
169
|
+
main_directory,
|
|
170
|
+
allowed_exts='.py',
|
|
171
|
+
exclude_dirs=['depriciate', 'junk'],
|
|
172
|
+
files_only=True
|
|
173
|
+
)
|
|
174
|
+
for file_path in all_local_scripts:
|
|
175
|
+
deps = get_all_pkg_paths(file_path)
|
|
176
|
+
for dep in deps:
|
|
177
|
+
if dep and os.path.isfile(dep):
|
|
178
|
+
graph[file_path].append(dep)
|
|
179
|
+
return graph
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
def find_circular_chains(graph):
|
|
183
|
+
"""Detect circular imports and return their full dependency paths."""
|
|
184
|
+
visited, cycles = set(), []
|
|
185
|
+
|
|
186
|
+
def dfs(node, path):
|
|
187
|
+
visited.add(node)
|
|
188
|
+
path.append(node)
|
|
189
|
+
for dep in graph.get(node, []):
|
|
190
|
+
if dep not in path:
|
|
191
|
+
dfs(dep, path.copy())
|
|
192
|
+
else:
|
|
193
|
+
# Found a circular import
|
|
194
|
+
cycle_start = path.index(dep)
|
|
195
|
+
cycle = path[cycle_start:] + [dep]
|
|
196
|
+
if cycle not in cycles:
|
|
197
|
+
cycles.append(cycle)
|
|
198
|
+
return
|
|
199
|
+
|
|
200
|
+
for start in graph:
|
|
201
|
+
dfs(start, [])
|
|
202
|
+
return cycles
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
def explain_circular_imports(cycles):
|
|
206
|
+
"""Pretty-print circular import chains with file names and import lines."""
|
|
207
|
+
for i, cycle in enumerate(cycles, 1):
|
|
208
|
+
print(f"\n🔁 Circular import {i}:")
|
|
209
|
+
for j in range(len(cycle) - 1):
|
|
210
|
+
src, dst = cycle[j], cycle[j + 1]
|
|
211
|
+
print(f" {os.path.basename(src)} → {os.path.basename(dst)}")
|
|
212
|
+
print(f" ^ back to {os.path.basename(cycle[0])}")
|
|
213
|
+
main_directory = "/home/flerb/Documents/pythonTools/modules/src/modules/abstract_utilities/src/abstract_utilities"
|
|
214
|
+
|
|
215
|
+
graph = build_dependency_graph(main_directory)
|
|
216
|
+
cycles = find_circular_chains(graph)
|
|
217
|
+
|
|
218
|
+
if not cycles:
|
|
219
|
+
print("✅ No circular imports found.")
|
|
220
|
+
else:
|
|
221
|
+
print(f"❌ Found {len(cycles)} circular import(s).")
|
|
222
|
+
explain_circular_imports(cycles)
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
from abstract_utilities import *
|
|
2
|
+
import os
|
|
3
|
+
from collections import defaultdict
|
|
4
|
+
|
|
5
|
+
def get_path_or_init(pkg_info):
|
|
6
|
+
root_dirname = pkg_info.get("root_dirname")
|
|
7
|
+
pkg = pkg_info.get("pkg")
|
|
8
|
+
rel_path = pkg.replace('.', '/')
|
|
9
|
+
dirname = os.path.dirname(root_dirname)
|
|
10
|
+
pkg_path = os.path.join(dirname, rel_path)
|
|
11
|
+
pkg_py_path = f"{pkg_path}.py"
|
|
12
|
+
if os.path.isfile(pkg_py_path):
|
|
13
|
+
return pkg_py_path
|
|
14
|
+
pkg_init_path = os.path.join(pkg_path, '__init__.py')
|
|
15
|
+
if os.path.isdir(pkg_path) and os.path.isfile(pkg_init_path):
|
|
16
|
+
return pkg_init_path
|
|
17
|
+
# optional: silence instead of blocking input()
|
|
18
|
+
print(f"⚠️ not found == {pkg_info}")
|
|
19
|
+
return None
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def get_dot_fro_line(line, dirname=None, file_path=None, get_info=False):
|
|
23
|
+
info_js = {"nuline": line, "og_line": line, "pkg": line, "dirname": dirname,
|
|
24
|
+
"file_path": file_path, "root_dirname": None, "local": False}
|
|
25
|
+
if dirname and is_file(dirname):
|
|
26
|
+
file_path = dirname
|
|
27
|
+
dirname = os.path.dirname(dirname)
|
|
28
|
+
info_js["file_path"] = file_path
|
|
29
|
+
info_js["dirname"] = dirname
|
|
30
|
+
|
|
31
|
+
from_line = line.split(FROM_TAG)[-1]
|
|
32
|
+
dot_fro = ""
|
|
33
|
+
for char in from_line:
|
|
34
|
+
if char != '.':
|
|
35
|
+
pkg = f"{dot_fro}{eatAll(from_line, '.')}"
|
|
36
|
+
nuline = f"from {pkg}"
|
|
37
|
+
info_js["nuline"] = nuline
|
|
38
|
+
info_js["pkg"] = pkg
|
|
39
|
+
break
|
|
40
|
+
if dirname:
|
|
41
|
+
info_js["root_dirname"] = dirname
|
|
42
|
+
dirbase = os.path.basename(dirname)
|
|
43
|
+
dirname = os.path.dirname(dirname)
|
|
44
|
+
dot_fro = f"{dirbase}.{dot_fro}"
|
|
45
|
+
|
|
46
|
+
if get_info:
|
|
47
|
+
if dot_fro and os.path.isdir(info_js.get("root_dirname") or ""):
|
|
48
|
+
info_js["local"] = True
|
|
49
|
+
info_js["pkg_path"] = get_path_or_init(info_js)
|
|
50
|
+
return info_js
|
|
51
|
+
return line
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def return_local_imps(file_path):
|
|
55
|
+
local_imps = []
|
|
56
|
+
dirname = os.path.dirname(file_path)
|
|
57
|
+
imports_js = get_all_imports(file_path)
|
|
58
|
+
for pkg, imps in imports_js.items():
|
|
59
|
+
if pkg not in ['context', 'nulines']:
|
|
60
|
+
full_imp_info = get_dot_fro_line(pkg, dirname, file_path=file_path, get_info=True)
|
|
61
|
+
if full_imp_info.get("local"):
|
|
62
|
+
local_imps.append(full_imp_info)
|
|
63
|
+
return local_imps
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def get_all_pkg_paths(file_path):
|
|
67
|
+
pkg_paths = []
|
|
68
|
+
local_imps = return_local_imps(file_path)
|
|
69
|
+
for local_imp in local_imps:
|
|
70
|
+
pkg_path = local_imp.get('pkg_path')
|
|
71
|
+
if pkg_path:
|
|
72
|
+
pkg_paths.append(pkg_path)
|
|
73
|
+
return pkg_paths
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
# --- NEW: Build dependency graph and detect circular imports ---
|
|
77
|
+
|
|
78
|
+
def build_graph(main_directory):
|
|
79
|
+
dirs, all_local_scripts = get_files_and_dirs(main_directory, allowd_exts='.py', files_only=True)
|
|
80
|
+
graph = defaultdict(set)
|
|
81
|
+
for file_path in all_local_scripts:
|
|
82
|
+
deps = get_all_pkg_paths(file_path)
|
|
83
|
+
for dep in deps:
|
|
84
|
+
if dep: # only valid files
|
|
85
|
+
graph[file_path].add(dep)
|
|
86
|
+
return graph
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def find_cycles(graph):
|
|
90
|
+
visited, stack, cycles = set(), [], []
|
|
91
|
+
|
|
92
|
+
def dfs(node, path):
|
|
93
|
+
visited.add(node)
|
|
94
|
+
path.append(node)
|
|
95
|
+
for dep in graph.get(node, []):
|
|
96
|
+
if dep not in visited:
|
|
97
|
+
dfs(dep, path.copy())
|
|
98
|
+
elif dep in path:
|
|
99
|
+
cycle_start = path.index(dep)
|
|
100
|
+
cycles.append(path[cycle_start:] + [dep])
|
|
101
|
+
|
|
102
|
+
for node in graph:
|
|
103
|
+
if node not in visited:
|
|
104
|
+
dfs(node, [])
|
|
105
|
+
return cycles
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
if __name__ == "__main__":
|
|
109
|
+
main_directory = "/home/flerb/Documents/pythonTools/modules/src/modules/abstract_utilities/src/abstract_utilities"
|
|
110
|
+
graph = build_graph(main_directory)
|
|
111
|
+
cycles = find_cycles(graph)
|
|
112
|
+
|
|
113
|
+
if not cycles:
|
|
114
|
+
print("✅ No circular imports found.")
|
|
115
|
+
else:
|
|
116
|
+
print("❌ Circular imports detected:")
|
|
117
|
+
for cycle in cycles:
|
|
118
|
+
print(" → ".join(cycle))
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
from ..imports import os,shlex
|
|
2
|
+
from ..safe_utils import safe_join
|
|
3
|
+
from .utils import *
|
|
4
|
+
def get_dirs(path):
|
|
5
|
+
"""
|
|
6
|
+
Get List of Immediate Subdirectories in a Path
|
|
7
|
+
|
|
8
|
+
This function uses the os.walk method to traverse through a directory tree and returns a list of immediate subdirectories
|
|
9
|
+
within the specified path.
|
|
10
|
+
|
|
11
|
+
Parameters:
|
|
12
|
+
- path (str): The path for which subdirectories need to be retrieved.
|
|
13
|
+
|
|
14
|
+
Returns:
|
|
15
|
+
- subdirectories (list): A list of immediate subdirectories within the specified path.
|
|
16
|
+
|
|
17
|
+
Example:
|
|
18
|
+
subdirs = get_dirs("/path/to/directory")
|
|
19
|
+
print("Immediate Subdirectories:", subdirs)
|
|
20
|
+
"""
|
|
21
|
+
from os import walk
|
|
22
|
+
for (dirpath, dirnames, filenames) in walk(path):
|
|
23
|
+
return dirnames
|
|
24
|
+
def get_directory(file_path: str) -> str:
|
|
25
|
+
"""
|
|
26
|
+
Extracts and returns the directory path from a given file path.
|
|
27
|
+
|
|
28
|
+
Args:
|
|
29
|
+
file_path (str): A string representing the file path.
|
|
30
|
+
|
|
31
|
+
Returns:
|
|
32
|
+
str: The directory path extracted from the file path.
|
|
33
|
+
"""
|
|
34
|
+
return file_path[:-len(get_base_name(file_path))]
|
|
35
|
+
def if_not_last_child_join(path:str,child:str):
|
|
36
|
+
"""
|
|
37
|
+
Adds a child path to the given path if it's not already present at the end.
|
|
38
|
+
|
|
39
|
+
Args:
|
|
40
|
+
path (str): The parent path.
|
|
41
|
+
child (str): The child path to add.
|
|
42
|
+
|
|
43
|
+
Returns:
|
|
44
|
+
str: The updated path.
|
|
45
|
+
"""
|
|
46
|
+
if path.endswith(child):
|
|
47
|
+
return path
|
|
48
|
+
return simple_path_join(path, child)
|
|
49
|
+
def createFolds(ls: list) -> None:
|
|
50
|
+
"""
|
|
51
|
+
Creates multiple directories.
|
|
52
|
+
|
|
53
|
+
Args:
|
|
54
|
+
ls (list): The list of directory paths to create.
|
|
55
|
+
"""
|
|
56
|
+
for k in range(len(ls)):
|
|
57
|
+
mkdirs(ls[k])
|
|
58
|
+
def list_directory_contents(path: str) -> list:
|
|
59
|
+
"""Returns a list of directory contents or a list with a single file, if the path is a file.
|
|
60
|
+
|
|
61
|
+
Args:
|
|
62
|
+
path (str): The path of the directory or file.
|
|
63
|
+
|
|
64
|
+
Returns:
|
|
65
|
+
list: A list of directory contents or a list with a single file path.
|
|
66
|
+
"""
|
|
67
|
+
if is_file(path):
|
|
68
|
+
return [path]
|
|
69
|
+
elif is_valid_path(path):
|
|
70
|
+
return os.listdir(path)
|
|
71
|
+
return [path]
|
|
72
|
+
def is_string_in_dir(path,strings):
|
|
73
|
+
dirname = path
|
|
74
|
+
if is_file(path):
|
|
75
|
+
dirname = os.path.dirname(path)
|
|
76
|
+
pieces = [pa for pa in dirname.split('/') if pa and pa in strings]
|
|
77
|
+
logger.info(f"pieces = {pieces}\nstrings == {strings}")
|
|
78
|
+
if pieces:
|
|
79
|
+
return True
|
|
80
|
+
return False
|
|
81
|
+
def raw_create_dirs(*paths):
|
|
82
|
+
"""Recursively create all directories along the given path."""
|
|
83
|
+
full_path = os.path.abspath(safe_join(*paths))
|
|
84
|
+
sub_parts = [p for p in full_path.split(os.sep) if p]
|
|
85
|
+
|
|
86
|
+
current_path = "/" if full_path.startswith(os.sep) else ""
|
|
87
|
+
for part in sub_parts:
|
|
88
|
+
current_path = safe_join(current_path, part)
|
|
89
|
+
os.makedirs(current_path, exist_ok=True)
|
|
90
|
+
return full_path
|
|
91
|
+
mkdirs=raw_create_dirs
|
|
92
|
+
makedirs = mkdirs
|
|
93
|
+
make_dirs = makedirs
|
|
94
|
+
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
from ..imports import *
|
|
2
|
+
from .directory_utils import *
|
|
3
|
+
def get_file_name(file_path: str) -> str:
|
|
4
|
+
"""
|
|
5
|
+
Retrieves and returns the base name of a file from a given file path.
|
|
6
|
+
|
|
7
|
+
Args:
|
|
8
|
+
file_path (str): A string representing the file path.
|
|
9
|
+
|
|
10
|
+
Returns:
|
|
11
|
+
str: The base name of the file (without extension).
|
|
12
|
+
"""
|
|
13
|
+
return split_text(get_base_name(file_path))[0]
|
|
14
|
+
def get_abs_name_of_this():
|
|
15
|
+
"""
|
|
16
|
+
Returns the absolute name of the current module.
|
|
17
|
+
|
|
18
|
+
Returns:
|
|
19
|
+
Path: The absolute name of the current module.
|
|
20
|
+
"""
|
|
21
|
+
return os.path.abspath(__name__)
|
|
22
|
+
def sanitize_filename(name: str):
|
|
23
|
+
"""
|
|
24
|
+
Sanitize a filename by removing invalid characters.
|
|
25
|
+
|
|
26
|
+
Args:
|
|
27
|
+
name (str): Filename to sanitize.
|
|
28
|
+
|
|
29
|
+
Returns:
|
|
30
|
+
str: Sanitized filename.
|
|
31
|
+
"""
|
|
32
|
+
return re.sub(r'[\\/*?:"<>|]', "", name)
|
|
33
|
+
def get_base_name(file_path: str) -> str:
|
|
34
|
+
"""
|
|
35
|
+
Extracts and returns the base name of a file from a given file path.
|
|
36
|
+
|
|
37
|
+
Args:
|
|
38
|
+
file_path (str): A string representing the file path.
|
|
39
|
+
|
|
40
|
+
Returns:
|
|
41
|
+
str: The base name of the file.
|
|
42
|
+
"""
|
|
43
|
+
return os.path.basename(file_path)
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
from ..imports import *
|
|
2
|
+
from .name_utils import *
|
|
3
|
+
def get_os_info():
|
|
4
|
+
"""
|
|
5
|
+
Get Operating System Information
|
|
6
|
+
|
|
7
|
+
This function retrieves information about the current operating system, including its name and bit size.
|
|
8
|
+
|
|
9
|
+
Returns:
|
|
10
|
+
- os_info (dict): A dictionary containing the operating system information.
|
|
11
|
+
Keys:
|
|
12
|
+
- "operating_system" (str): The name of the operating system (e.g., "Windows", "Linux", "Darwin").
|
|
13
|
+
- "bit_size" (str): The bit size of the operating system (e.g., "32bit", "64bit").
|
|
14
|
+
|
|
15
|
+
Example:
|
|
16
|
+
os_info = get_os_info()
|
|
17
|
+
print("Operating System:", os_info["operating_system"])
|
|
18
|
+
print("Bit Size:", os_info["bit_size"])
|
|
19
|
+
"""
|
|
20
|
+
os_name = platform.system()
|
|
21
|
+
bit_size = platform.architecture()[0]
|
|
22
|
+
return {"operating_system": os_name, "bit_size": bit_size}
|
|
23
|
+
def mkGb(k) -> float:
|
|
24
|
+
"""
|
|
25
|
+
Converts a value to Gigabytes (GB).
|
|
26
|
+
|
|
27
|
+
Args:
|
|
28
|
+
k (float): The value to convert to GB.
|
|
29
|
+
|
|
30
|
+
Returns:
|
|
31
|
+
float: The value converted to GB.
|
|
32
|
+
"""
|
|
33
|
+
return float(float(k)*(10**9))
|
|
34
|
+
|
|
35
|
+
def mkGbTrunk(k) -> float:
|
|
36
|
+
"""
|
|
37
|
+
Converts a value to Gigabytes (GB) and truncates the result to five decimal places.
|
|
38
|
+
|
|
39
|
+
Args:
|
|
40
|
+
k (float): The value to convert to GB.
|
|
41
|
+
|
|
42
|
+
Returns:
|
|
43
|
+
float: The value converted to GB and truncated to five decimal places.
|
|
44
|
+
"""
|
|
45
|
+
return trunc(mkGb(k), 5)
|
|
46
|
+
|
|
47
|
+
def mkGbTrunFroPathTot(k) -> float:
|
|
48
|
+
"""
|
|
49
|
+
Fetches the file size from a path, converts it to Gigabytes (GB) and truncates the result to five decimal places.
|
|
50
|
+
|
|
51
|
+
Args:
|
|
52
|
+
k (str): The file path.
|
|
53
|
+
|
|
54
|
+
Returns:
|
|
55
|
+
float: The file size converted to GB and truncated to five decimal places.
|
|
56
|
+
"""
|
|
57
|
+
return trunc(mkGb(s.path.getsize(k)), 5)
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
from ..imports import *
|
|
2
|
+
def split_text(string: str) -> tuple:
|
|
3
|
+
"""
|
|
4
|
+
Splits a string into its base name and extension and returns them as a tuple.
|
|
5
|
+
|
|
6
|
+
Args:
|
|
7
|
+
string (str): A string to be split, typically representing a file name.
|
|
8
|
+
|
|
9
|
+
Returns:
|
|
10
|
+
tuple: A tuple containing the base name and extension of the input string.
|
|
11
|
+
"""
|
|
12
|
+
return os.path.splitext(string)
|
|
13
|
+
def get_ext(file_path: str) -> str:
|
|
14
|
+
"""
|
|
15
|
+
Retrieves and returns the extension of a file from a given file path.
|
|
16
|
+
|
|
17
|
+
Args:
|
|
18
|
+
file_path (str): A string representing the file path.
|
|
19
|
+
|
|
20
|
+
Returns:
|
|
21
|
+
str: The extension of the file (including the dot).
|
|
22
|
+
"""
|
|
23
|
+
return split_text(get_base_name(file_path))[1]
|
|
24
|
+
|
|
25
|
+
def get_slash():
|
|
26
|
+
"""
|
|
27
|
+
Returns the appropriate file path separator depending on the current operating system.
|
|
28
|
+
"""
|
|
29
|
+
slash = '/' # Assume a Unix-like system by default
|
|
30
|
+
if slash not in get_current_path():
|
|
31
|
+
slash = '\\' # Use backslash for Windows systems
|
|
32
|
+
return slash
|
|
33
|
+
def get_current_path():
|
|
34
|
+
"""
|
|
35
|
+
Returns the current working directory.
|
|
36
|
+
|
|
37
|
+
Returns:
|
|
38
|
+
str: The current working directory.
|
|
39
|
+
"""
|
|
40
|
+
return os.getcwd()
|
|
41
|
+
|
|
42
|
+
def get_home_folder():
|
|
43
|
+
"""
|
|
44
|
+
Returns the path to the home directory of the current user.
|
|
45
|
+
|
|
46
|
+
Returns:
|
|
47
|
+
str: The path to the home directory.
|
|
48
|
+
"""
|
|
49
|
+
return os.path.expanduser("~")
|
|
50
|
+
def simple_path_join(path_A:str, path_B:str):
|
|
51
|
+
"""
|
|
52
|
+
Join two paths using the appropriate file path separator.
|
|
53
|
+
|
|
54
|
+
Args:
|
|
55
|
+
path_A (str): The first path to join.
|
|
56
|
+
path_B (str): The second path to join.
|
|
57
|
+
|
|
58
|
+
Returns:
|
|
59
|
+
str: The joined path.
|
|
60
|
+
"""
|
|
61
|
+
return os.path.join(str(path_A), str(path_B))
|
|
62
|
+
|
|
63
|
+
def path_join(path_A, path_B=None):
|
|
64
|
+
"""
|
|
65
|
+
Joins two paths or a list of paths using the appropriate file path separator.
|
|
66
|
+
|
|
67
|
+
Args:
|
|
68
|
+
path_A (str or list): The first path or list of paths to join.
|
|
69
|
+
path_B (str, optional): The second path to join. Defaults to None.
|
|
70
|
+
|
|
71
|
+
Returns:
|
|
72
|
+
str: The joined path.
|
|
73
|
+
"""
|
|
74
|
+
if path_B is not None: # If path_B is provided, join path_A and path_B
|
|
75
|
+
return simple_path_join(path_A, path_B)
|
|
76
|
+
if isinstance(path_A, list): # If path_A is a list, join all paths in the list
|
|
77
|
+
path = path_A[0]
|
|
78
|
+
for k in range(1, len(path_A)):
|
|
79
|
+
path = simple_path_join(path, path_A[k])
|
|
80
|
+
return path
|
|
81
|
+
def update_global_variable(name: str, value) -> None:
|
|
82
|
+
"""Updates the global variable with the provided name and value.
|
|
83
|
+
|
|
84
|
+
Args:
|
|
85
|
+
name (str): The name of the global variable.
|
|
86
|
+
value: The value to assign to the global variable.
|
|
87
|
+
|
|
88
|
+
Returns:
|
|
89
|
+
None
|
|
90
|
+
"""
|
|
91
|
+
globals()[name] = value
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def trunc(a: float, x: int) -> float:
|
|
95
|
+
"""
|
|
96
|
+
Truncates a float number to a specific number of decimal places.
|
|
97
|
+
|
|
98
|
+
Args:
|
|
99
|
+
a (float): The number to truncate.
|
|
100
|
+
x (int): The number of decimal places to retain.
|
|
101
|
+
|
|
102
|
+
Returns:
|
|
103
|
+
float: The truncated float number.
|
|
104
|
+
"""
|
|
105
|
+
temp = str(a)
|
|
106
|
+
for i in range(len(temp)):
|
|
107
|
+
if temp[i] == '.':
|
|
108
|
+
try:
|
|
109
|
+
return float(temp[:i+x+1])
|
|
110
|
+
except:
|
|
111
|
+
return float(temp)
|
|
112
|
+
return float(temp)
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
from ...imports import os
|
|
2
2
|
from dotenv import load_dotenv
|
|
3
|
+
from ...ssh_utils import is_file
|
|
3
4
|
from ...string_utils import eatAll,eatInner,eatOuter
|
|
4
5
|
from ...safe_utils import safe_split
|
|
5
6
|
from ...compare_utils import line_contains
|
|
6
7
|
from ...type_utils import is_list,is_bool
|
|
7
|
-
from ...
|
|
8
|
+
from ...directory_utils import get_slash,path_join,if_not_last_child_join,get_home_folder,simple_path_join
|
|
8
9
|
DEFAULT_FILE_NAME = '.env'
|
|
9
10
|
DEFAULT_KEY = 'MY_PASSWORD'
|
|
@@ -5,5 +5,4 @@ from ...env_utils import *
|
|
|
5
5
|
from ...read_write_utils import read_from_file,write_to_file
|
|
6
6
|
from ...log_utils import get_logFile
|
|
7
7
|
from ...class_utils import get_caller, get_caller_path, get_caller_dir,SingletonMeta,run_pruned_func
|
|
8
|
-
|
|
9
|
-
from ...string_utils import get_from_kwargs,eatAll
|
|
8
|
+
|