abstract-utilities 0.2.2.593__py3-none-any.whl → 0.2.2.667__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.

Files changed (47) hide show
  1. abstract_utilities/__init__.py +13 -4
  2. abstract_utilities/class_utils/abstract_classes.py +104 -34
  3. abstract_utilities/class_utils/caller_utils.py +39 -0
  4. abstract_utilities/class_utils/global_utils.py +35 -21
  5. abstract_utilities/class_utils/imports/imports.py +1 -1
  6. abstract_utilities/directory_utils/src/directory_utils.py +2 -0
  7. abstract_utilities/file_utils/imports/classes.py +59 -55
  8. abstract_utilities/file_utils/src/file_filters/__init__.py +0 -3
  9. abstract_utilities/file_utils/src/file_filters/ensure_utils.py +382 -10
  10. abstract_utilities/file_utils/src/file_filters/filter_params.py +64 -0
  11. abstract_utilities/file_utils/src/file_filters/predicate_utils.py +21 -91
  12. abstract_utilities/file_utils/src/initFunctionsGen.py +36 -23
  13. abstract_utilities/file_utils/src/initFunctionsGens.py +280 -0
  14. abstract_utilities/import_utils/imports/__init__.py +1 -1
  15. abstract_utilities/import_utils/imports/init_imports.py +3 -0
  16. abstract_utilities/import_utils/imports/module_imports.py +2 -1
  17. abstract_utilities/import_utils/imports/utils.py +1 -1
  18. abstract_utilities/import_utils/src/__init__.py +1 -0
  19. abstract_utilities/import_utils/src/extract_utils.py +2 -2
  20. abstract_utilities/import_utils/src/import_functions.py +30 -10
  21. abstract_utilities/import_utils/src/import_utils.py +39 -0
  22. abstract_utilities/import_utils/src/layze_import_utils/__init__.py +2 -0
  23. abstract_utilities/import_utils/src/layze_import_utils/lazy_utils.py +41 -0
  24. abstract_utilities/import_utils/src/layze_import_utils/nullProxy.py +32 -0
  25. abstract_utilities/import_utils/src/nullProxy.py +30 -0
  26. abstract_utilities/import_utils/src/sysroot_utils.py +1 -1
  27. abstract_utilities/imports.py +3 -2
  28. abstract_utilities/json_utils/json_utils.py +11 -3
  29. abstract_utilities/log_utils/log_file.py +73 -25
  30. abstract_utilities/parse_utils/parse_utils.py +23 -0
  31. abstract_utilities/path_utils/imports/module_imports.py +1 -1
  32. abstract_utilities/path_utils/path_utils.py +7 -12
  33. abstract_utilities/read_write_utils/imports/imports.py +1 -1
  34. abstract_utilities/read_write_utils/read_write_utils.py +102 -32
  35. abstract_utilities/type_utils/__init__.py +5 -1
  36. abstract_utilities/type_utils/get_type.py +116 -0
  37. abstract_utilities/type_utils/imports/__init__.py +1 -0
  38. abstract_utilities/type_utils/imports/constants.py +134 -0
  39. abstract_utilities/type_utils/imports/module_imports.py +25 -1
  40. abstract_utilities/type_utils/is_type.py +455 -0
  41. abstract_utilities/type_utils/make_type.py +126 -0
  42. abstract_utilities/type_utils/mime_types.py +68 -0
  43. abstract_utilities/type_utils/type_utils.py +0 -877
  44. {abstract_utilities-0.2.2.593.dist-info → abstract_utilities-0.2.2.667.dist-info}/METADATA +1 -1
  45. {abstract_utilities-0.2.2.593.dist-info → abstract_utilities-0.2.2.667.dist-info}/RECORD +47 -36
  46. {abstract_utilities-0.2.2.593.dist-info → abstract_utilities-0.2.2.667.dist-info}/WHEEL +0 -0
  47. {abstract_utilities-0.2.2.593.dist-info → abstract_utilities-0.2.2.667.dist-info}/top_level.txt +0 -0
@@ -34,8 +34,7 @@ from .json_utils import (unified_json_loader,
34
34
  get_result_from_data,
35
35
  flatten_json
36
36
  )
37
- from .read_write_utils import (read_from_file,
38
- write_to_file)
37
+
39
38
  from .directory_utils import *
40
39
  from .path_utils import *
41
40
  from .file_utils import *
