abstract-utilities 0.2.2.513__py3-none-any.whl → 0.2.2.583__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/class_utils/caller_utils.py +18 -0
- abstract_utilities/class_utils/global_utils.py +3 -2
- abstract_utilities/class_utils/imports/imports.py +1 -1
- abstract_utilities/directory_utils/__init__.py +2 -4
- 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/src/__init__.py +4 -0
- abstract_utilities/directory_utils/src/directory_utils.py +108 -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/file_utils/imports/constants.py +81 -7
- abstract_utilities/file_utils/imports/imports.py +0 -4
- abstract_utilities/file_utils/imports/module_imports.py +1 -1
- abstract_utilities/file_utils/src/__init__.py +2 -4
- abstract_utilities/file_utils/src/file_filters/__init__.py +4 -0
- abstract_utilities/file_utils/src/file_filters/ensure_utils.py +116 -0
- abstract_utilities/file_utils/src/file_filters/filter_params.py +86 -0
- abstract_utilities/file_utils/src/file_filters/filter_utils.py +78 -0
- abstract_utilities/file_utils/src/file_filters/predicate_utils.py +114 -0
- abstract_utilities/file_utils/src/file_filters.py +114 -47
- abstract_utilities/file_utils/src/file_reader.py +0 -64
- abstract_utilities/file_utils/src/file_utils.py +7 -130
- abstract_utilities/file_utils/src/filter_params.py +128 -86
- abstract_utilities/file_utils/src/find_collect.py +85 -165
- abstract_utilities/file_utils/src/find_content.py +210 -0
- abstract_utilities/file_utils/src/initFunctionsGen.py +3 -9
- abstract_utilities/file_utils/src/reader_utils/__init__.py +4 -0
- abstract_utilities/file_utils/src/reader_utils/directory_reader.py +53 -0
- abstract_utilities/file_utils/src/reader_utils/file_reader.py +543 -0
- abstract_utilities/file_utils/src/reader_utils/file_readers.py +376 -0
- abstract_utilities/file_utils/src/reader_utils/imports.py +18 -0
- abstract_utilities/file_utils/src/reader_utils/pdf_utils.py +300 -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/import_utils/circular_import_finder.py +222 -0
- abstract_utilities/import_utils/circular_import_finder2.py +118 -0
- abstract_utilities/import_utils/imports/module_imports.py +3 -1
- abstract_utilities/import_utils/src/clean_imports.py +156 -25
- abstract_utilities/import_utils/src/dot_utils.py +11 -0
- abstract_utilities/import_utils/src/extract_utils.py +4 -0
- abstract_utilities/import_utils/src/import_functions.py +47 -2
- abstract_utilities/import_utils/src/pkg_utils.py +58 -4
- abstract_utilities/import_utils/src/sysroot_utils.py +56 -1
- abstract_utilities/log_utils/log_file.py +3 -2
- abstract_utilities/path_utils/path_utils.py +25 -23
- abstract_utilities/safe_utils/safe_utils.py +30 -0
- {abstract_utilities-0.2.2.513.dist-info → abstract_utilities-0.2.2.583.dist-info}/METADATA +1 -1
- {abstract_utilities-0.2.2.513.dist-info → abstract_utilities-0.2.2.583.dist-info}/RECORD +68 -28
- {abstract_utilities-0.2.2.513.dist-info → abstract_utilities-0.2.2.583.dist-info}/WHEEL +0 -0
- {abstract_utilities-0.2.2.513.dist-info → abstract_utilities-0.2.2.583.dist-info}/top_level.txt +0 -0
|
@@ -1,4 +1,22 @@
|
|
|
1
1
|
from .imports import *
|
|
2
|
+
|
|
3
|
+
def get_initial_caller() -> str:
|
|
4
|
+
"""
|
|
5
|
+
Return the TRUE original caller: the entrypoint script that launched the program.
|
|
6
|
+
"""
|
|
7
|
+
main_mod = sys.modules.get('__main__')
|
|
8
|
+
|
|
9
|
+
# interactive environments (REPL) may have no __file__
|
|
10
|
+
if not main_mod or not hasattr(main_mod, '__file__'):
|
|
11
|
+
return None
|
|
12
|
+
|
|
13
|
+
return os.path.realpath(main_mod.__file__)
|
|
14
|
+
def get_initial_caller_dir() -> str:
|
|
15
|
+
"""
|
|
16
|
+
Return the directory of the TRUE original entrypoint script.
|
|
17
|
+
"""
|
|
18
|
+
caller = get_initial_caller()
|
|
19
|
+
return os.path.dirname(caller) if caller else None
|
|
2
20
|
def get_caller(i: Optional[int] = None) -> str:
|
|
3
21
|
"""
|
|
4
22
|
Return the filename of the calling frame.
|
|
@@ -48,9 +48,10 @@ def change_glob(var: str, val: any, glob: dict = return_globals()) -> any:
|
|
|
48
48
|
def get_globes(string:str='',glob:dict=return_globals()):
|
|
49
49
|
if string in glob:
|
|
50
50
|
return glob[string]
|
|
51
|
-
def if_none_default(string:str, default:any,glob:dict=return_globals()):
|
|
51
|
+
def if_none_default(string:str, default:any,glob:dict=return_globals(),typ=None):
|
|
52
|
+
|
|
52
53
|
piece = get_globes(string=string,glob=glob)
|
|
53
|
-
if piece is None:
|
|
54
|
+
if piece is None or (typ and not isinstance(piece,typ)):
|
|
54
55
|
piece = default
|
|
55
56
|
return change_glob(var=string,val=piece,glob=glob)
|
|
56
57
|
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
from ...imports import inspect,os,json,functools,inspect,glob
|
|
1
|
+
from ...imports import inspect,os,json,functools,inspect,glob,sys
|
|
2
2
|
from typing import *
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from ...imports import os,shlex
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
from .utils import *
|
|
3
|
+
|
|
4
|
+
def get_shortest_path(*args):
|
|
5
|
+
shortest = None
|
|
6
|
+
args = {arg:str(arg).split('/') for arg in args if arg}
|
|
7
|
+
for path,spl in args.items():
|
|
8
|
+
if shortest is None or (len(spl)<len(shortest[-1])):
|
|
9
|
+
shortest=[path,spl]
|
|
10
|
+
return shortest[0]
|
|
11
|
+
def get_common_root(paths):
|
|
12
|
+
"""
|
|
13
|
+
Compute the deepest common parent directory of all sysroots.
|
|
14
|
+
"""
|
|
15
|
+
paths = [Path(p).resolve() for p in paths]
|
|
16
|
+
common = os.path.commonpath(paths)
|
|
17
|
+
return common
|
|
18
|
+
|
|
19
|
+
def get_dirs(path):
|
|
20
|
+
"""
|
|
21
|
+
Get List of Immediate Subdirectories in a Path
|
|
22
|
+
|
|
23
|
+
This function uses the os.walk method to traverse through a directory tree and returns a list of immediate subdirectories
|
|
24
|
+
within the specified path.
|
|
25
|
+
|
|
26
|
+
Parameters:
|
|
27
|
+
- path (str): The path for which subdirectories need to be retrieved.
|
|
28
|
+
|
|
29
|
+
Returns:
|
|
30
|
+
- subdirectories (list): A list of immediate subdirectories within the specified path.
|
|
31
|
+
|
|
32
|
+
Example:
|
|
33
|
+
subdirs = get_dirs("/path/to/directory")
|
|
34
|
+
print("Immediate Subdirectories:", subdirs)
|
|
35
|
+
"""
|
|
36
|
+
from os import walk
|
|
37
|
+
for (dirpath, dirnames, filenames) in walk(path):
|
|
38
|
+
return dirnames
|
|
39
|
+
def get_directory(file_path: str) -> str:
|
|
40
|
+
"""
|
|
41
|
+
Extracts and returns the directory path from a given file path.
|
|
42
|
+
|
|
43
|
+
Args:
|
|
44
|
+
file_path (str): A string representing the file path.
|
|
45
|
+
|
|
46
|
+
Returns:
|
|
47
|
+
str: The directory path extracted from the file path.
|
|
48
|
+
"""
|
|
49
|
+
return os.path.dirname(file_path)
|
|
50
|
+
def if_not_last_child_join(path:str,child:str):
|
|
51
|
+
"""
|
|
52
|
+
Adds a child path to the given path if it's not already present at the end.
|
|
53
|
+
|
|
54
|
+
Args:
|
|
55
|
+
path (str): The parent path.
|
|
56
|
+
child (str): The child path to add.
|
|
57
|
+
|
|
58
|
+
Returns:
|
|
59
|
+
str: The updated path.
|
|
60
|
+
"""
|
|
61
|
+
if path.endswith(child):
|
|
62
|
+
return path
|
|
63
|
+
return simple_path_join(path, child)
|
|
64
|
+
def createFolds(ls: list) -> None:
|
|
65
|
+
"""
|
|
66
|
+
Creates multiple directories.
|
|
67
|
+
|
|
68
|
+
Args:
|
|
69
|
+
ls (list): The list of directory paths to create.
|
|
70
|
+
"""
|
|
71
|
+
for k in range(len(ls)):
|
|
72
|
+
mkdirs(ls[k])
|
|
73
|
+
def list_directory_contents(path: str) -> list:
|
|
74
|
+
"""Returns a list of directory contents or a list with a single file, if the path is a file.
|
|
75
|
+
|
|
76
|
+
Args:
|
|
77
|
+
path (str): The path of the directory or file.
|
|
78
|
+
|
|
79
|
+
Returns:
|
|
80
|
+
list: A list of directory contents or a list with a single file path.
|
|
81
|
+
"""
|
|
82
|
+
if is_file(path):
|
|
83
|
+
return [path]
|
|
84
|
+
elif is_valid_path(path):
|
|
85
|
+
return os.listdir(path)
|
|
86
|
+
return [path]
|
|
87
|
+
def is_string_in_dir(path,strings,**kwargs):
|
|
88
|
+
dirname = path
|
|
89
|
+
if is_file(path,**kwargs):
|
|
90
|
+
dirname = os.path.dirname(path)
|
|
91
|
+
pieces = [pa for pa in dirname.split('/') if pa and pa in strings]
|
|
92
|
+
if pieces:
|
|
93
|
+
return True
|
|
94
|
+
return False
|
|
95
|
+
def raw_create_dirs(*paths):
|
|
96
|
+
"""Recursively create all directories along the given path."""
|
|
97
|
+
full_path = os.path.abspath(safe_join(*paths))
|
|
98
|
+
sub_parts = [p for p in full_path.split(os.sep) if p]
|
|
99
|
+
|
|
100
|
+
current_path = "/" if full_path.startswith(os.sep) else ""
|
|
101
|
+
for part in sub_parts:
|
|
102
|
+
current_path = safe_join(current_path, part)
|
|
103
|
+
os.makedirs(current_path, exist_ok=True)
|
|
104
|
+
return full_path
|
|
105
|
+
mkdirs=raw_create_dirs
|
|
106
|
+
makedirs = mkdirs
|
|
107
|
+
make_dirs = makedirs
|
|
108
|
+
|
|
@@ -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,116 @@
|
|
|
1
|
+
from ..imports import *
|
|
2
|
+
def split_text(string: str) -> tuple:
|
|
3
|
+
"""
|
|
4
|
+
Splits a string into its base name and extension and returns them as a tuple.
|
|
5
|
+
|
|
6
|
+
Args:
|
|
7
|
+
string (str): A string to be split, typically representing a file name.
|
|
8
|
+
|
|
9
|
+
Returns:
|
|
10
|
+
tuple: A tuple containing the base name and extension of the input string.
|
|
11
|
+
"""
|
|
12
|
+
return os.path.splitext(string)
|
|
13
|
+
def get_ext(file_path: str) -> str:
|
|
14
|
+
"""
|
|
15
|
+
Retrieves and returns the extension of a file from a given file path.
|
|
16
|
+
|
|
17
|
+
Args:
|
|
18
|
+
file_path (str): A string representing the file path.
|
|
19
|
+
|
|
20
|
+
Returns:
|
|
21
|
+
str: The extension of the file (including the dot).
|
|
22
|
+
"""
|
|
23
|
+
return split_text(get_base_name(file_path))[1]
|
|
24
|
+
|
|
25
|
+
def get_slash():
|
|
26
|
+
"""
|
|
27
|
+
Returns the appropriate file path separator depending on the current operating system.
|
|
28
|
+
"""
|
|
29
|
+
slash = '/' # Assume a Unix-like system by default
|
|
30
|
+
if slash not in get_current_path():
|
|
31
|
+
slash = '\\' # Use backslash for Windows systems
|
|
32
|
+
return slash
|
|
33
|
+
def get_current_path():
|
|
34
|
+
"""
|
|
35
|
+
Returns the current working directory.
|
|
36
|
+
|
|
37
|
+
Returns:
|
|
38
|
+
str: The current working directory.
|
|
39
|
+
"""
|
|
40
|
+
return os.getcwd()
|
|
41
|
+
|
|
42
|
+
def get_home_folder():
|
|
43
|
+
"""
|
|
44
|
+
Returns the path to the home directory of the current user.
|
|
45
|
+
|
|
46
|
+
Returns:
|
|
47
|
+
str: The path to the home directory.
|
|
48
|
+
"""
|
|
49
|
+
return os.path.expanduser("~")
|
|
50
|
+
def simple_path_join(path_A:str, path_B:str):
|
|
51
|
+
"""
|
|
52
|
+
Join two paths using the appropriate file path separator.
|
|
53
|
+
|
|
54
|
+
Args:
|
|
55
|
+
path_A (str): The first path to join.
|
|
56
|
+
path_B (str): The second path to join.
|
|
57
|
+
|
|
58
|
+
Returns:
|
|
59
|
+
str: The joined path.
|
|
60
|
+
"""
|
|
61
|
+
return os.path.join(str(path_A), str(path_B))
|
|
62
|
+
|
|
63
|
+
def path_join(path_A, path_B=None):
|
|
64
|
+
"""
|
|
65
|
+
Joins two paths or a list of paths using the appropriate file path separator.
|
|
66
|
+
|
|
67
|
+
Args:
|
|
68
|
+
path_A (str or list): The first path or list of paths to join.
|
|
69
|
+
path_B (str, optional): The second path to join. Defaults to None.
|
|
70
|
+
|
|
71
|
+
Returns:
|
|
72
|
+
str: The joined path.
|
|
73
|
+
"""
|
|
74
|
+
if path_B is not None: # If path_B is provided, join path_A and path_B
|
|
75
|
+
return simple_path_join(path_A, path_B)
|
|
76
|
+
if isinstance(path_A, list): # If path_A is a list, join all paths in the list
|
|
77
|
+
path = path_A[0]
|
|
78
|
+
for k in range(1, len(path_A)):
|
|
79
|
+
path = simple_path_join(path, path_A[k])
|
|
80
|
+
return path
|
|
81
|
+
def update_global_variable(name: str, value) -> None:
|
|
82
|
+
"""Updates the global variable with the provided name and value.
|
|
83
|
+
|
|
84
|
+
Args:
|
|
85
|
+
name (str): The name of the global variable.
|
|
86
|
+
value: The value to assign to the global variable.
|
|
87
|
+
|
|
88
|
+
Returns:
|
|
89
|
+
None
|
|
90
|
+
"""
|
|
91
|
+
globals()[name] = value
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def trunc(a: float, x: int) -> float:
|
|
95
|
+
"""
|
|
96
|
+
Truncates a float number to a specific number of decimal places.
|
|
97
|
+
|
|
98
|
+
Args:
|
|
99
|
+
a (float): The number to truncate.
|
|
100
|
+
x (int): The number of decimal places to retain.
|
|
101
|
+
|
|
102
|
+
Returns:
|
|
103
|
+
float: The truncated float number.
|
|
104
|
+
"""
|
|
105
|
+
temp = str(a)
|
|
106
|
+
for i in range(len(temp)):
|
|
107
|
+
if temp[i] == '.':
|
|
108
|
+
try:
|
|
109
|
+
return float(temp[:i+x+1])
|
|
110
|
+
except:
|
|
111
|
+
return float(temp)
|
|
112
|
+
return float(temp)
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
|
|
@@ -1,15 +1,40 @@
|
|
|
1
1
|
from .imports import *
|
|
2
2
|
from .module_imports import *
|
|
3
|
+
# -------------------------
|
|
4
|
+
# Config dataclass
|
|
5
|
+
# -------------------------
|
|
6
|
+
|
|
3
7
|
@dataclass
|
|
4
8
|
class ScanConfig:
|
|
5
9
|
allowed_exts: Set[str]
|
|
6
|
-
|
|
10
|
+
exclude_exts: Set[str]
|
|
7
11
|
allowed_types: Set[str]
|
|
8
12
|
exclude_types: Set[str]
|
|
9
13
|
allowed_dirs: List[str] = field(default_factory=list)
|
|
10
14
|
exclude_dirs: List[str] = field(default_factory=list)
|
|
11
15
|
allowed_patterns: List[str] = field(default_factory=list)
|
|
12
16
|
exclude_patterns: List[str] = field(default_factory=list)
|
|
17
|
+
|
|
18
|
+
@dataclass
|
|
19
|
+
class SearchParams(ScanConfig):
|
|
20
|
+
directories: List[str] = field(default_factory=list)
|
|
21
|
+
add: bool = False
|
|
22
|
+
recursive: bool = True
|
|
23
|
+
strings: List[str] = field(default_factory=list)
|
|
24
|
+
total_strings: bool = False
|
|
25
|
+
parse_lines: bool = False
|
|
26
|
+
spec_line: Union[bool, int] = False
|
|
27
|
+
get_lines: bool = False
|
|
28
|
+
|
|
29
|
+
@dataclass
|
|
30
|
+
class AllParams(SearchParams):
|
|
31
|
+
cfg = None
|
|
32
|
+
allowed: Optional[Callable[[str], bool]] = None
|
|
33
|
+
include_files: bool = True
|
|
34
|
+
recursive: bool = True
|
|
35
|
+
# -------------------------
|
|
36
|
+
# Default sets
|
|
37
|
+
# -------------------------
|
|
13
38
|
DEFAULT_ALLOWED_EXTS: Set[str] = {
|
|
14
39
|
".py", ".pyw", # python
|
|
15
40
|
".js", ".jsx", ".ts", ".tsx", ".mjs", # JS/TS
|
|
@@ -27,11 +52,15 @@ DEFAULT_EXCLUDE_TYPES: Set[str] = {
|
|
|
27
52
|
}
|
|
28
53
|
|
|
29
54
|
# never want these—even if they sneak into ALLOWED
|
|
30
|
-
_unallowed = set(get_media_exts(DEFAULT_EXCLUDE_TYPES)) | {
|
|
31
|
-
|
|
55
|
+
_unallowed = set(get_media_exts(DEFAULT_EXCLUDE_TYPES)) | {
|
|
56
|
+
".bak", ".shp", ".cpg", ".dbf", ".shx", ".geojson",
|
|
57
|
+
".pyc", ".prj", ".sbn", ".sbx"
|
|
58
|
+
}
|
|
59
|
+
DEFAULT_EXCLUDE_EXTS = {e.split('.')[-1] for e in _unallowed if e not in DEFAULT_ALLOWED_EXTS}
|
|
32
60
|
|
|
33
61
|
DEFAULT_EXCLUDE_DIRS: Set[str] = {
|
|
34
|
-
"node_modules", "old","__pycache__", "backups", "backup",
|
|
62
|
+
"node_modules", "old","__pycache__", "backups", "backup",
|
|
63
|
+
"backs", "trash", "depriciated", "old", "__init__"
|
|
35
64
|
}
|
|
36
65
|
|
|
37
66
|
DEFAULT_EXCLUDE_PATTERNS: Set[str] = {
|
|
@@ -40,6 +69,51 @@ DEFAULT_EXCLUDE_PATTERNS: Set[str] = {
|
|
|
40
69
|
REMOTE_RE = re.compile(r"^(?P<host>[^:\s]+@[^:\s]+):(?P<path>/.*)$")
|
|
41
70
|
AllowedPredicate = Optional[Callable[[str], bool]]
|
|
42
71
|
DEFAULT_EXCLUDE_FILE_PATTERNS=DEFAULT_EXCLUDE_PATTERNS
|
|
43
|
-
|
|
44
|
-
DEFAULT_ALLOWED_DIRS="*"
|
|
45
|
-
DEFAULT_ALLOWED_TYPES="*"
|
|
72
|
+
DEFAULT_ALLOWED_PATTERNS: List[str] = ["*"]
|
|
73
|
+
DEFAULT_ALLOWED_DIRS: List[str] = ["*"]
|
|
74
|
+
DEFAULT_ALLOWED_TYPES: List[str] = ["*"]
|
|
75
|
+
CANONICAL_MAP = {
|
|
76
|
+
"directories": ["directory", "directories", "dir","dirs","directory","directories","d","dirname", "paths", "path","roots","root"],
|
|
77
|
+
"files":["file","filepath","file_path","files","filepaths","file_paths","paths", "path","f"],
|
|
78
|
+
"allowed_exts": ["allow_ext", "allowed_ext", "include_ext", "include_exts", "exts_allowed"],
|
|
79
|
+
"exclude_exts": ["exclude_ext", "excluded_ext", "excluded_exts", "unallowed_ext", "unallowed_exts"],
|
|
80
|
+
"allowed_types": ["allow_type", "allowed_type", "include_type", "include_types", "types_allowed"],
|
|
81
|
+
"exclude_types": ["exclude_type", "excluded_type", "excluded_types", "unallowed_type", "unallowed_types"],
|
|
82
|
+
"allowed_dirs": ["allow_dir", "allowed_dir", "include_dir", "include_dirs", "dirs_allowed"],
|
|
83
|
+
"exclude_dirs": ["exclude_dir", "excluded_dir", "excluded_dirs", "unallowed_dir", "unallowed_dirs"],
|
|
84
|
+
"allowed_patterns": ["allow_pattern", "allowed_pattern", "include_pattern", "include_patterns", "patterns_allowed"],
|
|
85
|
+
"exclude_patterns": ["exclude_pattern", "excluded_pattern", "excluded_patterns", "unallowed_pattern", "unallowed_patterns"],
|
|
86
|
+
"add":["add"],
|
|
87
|
+
"cfg":["cfg"],
|
|
88
|
+
"recursive":["recursive"],
|
|
89
|
+
"strings":["strings"],
|
|
90
|
+
"total_strings":["total_strings"],
|
|
91
|
+
"parse_lines":["parse_lines"],
|
|
92
|
+
"spec_line":["spec_line"],
|
|
93
|
+
"get_lines":["get_lines"]
|
|
94
|
+
}
|
|
95
|
+
DEFAULT_ALLOWED_EXCLUDE_MAP={
|
|
96
|
+
"allowed_exts": {"default":DEFAULT_ALLOWED_EXTS,"type":type(DEFAULT_ALLOWED_EXTS),"canonical":CANONICAL_MAP.get("allowed_exts")},
|
|
97
|
+
"exclude_exts": {"default":DEFAULT_EXCLUDE_EXTS,"type":type(DEFAULT_EXCLUDE_EXTS),"canonical":CANONICAL_MAP.get("exclude_exts")},
|
|
98
|
+
"allowed_types": {"default":DEFAULT_ALLOWED_TYPES,"type":type(DEFAULT_ALLOWED_TYPES),"canonical":CANONICAL_MAP.get("allowed_types")},
|
|
99
|
+
"exclude_types": {"default":DEFAULT_EXCLUDE_TYPES,"type":type(DEFAULT_EXCLUDE_TYPES),"canonical":CANONICAL_MAP.get("exclude_types")},
|
|
100
|
+
"allowed_dirs": {"default":DEFAULT_ALLOWED_DIRS,"type":type(DEFAULT_ALLOWED_DIRS),"canonical":CANONICAL_MAP.get("allowed_dirs")},
|
|
101
|
+
"exclude_dirs": {"default":DEFAULT_EXCLUDE_DIRS,"type":type(DEFAULT_EXCLUDE_DIRS),"canonical":CANONICAL_MAP.get("exclude_dirs")},
|
|
102
|
+
"allowed_patterns": {"default":DEFAULT_ALLOWED_PATTERNS,"type":type(DEFAULT_ALLOWED_PATTERNS),"canonical":CANONICAL_MAP.get("allowed_patterns")},
|
|
103
|
+
"exclude_patterns": {"default":DEFAULT_EXCLUDE_PATTERNS,"type":type(DEFAULT_EXCLUDE_PATTERNS),"canonical":CANONICAL_MAP.get("exclude_patterns")},
|
|
104
|
+
}
|
|
105
|
+
DEFAULT_CANONICAL_MAP={
|
|
106
|
+
"directories":{"default":[],"type":list,"canonical":CANONICAL_MAP.get("directories")},
|
|
107
|
+
"files":{"default":[],"type":list,"canonical":CANONICAL_MAP.get("files")},
|
|
108
|
+
**DEFAULT_ALLOWED_EXCLUDE_MAP,
|
|
109
|
+
"allowed":{"default":None,"type":bool,"canonical":CANONICAL_MAP.get("allowed")},
|
|
110
|
+
"add":{"default":False,"type":bool,"canonical":CANONICAL_MAP.get("add")},
|
|
111
|
+
"recursive":{"default":True,"type":bool,"canonical":CANONICAL_MAP.get("recursive")},
|
|
112
|
+
"strings":{"default":None,"type":list,"canonical":CANONICAL_MAP.get("strings")},
|
|
113
|
+
"total_strings":{"default":False,"type":bool,"canonical":CANONICAL_MAP.get("total_strings")},
|
|
114
|
+
"parse_lines":{"default":False,"type":bool,"canonical":CANONICAL_MAP.get("parse_lines")},
|
|
115
|
+
"spec_line":{"default":False,"type":bool,"canonical":CANONICAL_MAP.get("spec_line")},
|
|
116
|
+
"get_lines":{"default":False,"type":bool,"canonical":CANONICAL_MAP.get("get_lines")},
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
|
|
@@ -4,5 +4,5 @@ from ...ssh_utils import *
|
|
|
4
4
|
from ...env_utils import *
|
|
5
5
|
from ...read_write_utils import read_from_file,write_to_file
|
|
6
6
|
from ...log_utils import get_logFile
|
|
7
|
-
from ...class_utils import get_caller, get_caller_path, get_caller_dir,SingletonMeta,run_pruned_func
|
|
7
|
+
from ...class_utils import get_caller,get_initial_caller_dir, get_caller_path, get_caller_dir,SingletonMeta,run_pruned_func
|
|
8
8
|
|
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
from .file_filters import *
|
|
2
|
-
from .file_utils import *
|
|
3
|
-
from .filter_params import *
|
|
4
2
|
from .map_utils import *
|
|
5
|
-
from .
|
|
6
|
-
from .file_reader import *
|
|
3
|
+
from .reader_utils import *
|
|
7
4
|
from .find_collect import *
|
|
5
|
+
from .find_content import *
|
|
8
6
|
from .initFunctionsGen import call_for_all_tabs,get_for_all_tabs
|
|
9
7
|
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
from .filter_utils import *
|
|
2
|
+
def normalize_listlike(value, typ=list, sep=','):
|
|
3
|
+
"""Normalize comma-separated or iterable values into the desired type."""
|
|
4
|
+
if value in [True, None, False]:
|
|
5
|
+
return value
|
|
6
|
+
if isinstance(value, str):
|
|
7
|
+
value = [v.strip() for v in value.split(sep) if v.strip()]
|
|
8
|
+
return typ(value)
|
|
9
|
+
|
|
10
|
+
def ensure_exts(exts):
|
|
11
|
+
if exts in [True, None, False]:
|
|
12
|
+
return exts
|
|
13
|
+
out = []
|
|
14
|
+
for ext in normalize_listlike(exts, list):
|
|
15
|
+
if not ext.startswith('.'):
|
|
16
|
+
ext = f".{ext}"
|
|
17
|
+
out.append(ext)
|
|
18
|
+
return set(out)
|
|
19
|
+
|
|
20
|
+
def ensure_patterns(patterns):
|
|
21
|
+
"""Normalize pattern list and ensure they are valid globs."""
|
|
22
|
+
if patterns in [True, None, False]:
|
|
23
|
+
return patterns
|
|
24
|
+
patterns = normalize_listlike(patterns, list)
|
|
25
|
+
out = []
|
|
26
|
+
for pattern in patterns:
|
|
27
|
+
if not pattern:
|
|
28
|
+
continue
|
|
29
|
+
if '*' not in pattern and '?' not in pattern:
|
|
30
|
+
# Implicitly make it a prefix match
|
|
31
|
+
if pattern.startswith('.') or pattern.startswith('~'):
|
|
32
|
+
pattern = f"*{pattern}"
|
|
33
|
+
else:
|
|
34
|
+
pattern = f"{pattern}*"
|
|
35
|
+
out.append(pattern)
|
|
36
|
+
return out
|
|
37
|
+
def ensure_directories(*args,**kwargs):
|
|
38
|
+
directories = []
|
|
39
|
+
for arg in args:
|
|
40
|
+
arg_str = str(arg)
|
|
41
|
+
|
|
42
|
+
if run_pruned_func(is_dir,arg_str,**kwargs):
|
|
43
|
+
directories.append(arg_str)
|
|
44
|
+
elif run_pruned_func(is_file,arg_str,**kwargs):
|
|
45
|
+
dirname = os.path.dirname(arg_str)
|
|
46
|
+
directories.append(dirname)
|
|
47
|
+
safe_directories = get_dir_filter_kwargs(**kwargs)
|
|
48
|
+
directories+= make_list(safe_directories.get('directories',get_initial_caller_dir()))
|
|
49
|
+
return list(set([r for r in directories if r]))
|
|
50
|
+
def get_proper_type_str(string):
|
|
51
|
+
if not string:
|
|
52
|
+
return None
|
|
53
|
+
string_lower = string.lower()
|
|
54
|
+
items = {
|
|
55
|
+
"d":["dir","dirs","directory","directories","d","dirname"],
|
|
56
|
+
"f":["file","filepath","file_path","files","filepaths","file_paths","f"]
|
|
57
|
+
}
|
|
58
|
+
for key,values in items.items():
|
|
59
|
+
if string_lower in values:
|
|
60
|
+
return key
|
|
61
|
+
init = string_lower[0] if len(string_lower)>0 else None
|
|
62
|
+
if init in items:
|
|
63
|
+
return init
|
|
64
|
+
def check_path_type(
|
|
65
|
+
path: str,
|
|
66
|
+
user: Optional[str] = None,
|
|
67
|
+
host: Optional[str] = None,
|
|
68
|
+
user_as_host: Optional[str] = None,
|
|
69
|
+
use_shell: bool = False
|
|
70
|
+
) -> Literal["file", "directory", "missing", "unknown"]:
|
|
71
|
+
"""
|
|
72
|
+
Determine whether a given path is a file, directory, or missing.
|
|
73
|
+
Works locally or remotely (via SSH).
|
|
74
|
+
|
|
75
|
+
Args:
|
|
76
|
+
path: The path to check.
|
|
77
|
+
user, host, user_as_host: SSH parameters if remote.
|
|
78
|
+
use_shell: Force shell test instead of Python os.path.
|
|
79
|
+
Returns:
|
|
80
|
+
One of: 'file', 'directory', 'missing', or 'unknown'
|
|
81
|
+
"""
|
|
82
|
+
|
|
83
|
+
# --- remote check if user/host is given ---
|
|
84
|
+
if user_as_host or (user and host):
|
|
85
|
+
remote_target = user_as_host or f"{user}@{host}"
|
|
86
|
+
cmd = f"if [ -f '{path}' ]; then echo file; elif [ -d '{path}' ]; then echo directory; else echo missing; fi"
|
|
87
|
+
try:
|
|
88
|
+
result = subprocess.check_output(
|
|
89
|
+
["ssh", remote_target, cmd],
|
|
90
|
+
stderr=subprocess.DEVNULL,
|
|
91
|
+
text=True,
|
|
92
|
+
timeout=5
|
|
93
|
+
).strip()
|
|
94
|
+
return result if result in ("file", "directory", "missing") else "unknown"
|
|
95
|
+
except Exception:
|
|
96
|
+
return "unknown"
|
|
97
|
+
|
|
98
|
+
# --- local check ---
|
|
99
|
+
if not use_shell:
|
|
100
|
+
if os.path.isfile(path):
|
|
101
|
+
return "file"
|
|
102
|
+
elif os.path.isdir(path):
|
|
103
|
+
return "directory"
|
|
104
|
+
elif not os.path.exists(path):
|
|
105
|
+
return "missing"
|
|
106
|
+
return "unknown"
|
|
107
|
+
else:
|
|
108
|
+
# fallback using shell tests (useful for sandboxed contexts)
|
|
109
|
+
cmd = f"if [ -f '{path}' ]; then echo file; elif [ -d '{path}' ]; then echo directory; else echo missing; fi"
|
|
110
|
+
try:
|
|
111
|
+
output = subprocess.check_output(
|
|
112
|
+
cmd, shell=True, stderr=subprocess.DEVNULL, text=True
|
|
113
|
+
).strip()
|
|
114
|
+
return output if output in ("file", "directory", "missing") else "unknown"
|
|
115
|
+
except Exception:
|
|
116
|
+
return "unknown"
|