abstract-utilities 0.2.2.466__py3-none-any.whl → 0.2.2.468__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/file_utils/imports/clean_imps.py +158 -0
- abstract_utilities/file_utils/imports/file_functions.py +1 -1
- abstract_utilities/file_utils/imports/imports.py +227 -1
- abstract_utilities/read_write_utils.py +1 -1
- abstract_utilities/robust_reader/imports/imports.py +0 -9
- abstract_utilities/robust_readers/import_utils/__init__.py +1 -0
- abstract_utilities/robust_readers/import_utils/clean_imports.py +215 -0
- {abstract_utilities-0.2.2.466.dist-info → abstract_utilities-0.2.2.468.dist-info}/METADATA +1 -1
- {abstract_utilities-0.2.2.466.dist-info → abstract_utilities-0.2.2.468.dist-info}/RECORD +11 -9
- {abstract_utilities-0.2.2.466.dist-info → abstract_utilities-0.2.2.468.dist-info}/WHEEL +0 -0
- {abstract_utilities-0.2.2.466.dist-info → abstract_utilities-0.2.2.468.dist-info}/top_level.txt +0 -0
|
@@ -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)
|
|
@@ -5,10 +5,34 @@
|
|
|
5
5
|
# from ..imports import *
|
|
6
6
|
# ============================================================
|
|
7
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
|
|
8
32
|
# ---- Core standard library modules -------------------------
|
|
9
33
|
import os, sys, re, shlex, glob, platform, textwrap, subprocess, inspect, json, time
|
|
10
34
|
import tempfile, shutil, logging, pathlib, fnmatch, importlib, importlib.util, types
|
|
11
|
-
|
|
35
|
+
|
|
12
36
|
from datetime import datetime
|
|
13
37
|
from types import ModuleType
|
|
14
38
|
|
|
@@ -38,3 +62,205 @@ from pprint import pprint
|
|
|
38
62
|
# AUTO-EXPORT ALL NON-PRIVATE NAMES
|
|
39
63
|
# ============================================================
|
|
40
64
|
__all__ = [name for name in globals() if not name.startswith("_")]
|
|
65
|
+
from abstract_utilities import read_from_file,eatAll,inspect, os
|
|
66
|
+
import_tag = 'import '
|
|
67
|
+
from_tag = 'from '
|
|
68
|
+
def get_caller_path(i=None):
|
|
69
|
+
i = i or 1
|
|
70
|
+
frame = inspect.stack()[i]
|
|
71
|
+
return os.path.abspath(frame.filename)
|
|
72
|
+
def make_list(obj:any) -> list:
|
|
73
|
+
"""
|
|
74
|
+
Converts the input object to a list. If the object is already a list, it is returned as is.
|
|
75
|
+
|
|
76
|
+
Args:
|
|
77
|
+
obj: The object to convert.
|
|
78
|
+
|
|
79
|
+
Returns:
|
|
80
|
+
list: The object as a list.
|
|
81
|
+
"""
|
|
82
|
+
if isinstance(obj,str):
|
|
83
|
+
if ',' in obj:
|
|
84
|
+
obj = obj.split(',')
|
|
85
|
+
if isinstance(obj,set) or isinstance(obj,tuple):
|
|
86
|
+
return list(obj)
|
|
87
|
+
if isinstance(obj, list):
|
|
88
|
+
return obj
|
|
89
|
+
return [obj]
|
|
90
|
+
def getAlphas(lower=True,capitalize=False,listObj=False):
|
|
91
|
+
obj = ''
|
|
92
|
+
alphas = 'abcdefghijklmoprstuvwxyz'
|
|
93
|
+
if lower:
|
|
94
|
+
obj+=alphas
|
|
95
|
+
if capitalize:
|
|
96
|
+
obj+=alphas.upper()
|
|
97
|
+
if listObj:
|
|
98
|
+
obj = list(obj)
|
|
99
|
+
return obj
|
|
100
|
+
def getInts(string=False,listObj=False):
|
|
101
|
+
obj=12345678909
|
|
102
|
+
if string:
|
|
103
|
+
obj = str(obj)
|
|
104
|
+
if listObj:
|
|
105
|
+
obj = list(obj)
|
|
106
|
+
return obj
|
|
107
|
+
def get_alpha_ints(ints=True,alpha=True,lower=True,capitalize=True,string=True,listObj=True):
|
|
108
|
+
objs = [] if listObj else ""
|
|
109
|
+
if ints:
|
|
110
|
+
objs+=getInts(string=string,listObj=listObj)
|
|
111
|
+
if alpha:
|
|
112
|
+
objs+=getAlphas(lower=lower,capitalize=capitalize,listObj=listObj)
|
|
113
|
+
return objs
|
|
114
|
+
def eatElse(
|
|
115
|
+
stringObj,
|
|
116
|
+
chars=None,
|
|
117
|
+
ints=True,
|
|
118
|
+
alpha=True,
|
|
119
|
+
lower=True,
|
|
120
|
+
capitalize=True,
|
|
121
|
+
string=True,
|
|
122
|
+
listObj=True
|
|
123
|
+
):
|
|
124
|
+
alpha_ints = get_alpha_ints(
|
|
125
|
+
ints=True,
|
|
126
|
+
alpha=True,
|
|
127
|
+
lower=True,
|
|
128
|
+
capitalize=True,
|
|
129
|
+
string=True,
|
|
130
|
+
listObj=True
|
|
131
|
+
)
|
|
132
|
+
chars = make_list(chars or [])+alpha_ints
|
|
133
|
+
|
|
134
|
+
while True:
|
|
135
|
+
if stringObj:
|
|
136
|
+
str_0 = stringObj[0] not in chars
|
|
137
|
+
str_1 = stringObj[-1] not in chars
|
|
138
|
+
str_eat = str_0 or str_1
|
|
139
|
+
if not str_eat:
|
|
140
|
+
return stringObj
|
|
141
|
+
if stringObj and str_0:
|
|
142
|
+
stringObj = stringObj[1:] if len(stringObj) !=1 else ""
|
|
143
|
+
if stringObj and str_1:
|
|
144
|
+
stringObj = stringObj[:-1] if len(stringObj) !=1 else ""
|
|
145
|
+
else:
|
|
146
|
+
return stringObj
|
|
147
|
+
def clean_line(line):
|
|
148
|
+
return eatAll(line,[' ','','\t','\n'])
|
|
149
|
+
def is_line_import(line):
|
|
150
|
+
if line and (line.startswith(from_tag) or line.startswith(import_tag)):
|
|
151
|
+
return True
|
|
152
|
+
return False
|
|
153
|
+
def is_line_group_import(line):
|
|
154
|
+
if line and (line.startswith(from_tag) and import_tag in line):
|
|
155
|
+
return True
|
|
156
|
+
return False
|
|
157
|
+
def get_import_pkg(line):
|
|
158
|
+
if is_line_group_import(line):
|
|
159
|
+
|
|
160
|
+
return clean_line(line.split(from_tag)[1].split(import_tag)[0])
|
|
161
|
+
def get_imports_from_import_pkg(line):
|
|
162
|
+
if is_line_group_import(line):
|
|
163
|
+
return get_cleaned_import_list(line,commaClean=True)
|
|
164
|
+
def add_imports_to_import_pkg_js(import_pkg,imports,import_pkg_js=None):
|
|
165
|
+
import_pkg_js = import_pkg_js or {}
|
|
166
|
+
imports = clean_imports(imports)
|
|
167
|
+
if import_pkg not in import_pkg_js:
|
|
168
|
+
i = len(import_pkg_js["nulines"])
|
|
169
|
+
import_pkg_js[import_pkg]={"imports":imports,"line":i}
|
|
170
|
+
import_line = f"from {import_pkg} import "
|
|
171
|
+
if import_pkg == "import":
|
|
172
|
+
import_line = import_tag
|
|
173
|
+
import_pkg_js["nulines"].append(import_line)
|
|
174
|
+
else:
|
|
175
|
+
import_pkg_js[import_pkg]["imports"]+=imports
|
|
176
|
+
return import_pkg_js
|
|
177
|
+
def update_import_pkg_js(line,import_pkg_js=None):
|
|
178
|
+
import_pkg_js = import_pkg_js or {}
|
|
179
|
+
if is_line_group_import(line):
|
|
180
|
+
import_pkg = get_import_pkg(line)
|
|
181
|
+
imports = get_imports_from_import_pkg(line)
|
|
182
|
+
import_pkg_js = add_imports_to_import_pkg_js(import_pkg,imports,import_pkg_js=import_pkg_js)
|
|
183
|
+
else:
|
|
184
|
+
if len(import_pkg_js["nulines"]) >0 and line == '' and is_line_import(import_pkg_js["nulines"][-1]):
|
|
185
|
+
pass
|
|
186
|
+
else:
|
|
187
|
+
import_pkg_js["nulines"].append(line)
|
|
188
|
+
return import_pkg_js
|
|
189
|
+
def is_from_line_group(line):
|
|
190
|
+
if line and line.startswith(from_tag) and import_tag in line and '(' in line:
|
|
191
|
+
import_spl = line.split(import_tag)[-1]
|
|
192
|
+
import_spl_clean = clean_line(line)
|
|
193
|
+
if not import_spl_clean.endswith(')'):
|
|
194
|
+
return True
|
|
195
|
+
return False
|
|
196
|
+
def clean_imports(imports,commaClean=True):
|
|
197
|
+
chars=["*"]
|
|
198
|
+
if not commaClean:
|
|
199
|
+
chars.append(',')
|
|
200
|
+
if isinstance(imports,str):
|
|
201
|
+
imports = imports.split(',')
|
|
202
|
+
return [eatElse(imp,chars=chars) for imp in imports if imp]
|
|
203
|
+
def get_cleaned_import_list(line,commaClean=True):
|
|
204
|
+
cleaned_import_list=[]
|
|
205
|
+
if import_tag in line:
|
|
206
|
+
imports = line.split(import_tag)[1]
|
|
207
|
+
cleaned_import_list+=clean_imports(imports,commaClean=commaClean)
|
|
208
|
+
return cleaned_import_list
|
|
209
|
+
def combine_lone_imports(text=None,file_path=None,write=False):
|
|
210
|
+
text = text or ''
|
|
211
|
+
imports_js = {}
|
|
212
|
+
if file_path and os.path.isfile(file_path):
|
|
213
|
+
text+=read_from_file(file_path)
|
|
214
|
+
lines = text.split('\n')
|
|
215
|
+
cleaned_import_list=[]
|
|
216
|
+
nu_lines = []
|
|
217
|
+
j=None
|
|
218
|
+
is_from_group = False
|
|
219
|
+
import_pkg_js={"nulines":[]}
|
|
220
|
+
for line in lines:
|
|
221
|
+
if line.startswith(import_tag) and ' from ' not in line:
|
|
222
|
+
cleaned_import_list = get_cleaned_import_list(line)
|
|
223
|
+
import_pkg_js = add_imports_to_import_pkg_js("import",cleaned_import_list,import_pkg_js=import_pkg_js)
|
|
224
|
+
else:
|
|
225
|
+
if is_from_group:
|
|
226
|
+
import_pkg=is_from_group
|
|
227
|
+
line = clean_line(line)
|
|
228
|
+
if line.endswith(')'):
|
|
229
|
+
is_from_group=False
|
|
230
|
+
line=line[:-1]
|
|
231
|
+
imports_from_import_pkg = clean_imports(line)
|
|
232
|
+
import_pkg_js = add_imports_to_import_pkg_js(import_pkg,imports_from_import_pkg,import_pkg_js=import_pkg_js)
|
|
233
|
+
|
|
234
|
+
else:
|
|
235
|
+
import_pkg_js=update_import_pkg_js(line,import_pkg_js=import_pkg_js)
|
|
236
|
+
if is_from_line_group(line) and is_from_group == False:
|
|
237
|
+
|
|
238
|
+
is_from_group=get_import_pkg(line)
|
|
239
|
+
nu_lines = import_pkg_js["nulines"]
|
|
240
|
+
for pkg,values in import_pkg_js.items():
|
|
241
|
+
comments = []
|
|
242
|
+
if pkg != "nulines":
|
|
243
|
+
line = values.get('line')
|
|
244
|
+
imports = values.get('imports')
|
|
245
|
+
for i,imp in enumerate(imports):
|
|
246
|
+
if '#' in imp:
|
|
247
|
+
imp_spl = imp.split('#')
|
|
248
|
+
comments.append(imp_spl[-1])
|
|
249
|
+
imports[i] = clean_line(imp_spl[0])
|
|
250
|
+
imports = list(set(imports))
|
|
251
|
+
if '*' in imports:
|
|
252
|
+
imports="*"
|
|
253
|
+
else:
|
|
254
|
+
imports=','.join(imports)
|
|
255
|
+
if comments:
|
|
256
|
+
comments=','.join(comments)
|
|
257
|
+
imports+=f" #{comments}"
|
|
258
|
+
|
|
259
|
+
nu_lines[line] += imports
|
|
260
|
+
contents = '\n'.join(nu_lines)
|
|
261
|
+
if file_path and write:
|
|
262
|
+
write_to_file(contents=contents,file_path=file_path)
|
|
263
|
+
return contents
|
|
264
|
+
abs_path = get_caller_path()
|
|
265
|
+
nu_contents = combine_lone_imports(file_path=abs_path)
|
|
266
|
+
input(nu_contents)
|
|
@@ -14,7 +14,7 @@ Usage:
|
|
|
14
14
|
|
|
15
15
|
import os
|
|
16
16
|
import shlex
|
|
17
|
-
from .ssh_utils.utils import run_cmd,get_print_sudo_cmd
|
|
17
|
+
from .ssh_utils.utils import run_cmd,get_print_sudo_cmd,run_local_cmd,run_remote_cmd
|
|
18
18
|
from .file_utils.file_utils.type_checks import is_file,is_dir
|
|
19
19
|
from .abstract_classes import run_pruned_func
|
|
20
20
|
_FILE_PATH_KEYS = ['file', 'filepath', 'file_path', 'path', 'directory', 'f', 'dst', 'dest']
|
|
@@ -1,12 +1,3 @@
|
|
|
1
|
-
import os,tempfile,shutil,logging,ezodf,fnmatch,pytesseract,pdfplumber
|
|
2
|
-
import pandas as pd
|
|
3
|
-
import geopandas as gpd
|
|
4
|
-
from datetime import datetime
|
|
5
|
-
from pathlib import Path
|
|
6
|
-
from typing import *
|
|
7
|
-
from werkzeug.utils import secure_filename
|
|
8
|
-
from werkzeug.datastructures import FileStorage
|
|
9
|
-
from pdf2image import convert_from_path # only used for OCR fallback
|
|
10
1
|
from ...abstract_classes import SingletonMeta
|
|
11
2
|
from ..pdf_utils import *
|
|
12
3
|
from ...read_write_utils import *
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
from abstract_utilities import read_from_file,eatAll,inspect, os
|
|
2
|
+
import_tag = 'import '
|
|
3
|
+
from_tag = 'from '
|
|
4
|
+
def get_caller_path(i=None):
|
|
5
|
+
i = i or 1
|
|
6
|
+
frame = inspect.stack()[i]
|
|
7
|
+
return os.path.abspath(frame.filename)
|
|
8
|
+
def make_list(obj:any) -> list:
|
|
9
|
+
"""
|
|
10
|
+
Converts the input object to a list. If the object is already a list, it is returned as is.
|
|
11
|
+
|
|
12
|
+
Args:
|
|
13
|
+
obj: The object to convert.
|
|
14
|
+
|
|
15
|
+
Returns:
|
|
16
|
+
list: The object as a list.
|
|
17
|
+
"""
|
|
18
|
+
if isinstance(obj,str):
|
|
19
|
+
if ',' in obj:
|
|
20
|
+
obj = obj.split(',')
|
|
21
|
+
if isinstance(obj,set) or isinstance(obj,tuple):
|
|
22
|
+
return list(obj)
|
|
23
|
+
if isinstance(obj, list):
|
|
24
|
+
return obj
|
|
25
|
+
return [obj]
|
|
26
|
+
def getAlphas(lower=True,capitalize=False,listObj=False):
|
|
27
|
+
obj = ''
|
|
28
|
+
alphas = 'abcdefghijklmoprstuvwxyz'
|
|
29
|
+
if lower:
|
|
30
|
+
obj+=alphas
|
|
31
|
+
if capitalize:
|
|
32
|
+
obj+=alphas.upper()
|
|
33
|
+
if listObj:
|
|
34
|
+
obj = list(obj)
|
|
35
|
+
return obj
|
|
36
|
+
def getInts(string=False,listObj=False):
|
|
37
|
+
obj=12345678909
|
|
38
|
+
if string:
|
|
39
|
+
obj = str(obj)
|
|
40
|
+
if listObj:
|
|
41
|
+
obj = list(obj)
|
|
42
|
+
return obj
|
|
43
|
+
def get_alpha_ints(ints=True,alpha=True,lower=True,capitalize=True,string=True,listObj=True):
|
|
44
|
+
objs = [] if listObj else ""
|
|
45
|
+
if ints:
|
|
46
|
+
objs+=getInts(string=string,listObj=listObj)
|
|
47
|
+
if alpha:
|
|
48
|
+
objs+=getAlphas(lower=lower,capitalize=capitalize,listObj=listObj)
|
|
49
|
+
return objs
|
|
50
|
+
def eatElse(
|
|
51
|
+
stringObj,
|
|
52
|
+
chars=None,
|
|
53
|
+
ints=True,
|
|
54
|
+
alpha=True,
|
|
55
|
+
lower=True,
|
|
56
|
+
capitalize=True,
|
|
57
|
+
string=True,
|
|
58
|
+
listObj=True
|
|
59
|
+
):
|
|
60
|
+
alpha_ints = get_alpha_ints(
|
|
61
|
+
ints=True,
|
|
62
|
+
alpha=True,
|
|
63
|
+
lower=True,
|
|
64
|
+
capitalize=True,
|
|
65
|
+
string=True,
|
|
66
|
+
listObj=True
|
|
67
|
+
)
|
|
68
|
+
chars = make_list(chars or [])+alpha_ints
|
|
69
|
+
|
|
70
|
+
while True:
|
|
71
|
+
if stringObj:
|
|
72
|
+
str_0 = stringObj[0] not in chars
|
|
73
|
+
str_1 = stringObj[-1] not in chars
|
|
74
|
+
str_eat = str_0 or str_1
|
|
75
|
+
if not str_eat:
|
|
76
|
+
return stringObj
|
|
77
|
+
if stringObj and str_0:
|
|
78
|
+
stringObj = stringObj[1:] if len(stringObj) !=1 else ""
|
|
79
|
+
if stringObj and str_1:
|
|
80
|
+
stringObj = stringObj[:-1] if len(stringObj) !=1 else ""
|
|
81
|
+
else:
|
|
82
|
+
return stringObj
|
|
83
|
+
def clean_line(line):
|
|
84
|
+
return eatAll(line,[' ','','\t','\n'])
|
|
85
|
+
def is_line_import(line):
|
|
86
|
+
if line and (line.startswith(from_tag) or line.startswith(import_tag)):
|
|
87
|
+
return True
|
|
88
|
+
return False
|
|
89
|
+
def is_line_group_import(line):
|
|
90
|
+
if line and (line.startswith(from_tag) and import_tag in line):
|
|
91
|
+
return True
|
|
92
|
+
return False
|
|
93
|
+
def get_import_pkg(line):
|
|
94
|
+
if is_line_group_import(line):
|
|
95
|
+
|
|
96
|
+
return clean_line(line.split(from_tag)[1].split(import_tag)[0])
|
|
97
|
+
def get_imports_from_import_pkg(line):
|
|
98
|
+
if is_line_group_import(line):
|
|
99
|
+
return get_cleaned_import_list(line,commaClean=True)
|
|
100
|
+
##def is_local_or_import(pkg_name,file_path):
|
|
101
|
+
## dirname = os.path.dirname(file_path)
|
|
102
|
+
## parts = part for part in dirname.split('/') if part]
|
|
103
|
+
## i=0
|
|
104
|
+
## pkg = pkg_name
|
|
105
|
+
## while True:
|
|
106
|
+
## if pkg and pkg.startswith('.'):
|
|
107
|
+
## i +=1
|
|
108
|
+
## pkg=pkg[1:] if len(pkg)>1 else ""
|
|
109
|
+
## else:
|
|
110
|
+
## if i > 1 and len(parts)>=i:
|
|
111
|
+
## pkg = '.'.join(parts[:-i])
|
|
112
|
+
## .{pkg}"
|
|
113
|
+
## parts
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def add_imports_to_import_pkg_js(import_pkg,imports,import_pkg_js=None):
|
|
117
|
+
import_pkg_js = import_pkg_js or {}
|
|
118
|
+
imports = clean_imports(imports)
|
|
119
|
+
if import_pkg not in import_pkg_js:
|
|
120
|
+
i = len(import_pkg_js["nulines"])
|
|
121
|
+
import_pkg_js[import_pkg]={"imports":imports,"line":i}
|
|
122
|
+
import_line = f"from {import_pkg} import "
|
|
123
|
+
if import_pkg == "import":
|
|
124
|
+
import_line = import_tag
|
|
125
|
+
import_pkg_js["nulines"].append(import_line)
|
|
126
|
+
else:
|
|
127
|
+
import_pkg_js[import_pkg]["imports"]+=imports
|
|
128
|
+
return import_pkg_js
|
|
129
|
+
def update_import_pkg_js(line,import_pkg_js=None):
|
|
130
|
+
import_pkg_js = import_pkg_js or {}
|
|
131
|
+
if is_line_group_import(line):
|
|
132
|
+
import_pkg = get_import_pkg(line)
|
|
133
|
+
imports = get_imports_from_import_pkg(line)
|
|
134
|
+
import_pkg_js = add_imports_to_import_pkg_js(import_pkg,imports,import_pkg_js=import_pkg_js)
|
|
135
|
+
else:
|
|
136
|
+
if len(import_pkg_js["nulines"]) >0 and line == '' and is_line_import(import_pkg_js["nulines"][-1]):
|
|
137
|
+
pass
|
|
138
|
+
else:
|
|
139
|
+
import_pkg_js["nulines"].append(line)
|
|
140
|
+
return import_pkg_js
|
|
141
|
+
def is_from_line_group(line):
|
|
142
|
+
if line and line.startswith(from_tag) and import_tag in line and '(' in line:
|
|
143
|
+
import_spl = line.split(import_tag)[-1]
|
|
144
|
+
import_spl_clean = clean_line(line)
|
|
145
|
+
if not import_spl_clean.endswith(')'):
|
|
146
|
+
return True
|
|
147
|
+
return False
|
|
148
|
+
def clean_imports(imports,commaClean=True):
|
|
149
|
+
chars=["*"]
|
|
150
|
+
if not commaClean:
|
|
151
|
+
chars.append(',')
|
|
152
|
+
if isinstance(imports,str):
|
|
153
|
+
imports = imports.split(',')
|
|
154
|
+
return [eatElse(imp,chars=chars) for imp in imports if imp]
|
|
155
|
+
def get_cleaned_import_list(line,commaClean=True):
|
|
156
|
+
cleaned_import_list=[]
|
|
157
|
+
if import_tag in line:
|
|
158
|
+
imports = line.split(import_tag)[1]
|
|
159
|
+
cleaned_import_list+=clean_imports(imports,commaClean=commaClean)
|
|
160
|
+
return cleaned_import_list
|
|
161
|
+
def clean_imports(text=None,file_path=None,write=False):
|
|
162
|
+
text = text or ''
|
|
163
|
+
imports_js = {}
|
|
164
|
+
if file_path and os.path.isfile(file_path):
|
|
165
|
+
text+=read_from_file(file_path)
|
|
166
|
+
lines = text.split('\n')
|
|
167
|
+
cleaned_import_list=[]
|
|
168
|
+
nu_lines = []
|
|
169
|
+
j=None
|
|
170
|
+
is_from_group = False
|
|
171
|
+
import_pkg_js={"nulines":[],"file_path":file_path}
|
|
172
|
+
for line in lines:
|
|
173
|
+
if line.startswith(import_tag) and ' from ' not in line:
|
|
174
|
+
cleaned_import_list = get_cleaned_import_list(line)
|
|
175
|
+
import_pkg_js = add_imports_to_import_pkg_js("import",cleaned_import_list,import_pkg_js=import_pkg_js)
|
|
176
|
+
else:
|
|
177
|
+
if is_from_group:
|
|
178
|
+
import_pkg=is_from_group
|
|
179
|
+
line = clean_line(line)
|
|
180
|
+
if line.endswith(')'):
|
|
181
|
+
is_from_group=False
|
|
182
|
+
line=line[:-1]
|
|
183
|
+
imports_from_import_pkg = clean_imports(line)
|
|
184
|
+
import_pkg_js = add_imports_to_import_pkg_js(import_pkg,imports_from_import_pkg,import_pkg_js=import_pkg_js)
|
|
185
|
+
|
|
186
|
+
else:
|
|
187
|
+
import_pkg_js=update_import_pkg_js(line,import_pkg_js=import_pkg_js)
|
|
188
|
+
if is_from_line_group(line) and is_from_group == False:
|
|
189
|
+
|
|
190
|
+
is_from_group=get_import_pkg(line)
|
|
191
|
+
nu_lines = import_pkg_js["nulines"]
|
|
192
|
+
for pkg,values in import_pkg_js.items():
|
|
193
|
+
comments = []
|
|
194
|
+
if pkg not in ["nulines","file_path"]:
|
|
195
|
+
line = values.get('line')
|
|
196
|
+
imports = values.get('imports')
|
|
197
|
+
for i,imp in enumerate(imports):
|
|
198
|
+
if '#' in imp:
|
|
199
|
+
imp_spl = imp.split('#')
|
|
200
|
+
comments.append(imp_spl[-1])
|
|
201
|
+
imports[i] = clean_line(imp_spl[0])
|
|
202
|
+
imports = list(set(imports))
|
|
203
|
+
if '*' in imports:
|
|
204
|
+
imports="*"
|
|
205
|
+
else:
|
|
206
|
+
imports=','.join(imports)
|
|
207
|
+
if comments:
|
|
208
|
+
comments=','.join(comments)
|
|
209
|
+
imports+=f" #{comments}"
|
|
210
|
+
|
|
211
|
+
nu_lines[line] += imports
|
|
212
|
+
contents = '\n'.join(nu_lines)
|
|
213
|
+
if file_path and write:
|
|
214
|
+
write_to_file(contents=contents,file_path=file_path)
|
|
215
|
+
return contents
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: abstract_utilities
|
|
3
|
-
Version: 0.2.2.
|
|
3
|
+
Version: 0.2.2.468
|
|
4
4
|
Summary: abstract_utilities is a collection of utility modules providing a variety of functions to aid in tasks such as data comparison, list manipulation, JSON handling, string manipulation, mathematical computations, and time operations.
|
|
5
5
|
Home-page: https://github.com/AbstractEndeavors/abstract_utilities
|
|
6
6
|
Author: putkoff
|
|
@@ -14,7 +14,7 @@ abstract_utilities/log_utils.py,sha256=W74Y-CmdQP4Kj88HmAgejVxWgyWlvgCKMwLvOfyFf
|
|
|
14
14
|
abstract_utilities/math_utils.py,sha256=0o1ls1En03UAkYmxTBildCCJDfHygmNuvVnrNrLYtK0,6578
|
|
15
15
|
abstract_utilities/parse_utils.py,sha256=Z5OGRwHuzCzY91fz0JJojk1BPAo1XF2quNNLuBF4_Vk,18602
|
|
16
16
|
abstract_utilities/path_utils.py,sha256=X_U9cPBbNu5Wi0F3hQE0gXQX1gfhzxhxALbairTEOZU,19252
|
|
17
|
-
abstract_utilities/read_write_utils.py,sha256=
|
|
17
|
+
abstract_utilities/read_write_utils.py,sha256=S8Sj0mtErRze9uOOzdWl2ZreREdXKKQMbyQKC75c_Vc,6921
|
|
18
18
|
abstract_utilities/safe_utils.py,sha256=_uoZny6dJjopVakOiaf0UIZcvRRXMh51FpfDUooe0xY,3733
|
|
19
19
|
abstract_utilities/string_clean.py,sha256=-gY9i2yqjX5UClvSaKsSrzA4GjR7eaNI3GnFjZpt2bo,5923
|
|
20
20
|
abstract_utilities/string_utils.py,sha256=02xdIEDySWEexrtY3skBYOdeDmr3XgE5j1nqAeAv7w8,352
|
|
@@ -58,20 +58,22 @@ abstract_utilities/file_utils/file_utils/imports/imports.py,sha256=nLtDCj-E9htQ1
|
|
|
58
58
|
abstract_utilities/file_utils/file_utils/imports/module_imports.py,sha256=pvRkjtWxQ9R4TisCKMQ9asDak63Y9eMGbpCB7DsEyqs,453
|
|
59
59
|
abstract_utilities/file_utils/imports/__init__.py,sha256=T-vdIj87HUj6H9tWMA67gNoEwLjnkMmEJpX5fN144fI,131
|
|
60
60
|
abstract_utilities/file_utils/imports/classes.py,sha256=zw16D_h5AxJiks4ydbqkWkXVfvgmE-BpiC4eKInY_KI,12259
|
|
61
|
+
abstract_utilities/file_utils/imports/clean_imps.py,sha256=DB_NEKR8YLla5qCkTMuNscMoTnipEm3nCWnaH8wqQDc,5287
|
|
61
62
|
abstract_utilities/file_utils/imports/constants.py,sha256=eIeSj48vtfa8CTYKuuZXbgJQepBrMracfVguaSuN41U,1626
|
|
62
|
-
abstract_utilities/file_utils/imports/file_functions.py,sha256=
|
|
63
|
-
abstract_utilities/file_utils/imports/imports.py,sha256=
|
|
63
|
+
abstract_utilities/file_utils/imports/file_functions.py,sha256=brQha7TV9DaJe-hZSuHoFZBUI_45hxrGOIBTAojPWU8,297
|
|
64
|
+
abstract_utilities/file_utils/imports/imports.py,sha256=xsxfaNE7uvdJH8WKUMk1RNTq8CpIC6adAwno96Dmvuc,9147
|
|
64
65
|
abstract_utilities/file_utils/imports/module_imports.py,sha256=BROjglIl217zEuU0kwRilkK9vLrYC9e44AS5HS8HwD0,513
|
|
65
66
|
abstract_utilities/robust_reader/__init__.py,sha256=4i6qW4lwhdYuoO5-p9Xbt8Lpmr3hzCh9Rgb9y19QJwk,28
|
|
66
67
|
abstract_utilities/robust_reader/file_reader2.py,sha256=U-5opkLu-bct091Eb-5CiNBTf0UFoSITYi8zR-Sz38w,25077
|
|
67
68
|
abstract_utilities/robust_reader/file_readers.py,sha256=U-5opkLu-bct091Eb-5CiNBTf0UFoSITYi8zR-Sz38w,25077
|
|
68
69
|
abstract_utilities/robust_reader/sadfsad.py,sha256=gH2ebI9KfiYFv78jzPGk8WPST_FGtojnd_yDwrcvQoM,25282
|
|
69
70
|
abstract_utilities/robust_reader/imports/__init__.py,sha256=mp6T1rBZdOzj0IDkvlteTqtyKJiOZaJTlgrjTdHO1Qw,23
|
|
70
|
-
abstract_utilities/robust_reader/imports/imports.py,sha256=
|
|
71
|
+
abstract_utilities/robust_reader/imports/imports.py,sha256=4pqr_mOX7s5YFC3UekLlA7eakBgq_wACq2FYBlwCiec,106
|
|
71
72
|
abstract_utilities/robust_readers/__init__.py,sha256=_1vhOG1FJnfrRK0ubkT1v6U6udHMIk3qiy1qajL1IiM,55
|
|
72
73
|
abstract_utilities/robust_readers/imports.py,sha256=FtNxdPoLeeNycDnl-6rBGxBfYjhQ7VhmI5guj8XKFcU,355
|
|
73
74
|
abstract_utilities/robust_readers/initFuncGen.py,sha256=nrQn1KpSlPNKoOngN1uizVwNMA4llrcd8aGKqlzpXzI,5436
|
|
74
|
-
abstract_utilities/robust_readers/import_utils/__init__.py,sha256=
|
|
75
|
+
abstract_utilities/robust_readers/import_utils/__init__.py,sha256=0XaHXUzvgMjSV-4VXcBB-sLcXzL3U3ssHZ95YkvgS9w,195
|
|
76
|
+
abstract_utilities/robust_readers/import_utils/clean_imports.py,sha256=kl6Ne06YVZDPHgs59qek3m-KmF3xfWWZbJchv0FS8Q4,7483
|
|
75
77
|
abstract_utilities/robust_readers/import_utils/dot_utils.py,sha256=pmwnY461mOnDjIjgHD6H9MhQXFaF-q8kWerJDgJ1DuI,2364
|
|
76
78
|
abstract_utilities/robust_readers/import_utils/function_utils.py,sha256=Q9NKvRov3uAaz2Aal3d6fb_opWNXHF9C8GSKOjgfO8Y,1622
|
|
77
79
|
abstract_utilities/robust_readers/import_utils/import_utils.py,sha256=l0GYdtj5FEYX2yknL-8ru7_U2Sp9Hi1NpegqWPLRMc8,11705
|
|
@@ -84,7 +86,7 @@ abstract_utilities/ssh_utils/classes.py,sha256=3Q9BfLpyagNFYyiF4bt-5UCezeUJv9NK9
|
|
|
84
86
|
abstract_utilities/ssh_utils/imports.py,sha256=oX8WAv-pkhizzko_h3fIUp9Vhsse4nR7RN2vwONxIx0,317
|
|
85
87
|
abstract_utilities/ssh_utils/pexpect_utils.py,sha256=JBdOIXBTXAqE5TrsFjmPWJgwSaWyRJN8rbJ6y3_zKPY,10556
|
|
86
88
|
abstract_utilities/ssh_utils/utils.py,sha256=smUWAx3nW1h0etTndJ_te9bkUX5YzQ8kYd9_gD1TXLk,4882
|
|
87
|
-
abstract_utilities-0.2.2.
|
|
88
|
-
abstract_utilities-0.2.2.
|
|
89
|
-
abstract_utilities-0.2.2.
|
|
90
|
-
abstract_utilities-0.2.2.
|
|
89
|
+
abstract_utilities-0.2.2.468.dist-info/METADATA,sha256=9hAVfrsbxq20y58SW5cZC5ORLn4tvmi0-KB59Y2al50,28108
|
|
90
|
+
abstract_utilities-0.2.2.468.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
91
|
+
abstract_utilities-0.2.2.468.dist-info/top_level.txt,sha256=BF0GZ0xVFfN1K-hFIWPO3viNsOs1sSF86n1vHBg39FM,19
|
|
92
|
+
abstract_utilities-0.2.2.468.dist-info/RECORD,,
|
|
File without changes
|
{abstract_utilities-0.2.2.466.dist-info → abstract_utilities-0.2.2.468.dist-info}/top_level.txt
RENAMED
|
File without changes
|