@@ -84,7 +83,8 @@ from .type_utils import (make_bool,
84
83
  get_numbers_comp,
85
84
  is_any_instance,
86
85
  break_string,
87
- MIME_TYPES
86
+ MIME_TYPES,
87
+ get_if_None
88
88
  )
89
89
  get_media_types = get_all_types = get_all_file_types
90
90
  from .math_utils import (convert_to_percentage,
@@ -108,7 +108,8 @@ from .parse_utils import (num_tokens_from_string,
108
108
  chunk_any_to_tokens,
109
109
  detect_language_from_text,
110
110
  chunk_by_language_context,
111
- search_code)
111
+ search_code,
112
+ get_within_quotes)
112
113
 
113
114
  from .log_utils import get_caller_info,get_logFile,print_or_log,get_json_call_response,initialize_call_log
114
115
  from .error_utils import try_func
@@ -119,3 +120,11 @@ from .path_utils import *
119
120
  from .file_utils import *
120
121
  from .string_utils import *
121
122
  from .import_utils import *
123
+ from .read_write_utils import (read_from_file,
124
+ write_to_file,
125
+ make_dirs,
126
+ make_dirs,
127
+ copy_files,
128
+ make_path,
129
+ run_cmd
130
+ )
@@ -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
- Adapt the provided args/kwargs to fit the signature of func.
32
- Returns (args, kwargs) suitable for calling func.
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
- # Handle positional arguments
38
- new_args = []
39
- args_iter = iter(args)
40
- for name, param in params.items():
41
- if param.kind in (inspect.Parameter.POSITIONAL_ONLY,
42
- inspect.Parameter.POSITIONAL_OR_KEYWORD):
43
- try:
44
- new_args.append(next(args_iter))
45
- except StopIteration:
46
- break
47
- elif param.kind == inspect.Parameter.VAR_POSITIONAL:
48
- # collect all remaining args
49
- new_args.extend(args_iter)
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
- break
53
-
54
- # Handle keyword arguments
55
- new_kwargs = {}
56
- for name, param in params.items():
57
- if name in kwargs:
58
- new_kwargs[name] = kwargs[name]
59
- elif param.default is inspect.Parameter.empty and param.kind == inspect.Parameter.KEYWORD_ONLY:
60
- # Required keyword not provided
61
- raise TypeError(f"Missing required keyword argument: {name}")
62
-
63
- # Only include keywords func accepts
64
- accepted_names = {
65
- name for name, p in params.items()
66
- if p.kind in (inspect.Parameter.POSITIONAL_OR_KEYWORD,
67
- inspect.Parameter.KEYWORD_ONLY)
68
- }
69
- new_kwargs = {k: v for k, v in new_kwargs.items() if k in accepted_names}
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):
@@ -17,6 +17,7 @@ def get_initial_caller_dir() -> str:
17
17
  """
18
18
  caller = get_initial_caller()
19
19
  return os.path.dirname(caller) if caller else None
20
+
20
21
  def get_caller(i: Optional[int] = None) -> str:
21
22
  """
22
23
  Return the filename of the calling frame.
@@ -51,3 +52,41 @@ def get_caller_dir(i: Optional[int] = None) -> str:
51
52
  depth = 1 if i is None else int(i)
52
53
  abspath = get_caller_path(depth + 1)
53
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,3 +1,22 @@
1
+ import importlib, sys,os
2
+ from .caller_utils import get_initial_caller
3
+ def file_to_module_name(path):
4
+ path = os.path.realpath(path)
5
+ # Find a path that exists in sys.path
6
+ for base in sys.path:
7
+ base = os.path.realpath(base)
8
+ if path.startswith(base):
9
+ rel = os.path.relpath(path, base)
10
+ mod = os.path.splitext(rel)[0]
11
+ return mod.replace(os.sep, ".")
12
+ # fallback (never should be used)
13
+ return os.path.splitext(os.path.basename(path))[0]
14
+ def get_globals_from_path(module_path: str=None):
15
+ module_path = module_path or get_initial_caller()
16
+ module_name = file_to_module_name(module_path)
17
+ if module_name not in sys.modules:
18
+ importlib.import_module(module_name)
19
+ return sys.modules[module_name].__dict__
1
20
  def global_registry(name:str,glob:dict):
2
21
  global_ledger = if_none_default(string='global_ledger',glob=globals(),default={"registry_names":[],"registry_index":[]})
3
22
  if name not in global_ledger['registry_names']:
@@ -31,27 +50,22 @@ def return_globals() -> dict:
31
50
  dict: The global variables dictionary.
