abstract-utilities 0.2.2.627__py3-none-any.whl → 0.2.2.700__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 +7 -3
- abstract_utilities/class_utils/abstract_classes.py +104 -34
- abstract_utilities/class_utils/caller_utils.py +38 -0
- abstract_utilities/class_utils/imports/imports.py +1 -1
- abstract_utilities/file_utils/imports/classes.py +59 -55
- abstract_utilities/file_utils/imports/module_imports.py +1 -1
- abstract_utilities/file_utils/src/file_filters/__init__.py +0 -3
- abstract_utilities/file_utils/src/file_filters/ensure_utils.py +382 -10
- abstract_utilities/file_utils/src/file_filters/filter_params.py +64 -0
- abstract_utilities/file_utils/src/file_filters/predicate_utils.py +2 -74
- abstract_utilities/file_utils/src/find_collect.py +10 -0
- abstract_utilities/import_utils/imports/__init__.py +1 -1
- abstract_utilities/import_utils/imports/init_imports.py +3 -0
- abstract_utilities/import_utils/imports/module_imports.py +1 -0
- abstract_utilities/import_utils/imports/utils.py +1 -1
- abstract_utilities/import_utils/src/__init__.py +1 -0
- abstract_utilities/import_utils/src/import_utils.py +39 -0
- abstract_utilities/import_utils/src/layze_import_utils/__init__.py +2 -0
- abstract_utilities/import_utils/src/layze_import_utils/lazy_utils.py +41 -0
- abstract_utilities/import_utils/src/layze_import_utils/nullProxy.py +37 -0
- abstract_utilities/import_utils/src/nullProxy.py +30 -0
- abstract_utilities/imports.py +5 -2
- abstract_utilities/json_utils/imports/imports.py +1 -1
- abstract_utilities/json_utils/json_utils.py +37 -3
- abstract_utilities/list_utils/list_utils.py +3 -0
- abstract_utilities/log_utils/log_file.py +82 -27
- abstract_utilities/path_utils/imports/module_imports.py +1 -1
- abstract_utilities/path_utils/path_utils.py +7 -12
- abstract_utilities/read_write_utils/read_write_utils.py +63 -30
- abstract_utilities/type_utils/__init__.py +5 -1
- abstract_utilities/type_utils/get_type.py +120 -0
- abstract_utilities/type_utils/imports/__init__.py +1 -0
- abstract_utilities/type_utils/imports/constants.py +134 -0
- abstract_utilities/type_utils/imports/module_imports.py +25 -1
- abstract_utilities/type_utils/is_type.py +455 -0
- abstract_utilities/type_utils/make_type.py +126 -0
- abstract_utilities/type_utils/mime_types.py +68 -0
- abstract_utilities/type_utils/type_utils.py +0 -877
- {abstract_utilities-0.2.2.627.dist-info → abstract_utilities-0.2.2.700.dist-info}/METADATA +1 -1
- {abstract_utilities-0.2.2.627.dist-info → abstract_utilities-0.2.2.700.dist-info}/RECORD +42 -32
- {abstract_utilities-0.2.2.627.dist-info → abstract_utilities-0.2.2.700.dist-info}/WHEEL +0 -0
- {abstract_utilities-0.2.2.627.dist-info → abstract_utilities-0.2.2.700.dist-info}/top_level.txt +0 -0
abstract_utilities/__init__.py
CHANGED
|
@@ -32,7 +32,8 @@ from .json_utils import (unified_json_loader,
|
|
|
32
32
|
safe_write_to_file,
|
|
33
33
|
safe_save_updated_json_data,
|
|
34
34
|
get_result_from_data,
|
|
35
|
-
flatten_json
|
|
35
|
+
flatten_json,
|
|
36
|
+
to_json_safe
|
|
36
37
|
)
|
|
37
38
|
|
|
38
39
|
from .directory_utils import *
|
|
@@ -45,7 +46,9 @@ from .list_utils import (get_highest_value_obj,
|
|
|
45
46
|
compare_lists,
|
|
46
47
|
get_symetric_difference,
|
|
47
48
|
list_set,
|
|
48
|
-
make_list_it
|
|
49
|
+
make_list_it,
|
|
50
|
+
get_single_from_list
|
|
51
|
+
)
|
|
49
52
|
from .time_utils import (get_time_stamp,
|
|
50
53
|
get_sleep,
|
|
51
54
|
sleep_count_down,
|
|
@@ -83,7 +86,8 @@ from .type_utils import (make_bool,
|
|
|
83
86
|
get_numbers_comp,
|
|
84
87
|
is_any_instance,
|
|
85
88
|
break_string,
|
|
86
|
-
MIME_TYPES
|
|
89
|
+
MIME_TYPES,
|
|
90
|
+
get_if_None
|
|
87
91
|
)
|
|
88
92
|
get_media_types = get_all_types = get_all_file_types
|
|
89
93
|
from .math_utils import (convert_to_percentage,
|
|
@@ -26,47 +26,117 @@ def get_inputs(cls, *args, **kwargs):
|
|
|
26
26
|
return cls(**values)
|
|
27
27
|
|
|
28
28
|
|
|
29
|
+
from abstract_utilities import *
|
|
30
|
+
class SingletonMeta(type):
|
|
31
|
+
_instances = {}
|
|
32
|
+
def __call__(cls, *args, **kwargs):
|
|
33
|
+
if cls not in cls._instances:
|
|
34
|
+
cls._instances[cls] = super(SingletonMeta, cls).__call__(*args, **kwargs)
|
|
35
|
+
return cls._instances[cls]
|
|
36
|
+
|
|
37
|
+
def get_inputs(cls, *args, **kwargs):
|
|
38
|
+
"""
|
|
39
|
+
Dynamically construct a dataclass instance from args and kwargs,
|
|
40
|
+
filling missing values from defaults in the dataclass.
|
|
41
|
+
"""
|
|
42
|
+
fields = list(cls.__annotations__.keys())
|
|
43
|
+
values = {}
|
|
44
|
+
|
|
45
|
+
args = list(args)
|
|
46
|
+
for field in fields:
|
|
47
|
+
if field in kwargs:
|
|
48
|
+
values[field] = kwargs[field]
|
|
49
|
+
elif args:
|
|
50
|
+
values[field] = args.pop(0)
|
|
51
|
+
else:
|
|
52
|
+
values[field] = getattr(cls(), field) # default from dataclass
|
|
53
|
+
|
|
54
|
+
return cls(*args,**values)
|
|
55
|
+
def get_input_params(func):
|
|
56
|
+
sig = inspect.signature(func)
|
|
57
|
+
return sig.parameters
|
|
58
|
+
def get_args(func, *args,**kwargs):
|
|
59
|
+
parameters = get_input_params(func)
|
|
60
|
+
parameters = dict(parameters)
|
|
61
|
+
for key,value in parameters.items():
|
|
62
|
+
value = str(value)
|
|
63
|
+
if value.startswith('**'):
|
|
64
|
+
kwargs_key = key
|
|
65
|
+
elif value.startswith('*'):
|
|
66
|
+
args_key = key
|
|
67
|
+
kwargs_copy = kwargs.copy()
|
|
68
|
+
for k_key,k_value in kwargs.items():
|
|
69
|
+
if args_key == k_key and isinstance(k_value,list or tuple or set):
|
|
70
|
+
args = args | tuple(k_value)
|
|
71
|
+
del kwargs[k_key]
|
|
72
|
+
return args,kwargs
|
|
73
|
+
|
|
29
74
|
def prune_inputs(func, *args, **kwargs):
|
|
30
75
|
"""
|
|
31
|
-
|
|
32
|
-
|
|
76
|
+
Smart argument adapter:
|
|
77
|
+
- Detects if func accepts *args or **kwargs
|
|
78
|
+
- Builds new positional arguments from kwargs when appropriate
|
|
79
|
+
- Handles explicit {"args": [...]} convention
|
|
33
80
|
"""
|
|
81
|
+
|
|
34
82
|
sig = inspect.signature(func)
|
|
35
83
|
params = sig.parameters
|
|
36
84
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
new_args.extend(
|
|
50
|
-
break
|
|
85
|
+
has_varargs = any(p.kind == inspect.Parameter.VAR_POSITIONAL for p in params.values())
|
|
86
|
+
has_varkw = any(p.kind == inspect.Parameter.VAR_KEYWORD for p in params.values())
|
|
87
|
+
|
|
88
|
+
new_args = list(args)
|
|
89
|
+
new_kwargs = dict(kwargs)
|
|
90
|
+
|
|
91
|
+
# -----------------------------------------------------------
|
|
92
|
+
# 1. If user provided explicit args: {"args": [...]}
|
|
93
|
+
# -----------------------------------------------------------
|
|
94
|
+
if "args" in new_kwargs:
|
|
95
|
+
explicit_args = new_kwargs.pop("args")
|
|
96
|
+
if isinstance(explicit_args, (list, tuple)):
|
|
97
|
+
new_args.extend(explicit_args)
|
|
51
98
|
else:
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
#
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
99
|
+
new_args.append(explicit_args)
|
|
100
|
+
|
|
101
|
+
# -----------------------------------------------------------
|
|
102
|
+
# 2. If function has *args, infer which kwargs belong there
|
|
103
|
+
# -----------------------------------------------------------
|
|
104
|
+
if has_varargs:
|
|
105
|
+
|
|
106
|
+
# Heuristic rules for upgrading kwargs to args:
|
|
107
|
+
# - if the function has NO named params, treat all scalar kwargs as positional
|
|
108
|
+
# - common param names like "file_path" also qualify
|
|
109
|
+
preferred_as_args = {"path", "file", "file_path", "filename", "value"}
|
|
110
|
+
|
|
111
|
+
positional_candidates = []
|
|
112
|
+
|
|
113
|
+
for k in list(new_kwargs.keys()):
|
|
114
|
+
v = new_kwargs[k]
|
|
115
|
+
|
|
116
|
+
# candidate rules:
|
|
117
|
+
if k in preferred_as_args:
|
|
118
|
+
positional_candidates.append(v)
|
|
119
|
+
del new_kwargs[k]
|
|
120
|
+
|
|
121
|
+
# scalars but not mappings/lists (optional)
|
|
122
|
+
elif isinstance(v, (str, int, float)) and len(positional_candidates) == 0:
|
|
123
|
+
positional_candidates.append(v)
|
|
124
|
+
del new_kwargs[k]
|
|
125
|
+
|
|
126
|
+
new_args.extend(positional_candidates)
|
|
127
|
+
|
|
128
|
+
# -----------------------------------------------------------
|
|
129
|
+
# 3. If function does NOT accept **kwargs → strip extras
|
|
130
|
+
# -----------------------------------------------------------
|
|
131
|
+
if not has_varkw:
|
|
132
|
+
allowed = {
|
|
133
|
+
name for name, p in params.items()
|
|
134
|
+
if p.kind in (
|
|
135
|
+
inspect.Parameter.POSITIONAL_OR_KEYWORD,
|
|
136
|
+
inspect.Parameter.KEYWORD_ONLY
|
|
137
|
+
)
|
|
138
|
+
}
|
|
139
|
+
new_kwargs = {k: v for k, v in new_kwargs.items() if k in allowed}
|
|
70
140
|
|
|
71
141
|
return tuple(new_args), new_kwargs
|
|
72
142
|
def run_pruned_func(func, *args, **kwargs):
|
|
@@ -52,3 +52,41 @@ def get_caller_dir(i: Optional[int] = None) -> str:
|
|
|
52
52
|
depth = 1 if i is None else int(i)
|
|
53
53
|
abspath = get_caller_path(depth + 1)
|
|
54
54
|
return os.path.dirname(abspath)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def get_original_caller_dir(levels_up: int = None) -> Path:
|
|
58
|
+
"""
|
|
59
|
+
Return the directory of the *original* caller in the call stack.
|
|
60
|
+
|
|
61
|
+
levels_up:
|
|
62
|
+
- None → automatically goes to the bottom-most user-level caller.
|
|
63
|
+
- N → manually walk up N frames for custom behavior.
|
|
64
|
+
|
|
65
|
+
Returns:
|
|
66
|
+
Path object pointing to caller's directory.
|
|
67
|
+
"""
|
|
68
|
+
|
|
69
|
+
stack = inspect.stack()
|
|
70
|
+
|
|
71
|
+
# If the user specifies an exact depth
|
|
72
|
+
if levels_up is not None:
|
|
73
|
+
target = min(levels_up + 1, len(stack) - 1)
|
|
74
|
+
frame = stack[target]
|
|
75
|
+
return Path(frame.filename).resolve().parent
|
|
76
|
+
|
|
77
|
+
# Otherwise, auto-detect the FIRST file that isn't inside site-packages or abstract_* utilities
|
|
78
|
+
for frameinfo in reversed(stack):
|
|
79
|
+
file_path = Path(frameinfo.filename).resolve()
|
|
80
|
+
|
|
81
|
+
# Skip internal interpreter/frame files
|
|
82
|
+
if "site-packages" in str(file_path):
|
|
83
|
+
continue
|
|
84
|
+
if "abstract_" in file_path.name:
|
|
85
|
+
continue
|
|
86
|
+
if file_path.name.startswith("<"):
|
|
87
|
+
continue
|
|
88
|
+
|
|
89
|
+
return file_path.parent
|
|
90
|
+
|
|
91
|
+
# Fallback: last entry in the stack
|
|
92
|
+
return Path(stack[-1].filename).resolve().parent
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
from ...imports import inspect,os,json,functools,inspect,glob,sys
|
|
1
|
+
from ...imports import inspect,os,json,functools,inspect,glob,sys,Path
|
|
2
2
|
from typing import *
|
|
@@ -50,19 +50,20 @@ def is_file(
|
|
|
50
50
|
env_path=None,
|
|
51
51
|
**kwargs
|
|
52
52
|
):
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
53
|
+
if path:
|
|
54
|
+
contingencies = list(set([user_at_host,password,key,env_path]))
|
|
55
|
+
len_contingencies = len(contingencies)
|
|
56
|
+
is_potential = (len_contingencies >1 or (None not in contingencies))
|
|
57
|
+
if not is_potential:
|
|
58
|
+
return os.path.isfile(path)
|
|
59
|
+
cmd = get_item_check_cmd(path,file=True)
|
|
60
|
+
return run_cmd(cmd=cmd,
|
|
61
|
+
user_at_host=user_at_host,
|
|
62
|
+
password=password,
|
|
63
|
+
key=key,
|
|
64
|
+
env_path=env_path,
|
|
65
|
+
**kwargs
|
|
66
|
+
)
|
|
66
67
|
def is_dir(
|
|
67
68
|
path,
|
|
68
69
|
user_at_host=None,
|
|
@@ -71,34 +72,13 @@ def is_dir(
|
|
|
71
72
|
env_path=None,
|
|
72
73
|
**kwargs
|
|
73
74
|
):
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
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)
|
|
75
|
+
if path:
|
|
76
|
+
contingencies = list(set([user_at_host,password,key,env_path]))
|
|
77
|
+
len_contingencies = len(contingencies)
|
|
78
|
+
is_potential = (len_contingencies >1 or (None not in contingencies))
|
|
79
|
+
if not is_potential:
|
|
80
|
+
return os.path.isdir(path)
|
|
81
|
+
cmd = get_item_check_cmd(path,file=False,directory=True)
|
|
102
82
|
return run_cmd(cmd=cmd,
|
|
103
83
|
user_at_host=user_at_host,
|
|
104
84
|
password=password,
|
|
@@ -106,7 +86,7 @@ def is_exists(
|
|
|
106
86
|
env_path=env_path,
|
|
107
87
|
**kwargs
|
|
108
88
|
)
|
|
109
|
-
def
|
|
89
|
+
def is_exists(
|
|
110
90
|
path,
|
|
111
91
|
user_at_host=None,
|
|
112
92
|
password=None,
|
|
@@ -114,25 +94,49 @@ def is_any(
|
|
|
114
94
|
env_path=None,
|
|
115
95
|
**kwargs
|
|
116
96
|
):
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
97
|
+
if path:
|
|
98
|
+
contingencies = list(set([user_at_host,password,key,env_path]))
|
|
99
|
+
len_contingencies = len(contingencies)
|
|
100
|
+
is_potential = (len_contingencies >1 or (None not in contingencies))
|
|
101
|
+
if not is_potential:
|
|
102
|
+
return os.path.exists(path)
|
|
103
|
+
if is_potential == True:
|
|
104
|
+
cmd = get_item_check_cmd(path,exists=True)
|
|
105
|
+
return run_cmd(cmd=cmd,
|
|
126
106
|
user_at_host=user_at_host,
|
|
127
107
|
password=password,
|
|
128
108
|
key=key,
|
|
129
109
|
env_path=env_path,
|
|
130
110
|
**kwargs
|
|
131
111
|
)
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
112
|
+
def is_any(
|
|
113
|
+
path,
|
|
114
|
+
user_at_host=None,
|
|
115
|
+
password=None,
|
|
116
|
+
key=None,
|
|
117
|
+
env_path=None,
|
|
118
|
+
**kwargs
|
|
119
|
+
):
|
|
120
|
+
if path:
|
|
121
|
+
contingencies = list(set([user_at_host,password,key,env_path]))
|
|
122
|
+
len_contingencies = len(contingencies)
|
|
123
|
+
is_potential = (len_contingencies >1 or (None not in contingencies))
|
|
124
|
+
if not is_potential:
|
|
125
|
+
return os.path.exists(path)
|
|
126
|
+
if is_potential == True:
|
|
127
|
+
out_js = get_all_item_check_cmd(path,file=True,directory=True,exists=True)
|
|
128
|
+
for typ,cmd in out_js.items():
|
|
129
|
+
response = run_cmd(cmd=cmd,
|
|
130
|
+
user_at_host=user_at_host,
|
|
131
|
+
password=password,
|
|
132
|
+
key=key,
|
|
133
|
+
env_path=env_path,
|
|
134
|
+
**kwargs
|
|
135
|
+
)
|
|
136
|
+
result = "__OK__" in (response or "")
|
|
137
|
+
if result:
|
|
138
|
+
return typ
|
|
139
|
+
|
|
136
140
|
class PathBackend(Protocol):
|
|
137
141
|
def join(self, *parts: str) -> str: ...
|
|
138
142
|
def isfile(self, path: str) -> bool: ...
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
from ...list_utils import make_list
|
|
2
|
-
from ...type_utils import get_media_exts, is_media_type, MIME_TYPES, is_str
|
|
2
|
+
from ...type_utils import get_media_exts, is_media_type, MIME_TYPES, is_str,if_not_bool_default
|
|
3
3
|
from ...ssh_utils import *
|
|
4
4
|
from ...env_utils import *
|
|
5
5
|
from ...read_write_utils import read_from_file,write_to_file
|