abstract-utilities 0.2.2.496__py3-none-any.whl → 0.2.2.504__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 +5 -9
- abstract_utilities/class_utils/__init__.py +7 -0
- abstract_utilities/class_utils/abstract_classes.py +74 -0
- abstract_utilities/class_utils/caller_utils.py +35 -0
- abstract_utilities/class_utils/class_utils.py +109 -0
- abstract_utilities/class_utils/function_utils.py +153 -0
- abstract_utilities/class_utils/global_utils.py +56 -0
- abstract_utilities/class_utils/imports/__init__.py +2 -0
- abstract_utilities/class_utils/imports/imports.py +2 -0
- abstract_utilities/class_utils/imports/utils.py +40 -0
- abstract_utilities/class_utils/module_utils.py +63 -0
- abstract_utilities/env_utils/imports/imports.py +3 -2
- abstract_utilities/error_utils/__init__.py +2 -0
- abstract_utilities/error_utils/error_utils.py +25 -0
- abstract_utilities/error_utils/imports/__init__.py +2 -0
- abstract_utilities/error_utils/imports/imports.py +1 -0
- abstract_utilities/error_utils/imports/module_imports.py +1 -0
- abstract_utilities/file_utils/imports/imports.py +3 -18
- abstract_utilities/file_utils/imports/module_imports.py +3 -6
- abstract_utilities/file_utils/src/type_checks.py +0 -1
- abstract_utilities/hash_utils/__init__.py +2 -0
- abstract_utilities/hash_utils/hash_utils.py +5 -0
- abstract_utilities/hash_utils/imports/__init__.py +2 -0
- abstract_utilities/hash_utils/imports/imports.py +1 -0
- abstract_utilities/hash_utils/imports/module_imports.py +0 -0
- abstract_utilities/history_utils/__init__.py +2 -0
- abstract_utilities/history_utils/history_utils.py +37 -0
- abstract_utilities/history_utils/imports/__init__.py +2 -0
- abstract_utilities/history_utils/imports/imports.py +1 -0
- abstract_utilities/history_utils/imports/module_imports.py +0 -0
- abstract_utilities/import_utils/imports/imports.py +1 -1
- abstract_utilities/import_utils/imports/module_imports.py +1 -1
- abstract_utilities/import_utils/src/__init__.py +1 -1
- abstract_utilities/import_utils/src/clean_imports.py +31 -5
- abstract_utilities/import_utils/src/dot_utils.py +9 -0
- abstract_utilities/import_utils/src/package_utilss/__init__.py +139 -0
- abstract_utilities/import_utils/src/package_utilss/context_utils.py +27 -0
- abstract_utilities/import_utils/src/package_utilss/import_collectors.py +53 -0
- abstract_utilities/import_utils/src/package_utilss/path_utils.py +28 -0
- abstract_utilities/import_utils/src/package_utilss/safe_import.py +27 -0
- abstract_utilities/import_utils/src/pkg_utils.py +140 -0
- abstract_utilities/imports.py +18 -0
- abstract_utilities/json_utils/__init__.py +2 -0
- abstract_utilities/json_utils/imports/__init__.py +2 -0
- abstract_utilities/json_utils/imports/imports.py +2 -0
- abstract_utilities/json_utils/imports/module_imports.py +5 -0
- abstract_utilities/json_utils/json_utils.py +743 -0
- abstract_utilities/list_utils/__init__.py +2 -0
- abstract_utilities/list_utils/imports/__init__.py +2 -0
- abstract_utilities/list_utils/imports/imports.py +1 -0
- abstract_utilities/list_utils/imports/module_imports.py +0 -0
- abstract_utilities/list_utils/list_utils.py +199 -0
- abstract_utilities/log_utils/__init__.py +5 -0
- abstract_utilities/log_utils/abstractLogManager.py +64 -0
- abstract_utilities/log_utils/call_response.py +68 -0
- abstract_utilities/log_utils/imports/__init__.py +2 -0
- abstract_utilities/log_utils/imports/imports.py +7 -0
- abstract_utilities/log_utils/imports/module_imports.py +2 -0
- abstract_utilities/log_utils/log_file.py +56 -0
- abstract_utilities/log_utils/logger_callable.py +49 -0
- abstract_utilities/math_utils/__init__.py +2 -0
- abstract_utilities/math_utils/imports/__init__.py +2 -0
- abstract_utilities/math_utils/imports/imports.py +2 -0
- abstract_utilities/math_utils/imports/module_imports.py +1 -0
- abstract_utilities/math_utils/math_utils.py +208 -0
- abstract_utilities/parse_utils/__init__.py +2 -0
- abstract_utilities/parse_utils/imports/__init__.py +3 -0
- abstract_utilities/parse_utils/imports/constants.py +10 -0
- abstract_utilities/parse_utils/imports/imports.py +2 -0
- abstract_utilities/parse_utils/imports/module_imports.py +4 -0
- abstract_utilities/parse_utils/parse_utils.py +516 -0
- abstract_utilities/path_utils/__init__.py +2 -0
- abstract_utilities/path_utils/imports/__init__.py +2 -0
- abstract_utilities/path_utils/imports/imports.py +1 -0
- abstract_utilities/path_utils/imports/module_imports.py +6 -0
- abstract_utilities/path_utils/path_utils.py +715 -0
- abstract_utilities/path_utils.py +94 -2
- abstract_utilities/read_write_utils/__init__.py +1 -0
- abstract_utilities/read_write_utils/imports/__init__.py +2 -0
- abstract_utilities/read_write_utils/imports/imports.py +2 -0
- abstract_utilities/read_write_utils/imports/module_imports.py +5 -0
- abstract_utilities/read_write_utils/read_write_utils.py +338 -0
- abstract_utilities/read_write_utils.py +2 -4
- abstract_utilities/safe_utils/__init__.py +2 -0
- abstract_utilities/safe_utils/imports/__init__.py +3 -0
- abstract_utilities/safe_utils/imports/imports.py +1 -0
- abstract_utilities/safe_utils/imports/module_imports.py +2 -0
- abstract_utilities/safe_utils/safe_utils.py +130 -0
- abstract_utilities/ssh_utils/__init__.py +2 -1
- abstract_utilities/ssh_utils/classes.py +0 -1
- abstract_utilities/ssh_utils/cmd_utils.py +207 -0
- abstract_utilities/ssh_utils/imports/__init__.py +3 -0
- abstract_utilities/ssh_utils/imports/imports.py +5 -0
- abstract_utilities/ssh_utils/imports/module_imports.py +5 -0
- abstract_utilities/ssh_utils/imports/utils.py +189 -0
- abstract_utilities/ssh_utils/pexpect_utils.py +11 -18
- abstract_utilities/string_utils/__init__.py +4 -0
- abstract_utilities/string_utils/clean_utils.py +28 -0
- abstract_utilities/string_utils/eat_utils.py +103 -0
- abstract_utilities/string_utils/imports/__init__.py +3 -0
- abstract_utilities/string_utils/imports/imports.py +2 -0
- abstract_utilities/string_utils/imports/module_imports.py +2 -0
- abstract_utilities/string_utils/imports/utils.py +81 -0
- abstract_utilities/string_utils/replace_utils.py +27 -0
- abstract_utilities/thread_utils/__init__.py +2 -0
- abstract_utilities/thread_utils/imports/__init__.py +2 -0
- abstract_utilities/thread_utils/imports/imports.py +2 -0
- abstract_utilities/thread_utils/imports/module_imports.py +2 -0
- abstract_utilities/thread_utils/thread_utils.py +140 -0
- abstract_utilities/time_utils/__init__.py +2 -0
- abstract_utilities/time_utils/imports/__init__.py +2 -0
- abstract_utilities/time_utils/imports/imports.py +3 -0
- abstract_utilities/time_utils/imports/module_imports.py +1 -0
- abstract_utilities/time_utils/time_utils.py +392 -0
- abstract_utilities/type_utils/__init__.py +3 -0
- abstract_utilities/type_utils/alpha_utils.py +59 -0
- abstract_utilities/type_utils/imports/__init__.py +2 -0
- abstract_utilities/type_utils/imports/imports.py +4 -0
- abstract_utilities/type_utils/imports/module_imports.py +1 -0
- abstract_utilities/type_utils/num_utils.py +19 -0
- abstract_utilities/type_utils/type_utils.py +981 -0
- {abstract_utilities-0.2.2.496.dist-info → abstract_utilities-0.2.2.504.dist-info}/METADATA +1 -1
- abstract_utilities-0.2.2.504.dist-info/RECORD +229 -0
- abstract_utilities-0.2.2.496.dist-info/RECORD +0 -123
- {abstract_utilities-0.2.2.496.dist-info → abstract_utilities-0.2.2.504.dist-info}/WHEEL +0 -0
- {abstract_utilities-0.2.2.496.dist-info → abstract_utilities-0.2.2.504.dist-info}/top_level.txt +0 -0
|
@@ -6,23 +6,17 @@
|
|
|
6
6
|
# ============================================================
|
|
7
7
|
|
|
8
8
|
|
|
9
|
-
import
|
|
10
|
-
import sys, importlib,os
|
|
11
|
-
import sys, importlib, os, inspect
|
|
9
|
+
from ...imports import *
|
|
12
10
|
from pathlib import Path
|
|
13
|
-
|
|
11
|
+
|
|
14
12
|
|
|
15
13
|
|
|
16
14
|
|
|
17
15
|
from typing import *
|
|
18
|
-
import re
|
|
19
16
|
|
|
20
17
|
from typing import *
|
|
21
18
|
from types import MethodType
|
|
22
|
-
|
|
23
|
-
import os,tempfile,shutil,logging,ezodf,fnmatch,pytesseract,pdfplumber
|
|
24
|
-
import pandas as pd
|
|
25
|
-
import geopandas as gpd
|
|
19
|
+
|
|
26
20
|
from datetime import datetime
|
|
27
21
|
|
|
28
22
|
from typing import *
|
|
@@ -30,8 +24,6 @@ from werkzeug.utils import secure_filename
|
|
|
30
24
|
from werkzeug.datastructures import FileStorage
|
|
31
25
|
from pdf2image import convert_from_path # only used for OCR fallback
|
|
32
26
|
# ---- 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
27
|
|
|
36
28
|
from datetime import datetime
|
|
37
29
|
from types import ModuleType
|
|
@@ -44,18 +36,11 @@ from typing import (
|
|
|
44
36
|
)
|
|
45
37
|
|
|
46
38
|
# ---- 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
39
|
from pdf2image import convert_from_path
|
|
54
40
|
from werkzeug.utils import secure_filename
|
|
55
41
|
from werkzeug.datastructures import FileStorage
|
|
56
42
|
|
|
57
43
|
# ---- Helpers ------------------------------------------------
|
|
58
|
-
import textwrap as tw
|
|
59
44
|
from pprint import pprint
|
|
60
45
|
|
|
61
46
|
# ============================================================
|
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
from .imports import *
|
|
2
|
-
from ...string_clean import eatAll
|
|
3
1
|
from ...list_utils import make_list
|
|
4
2
|
from ...type_utils import get_media_exts, is_media_type, MIME_TYPES, is_str
|
|
5
3
|
from ...ssh_utils import *
|
|
6
4
|
from ...env_utils import *
|
|
7
|
-
from ...read_write_utils import
|
|
8
|
-
from ...abstract_classes import SingletonMeta
|
|
5
|
+
from ...read_write_utils import read_from_file,write_to_file
|
|
9
6
|
from ...log_utils import get_logFile
|
|
10
|
-
from ...class_utils import get_caller, get_caller_path, get_caller_dir
|
|
7
|
+
from ...class_utils import get_caller, get_caller_path, get_caller_dir,SingletonMeta,run_pruned_func
|
|
11
8
|
from ...ssh_utils import run_cmd
|
|
12
|
-
from ...string_utils import get_from_kwargs
|
|
9
|
+
from ...string_utils import get_from_kwargs,eatAll
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from ...imports import hashlib
|
|
File without changes
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
class HistoryManager:
|
|
2
|
+
def __init__(self):
|
|
3
|
+
self.history_names = {}
|
|
4
|
+
|
|
5
|
+
def add_history_name(self, name, initial_data=''):
|
|
6
|
+
self.history_names[name] = {"history": [initial_data], "history_redo": []}
|
|
7
|
+
return name
|
|
8
|
+
|
|
9
|
+
def transfer_state(self, primary_data, secondary_data):
|
|
10
|
+
"""
|
|
11
|
+
Transfer the latest state from primary_data to secondary_data
|
|
12
|
+
Returns the modified primary and secondary data lists
|
|
13
|
+
"""
|
|
14
|
+
self.last_data = None
|
|
15
|
+
# If there's data in primary, transfer the latest to secondary
|
|
16
|
+
if primary_data:
|
|
17
|
+
self.last_data = primary_data.pop()
|
|
18
|
+
secondary_data.append(self.last_data)
|
|
19
|
+
|
|
20
|
+
return primary_data, secondary_data
|
|
21
|
+
|
|
22
|
+
def add_to_history(self, name, data):
|
|
23
|
+
# Clear the redo history when a new state is added
|
|
24
|
+
self.history_names[name]['history_redo'] = []
|
|
25
|
+
self.history_names[name]['history'].append(data)
|
|
26
|
+
|
|
27
|
+
def redo(self, name):
|
|
28
|
+
# Redo by transferring state from redo history to actual history
|
|
29
|
+
self.history_names[name]["history_redo"], self.history_names[name]["history"] = self.transfer_state(
|
|
30
|
+
self.history_names[name]["history_redo"], self.history_names[name]["history"])
|
|
31
|
+
return self.last_data
|
|
32
|
+
|
|
33
|
+
def undo(self, name):
|
|
34
|
+
# Undo by transferring state from actual history to redo history
|
|
35
|
+
self.history_names[name]["history"], self.history_names[name]["history_redo"] = self.transfer_state(
|
|
36
|
+
self.history_names[name]["history"], self.history_names[name]["history_redo"])
|
|
37
|
+
return self.last_data
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
File without changes
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
from ...read_write_utils import read_from_file,write_to_file,get_text_or_read
|
|
2
|
-
from ...
|
|
2
|
+
from ...string_utils import eatAll,eatInner,eatElse,clean_line
|
|
3
3
|
from ...class_utils import get_caller_path
|
|
4
4
|
from ...list_utils import make_list
|
|
5
5
|
from ...path_utils import get_file_parts
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
from ..imports import *
|
|
2
|
-
|
|
3
|
-
from .package_utils import *
|
|
2
|
+
from .pkg_utils import *
|
|
4
3
|
|
|
5
4
|
def get_text_or_read(text=None,file_path=None):
|
|
6
5
|
text = text or ''
|
|
@@ -54,7 +53,8 @@ def get_all_imports(text=None,file_path=None,import_pkg_js=None):
|
|
|
54
53
|
if is_from_line_group(line) and is_from_group == False:
|
|
55
54
|
is_from_group=get_import_pkg(line)
|
|
56
55
|
return import_pkg_js
|
|
57
|
-
|
|
56
|
+
|
|
57
|
+
def clean_imports(text=None,file_path=None,import_pkg_js=None,fill_nulines=False):
|
|
58
58
|
if text and os.path.isfile(text):
|
|
59
59
|
file_path = text
|
|
60
60
|
text = read_from_file(text)
|
|
@@ -65,7 +65,7 @@ def clean_all_imports(text=None,file_path=None,import_pkg_js=None):
|
|
|
65
65
|
for pkg,values in import_pkg_js.items():
|
|
66
66
|
comments = []
|
|
67
67
|
if pkg not in ["context"]:
|
|
68
|
-
|
|
68
|
+
|
|
69
69
|
imports = values.get('imports')
|
|
70
70
|
for i,imp in enumerate(imports):
|
|
71
71
|
if '#' in imp:
|
|
@@ -81,9 +81,35 @@ def clean_all_imports(text=None,file_path=None,import_pkg_js=None):
|
|
|
81
81
|
comments=','.join(comments)
|
|
82
82
|
imports+=f" #{comments}"
|
|
83
83
|
import_pkg_js[pkg]["imports"]=imports
|
|
84
|
-
|
|
84
|
+
if fill_nulines:
|
|
85
|
+
line = values.get('line')
|
|
86
|
+
if len(nu_lines) >= line:
|
|
87
|
+
nu_lines[line] += imports
|
|
88
|
+
return import_pkg_js
|
|
89
|
+
def clean_all_imports(text=None,file_path=None,import_pkg_js=None,fill_nulines=False):
|
|
90
|
+
clean_imports(text=text,file_path=file_path,import_pkg_js=import_pkg_js,fill_nulines=import_pkg_js)
|
|
85
91
|
import_pkg_js["context"]["nulines"]=nu_lines
|
|
86
92
|
return import_pkg_js
|
|
93
|
+
def get_clean_import_string(import_pkg_js,fill_nulines=False,get_locals=False):
|
|
94
|
+
import_pkg_js = clean_imports(import_pkg_js=import_pkg_js,fill_nulines=fill_nulines)
|
|
95
|
+
import_ls = []
|
|
96
|
+
for key,values in import_pkg_js.items():
|
|
97
|
+
if key not in ['context','nulines']:
|
|
98
|
+
imports = None
|
|
99
|
+
input(values)
|
|
100
|
+
imp_values= values.get('imports')
|
|
101
|
+
if key == 'import':
|
|
102
|
+
imports = f'import {imp_values}'
|
|
103
|
+
elif get_locals or not key.startswith('.'):
|
|
104
|
+
imports = f'from {key} import {imp_values}'
|
|
105
|
+
if imports:
|
|
106
|
+
import_ls.append(imports)
|
|
107
|
+
return '\n'.join(import_ls)
|
|
108
|
+
def get_clean_imports_from_files(files):
|
|
109
|
+
import_pkg_js={}
|
|
110
|
+
for file in files:
|
|
111
|
+
import_pkg_js = get_all_imports(file,import_pkg_js=import_pkg_js)
|
|
112
|
+
return get_clean_import_string(import_pkg_js)
|
|
87
113
|
def get_dot_fro_line(line,dirname):
|
|
88
114
|
from_line = line.split(FROM_TAG)[-1]
|
|
89
115
|
dot_fro = ""
|
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
from ..imports import *
|
|
2
|
+
def get_Path(path):
|
|
3
|
+
if isinstance(path,str):
|
|
4
|
+
path = Path(str(path))
|
|
5
|
+
return path
|
|
2
6
|
def get_dot_range(filepath=None,list_obj=None):
|
|
3
7
|
imports = make_list(list_obj) or extract_imports(filepath)
|
|
4
8
|
highest=0
|
|
@@ -14,6 +18,8 @@ def dotted_from(file: Path, sysroot: Path) -> str:
|
|
|
14
18
|
e.g. /repo/abstract_ide/consoles/launcherWindowTab/functions/core_utils.py
|
|
15
19
|
sysroot=/repo -> abstract_ide.consoles.launcherWindowTab.functions.core_utils
|
|
16
20
|
"""
|
|
21
|
+
file = get_Path(file)
|
|
22
|
+
sysroot = get_Path(sysroot)
|
|
17
23
|
file = file.resolve()
|
|
18
24
|
stem = file.with_suffix("") # drop .py
|
|
19
25
|
return ".".join(stem.relative_to(sysroot).parts)
|
|
@@ -23,6 +29,8 @@ def to_dotted_name(file_path: Path, top_package: str) -> str:
|
|
|
23
29
|
-> abstract_ide.consoles.launcherWindowTab.functions.core_utils
|
|
24
30
|
"""
|
|
25
31
|
# find the index of the top_package in the path parts
|
|
32
|
+
file_path = get_Path(file_path)
|
|
33
|
+
top_package = get_Path(top_package)
|
|
26
34
|
parts = file_path.resolve().parts
|
|
27
35
|
i = None
|
|
28
36
|
if is_number(top_package):
|
|
@@ -43,6 +51,7 @@ def compute_dotted_and_sysroot(file_path: Path) -> Tuple[str, Path]:
|
|
|
43
51
|
sysroot will be the directory *above* the top package.
|
|
44
52
|
If not a package, we fall back to a repo-ish root guess (2 parents up).
|
|
45
53
|
"""
|
|
54
|
+
file_path = get_Path(file_path)
|
|
46
55
|
file_path = file_path.resolve()
|
|
47
56
|
stem = file_path.with_suffix("") # drop .py
|
|
48
57
|
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import ast
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
from importlib.util import find_spec
|
|
4
|
+
from typing import Dict, Set
|
|
5
|
+
import ast, sys
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from importlib.util import find_spec
|
|
8
|
+
from typing import Dict, Set
|
|
9
|
+
from src.abstract_utilities.import_utils import *
|
|
10
|
+
STDLIB_NAMES = set(sys.builtin_module_names)
|
|
11
|
+
def parse_imports(file_path: Path):
|
|
12
|
+
"""Return list of (module, level) for every import/from-import."""
|
|
13
|
+
try:
|
|
14
|
+
src = file_path.read_text(errors="ignore")
|
|
15
|
+
tree = ast.parse(src, filename=str(file_path))
|
|
16
|
+
except Exception:
|
|
17
|
+
return []
|
|
18
|
+
imports = []
|
|
19
|
+
for node in ast.walk(tree):
|
|
20
|
+
if isinstance(node, ast.Import):
|
|
21
|
+
for alias in node.names:
|
|
22
|
+
imports.append((alias.name, 0))
|
|
23
|
+
elif isinstance(node, ast.ImportFrom):
|
|
24
|
+
imports.append((node.module, node.level))
|
|
25
|
+
return imports
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def resolve_relative_import(base_file: Path, module: str | None, level: int) -> Path | None:
|
|
29
|
+
"""Follow a relative import path to its real file if it exists."""
|
|
30
|
+
base = base_file.parent
|
|
31
|
+
for _ in range(level - 1):
|
|
32
|
+
base = base.parent
|
|
33
|
+
if not module:
|
|
34
|
+
target = base
|
|
35
|
+
else:
|
|
36
|
+
target = base / module.replace(".", "/")
|
|
37
|
+
if (target / "__init__.py").exists():
|
|
38
|
+
return target / "__init__.py"
|
|
39
|
+
if target.with_suffix(".py").exists():
|
|
40
|
+
return target.with_suffix(".py")
|
|
41
|
+
return None
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def classify_import(mod_name: str, root_pkg: str) -> str:
|
|
47
|
+
"""Return 'local', 'internal', or 'external'."""
|
|
48
|
+
if not mod_name:
|
|
49
|
+
return "unknown"
|
|
50
|
+
if mod_name.startswith("."):
|
|
51
|
+
return "local"
|
|
52
|
+
if mod_name.split(".")[0] == root_pkg:
|
|
53
|
+
return "internal"
|
|
54
|
+
if mod_name.split(".")[0] in STDLIB_NAMES:
|
|
55
|
+
return "stdlib"
|
|
56
|
+
return "external"
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def follow_imports(entry: Path, root_pkg: str,
|
|
60
|
+
visited: Dict[Path, Dict[str, Set[str]]] | None = None):
|
|
61
|
+
"""
|
|
62
|
+
Recursively follow only internal/local imports.
|
|
63
|
+
Returns {file_path: {'internal': set(), 'external': set()}}
|
|
64
|
+
"""
|
|
65
|
+
visited = visited or {}
|
|
66
|
+
if entry in visited:
|
|
67
|
+
return visited
|
|
68
|
+
|
|
69
|
+
visited[entry] = {"internal": set(), "external": set()}
|
|
70
|
+
|
|
71
|
+
for mod, level in parse_imports(entry):
|
|
72
|
+
if level > 0:
|
|
73
|
+
target = resolve_relative_import(entry, mod, level)
|
|
74
|
+
if target:
|
|
75
|
+
visited[entry]["internal"].add(str(target))
|
|
76
|
+
follow_imports(target, root_pkg, visited)
|
|
77
|
+
continue
|
|
78
|
+
|
|
79
|
+
category = classify_import(mod, root_pkg)
|
|
80
|
+
if category == "internal":
|
|
81
|
+
spec = find_spec(mod)
|
|
82
|
+
if spec and spec.origin and spec.origin.endswith(".py"):
|
|
83
|
+
visited[entry]["internal"].add(spec.origin)
|
|
84
|
+
follow_imports(Path(spec.origin), root_pkg, visited)
|
|
85
|
+
elif category == "external":
|
|
86
|
+
visited[entry]["external"].add(mod)
|
|
87
|
+
elif category == "stdlib":
|
|
88
|
+
# stdlib gets treated like external but labeled
|
|
89
|
+
visited[entry]["external"].add(mod + " # stdlib")
|
|
90
|
+
|
|
91
|
+
return visited
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def build_master_imports(entry: Path, root_pkg: str, output: Path):
|
|
96
|
+
trace = follow_imports(entry, root_pkg)
|
|
97
|
+
lines = ["# Auto-generated master imports for abstract_utilities\n"]
|
|
98
|
+
all_modules = set()
|
|
99
|
+
external_modules = set()
|
|
100
|
+
imports = get_all_imports(path)
|
|
101
|
+
for _, data in trace.items():
|
|
102
|
+
for dep in data["internal"]:
|
|
103
|
+
path = Path(dep)
|
|
104
|
+
if path.suffix != ".py":
|
|
105
|
+
continue
|
|
106
|
+
try:
|
|
107
|
+
rel_parts = path.with_suffix("").parts
|
|
108
|
+
idx = rel_parts.index(root_pkg)
|
|
109
|
+
dotted = ".".join(rel_parts[idx:])
|
|
110
|
+
all_modules.add(dotted)
|
|
111
|
+
except ValueError:
|
|
112
|
+
continue
|
|
113
|
+
external_modules.update(data["external"])
|
|
114
|
+
|
|
115
|
+
for mod in sorted(all_modules):
|
|
116
|
+
short = mod.split(".", 1)[-1]
|
|
117
|
+
lines.append(f"from .{short} import *")
|
|
118
|
+
|
|
119
|
+
if external_modules:
|
|
120
|
+
lines.append("\n# External / stdlib imports (not traced, for reference)")
|
|
121
|
+
for ext in sorted(external_modules):
|
|
122
|
+
lines.append(f"# {ext}")
|
|
123
|
+
|
|
124
|
+
output.parent.mkdir(parents=True, exist_ok=True)
|
|
125
|
+
output.write_text("\n".join(lines+str(imports)))
|
|
126
|
+
print(f"✅ wrote master imports hub → {output}")
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
if __name__ == "__main__":
|
|
131
|
+
entry = Path(
|
|
132
|
+
"/home/flerb/Documents/pythonTools/modules/src/modules/abstract_utilities/src/"
|
|
133
|
+
"abstract_utilities/import_utils/src/import_functions.py"
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
pkg = "abstract_utilities"
|
|
138
|
+
out = entry.parents[4] / "imports" / "__init__.py"
|
|
139
|
+
build_master_imports(entry, pkg, out)
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import sys, os
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
from .path_utils import find_top_package_dir, derive_package_for_file
|
|
4
|
+
|
|
5
|
+
def ensure_package_context(file: str):
|
|
6
|
+
"""Ensure that running this file directly gives correct package context."""
|
|
7
|
+
file = file or os.getcwd()
|
|
8
|
+
here = Path(file).resolve()
|
|
9
|
+
top_pkg_dir = find_top_package_dir(here)
|
|
10
|
+
if not top_pkg_dir:
|
|
11
|
+
raise RuntimeError(f"No package context above {here}. Add __init__.py files up the tree.")
|
|
12
|
+
|
|
13
|
+
sysroot = top_pkg_dir.parent
|
|
14
|
+
if str(sysroot) not in sys.path:
|
|
15
|
+
sys.path.insert(0, str(sysroot))
|
|
16
|
+
|
|
17
|
+
parts = here.with_suffix("").relative_to(sysroot).parts
|
|
18
|
+
pkg_name = ".".join(parts[:-1])
|
|
19
|
+
if (__name__ == "__main__") and not globals().get("__package__"):
|
|
20
|
+
globals()["__package__"] = pkg_name
|
|
21
|
+
|
|
22
|
+
def ensure_caller_package(caller_file: str, caller_globals: dict | None = None) -> str:
|
|
23
|
+
"""Ensure sysroot is on sys.path and return caller's dotted package name."""
|
|
24
|
+
pkg, _ = derive_package_for_file(caller_file)
|
|
25
|
+
if caller_globals and caller_globals.get("__name__") == "__main__" and not caller_globals.get("__package__"):
|
|
26
|
+
caller_globals["__package__"] = pkg
|
|
27
|
+
return pkg
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
from ..imports import *
|
|
2
|
+
from .import_functions import *
|
|
3
|
+
|
|
4
|
+
def ensure_import_pkg_js(import_pkg_js=None, file_path=None):
|
|
5
|
+
import_pkg_js = import_pkg_js or {"context": {}}
|
|
6
|
+
if "context" not in import_pkg_js:
|
|
7
|
+
import_pkg_js["context"] = {}
|
|
8
|
+
for key in ["nulines", "file_path", "all_data"]:
|
|
9
|
+
import_pkg_js["context"].setdefault(key, [] if key != "file_path" else None)
|
|
10
|
+
|
|
11
|
+
if file_path and file_path != import_pkg_js["context"].get("file_path"):
|
|
12
|
+
found = False
|
|
13
|
+
nu_data = {
|
|
14
|
+
"file_path": import_pkg_js["context"]["file_path"],
|
|
15
|
+
"nulines": import_pkg_js["context"]["nulines"]
|
|
16
|
+
}
|
|
17
|
+
for i, data in enumerate(import_pkg_js["context"]["all_data"]):
|
|
18
|
+
if data.get("file_path") == import_pkg_js["context"]["file_path"]:
|
|
19
|
+
import_pkg_js["context"]["all_data"][i] = nu_data
|
|
20
|
+
found = True
|
|
21
|
+
break
|
|
22
|
+
if not found:
|
|
23
|
+
import_pkg_js["context"]["all_data"].append(nu_data)
|
|
24
|
+
import_pkg_js["context"]["nulines"] = []
|
|
25
|
+
import_pkg_js["context"]["file_path"] = file_path
|
|
26
|
+
return import_pkg_js
|
|
27
|
+
|
|
28
|
+
def add_imports_to_import_pkg_js(import_pkg, imports, import_pkg_js=None):
|
|
29
|
+
import_pkg_js = ensure_import_pkg_js(import_pkg_js)
|
|
30
|
+
imports = clean_imports(imports)
|
|
31
|
+
if import_pkg not in import_pkg_js:
|
|
32
|
+
i = len(import_pkg_js["context"]["nulines"])
|
|
33
|
+
file_path = import_pkg_js["context"]["file_path"]
|
|
34
|
+
file_parts = get_file_parts(file_path)
|
|
35
|
+
dirname = file_parts["dirname"]
|
|
36
|
+
import_pkg_js[import_pkg] = {"imports": imports, "line": i}
|
|
37
|
+
import_pkg_js["context"]["nulines"].append(f"from {import_pkg} import ")
|
|
38
|
+
else:
|
|
39
|
+
import_pkg_js[import_pkg]["imports"] += imports
|
|
40
|
+
return import_pkg_js
|
|
41
|
+
|
|
42
|
+
def update_import_pkg_js(line, import_pkg_js=None):
|
|
43
|
+
import_pkg_js = ensure_import_pkg_js(import_pkg_js)
|
|
44
|
+
if is_line_group_import(line):
|
|
45
|
+
import_pkg = get_import_pkg(line)
|
|
46
|
+
imports = get_imports_from_import_pkg(line)
|
|
47
|
+
import_pkg_js = add_imports_to_import_pkg_js(import_pkg, imports, import_pkg_js=import_pkg_js)
|
|
48
|
+
else:
|
|
49
|
+
if import_pkg_js["context"]["nulines"] and line == "" and is_line_import(import_pkg_js["context"]["nulines"][-1]):
|
|
50
|
+
pass
|
|
51
|
+
else:
|
|
52
|
+
import_pkg_js["context"]["nulines"].append(line)
|
|
53
|
+
return import_pkg_js
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import os, sys
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
|
|
4
|
+
def find_top_package_dir(p: Path) -> Path | None:
|
|
5
|
+
"""Walk upward until the topmost __init__.py-containing directory."""
|
|
6
|
+
p = p.resolve()
|
|
7
|
+
if p.is_file():
|
|
8
|
+
p = p.parent
|
|
9
|
+
top = None
|
|
10
|
+
while (p / "__init__.py").exists():
|
|
11
|
+
top = p
|
|
12
|
+
if p.parent == p:
|
|
13
|
+
break
|
|
14
|
+
p = p.parent
|
|
15
|
+
return top
|
|
16
|
+
|
|
17
|
+
def derive_package_for_file(file: str) -> tuple[str, Path]:
|
|
18
|
+
"""Return (pkg_name, sysroot) for the module file."""
|
|
19
|
+
here = Path(file).resolve()
|
|
20
|
+
top_pkg_dir = find_top_package_dir(here)
|
|
21
|
+
if not top_pkg_dir:
|
|
22
|
+
raise RuntimeError(f"No package context above {here}. Add __init__.py up the tree.")
|
|
23
|
+
sysroot = top_pkg_dir.parent
|
|
24
|
+
if str(sysroot) not in sys.path:
|
|
25
|
+
sys.path.insert(0, str(sysroot))
|
|
26
|
+
parts = here.with_suffix("").relative_to(sysroot).parts
|
|
27
|
+
pkg_name = ".".join(parts[:-1])
|
|
28
|
+
return pkg_name, sysroot
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import inspect, importlib
|
|
2
|
+
from .context_utils import ensure_caller_package
|
|
3
|
+
|
|
4
|
+
def safe_import(
|
|
5
|
+
name: str,
|
|
6
|
+
*,
|
|
7
|
+
member: str | None = None,
|
|
8
|
+
package: str | None = None,
|
|
9
|
+
file: str | None = None,
|
|
10
|
+
caller_globals: dict | None = None,
|
|
11
|
+
):
|
|
12
|
+
"""
|
|
13
|
+
Safe dynamic import that resolves relative imports when run as a script.
|
|
14
|
+
"""
|
|
15
|
+
if file is None:
|
|
16
|
+
frame = inspect.currentframe()
|
|
17
|
+
assert frame is not None
|
|
18
|
+
outer = frame.f_back
|
|
19
|
+
caller_file = (outer.f_globals.get("__file__") if outer else None) or __file__
|
|
20
|
+
else:
|
|
21
|
+
caller_file = file
|
|
22
|
+
|
|
23
|
+
if name.startswith(".") and not package:
|
|
24
|
+
package = ensure_caller_package(caller_file, caller_globals=caller_globals)
|
|
25
|
+
|
|
26
|
+
mod = importlib.import_module(name, package=package)
|
|
27
|
+
return getattr(mod, member) if member else mod
|