32
51
  """
33
52
  return globals()
34
- def change_glob(var: str, val: any, glob: dict = return_globals()) -> any:
35
- """
36
- Changes the value of a global variable.
37
-
38
- Args:
39
- var (str): The name of the global variable.
40
- val (any): The new value.
41
- glob (dict, optional): The dictionary of global variables. Defaults to the current globals.
42
-
43
- Returns:
44
- any: The new value of the variable.
45
- """
53
+ def get_true_globals():
54
+ return sys.modules['__main__'].__dict__
55
+ def change_glob(var: str, val: any, glob: dict = None) -> any:
56
+ if glob is None:
57
+ glob = get_true_globals()
46
58
  glob[var] = val
47
59
  return val
48
- def get_globes(string:str='',glob:dict=return_globals()):
49
- if string in glob:
50
- return glob[string]
51
- def if_none_default(string:str, default:any,glob:dict=return_globals(),typ=None):
52
-
53
- piece = get_globes(string=string,glob=glob)
54
- if piece is None or (typ and not isinstance(piece,typ)):
55
- piece = default
56
- return change_glob(var=string,val=piece,glob=glob)
57
60
 
61
+ def get_globes(string: str='', glob: dict=None):
62
+ if glob is None:
63
+ glob = get_true_globals()
64
+ return glob.get(string)
65
+ def if_none_default(string: str, default: any, glob: dict=None, typ=None):
66
+ if glob is None:
67
+ glob = get_true_globals()
68
+ piece = get_globes(string=string, glob=glob)
69
+ if piece is None or (typ and not isinstance(piece, typ)):
70
+ piece = default
71
+ return change_glob(var=string, val=piece, glob=glob)
@@ -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 *
@@ -102,7 +102,9 @@ def raw_create_dirs(*paths):
102
102
  current_path = safe_join(current_path, part)
103
103
  os.makedirs(current_path, exist_ok=True)
104
104
  return full_path
105
+
105
106
  mkdirs=raw_create_dirs
106
107
  makedirs = mkdirs
107
108
  make_dirs = makedirs
109
+ makeAllDirs = raw_create_dirs
108
110
 
@@ -50,19 +50,20 @@ def is_file(
50
50
  env_path=None,
51
51
  **kwargs
52
52
  ):
53
- contingencies = list(set([user_at_host,password,key,env_path]))
54
- len_contingencies = len(contingencies)
55
- is_potential = (len_contingencies >1 or (None not in contingencies))
56
- if not is_potential:
57
- return os.path.isfile(path)
58
- cmd = get_item_check_cmd(path,file=True)
59
- return run_cmd(cmd=cmd,
60
- user_at_host=user_at_host,
61
- password=password,
62
- key=key,
63
- env_path=env_path,
64
- **kwargs
65
- )
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
- contingencies = list(set([user_at_host,password,key,env_path]))
75
- len_contingencies = len(contingencies)
76
- is_potential = (len_contingencies >1 or (None not in contingencies))
77
- if not is_potential:
78
- return os.path.isdir(path)
79
- cmd = get_item_check_cmd(path,file=False,directory=True)
80
- return run_cmd(cmd=cmd,
81
- user_at_host=user_at_host,
82
- password=password,
83
- key=key,
84
- env_path=env_path,
85
- **kwargs
86
- )
87
- def is_exists(
88
- path,
89
- user_at_host=None,
90
- password=None,
91
- key=None,
92
- env_path=None,
93
- **kwargs
94
- ):
95
- contingencies = list(set([user_at_host,password,key,env_path]))
96
- len_contingencies = len(contingencies)
97
- is_potential = (len_contingencies >1 or (None not in contingencies))
98
- if not is_potential:
99
- return os.path.exists(path)
100
- if is_potential == True:
101
- cmd = get_item_check_cmd(path,exists=True)
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 is_any(
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
- contingencies = list(set([user_at_host,password,key,env_path]))
118
- len_contingencies = len(contingencies)
119
- is_potential = (len_contingencies >1 or (None not in contingencies))
120
- if not is_potential:
121
- return os.path.exists(path)
122
- if is_potential == True:
123
- out_js = get_all_item_check_cmd(path,file=True,directory=True,exists=True)
124
- for typ,cmd in out_js.items():
125
- response = run_cmd(cmd=cmd,
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
- result = "__OK__" in (response or "")
133
- if result:
134
- return typ
135
- return None
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,4 +1 @@
1
1
  from .ensure_utils import *
2
- from .filter_params import *
3
- from .filter_utils import *
4
- from .predicate_utils import *