abstract-utilities 0.2.2.486__py3-none-any.whl → 0.2.2.540__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/__init__.py +6 -10
- abstract_utilities/circular_import_finder.py +222 -0
- abstract_utilities/circular_import_finder2.py +118 -0
- 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/directory_utils/__init__.py +2 -0
- abstract_utilities/directory_utils/directory_utils.py +94 -0
- abstract_utilities/directory_utils/imports/__init__.py +2 -0
- abstract_utilities/directory_utils/imports/imports.py +1 -0
- abstract_utilities/directory_utils/imports/module_imports.py +2 -0
- abstract_utilities/directory_utils/name_utils.py +43 -0
- abstract_utilities/directory_utils/size_utils.py +57 -0
- abstract_utilities/directory_utils/src/__init__.py +4 -0
- abstract_utilities/directory_utils/src/directory_utils.py +92 -0
- abstract_utilities/directory_utils/src/name_utils.py +43 -0
- abstract_utilities/directory_utils/src/size_utils.py +57 -0
- abstract_utilities/directory_utils/src/utils.py +116 -0
- abstract_utilities/directory_utils/utils.py +116 -0
- abstract_utilities/env_utils/imports/imports.py +5 -3
- 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/__init__.py +1 -2
- abstract_utilities/file_utils/file_utils/type_checks.py +2 -1
- abstract_utilities/file_utils/imports/constants.py +84 -4
- abstract_utilities/file_utils/imports/imports.py +3 -18
- abstract_utilities/file_utils/imports/module_imports.py +2 -7
- abstract_utilities/file_utils/module_imports.py +12 -0
- abstract_utilities/file_utils/src/__init__.py +8 -0
- abstract_utilities/file_utils/src/file_filters.py +177 -0
- abstract_utilities/file_utils/src/file_reader.py +544 -0
- abstract_utilities/file_utils/src/file_utils.py +156 -0
- abstract_utilities/file_utils/src/filter_params.py +197 -0
- abstract_utilities/file_utils/src/find_collect.py +266 -0
- abstract_utilities/file_utils/src/initFunctionsGen.py +280 -0
- abstract_utilities/file_utils/src/map_utils.py +29 -0
- abstract_utilities/file_utils/src/pdf_utils.py +300 -0
- abstract_utilities/file_utils/src/type_checks.py +91 -0
- abstract_utilities/file_utils (2)/__init__.py +2 -0
- abstract_utilities/file_utils (2)/imports/__init__.py +2 -0
- abstract_utilities/file_utils (2)/imports/constants.py +118 -0
- abstract_utilities/file_utils (2)/imports/imports/__init__.py +3 -0
- abstract_utilities/file_utils (2)/imports/imports/constants.py +119 -0
- abstract_utilities/file_utils (2)/imports/imports/imports.py +46 -0
- abstract_utilities/file_utils (2)/imports/imports/module_imports.py +8 -0
- abstract_utilities/file_utils (2)/imports/utils/__init__.py +3 -0
- abstract_utilities/file_utils (2)/imports/utils/classes.py +379 -0
- abstract_utilities/file_utils (2)/imports/utils/clean_imps.py +155 -0
- abstract_utilities/file_utils (2)/imports/utils/filter_utils.py +341 -0
- abstract_utilities/file_utils (2)/src/__init__.py +8 -0
- abstract_utilities/file_utils (2)/src/file_filters.py +155 -0
- abstract_utilities/file_utils (2)/src/file_reader.py +604 -0
- abstract_utilities/file_utils (2)/src/find_collect.py +258 -0
- abstract_utilities/file_utils (2)/src/initFunctionsGen.py +286 -0
- abstract_utilities/file_utils (2)/src/map_utils.py +28 -0
- abstract_utilities/file_utils (2)/src/pdf_utils.py +300 -0
- 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/__init__.py +2 -0
- abstract_utilities/import_utils/imports/__init__.py +4 -0
- abstract_utilities/import_utils/imports/constants.py +2 -0
- abstract_utilities/import_utils/imports/imports.py +4 -0
- abstract_utilities/import_utils/imports/module_imports.py +6 -0
- abstract_utilities/import_utils/imports/utils.py +30 -0
- abstract_utilities/import_utils/src/__init__.py +7 -0
- abstract_utilities/import_utils/src/clean_imports.py +147 -0
- abstract_utilities/import_utils/src/dot_utils.py +69 -0
- abstract_utilities/import_utils/src/extract_utils.py +42 -0
- abstract_utilities/import_utils/src/import_functions.py +46 -0
- abstract_utilities/import_utils/src/import_utils.py +299 -0
- abstract_utilities/import_utils/src/package_utils/__init__.py +139 -0
- abstract_utilities/import_utils/src/package_utils/context_utils.py +27 -0
- abstract_utilities/import_utils/src/package_utils/import_collectors.py +53 -0
- abstract_utilities/import_utils/src/package_utils/path_utils.py +28 -0
- abstract_utilities/import_utils/src/package_utils/safe_import.py +27 -0
- abstract_utilities/import_utils/src/package_utils.py +140 -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/import_utils/src/sysroot_utils.py +57 -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 +58 -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 +3 -0
- abstract_utilities/path_utils/imports/imports.py +1 -0
- abstract_utilities/path_utils/imports/module_imports.py +8 -0
- abstract_utilities/path_utils/path_utils.py +251 -0
- abstract_utilities/path_utils.py +95 -14
- 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 +110 -60
- abstract_utilities/safe_utils/__init__.py +2 -0
- abstract_utilities/safe_utils/imports/__init__.py +3 -0
- abstract_utilities/safe_utils/imports/imports.py +2 -0
- abstract_utilities/safe_utils/imports/module_imports.py +2 -0
- abstract_utilities/safe_utils/safe_utils.py +136 -0
- abstract_utilities/ssh_utils/__init__.py +3 -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 +6 -0
- abstract_utilities/ssh_utils/imports/utils.py +189 -0
- abstract_utilities/ssh_utils/pexpect_utils.py +11 -18
- abstract_utilities/ssh_utils/type_checks.py +92 -0
- 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.486.dist-info → abstract_utilities-0.2.2.540.dist-info}/METADATA +1 -1
- abstract_utilities-0.2.2.540.dist-info/RECORD +263 -0
- imports/__init__.py +36 -0
- abstract_utilities-0.2.2.486.dist-info/RECORD +0 -92
- {abstract_utilities-0.2.2.486.dist-info → abstract_utilities-0.2.2.540.dist-info}/WHEEL +0 -0
- {abstract_utilities-0.2.2.486.dist-info → abstract_utilities-0.2.2.540.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
from .imports import *
|
|
2
|
+
def get_proper_args(function,args:(dict or list)={}):
|
|
3
|
+
"""Call a function with either positional or keyword arguments based on the provided args type."""
|
|
4
|
+
return function(*args) if isinstance(args, list) else function(**args)
|
|
5
|
+
def process_args(args):
|
|
6
|
+
"""
|
|
7
|
+
Processes the arguments for a function, replacing nested function calls with their results.
|
|
8
|
+
|
|
9
|
+
Args:
|
|
10
|
+
args (dict): A dictionary of arguments.
|
|
11
|
+
|
|
12
|
+
Returns:
|
|
13
|
+
dict: A dictionary of processed arguments.
|
|
14
|
+
"""
|
|
15
|
+
for key, value in args.items():
|
|
16
|
+
# check if value is a dict and has a 'type' key with value 'get'
|
|
17
|
+
if isinstance(value, dict) and value.get('type') == 'get':
|
|
18
|
+
function_name = value.get('name',None)
|
|
19
|
+
function_args = value.get('args', {})
|
|
20
|
+
instance = value.get('instance',None)
|
|
21
|
+
glob = value.get('global',globals())
|
|
22
|
+
# call the function and replace the arg with its result
|
|
23
|
+
args[key] = call_functions(function_name, function_args, instance, glob)
|
|
24
|
+
return args
|
|
25
|
+
def get_fun(js):
|
|
26
|
+
"""
|
|
27
|
+
Retrieves and calls a function with the given parameters.
|
|
28
|
+
|
|
29
|
+
Args:
|
|
30
|
+
js (dict): A dictionary that contains function details, including name, arguments, instance (optional), and global scope (optional).
|
|
31
|
+
|
|
32
|
+
Returns:
|
|
33
|
+
any: The result of the function call.
|
|
34
|
+
"""
|
|
35
|
+
# Get function details
|
|
36
|
+
function_name = js.get('name',None)
|
|
37
|
+
if function_name is None:
|
|
38
|
+
return None
|
|
39
|
+
function_args = js.get('args', {})
|
|
40
|
+
instance = js.get('instance',None)
|
|
41
|
+
glob = js.get('global',globals())
|
|
42
|
+
# Process arguments
|
|
43
|
+
function_args = process_args(function_args)
|
|
44
|
+
# If instance is not None, get the function from the instance, else get from globals
|
|
45
|
+
if instance is not None:
|
|
46
|
+
function = getattr(instance, function_name)
|
|
47
|
+
else:
|
|
48
|
+
function = glob[function_name]
|
|
49
|
+
# Get function's valid parameter keys
|
|
50
|
+
sig = inspect.signature(function)
|
|
51
|
+
valid_keys = sig.parameters.keys()
|
|
52
|
+
# Filter arguments to only those accepted by the function
|
|
53
|
+
filtered_args = {k: v for k, v in function_args.items() if k in valid_keys}
|
|
54
|
+
return call_functions(function_name, filtered_args, instance, glob)
|
|
55
|
+
def call_functions(function_name: str, args: dict = {}, instance=None, glob:(dict or bool)=globals()):
|
|
56
|
+
"""
|
|
57
|
+
Calls a function or a method with the given arguments.
|
|
58
|
+
|
|
59
|
+
Args:
|
|
60
|
+
function_name (str): The name of the function.
|
|
61
|
+
args (dict, optional): A dictionary of arguments to pass to the function. Defaults to None.
|
|
62
|
+
instance (optional): The instance on which to call the method. Defaults to None.
|
|
63
|
+
glob (optional): The global scope from which to retrieve the function. Defaults to globals().
|
|
64
|
+
|
|
65
|
+
Returns:
|
|
66
|
+
any: The result of the function or method call.
|
|
67
|
+
"""
|
|
68
|
+
glob = if_none_change(glob,globals())
|
|
69
|
+
args = if_none_change(args,{})
|
|
70
|
+
if instance is not None:
|
|
71
|
+
# Calls method on instance
|
|
72
|
+
method = getattr(instance, function_name)
|
|
73
|
+
return get_proper_args(method,args)
|
|
74
|
+
else:
|
|
75
|
+
# Calls function from globals
|
|
76
|
+
return get_proper_args(glob[function_name],args)
|
|
77
|
+
def get_all_functions_for_instance(instance):
|
|
78
|
+
"""
|
|
79
|
+
Retrieves all callable methods/functions of an object instance.
|
|
80
|
+
|
|
81
|
+
Args:
|
|
82
|
+
instance: The object instance for which to retrieve methods/functions.
|
|
83
|
+
|
|
84
|
+
Returns:
|
|
85
|
+
list: A list of method/function names that are callable on the given instance.
|
|
86
|
+
"""
|
|
87
|
+
return [method for method in dir(instance) if callable(getattr(instance, method))]
|
|
88
|
+
def get_all_params(instance, function_name):
|
|
89
|
+
"""
|
|
90
|
+
Retrieves information about the parameters of a callable method/function of an instance.
|
|
91
|
+
|
|
92
|
+
Args:
|
|
93
|
+
instance: The object instance containing the method.
|
|
94
|
+
function_name (str): The name of the method/function to inspect.
|
|
95
|
+
|
|
96
|
+
Returns:
|
|
97
|
+
dict: A dictionary containing parameter information, including names, defaults, kinds, and required parameters.
|
|
98
|
+
"""
|
|
99
|
+
# Use getattr() to get the method by name
|
|
100
|
+
method = getattr(instance, function_name, None)
|
|
101
|
+
|
|
102
|
+
if callable(method):
|
|
103
|
+
# Now you have the method, and you can inspect it
|
|
104
|
+
func_signature = inspect.signature(method)
|
|
105
|
+
parameters = func_signature.parameters
|
|
106
|
+
|
|
107
|
+
params = {"names": [], "defaults": [], "kinds": [], "required": []}
|
|
108
|
+
|
|
109
|
+
for param_name, param in parameters.items():
|
|
110
|
+
params["names"].append(param_name)
|
|
111
|
+
params["defaults"].append(param.default)
|
|
112
|
+
params["kinds"].append(param.kind)
|
|
113
|
+
|
|
114
|
+
if param.default == inspect._empty:
|
|
115
|
+
params["required"].append(param_name)
|
|
116
|
+
|
|
117
|
+
return params
|
|
118
|
+
else:
|
|
119
|
+
print(f"{function_name} is not a callable method of the instance.")
|
|
120
|
+
return None
|
|
121
|
+
def mk_fun(module,function):
|
|
122
|
+
"""
|
|
123
|
+
Checks if a function exists in a given module.
|
|
124
|
+
|
|
125
|
+
Args:
|
|
126
|
+
module: The module in which to look for the function.
|
|
127
|
+
function: The function to check.
|
|
128
|
+
|
|
129
|
+
Prints a statement indicating whether the function exists.
|
|
130
|
+
"""
|
|
131
|
+
if has_attribute(module,function):
|
|
132
|
+
print(f"The function {function} exists.")
|
|
133
|
+
else:
|
|
134
|
+
print(f"The function {function} does not exist.")
|
|
135
|
+
def alias(*aliases):
|
|
136
|
+
"""
|
|
137
|
+
Decorator to create multiple names for a function.
|
|
138
|
+
|
|
139
|
+
Args:
|
|
140
|
+
*aliases: Names to assign to the function.
|
|
141
|
+
|
|
142
|
+
Returns:
|
|
143
|
+
callable: Decorated function.
|
|
144
|
+
"""
|
|
145
|
+
def decorator(func):
|
|
146
|
+
@functools.wraps(func)
|
|
147
|
+
def wrapper(*args, **kwargs):
|
|
148
|
+
return func(*args, **kwargs)
|
|
149
|
+
# Assign aliases in the module's globals
|
|
150
|
+
for name in aliases:
|
|
151
|
+
globals()[name] = func
|
|
152
|
+
return wrapper
|
|
153
|
+
return decorator
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
def global_registry(name:str,glob:dict):
|
|
2
|
+
global_ledger = if_none_default(string='global_ledger',glob=globals(),default={"registry_names":[],"registry_index":[]})
|
|
3
|
+
if name not in global_ledger['registry_names']:
|
|
4
|
+
if glob == None:
|
|
5
|
+
return None
|
|
6
|
+
global_ledger['registry_names'].append(name)
|
|
7
|
+
global_ledger['registry_index'].append(glob)
|
|
8
|
+
length = len(global_ledger['registry_names'])
|
|
9
|
+
change_glob('global_ledger',global_ledger)
|
|
10
|
+
for i in range(0,length):
|
|
11
|
+
if name == global_ledger['registry_names'][i]:
|
|
12
|
+
return i
|
|
13
|
+
def get_registry_number(name:str):
|
|
14
|
+
return global_registry(name=name,glob=None)
|
|
15
|
+
def update_registry(var:str,val:any,name:str):
|
|
16
|
+
global_ledger=get_globes(string='global_ledger',glob=globals())
|
|
17
|
+
change_glob(var=var,val=val,glob=get_global_from_registry(name))
|
|
18
|
+
global_ledger['registry_index'][get_registry_number(name)] = get_global_from_registry(name)
|
|
19
|
+
change_glob(var='global_ledger',val=global_ledger)
|
|
20
|
+
def get_global_from_registry(name:str):
|
|
21
|
+
global_ledger=get_globes(string='global_ledger',glob=globals())
|
|
22
|
+
return global_ledger['registry_index'][get_registry_number(name)]
|
|
23
|
+
def return_globals() -> dict:
|
|
24
|
+
"""
|
|
25
|
+
Returns the global variables.
|
|
26
|
+
|
|
27
|
+
Args:
|
|
28
|
+
globs (dict, optional): The dictionary of global variables. Defaults to the current globals.
|
|
29
|
+
|
|
30
|
+
Returns:
|
|
31
|
+
dict: The global variables dictionary.
|
|
32
|
+
"""
|
|
33
|
+
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
|
+
"""
|
|
46
|
+
glob[var] = val
|
|
47
|
+
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()):
|
|
52
|
+
piece = get_globes(string=string,glob=glob)
|
|
53
|
+
if piece is None:
|
|
54
|
+
piece = default
|
|
55
|
+
return change_glob(var=string,val=piece,glob=glob)
|
|
56
|
+
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
from .imports import *
|
|
2
|
+
def remove_key(js: dict, key: any) -> dict:
|
|
3
|
+
"""Remove a key from a dictionary. If the key is not present,
|
|
4
|
+
no action is taken."""
|
|
5
|
+
js.pop(key, None)
|
|
6
|
+
return js
|
|
7
|
+
def if_none_change(obj, default):
|
|
8
|
+
"""Return a default value if the provided object is None."""
|
|
9
|
+
if obj == None:
|
|
10
|
+
obj = default
|
|
11
|
+
return obj
|
|
12
|
+
def has_attribute(module, function):
|
|
13
|
+
"""Check if a specific function exists in a given module."""
|
|
14
|
+
try:
|
|
15
|
+
bool_it = hasattr(module, function)
|
|
16
|
+
except:
|
|
17
|
+
bool_it =None
|
|
18
|
+
return bool_it
|
|
19
|
+
def get_type_list() -> list:
|
|
20
|
+
"""Get a list of common Python types."""
|
|
21
|
+
return ['None','str','int','float','bool','list','tuple','set','dict','frozenset','bytearray','bytes','memoryview','range','enumerate','zip','filter','map','property','slice','super','type','Exception','object']
|
|
22
|
+
def get_set_attr(parent,attr_name,value=None,valueFunc=None,default=False,*args,**kwargs):
|
|
23
|
+
attr_value = getattr(parent,attr_name,default)
|
|
24
|
+
if attr_value == False:
|
|
25
|
+
if value is None and valueFunc is not None:
|
|
26
|
+
value = valueFunc(*args,**kwargs)
|
|
27
|
+
setattr(parent,attr_name,value)
|
|
28
|
+
attr_value = getattr(parent,attr_name,default)
|
|
29
|
+
return attr_value
|
|
30
|
+
def get_dir(mod):
|
|
31
|
+
"""
|
|
32
|
+
Retrieves the directory of a module.
|
|
33
|
+
|
|
34
|
+
Args:
|
|
35
|
+
mod: The module.
|
|
36
|
+
|
|
37
|
+
Returns:
|
|
38
|
+
list: The list of attributes and methods in the module.
|
|
39
|
+
"""
|
|
40
|
+
return dir(mod)
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
from .imports import *
|
|
2
|
+
def get_module_obj(instance: any, obj: any):
|
|
3
|
+
"""
|
|
4
|
+
Retrieves an object from a module.
|
|
5
|
+
|
|
6
|
+
Args:
|
|
7
|
+
instance (any): The module instance.
|
|
8
|
+
obj (any): The object to retrieve.
|
|
9
|
+
|
|
10
|
+
Returns:
|
|
11
|
+
any: The retrieved object.
|
|
12
|
+
"""
|
|
13
|
+
return getattr(instance, obj)
|
|
14
|
+
|
|
15
|
+
def spec_type_mod(obj: any, st: str) -> bool:
|
|
16
|
+
"""
|
|
17
|
+
Checks if an object has a specific type.
|
|
18
|
+
|
|
19
|
+
Args:
|
|
20
|
+
obj (any): The object to check.
|
|
21
|
+
st (str): The specific type to check.
|
|
22
|
+
|
|
23
|
+
Returns:
|
|
24
|
+
bool: True if the object has the specified type, False otherwise.
|
|
25
|
+
"""
|
|
26
|
+
if obj.__class__.__name__ == st:
|
|
27
|
+
return True
|
|
28
|
+
return False
|
|
29
|
+
|
|
30
|
+
def get_type_mod(obj: any) -> str:
|
|
31
|
+
"""
|
|
32
|
+
Retrieves the type of an object.
|
|
33
|
+
|
|
34
|
+
Args:
|
|
35
|
+
obj (any): The object to get the type of.
|
|
36
|
+
|
|
37
|
+
Returns:
|
|
38
|
+
str: The type of the object.
|
|
39
|
+
"""
|
|
40
|
+
type_ls = get_types_list()
|
|
41
|
+
for k in range(len(type_ls)):
|
|
42
|
+
typ = str(type_ls[k])
|
|
43
|
+
if spec_type_mod(obj, typ):
|
|
44
|
+
return typ
|
|
45
|
+
return "NoneType"
|
|
46
|
+
|
|
47
|
+
def is_module_obj(instance: any, obj: str) -> bool:
|
|
48
|
+
"""
|
|
49
|
+
Checks if an object is part of a module.
|
|
50
|
+
|
|
51
|
+
Args:
|
|
52
|
+
instance (any): The module instance.
|
|
53
|
+
obj (str): The name of the object to check.
|
|
54
|
+
|
|
55
|
+
Returns:
|
|
56
|
+
bool: True if the object is part of the module, False otherwise.
|
|
57
|
+
"""
|
|
58
|
+
try:
|
|
59
|
+
if get_type_mod(getattr(instance, obj)) in [None, 'NoneType']:
|
|
60
|
+
return False
|
|
61
|
+
return True
|
|
62
|
+
except:
|
|
63
|
+
return False
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
from ..imports import os,shlex
|
|
2
|
+
from ..safe_utils import safe_join
|
|
3
|
+
from .utils import *
|
|
4
|
+
def get_dirs(path):
|
|
5
|
+
"""
|
|
6
|
+
Get List of Immediate Subdirectories in a Path
|
|
7
|
+
|
|
8
|
+
This function uses the os.walk method to traverse through a directory tree and returns a list of immediate subdirectories
|
|
9
|
+
within the specified path.
|
|
10
|
+
|
|
11
|
+
Parameters:
|
|
12
|
+
- path (str): The path for which subdirectories need to be retrieved.
|
|
13
|
+
|
|
14
|
+
Returns:
|
|
15
|
+
- subdirectories (list): A list of immediate subdirectories within the specified path.
|
|
16
|
+
|
|
17
|
+
Example:
|
|
18
|
+
subdirs = get_dirs("/path/to/directory")
|
|
19
|
+
print("Immediate Subdirectories:", subdirs)
|
|
20
|
+
"""
|
|
21
|
+
from os import walk
|
|
22
|
+
for (dirpath, dirnames, filenames) in walk(path):
|
|
23
|
+
return dirnames
|
|
24
|
+
def get_directory(file_path: str) -> str:
|
|
25
|
+
"""
|
|
26
|
+
Extracts and returns the directory path from a given file path.
|
|
27
|
+
|
|
28
|
+
Args:
|
|
29
|
+
file_path (str): A string representing the file path.
|
|
30
|
+
|
|
31
|
+
Returns:
|
|
32
|
+
str: The directory path extracted from the file path.
|
|
33
|
+
"""
|
|
34
|
+
return file_path[:-len(get_base_name(file_path))]
|
|
35
|
+
def if_not_last_child_join(path:str,child:str):
|
|
36
|
+
"""
|
|
37
|
+
Adds a child path to the given path if it's not already present at the end.
|
|
38
|
+
|
|
39
|
+
Args:
|
|
40
|
+
path (str): The parent path.
|
|
41
|
+
child (str): The child path to add.
|
|
42
|
+
|
|
43
|
+
Returns:
|
|
44
|
+
str: The updated path.
|
|
45
|
+
"""
|
|
46
|
+
if path.endswith(child):
|
|
47
|
+
return path
|
|
48
|
+
return simple_path_join(path, child)
|
|
49
|
+
def createFolds(ls: list) -> None:
|
|
50
|
+
"""
|
|
51
|
+
Creates multiple directories.
|
|
52
|
+
|
|
53
|
+
Args:
|
|
54
|
+
ls (list): The list of directory paths to create.
|
|
55
|
+
"""
|
|
56
|
+
for k in range(len(ls)):
|
|
57
|
+
mkdirs(ls[k])
|
|
58
|
+
def list_directory_contents(path: str) -> list:
|
|
59
|
+
"""Returns a list of directory contents or a list with a single file, if the path is a file.
|
|
60
|
+
|
|
61
|
+
Args:
|
|
62
|
+
path (str): The path of the directory or file.
|
|
63
|
+
|
|
64
|
+
Returns:
|
|
65
|
+
list: A list of directory contents or a list with a single file path.
|
|
66
|
+
"""
|
|
67
|
+
if is_file(path):
|
|
68
|
+
return [path]
|
|
69
|
+
elif is_valid_path(path):
|
|
70
|
+
return os.listdir(path)
|
|
71
|
+
return [path]
|
|
72
|
+
def is_string_in_dir(path,strings):
|
|
73
|
+
dirname = path
|
|
74
|
+
if is_file(path):
|
|
75
|
+
dirname = os.path.dirname(path)
|
|
76
|
+
pieces = [pa for pa in dirname.split('/') if pa and pa in strings]
|
|
77
|
+
logger.info(f"pieces = {pieces}\nstrings == {strings}")
|
|
78
|
+
if pieces:
|
|
79
|
+
return True
|
|
80
|
+
return False
|
|
81
|
+
def raw_create_dirs(*paths):
|
|
82
|
+
"""Recursively create all directories along the given path."""
|
|
83
|
+
full_path = os.path.abspath(safe_join(*paths))
|
|
84
|
+
sub_parts = [p for p in full_path.split(os.sep) if p]
|
|
85
|
+
|
|
86
|
+
current_path = "/" if full_path.startswith(os.sep) else ""
|
|
87
|
+
for part in sub_parts:
|
|
88
|
+
current_path = safe_join(current_path, part)
|
|
89
|
+
os.makedirs(current_path, exist_ok=True)
|
|
90
|
+
return full_path
|
|
91
|
+
mkdirs=raw_create_dirs
|
|
92
|
+
makedirs = mkdirs
|
|
93
|
+
make_dirs = makedirs
|
|
94
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from ...imports import os,shlex
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
from ..imports import *
|
|
2
|
+
from .directory_utils import *
|
|
3
|
+
def get_file_name(file_path: str) -> str:
|
|
4
|
+
"""
|
|
5
|
+
Retrieves and returns the base name of a file from a given file path.
|
|
6
|
+
|
|
7
|
+
Args:
|
|
8
|
+
file_path (str): A string representing the file path.
|
|
9
|
+
|
|
10
|
+
Returns:
|
|
11
|
+
str: The base name of the file (without extension).
|
|
12
|
+
"""
|
|
13
|
+
return split_text(get_base_name(file_path))[0]
|
|
14
|
+
def get_abs_name_of_this():
|
|
15
|
+
"""
|
|
16
|
+
Returns the absolute name of the current module.
|
|
17
|
+
|
|
18
|
+
Returns:
|
|
19
|
+
Path: The absolute name of the current module.
|
|
20
|
+
"""
|
|
21
|
+
return os.path.abspath(__name__)
|
|
22
|
+
def sanitize_filename(name: str):
|
|
23
|
+
"""
|
|
24
|
+
Sanitize a filename by removing invalid characters.
|
|
25
|
+
|
|
26
|
+
Args:
|
|
27
|
+
name (str): Filename to sanitize.
|
|
28
|
+
|
|
29
|
+
Returns:
|
|
30
|
+
str: Sanitized filename.
|
|
31
|
+
"""
|
|
32
|
+
return re.sub(r'[\\/*?:"<>|]', "", name)
|
|
33
|
+
def get_base_name(file_path: str) -> str:
|
|
34
|
+
"""
|
|
35
|
+
Extracts and returns the base name of a file from a given file path.
|
|
36
|
+
|
|
37
|
+
Args:
|
|
38
|
+
file_path (str): A string representing the file path.
|
|
39
|
+
|
|
40
|
+
Returns:
|
|
41
|
+
str: The base name of the file.
|
|
42
|
+
"""
|
|
43
|
+
return os.path.basename(file_path)
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
from ..imports import *
|
|
2
|
+
from .name_utils import *
|
|
3
|
+
def get_os_info():
|
|
4
|
+
"""
|
|
5
|
+
Get Operating System Information
|
|
6
|
+
|
|
7
|
+
This function retrieves information about the current operating system, including its name and bit size.
|
|
8
|
+
|
|
9
|
+
Returns:
|
|
10
|
+
- os_info (dict): A dictionary containing the operating system information.
|
|
11
|
+
Keys:
|
|
12
|
+
- "operating_system" (str): The name of the operating system (e.g., "Windows", "Linux", "Darwin").
|
|
13
|
+
- "bit_size" (str): The bit size of the operating system (e.g., "32bit", "64bit").
|
|
14
|
+
|
|
15
|
+
Example:
|
|
16
|
+
os_info = get_os_info()
|
|
17
|
+
print("Operating System:", os_info["operating_system"])
|
|
18
|
+
print("Bit Size:", os_info["bit_size"])
|
|
19
|
+
"""
|
|
20
|
+
os_name = platform.system()
|
|
21
|
+
bit_size = platform.architecture()[0]
|
|
22
|
+
return {"operating_system": os_name, "bit_size": bit_size}
|
|
23
|
+
def mkGb(k) -> float:
|
|
24
|
+
"""
|
|
25
|
+
Converts a value to Gigabytes (GB).
|
|
26
|
+
|
|
27
|
+
Args:
|
|
28
|
+
k (float): The value to convert to GB.
|
|
29
|
+
|
|
30
|
+
Returns:
|
|
31
|
+
float: The value converted to GB.
|
|
32
|
+
"""
|
|
33
|
+
return float(float(k)*(10**9))
|
|
34
|
+
|
|
35
|
+
def mkGbTrunk(k) -> float:
|
|
36
|
+
"""
|
|
37
|
+
Converts a value to Gigabytes (GB) and truncates the result to five decimal places.
|
|
38
|
+
|
|
39
|
+
Args:
|
|
40
|
+
k (float): The value to convert to GB.
|
|
41
|
+
|
|
42
|
+
Returns:
|
|
43
|
+
float: The value converted to GB and truncated to five decimal places.
|
|
44
|
+
"""
|
|
45
|
+
return trunc(mkGb(k), 5)
|
|
46
|
+
|
|
47
|
+
def mkGbTrunFroPathTot(k) -> float:
|
|
48
|
+
"""
|
|
49
|
+
Fetches the file size from a path, converts it to Gigabytes (GB) and truncates the result to five decimal places.
|
|
50
|
+
|
|
51
|
+
Args:
|
|
52
|
+
k (str): The file path.
|
|
53
|
+
|
|
54
|
+
Returns:
|
|
55
|
+
float: The file size converted to GB and truncated to five decimal places.
|
|
56
|
+
"""
|
|
57
|
+
return trunc(mkGb(s.path.getsize(k)), 5)
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
|
|
2
|
+
from .utils import *
|
|
3
|
+
def get_dirs(path):
|
|
4
|
+
"""
|
|
5
|
+
Get List of Immediate Subdirectories in a Path
|
|
6
|
+
|
|
7
|
+
This function uses the os.walk method to traverse through a directory tree and returns a list of immediate subdirectories
|
|
8
|
+
within the specified path.
|
|
9
|
+
|
|
10
|
+
Parameters:
|
|
11
|
+
- path (str): The path for which subdirectories need to be retrieved.
|
|
12
|
+
|
|
13
|
+
Returns:
|
|
14
|
+
- subdirectories (list): A list of immediate subdirectories within the specified path.
|
|
15
|
+
|
|
16
|
+
Example:
|
|
17
|
+
subdirs = get_dirs("/path/to/directory")
|
|
18
|
+
print("Immediate Subdirectories:", subdirs)
|
|
19
|
+
"""
|
|
20
|
+
from os import walk
|
|
21
|
+
for (dirpath, dirnames, filenames) in walk(path):
|
|
22
|
+
return dirnames
|
|
23
|
+
def get_directory(file_path: str) -> str:
|
|
24
|
+
"""
|
|
25
|
+
Extracts and returns the directory path from a given file path.
|
|
26
|
+
|
|
27
|
+
Args:
|
|
28
|
+
file_path (str): A string representing the file path.
|
|
29
|
+
|
|
30
|
+
Returns:
|
|
31
|
+
str: The directory path extracted from the file path.
|
|
32
|
+
"""
|
|
33
|
+
return os.path.dirname(file_path)
|
|
34
|
+
def if_not_last_child_join(path:str,child:str):
|
|
35
|
+
"""
|
|
36
|
+
Adds a child path to the given path if it's not already present at the end.
|
|
37
|
+
|
|
38
|
+
Args:
|
|
39
|
+
path (str): The parent path.
|
|
40
|
+
child (str): The child path to add.
|
|
41
|
+
|
|
42
|
+
Returns:
|
|
43
|
+
str: The updated path.
|
|
44
|
+
"""
|
|
45
|
+
if path.endswith(child):
|
|
46
|
+
return path
|
|
47
|
+
return simple_path_join(path, child)
|
|
48
|
+
def createFolds(ls: list) -> None:
|
|
49
|
+
"""
|
|
50
|
+
Creates multiple directories.
|
|
51
|
+
|
|
52
|
+
Args:
|
|
53
|
+
ls (list): The list of directory paths to create.
|
|
54
|
+
"""
|
|
55
|
+
for k in range(len(ls)):
|
|
56
|
+
mkdirs(ls[k])
|
|
57
|
+
def list_directory_contents(path: str) -> list:
|
|
58
|
+
"""Returns a list of directory contents or a list with a single file, if the path is a file.
|
|
59
|
+
|
|
60
|
+
Args:
|
|
61
|
+
path (str): The path of the directory or file.
|
|
62
|
+
|
|
63
|
+
Returns:
|
|
64
|
+
list: A list of directory contents or a list with a single file path.
|
|
65
|
+
"""
|
|
66
|
+
if is_file(path):
|
|
67
|
+
return [path]
|
|
68
|
+
elif is_valid_path(path):
|
|
69
|
+
return os.listdir(path)
|
|
70
|
+
return [path]
|
|
71
|
+
def is_string_in_dir(path,strings,**kwargs):
|
|
72
|
+
dirname = path
|
|
73
|
+
if is_file(path,**kwargs):
|
|
74
|
+
dirname = os.path.dirname(path)
|
|
75
|
+
pieces = [pa for pa in dirname.split('/') if pa and pa in strings]
|
|
76
|
+
if pieces:
|
|
77
|
+
return True
|
|
78
|
+
return False
|
|
79
|
+
def raw_create_dirs(*paths):
|
|
80
|
+
"""Recursively create all directories along the given path."""
|
|
81
|
+
full_path = os.path.abspath(safe_join(*paths))
|
|
82
|
+
sub_parts = [p for p in full_path.split(os.sep) if p]
|
|
83
|
+
|
|
84
|
+
current_path = "/" if full_path.startswith(os.sep) else ""
|
|
85
|
+
for part in sub_parts:
|
|
86
|
+
current_path = safe_join(current_path, part)
|
|
87
|
+
os.makedirs(current_path, exist_ok=True)
|
|
88
|
+
return full_path
|
|
89
|
+
mkdirs=raw_create_dirs
|
|
90
|
+
makedirs = mkdirs
|
|
91
|
+
make_dirs = makedirs
|
|
92
|
+
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
from ..imports import *
|
|
2
|
+
from .directory_utils import *
|
|
3
|
+
def get_file_name(file_path: str) -> str:
|
|
4
|
+
"""
|
|
5
|
+
Retrieves and returns the base name of a file from a given file path.
|
|
6
|
+
|
|
7
|
+
Args:
|
|
8
|
+
file_path (str): A string representing the file path.
|
|
9
|
+
|
|
10
|
+
Returns:
|
|
11
|
+
str: The base name of the file (without extension).
|
|
12
|
+
"""
|
|
13
|
+
return split_text(get_base_name(file_path))[0]
|
|
14
|
+
def get_abs_name_of_this():
|
|
15
|
+
"""
|
|
16
|
+
Returns the absolute name of the current module.
|
|
17
|
+
|
|
18
|
+
Returns:
|
|
19
|
+
Path: The absolute name of the current module.
|
|
20
|
+
"""
|
|
21
|
+
return os.path.abspath(__name__)
|
|
22
|
+
def sanitize_filename(name: str):
|
|
23
|
+
"""
|
|
24
|
+
Sanitize a filename by removing invalid characters.
|
|
25
|
+
|
|
26
|
+
Args:
|
|
27
|
+
name (str): Filename to sanitize.
|
|
28
|
+
|
|
29
|
+
Returns:
|
|
30
|
+
str: Sanitized filename.
|
|
31
|
+
"""
|
|
32
|
+
return re.sub(r'[\\/*?:"<>|]', "", name)
|
|
33
|
+
def get_base_name(file_path: str) -> str:
|
|
34
|
+
"""
|
|
35
|
+
Extracts and returns the base name of a file from a given file path.
|
|
36
|
+
|
|
37
|
+
Args:
|
|
38
|
+
file_path (str): A string representing the file path.
|
|
39
|
+
|
|
40
|
+
Returns:
|
|
41
|
+
str: The base name of the file.
|
|
42
|
+
"""
|
|
43
|
+
return os.path.basename(file_path)
|