abstract-utilities 0.2.2.449__py3-none-any.whl → 0.2.2.476__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 (68) hide show
  1. abstract_utilities/__init__.py +18 -44
  2. abstract_utilities/abstract_classes.py +49 -0
  3. abstract_utilities/class_utils.py +38 -3
  4. abstract_utilities/cmd_utils/imports/__init__.py +1 -0
  5. abstract_utilities/cmd_utils/imports/imports.py +10 -0
  6. abstract_utilities/cmd_utils/pexpect_utils.py +310 -0
  7. abstract_utilities/cmd_utils/user_utils.py +1 -1
  8. abstract_utilities/compare_utils/__init__.py +3 -0
  9. abstract_utilities/compare_utils/best_match.py +150 -0
  10. abstract_utilities/{compare_utils.py → compare_utils/compare_utils.py} +1 -1
  11. abstract_utilities/compare_utils/find_value.py +105 -0
  12. abstract_utilities/dynimport.py +7 -15
  13. abstract_utilities/env_utils/__init__.py +3 -0
  14. abstract_utilities/env_utils/abstractEnv.py +129 -0
  15. abstract_utilities/env_utils/envy_it.py +33 -0
  16. abstract_utilities/env_utils/imports/__init__.py +2 -0
  17. abstract_utilities/env_utils/imports/imports.py +8 -0
  18. abstract_utilities/env_utils/imports/utils.py +122 -0
  19. abstract_utilities/file_utils/__init__.py +3 -0
  20. abstract_utilities/file_utils/file_utils/__init__.py +8 -0
  21. abstract_utilities/file_utils/file_utils/file_filters.py +104 -0
  22. abstract_utilities/{robust_reader → file_utils/file_utils}/file_reader.py +5 -19
  23. abstract_utilities/{robust_readers/file_filters.py → file_utils/file_utils/file_utils.py} +5 -4
  24. abstract_utilities/{robust_readers → file_utils/file_utils}/filter_params.py +1 -38
  25. abstract_utilities/file_utils/file_utils/find_collect.py +154 -0
  26. abstract_utilities/file_utils/file_utils/imports/__init__.py +3 -0
  27. abstract_utilities/file_utils/file_utils/imports/constants.py +39 -0
  28. abstract_utilities/file_utils/file_utils/imports/file_functions.py +10 -0
  29. abstract_utilities/file_utils/file_utils/imports/imports.py +39 -0
  30. abstract_utilities/file_utils/file_utils/imports/module_imports.py +13 -0
  31. abstract_utilities/file_utils/file_utils/imports.py +10 -0
  32. abstract_utilities/file_utils/file_utils/map_utils.py +29 -0
  33. abstract_utilities/{robust_reader → file_utils/file_utils}/pdf_utils.py +1 -9
  34. abstract_utilities/file_utils/file_utils/type_checks.py +82 -0
  35. abstract_utilities/file_utils/imports/__init__.py +4 -0
  36. abstract_utilities/file_utils/imports/classes.py +381 -0
  37. abstract_utilities/file_utils/imports/clean_imps.py +158 -0
  38. abstract_utilities/file_utils/imports/constants.py +39 -0
  39. abstract_utilities/file_utils/imports/file_functions.py +10 -0
  40. abstract_utilities/file_utils/imports/imports.py +65 -0
  41. abstract_utilities/file_utils/imports/module_imports.py +13 -0
  42. abstract_utilities/file_utils/req.py +329 -0
  43. abstract_utilities/json_utils.py +35 -0
  44. abstract_utilities/log_utils.py +14 -3
  45. abstract_utilities/path_utils.py +90 -6
  46. abstract_utilities/read_write_utils.py +176 -154
  47. abstract_utilities/robust_reader/__init__.py +1 -1
  48. abstract_utilities/robust_reader/imports/__init__.py +1 -0
  49. abstract_utilities/robust_reader/imports/imports.py +3 -0
  50. abstract_utilities/robust_readers/__init__.py +0 -1
  51. abstract_utilities/robust_readers/import_utils/__init__.py +1 -0
  52. abstract_utilities/robust_readers/import_utils/clean_imports.py +175 -0
  53. abstract_utilities/robust_readers/imports.py +8 -0
  54. abstract_utilities/robust_readers/initFuncGen.py +92 -76
  55. abstract_utilities/safe_utils.py +133 -0
  56. abstract_utilities/ssh_utils/__init__.py +3 -0
  57. abstract_utilities/ssh_utils/classes.py +127 -0
  58. abstract_utilities/ssh_utils/imports.py +10 -0
  59. abstract_utilities/ssh_utils/pexpect_utils.py +315 -0
  60. abstract_utilities/ssh_utils/utils.py +188 -0
  61. abstract_utilities/string_clean.py +40 -1
  62. abstract_utilities/string_utils.py +48 -0
  63. abstract_utilities/type_utils.py +25 -2
  64. {abstract_utilities-0.2.2.449.dist-info → abstract_utilities-0.2.2.476.dist-info}/METADATA +15 -4
  65. abstract_utilities-0.2.2.476.dist-info/RECORD +92 -0
  66. {abstract_utilities-0.2.2.449.dist-info → abstract_utilities-0.2.2.476.dist-info}/WHEEL +1 -1
  67. abstract_utilities-0.2.2.449.dist-info/RECORD +0 -49
  68. {abstract_utilities-0.2.2.449.dist-info → abstract_utilities-0.2.2.476.dist-info}/top_level.txt +0 -0
@@ -9,7 +9,7 @@ Date: 05/31/2023
9
9
  Version: 0.1.2
10
10
  """
11
11
  import string
12
- from .type_utils import is_number,make_list
12
+ from ..type_utils import is_number,make_list
13
13
  def get_comp(string:str, string_2:str):
14
14
  """
15
15
  Calculates the similarity between two strings.
@@ -0,0 +1,105 @@
1
+ from __future__ import annotations
2
+ from typing import Any, Callable, Iterable, Iterator, List, Tuple, Union, Dict
3
+ import re
4
+
5
+ JSONLike = Union[dict, list, tuple, set, str, int, float, bool, None]
6
+ PathType = Tuple[Union[str, int], ...] # ('window_title',) or ('tabs', 2, 'name'), etc.
7
+
8
+ def iter_values(obj: JSONLike, path: PathType = ()) -> Iterator[Tuple[PathType, Any]]:
9
+ """
10
+ Depth-first walk of nested dict/list/tuple/set. Yields (path, value) for every leaf.
11
+ Path items are str (dict key) or int (list index).
12
+ """
13
+ if isinstance(obj, dict):
14
+ for k, v in obj.items():
15
+ yield from iter_values(v, path + (k,))
16
+ elif isinstance(obj, (list, tuple)):
17
+ for i, v in enumerate(obj):
18
+ yield from iter_values(v, path + (i,))
19
+ elif isinstance(obj, set):
20
+ for v in obj:
21
+ # sets are unordered/no index – use a pseudo-key
22
+ yield from iter_values(v, path + ('<setitem>',))
23
+ else:
24
+ # leaf (scalar)
25
+ yield path, obj
26
+
27
+ def _mk_predicate(
28
+ terms: Iterable[str] | None = None,
29
+ *,
30
+ case_insensitive: bool = True,
31
+ substring: bool = True,
32
+ regex: bool = False
33
+ ) -> Callable[[Any], bool]:
34
+ """
35
+ Build a predicate that checks a scalar value against terms.
36
+ """
37
+ terms = list(terms or [])
38
+ if regex:
39
+ flags = re.IGNORECASE if case_insensitive else 0
40
+ patterns = [re.compile(t, flags) for t in terms]
41
+ def pred(value: Any) -> bool:
42
+ if not isinstance(value, str):
43
+ return False
44
+ return any(p.search(value) for p in patterns)
45
+ return pred
46
+
47
+ # string contains (or equals)
48
+ def pred(value: Any) -> bool:
49
+ if not isinstance(value, str):
50
+ return False
51
+ v = value.lower() if case_insensitive else value
52
+ for t in terms:
53
+ t2 = t.lower() if case_insensitive else t
54
+ if (t2 in v) if substring else (t2 == v):
55
+ return True
56
+ return False
57
+
58
+ return pred
59
+
60
+ def search_values(
61
+ obj: JSONLike,
62
+ *,
63
+ terms: Iterable[str],
64
+ case_insensitive: bool = True,
65
+ substring: bool = True,
66
+ regex: bool = False,
67
+ ) -> List[Tuple[PathType, Any]]:
68
+ """
69
+ Return all (path, value) where value matches any term.
70
+ """
71
+ pred = _mk_predicate(terms, case_insensitive=case_insensitive, substring=substring, regex=regex)
72
+ results: List[Tuple[PathType, Any]] = []
73
+ for p, v in iter_values(obj):
74
+ if pred(v):
75
+ results.append((p, v))
76
+ return results
77
+
78
+ def any_match(
79
+ obj: JSONLike,
80
+ *,
81
+ terms: Iterable[str],
82
+ **kw
83
+ ) -> bool:
84
+ """Fast boolean check."""
85
+ pred = _mk_predicate(terms, **kw)
86
+ for _, v in iter_values(obj):
87
+ if pred(v):
88
+ return True
89
+ return False
90
+ def get_first_match(obj: JSONLike, *, terms: Iterable[str], **kw) -> Optional[Any]:
91
+ """Return just the first matching value, or None."""
92
+ pred = _mk_predicate(terms, **kw)
93
+ for _, v in iter_values(obj):
94
+ if pred(v):
95
+ return v
96
+ return None
97
+
98
+ def get_all_match(obj: JSONLike, *, terms: Iterable[str], **kw) -> List[Any]:
99
+ """Return all matching values as a flat list."""
100
+ pred = _mk_predicate(terms, **kw)
101
+ results: List[Any] = []
102
+ for _, v in iter_values(obj):
103
+ if pred(v):
104
+ results.append(v)
105
+ return results
@@ -1,12 +1,12 @@
1
1
  # abstract_utilities/dynimport.py
2
2
  from __future__ import annotations
3
- import importlib, sys, os
4
3
  from functools import lru_cache
5
- from typing import Any, Callable, Iterable, Optional
6
- import inspect, sys
4
+ from typing import *
5
+ import importlib, sys, os, sys,inspect
6
+ from typing import Optional
7
7
  from importlib import import_module
8
8
  from .type_utils import make_list
9
- from .robust_readers import get_for_all_tabs,call_for_all_tabs
9
+
10
10
  class _LazyAttr:
11
11
  """Lazy resolver proxy to avoid import-time cycles.
12
12
  First use triggers actual import & attribute lookup.
@@ -181,14 +181,6 @@ def get_many_module_imports(*args):
181
181
  all_modules[symbol] = get_abstract_import(module = module,symbol=symbol)
182
182
  import_symbols(all_modules)
183
183
  return all_modules
184
- def get_caller_path():
185
- frame = inspect.stack()[1]
186
- return os.path.abspath(frame.filename)
187
- def get_caller_dir():
188
- frame = inspect.stack()[1]
189
- abspath = os.path.abspath(frame.filename)
190
- return os.path.dirname(abspath)
191
- def call_for_all_tabs():
192
-
193
- root = get_caller_dir()
194
- get_for_all_tabs(root)
184
+
185
+
186
+
@@ -0,0 +1,3 @@
1
+ from .imports import *
2
+ from .abstractEnv import *
3
+ from .envy_it import *
@@ -0,0 +1,129 @@
1
+ from .imports import *
2
+ class abstractEnv:
3
+ def __init__(self,key='MY_PASSWORD',file_name=None,path=os.getcwd(),deep_scan=False):
4
+ file_name = file_name or '.env'
5
+ self.re_initialize(file_name=file_name,key=key,path=path,deep_scan=deep_scan)
6
+ def re_initialize(self,key='MY_PASSWORD',file_name='.env',path=os.getcwd(),deep_scan=False):
7
+ """
8
+ Initializes an AbstractEnv object to manage environment variables.
9
+
10
+ Args:
11
+ key (str, optional): The key to search for in the .env file. Defaults to 'MY_PASSWORD'.
12
+ file_name (str, optional): The name of the .env file. Defaults to '.env'.
13
+ path (str, optional): The path where the .env file is located. Defaults to the current working directory.
14
+ """
15
+
16
+ self.key = key or 'MY_PASSWORD'
17
+
18
+ file_name = file_name or '.env'
19
+ self.key = key or 'MY_PASSWORD'
20
+ self.deep_scan=deep_scan
21
+ self.current_folder = os.getcwd()
22
+ if path and os.path.isfile(path):
23
+ self.file_name = os.path.basename(path)
24
+ self.path = os.path.dirname(path)
25
+ else:
26
+ self.path = path or self.current_folder
27
+ self.file_name = file_name or '.env'
28
+ self.start_path_env = os.path.join(self.path,self.file_name)
29
+ self.home_folder = os.path.expanduser("~")
30
+ self.envy_all = os.path.join(self.home_folder,'.envy_all')
31
+ self.directories = self.get_directories()
32
+ self.env_value = self.find_and_read_env_file(key=self.key,file_name=self.file_name, path=self.path,initialize=False)
33
+
34
+ def find_and_read_env_file(self,key:str=None, file_name:str=None, path=None,initialize=True,deep_scan=False):
35
+ """
36
+ Search for an environment file and read a specific key from it.
37
+
38
+ Args:
39
+ file_name (str): Name of the .env file to be searched. Defaults to '.env'.
40
+ key (str): Key to be retrieved from the .env file. Defaults to 'MY_PASSWORD'.
41
+ start_path (str): Directory path to start the search from. If None, search starts from current directory.
42
+
43
+ Returns:
44
+ str: The value corresponding to the key if found, otherwise None.
45
+ """
46
+ # Set the default start_path to the current directory if it's None
47
+ # Try to find the file in the start_path
48
+ key = key or self.key
49
+ path = path or self.start_path_env
50
+ file_name = file_name or self.file_name
51
+ if initialize:
52
+ self.re_initialize(key=key,file_name=file_name,path=path)
53
+ for directory in self.directories:
54
+ if directory and os.path.isdir(directory) and self.file_name:
55
+ env_path = os.path.join(directory,self.file_name)
56
+ if os.path.isfile(env_path):
57
+ value = self.search_for_env_key(key=key,path=env_path)
58
+ self.env_path = os.path.join(directory,self.file_name)
59
+ if os.path.isfile(self.env_path):
60
+ value = self.search_for_env_key(key=key,path=self.env_path,deep_scan=deep_scan)
61
+ if value:
62
+ return value
63
+ def get_directories(self):
64
+ """
65
+ Retrieves a list of directories to search for the .env file.
66
+
67
+ Returns:
68
+ list: A list of directories including the specified path, current folder, home folder, and '.envy_all' directory.
69
+ """
70
+ directories=[]
71
+ for directory in [self.path,self.current_folder,self.home_folder,self.envy_all]:
72
+ if os.path.isdir(directory) and directory not in directories:
73
+ directories.append(directory)
74
+ return directories
75
+
76
+
77
+ def search_for_env_key(self,key:str=None,path:str=None,deep_scan=False):
78
+
79
+ """
80
+ Retrieves the value of a specified environment variable from a .env file.
81
+
82
+ Args:
83
+ key (str, optional): The key to search for in the .env file. Defaults to None.
84
+ path (str, optional): The path to the .env file. Defaults to None.
85
+ file_name (str, optional): The name of the .env file. Defaults to None.
86
+
87
+ Returns:
88
+ str: The value of the environment variable if found, otherwise None.
89
+ """
90
+
91
+ highest = [None,0,0.0]
92
+ key = key or self.default_env_key
93
+ path = path or self.start_path_env
94
+ if path and os.path.isfile(path):
95
+ with open(path, "r") as f:
96
+ for line in f:
97
+ line_key,line_value = split_eq(line)
98
+ # If the line contains the key, return the value after stripping extra characters
99
+ if line_key == key:
100
+ return line_value
101
+ if deep_scan:
102
+ line_keys = []
103
+ key_parts=0
104
+ for key_part in key.split('_'):
105
+ line_keys.append([])
106
+ if key_part in line_key:
107
+ line_keys[-1].append(line_key)
108
+ key_parts+=len(key_part)
109
+ if float(key_parts/len(key))>=0.5 and key_parts >highest[1]:
110
+ highest = [line_value,key_parts]
111
+ if deep_scan and highest[0] != None:
112
+ return line_value
113
+ def get_env_value(key:str=None,path:str=os.getcwd(),file_name:str=None):
114
+ """
115
+ Retrieves the value of the specified environment variable.
116
+
117
+ Args:
118
+ path (str): The path to the environment file. Defaults to None.
119
+ file_name (str): The name of the environment file. Defaults to '.env'.
120
+ key (str): The key to search for in the .env file. Defaults to 'MY_PASSWORD'.
121
+
122
+ Returns:
123
+ str: The value of the environment variable if found, otherwise None.
124
+ """
125
+ if safe_env_load(path):
126
+ return os.getenv(key)
127
+ return find_and_read_env_file(file_name=file_name, key=key, path=path_ls)
128
+
129
+ AbstractEnv = abstractEnv
@@ -0,0 +1,33 @@
1
+ from .imports import *
2
+ from .abstractEnv import abstractEnv
3
+ def get_env_value(key:str=None,path:str=None,file_name:str=None,deep_scan=False):
4
+ abstract_env = abstractEnv(key=key, file_name=file_name, path=path,deep_scan=deep_scan)
5
+
6
+ """
7
+ Retrieves the value of a specified environment variable from a .env file.
8
+
9
+ Args:
10
+ key (str, optional): The key to search for in the .env file. Defaults to None.
11
+ path (str, optional): The path to the .env file. Defaults to None.
12
+ file_name (str, optional): The name of the .env file. Defaults to None.
13
+
14
+ Returns:
15
+ str: The value of the environment variable if found, otherwise None.
16
+ """
17
+ return abstract_env.env_value
18
+
19
+
20
+ def get_env_path(key:str=None,path:str=None,file_name:str=None,deep_scan=False):
21
+ abstract_env = abstractEnv(key=key, file_name=file_name, path=path,deep_scan=deep_scan)
22
+ """
23
+ Retrieves the value of a specified environment variable from a .env file.
24
+
25
+ Args:
26
+ key (str, optional): The key to search for in the .env file. Defaults to None.
27
+ path (str, optional): The path to the .env file. Defaults to None.
28
+ file_name (str, optional): The name of the .env file. Defaults to None.
29
+
30
+ Returns:
31
+ str: The value of the environment variable if found, otherwise None.
32
+ """
33
+ return abstract_env.env_path
@@ -0,0 +1,2 @@
1
+ from .imports import *
2
+ from .utils import *
@@ -0,0 +1,8 @@
1
+ import os
2
+ from dotenv import load_dotenv
3
+ from ...string_clean import eatAll,eatInner,eatOuter,safe_split
4
+ from ...compare_utils import line_contains
5
+ from ...type_utils import is_list,is_bool
6
+ from ...path_utils import get_slash,path_join,if_not_last_child_join,get_home_folder,simple_path_join,is_file
7
+ DEFAULT_FILE_NAME = '.env'
8
+ DEFAULT_KEY = 'MY_PASSWORD'
@@ -0,0 +1,122 @@
1
+ from .imports import *
2
+ def find_and_read_env_file(key:str=DEFAULT_KEY,file_name:str=DEFAULT_FILE_NAME, start_path:str=None):
3
+ """
4
+ Search for an environment file and read a specific key from it.
5
+
6
+ Args:
7
+ file_name (str): Name of the .env file to be searched. Defaults to '.env'.
8
+ key (str): Key to be retrieved from the .env file. Defaults to 'MY_PASSWORD'.
9
+ start_path (str): Directory path to start the search from. If None, search starts from current directory.
10
+
11
+ Returns:
12
+ str: The value corresponding to the key if found, otherwise None.
13
+ """
14
+ # Set the default start_path to the current directory if it's None
15
+ directories = [start_path, os.getcwd(), get_home_folder(), simple_path_join(get_home_folder(),'.envy_all'),simple_path_join(get_home_folder(),'envy_all')]
16
+ if start_path in [None, os.getcwd()]:
17
+ directories = directories[1:]
18
+
19
+ # Try to find the file in the start_path
20
+ for k in range(0,len(directories)):
21
+ env_path = check_env_file(path=directories[k],file_name=file_name)
22
+ if not is_bool(env_path):
23
+ value = search_for_env_key(path=env_path,key=key)
24
+ if value != None:
25
+ return value
26
+
27
+ def search_for_env_key(key:str,path:str):
28
+ """
29
+ Search for a specific key in a .env file.
30
+
31
+ Args:
32
+ path (str): The path to the .env file.
33
+ key (str): The key to search for in the .env file.
34
+
35
+ Returns:
36
+ str: The value of the key if found, otherwise None.
37
+ """
38
+ with open(path, "r") as f:
39
+ for line in f:
40
+ eq_split = safe_split(line,['=',0])
41
+ # If the line contains the key, return the value after stripping extra characters
42
+ if line_contains(string=eq_split, compare=key):
43
+ return eatAll(line[len(eq_split):],[' ','','=']).strip()
44
+
45
+ def check_env_file(path:str,file_name:str=DEFAULT_FILE_NAME):
46
+ """
47
+ Check if the environment file exists in a specified path.
48
+
49
+ Args:
50
+ path (str): The path to check for the .env file.
51
+ file_name (str): The name of the .env file. Defaults to '.env'.
52
+
53
+ Returns:
54
+ str: The path of the .env file if it exists, otherwise False.
55
+ """
56
+ path = if_not_last_child_join(path=path, child=DEFAULT_FILE_NAME)
57
+ # Return the path if file exists, otherwise return False
58
+ if is_file(path):
59
+ return path
60
+ return False
61
+
62
+ def safe_env_load(path:str=None):
63
+ """
64
+ Safely load the .env file if it exists at a specified path.
65
+
66
+ Args:
67
+ path (str): The path to load the .env file from. If None, no operation is performed.
68
+
69
+ Returns:
70
+ bool: True if the .env file is successfully loaded, otherwise False.
71
+ """
72
+ if path == None:
73
+ return False
74
+ if is_file(path):
75
+ if str(safe_split(path,[get_slash(),-1]))[0] == '.':
76
+ load_dotenv(path)
77
+ return True
78
+ return False
79
+
80
+ def get_env_value(key:str=DEFAULT_KEY,path:str=None,file_name:str=DEFAULT_FILE_NAME):
81
+ """
82
+ Retrieves the value of the specified environment variable.
83
+
84
+ Args:
85
+ path (str): The path to the environment file. Defaults to None.
86
+ file_name (str): The name of the environment file. Defaults to '.env'.
87
+ key (str): The key to search for in the .env file. Defaults to 'MY_PASSWORD'.
88
+
89
+ Returns:
90
+ str: The value of the environment variable if found, otherwise None.
91
+ """
92
+ if safe_env_load(path):
93
+ return os.getenv(key)
94
+ return find_and_read_env_file(file_name=file_name, key=key, start_path=os.getcwd())
95
+ def split_eq(line):
96
+ """
97
+ Splits a string at the first equals sign '=' and cleans up the key and value.
98
+
99
+ Args:
100
+ line (str): The string to be split.
101
+
102
+ Returns:
103
+ list: A list containing the cleaned key and value. If '=' is not found, returns [line, None].
104
+ """
105
+ if '=' in line:
106
+ key_side = line.split('=')[0]
107
+ value_side = line[len(key_side+'='):]
108
+ return [eatOuter(key_side,[' ','','\t']),eatAll(value_side,[' ','','\t','\n'])]
109
+ return [line,None]
110
+ def dotenv_load(path:str=None):
111
+ """
112
+ Safely load the .env file if it exists at a specified path.
113
+
114
+ Args:
115
+ path (str): The path to load the .env file from. If None, no operation is performed.
116
+
117
+ Returns:
118
+ bool: True if the .env file is successfully loaded, otherwise False.
119
+ """
120
+ if path and os.path.isfile(path) and os.path.basename(path)[0] == '.':
121
+ load_dotenv(path)
122
+ return True
@@ -0,0 +1,3 @@
1
+ from .imports import *
2
+ from .file_utils import *
3
+ from .req import call_for_all_tabs,get_for_all_tabs
@@ -0,0 +1,8 @@
1
+ from .file_filters import *
2
+ from .file_utils import *
3
+ from .filter_params import *
4
+ from .map_utils import *
5
+ from .pdf_utils import *
6
+ from .file_reader import *
7
+ from .find_collect import *
8
+ from .type_checks import *
@@ -0,0 +1,104 @@
1
+ from .imports import *
2
+ from .filter_params import *
3
+ from .file_utils import *
4
+ ##from abstract_utilities import make_list,get_media_exts, is_media_type
5
+
6
+ def collect_filepaths(
7
+ directory: List[str],
8
+ cfg: ScanConfig=None,
9
+ allowed_exts: Optional[Set[str]] = False,
10
+ unallowed_exts: Optional[Set[str]] = False,
11
+ exclude_types: Optional[Set[str]] = False,
12
+ exclude_dirs: Optional[List[str]] = False,
13
+ exclude_patterns: Optional[List[str]] = False,
14
+ add=False,
15
+ allowed: Optional[Callable[[str], bool]] = None,
16
+ **kwargs
17
+ ) -> List[str]:
18
+ cfg = cfg or define_defaults(
19
+ allowed_exts=allowed_exts,
20
+ unallowed_exts=unallowed_exts,
21
+ exclude_types=exclude_types,
22
+ exclude_dirs=exclude_dirs,
23
+ exclude_patterns=exclude_patterns,
24
+ add = add
25
+ )
26
+ allowed = allowed or make_allowed_predicate(cfg)
27
+ directories = make_list(directory)
28
+ roots = [r for r in directories if r]
29
+
30
+ # your existing helpers (get_dirs, get_globs, etc.) stay the same
31
+ original_dirs = get_allowed_dirs(roots, allowed=allowed)
32
+ original_globs = get_globs(original_dirs)
33
+ files = get_allowed_files(original_globs, allowed=allowed)
34
+
35
+ for d in get_filtered_dirs(original_dirs, allowed=allowed):
36
+ files += get_filtered_files(d, allowed=allowed, files=files)
37
+
38
+ # de-dupe while preserving order
39
+ seen, out = set(), []
40
+ for f in files:
41
+ if f not in seen:
42
+ seen.add(f)
43
+ out.append(f)
44
+ return out
45
+
46
+
47
+ def _fast_walk(
48
+ root: Path,
49
+ exts: Iterable[str],
50
+ skip_dirs: Iterable[str] = (),
51
+ skip_patterns: Iterable[str] = (),
52
+ ) -> List[Path]:
53
+ exts = tuple(exts)
54
+ skip_dirs = set(sd.lower() for sd in skip_dirs or ())
55
+ skip_patterns = tuple(sp.lower() for sp in (skip_patterns or ()))
56
+
57
+ out = []
58
+ for p in root.rglob("*"):
59
+ # skip directories by name hit
60
+ if p.is_dir():
61
+ name = p.name.lower()
62
+ if name in skip_dirs:
63
+ # rglob doesn't let us prune mid-iteration cleanly; we just won't collect under it
64
+ continue
65
+ # nothing to collect for dirs
66
+ continue
67
+
68
+ # file filters
69
+ name = p.name.lower()
70
+ if any(fnmatch.fnmatch(name, pat) for pat in skip_patterns):
71
+ continue
72
+ if p.suffix.lower() in exts:
73
+ out.append(p)
74
+
75
+ # de-dup and normalize
76
+ return sorted({pp.resolve() for pp in out})
77
+
78
+
79
+ def enumerate_source_files(
80
+ src_root: Path,
81
+ cfg: Optional["ScanConfig"] = None,
82
+ *,
83
+ exts: Optional[Iterable[str]] = None,
84
+ fast_skip_dirs: Optional[Iterable[str]] = None,
85
+ fast_skip_patterns: Optional[Iterable[str]] = None,
86
+ ) -> List[Path]:
87
+ """
88
+ Unified enumerator:
89
+ - If `cfg` is provided: use collect_filepaths(...) with full rules.
90
+ - Else: fast walk using rglob over `exts` (defaults to EXTS) with optional light excludes.
91
+ """
92
+ src_root = Path(src_root)
93
+
94
+ if cfg is not None:
95
+ files = collect_filepaths([str(src_root)], cfg=cfg)
96
+ return sorted({Path(f).resolve() for f in files})
97
+
98
+ # Fast mode
99
+ return _fast_walk(
100
+ src_root,
101
+ exts or EXTS,
102
+ skip_dirs=fast_skip_dirs or (),
103
+ skip_patterns=fast_skip_patterns or (),
104
+ )
@@ -1,29 +1,15 @@
1
1
  # file_reader.py
2
- import os,tempfile,shutil,logging,ezodf,fnmatch
3
- from typing import Union
4
- import pandas as pd
5
- import geopandas as gpd
6
- from abstract_utilities import *
7
- from datetime import datetime
8
- from werkzeug.utils import secure_filename
9
- from werkzeug.datastructures import FileStorage
10
- from datetime import datetime
11
- from typing import Dict, Union, List
2
+ from .imports import *
3
+ # -------- Public API drop-ins that mirror your originals --------
4
+ from .filter_params import *
5
+ from .file_filters import *
6
+ from .file_utils import *
12
7
  from .pdf_utils import *
13
- import pdfplumber
14
- from pdf2image import convert_from_path # only used for OCR fallback
15
- import pytesseract
16
- from pathlib import Path
17
8
  # ---------------------------------------------------------------------------
18
9
  # NOTE: The following helper functions must be provided elsewhere:
19
10
  # - convert_date_string(s: str) -> datetime
20
11
  # - read_from_file(path: str) -> pd.DataFrame
21
12
  # ---------------------------------------------------------------------------
22
- DEFAULT_EXCLUDE_DIRS = {"node_modules", "__pycache__","backups","backup"}
23
- DEFAULT_EXCLUDE_FILE_PATTERNS = {"__init__*", "*.tmp", "*.log"}
24
- DEFAULT_EXCLUDE_TYPES = {"image","video","audio","presentation"}
25
- def get_ext(item):
26
- return item.split('.')[-1]
27
13
  def _should_skip_dir(dir_name: str, exclude_dirs: set[str]) -> bool:
28
14
  """
29
15
  Return True if dir_name match=self.exclude_types)es one of the excluded directory names exactly.
@@ -1,7 +1,7 @@
1
- from pathlib import Path
2
- from typing import *
3
- import fnmatch, os, glob
1
+
4
2
  from .filter_params import *
3
+ from .imports import *
4
+
5
5
  ##from abstract_utilities import make_list,get_media_exts, is_media_type
6
6
  def get_allowed_predicate(allowed=None):
7
7
  if allowed != False:
@@ -15,7 +15,8 @@ def get_allowed_predicate(allowed=None):
15
15
  return allowed
16
16
  def get_globs(items,recursive: bool = True,allowed=None):
17
17
  glob_paths = []
18
- for item in make_list(items):
18
+ items = [item for item in make_list(items) if item]
19
+ for item in items:
19
20
  pattern = os.path.join(item, "**/*") # include all files recursively\n
20
21
  nuItems = glob.glob(pattern, recursive=recursive)
21
22
  if allowed:
@@ -1,41 +1,4 @@
1
- from typing import *
2
- from ..type_utils import make_list,get_media_exts, is_media_type
3
- from ..string_clean import eatAll
4
- from dataclasses import dataclass, field
5
- @dataclass
6
- class ScanConfig:
7
- allowed_exts: Set[str]
8
- unallowed_exts: Set[str]
9
- exclude_types: Set[str]
10
- exclude_dirs: List[str] = field(default_factory=list)
11
- exclude_patterns: List[str] = field(default_factory=list)
12
- DEFAULT_ALLOWED_EXTS: Set[str] = {
13
- ".py", ".pyw", # python
14
- ".js", ".jsx", ".ts", ".tsx", ".mjs", # JS/TS
15
- ".html", ".htm", ".xml", # markup
16
- ".css", ".scss", ".sass", ".less", # styles
17
- ".json", ".yaml", ".yml", ".toml", ".ini", # configs
18
- ".cfg", ".md", ".markdown", ".rst", # docs
19
- ".sh", ".bash", ".env", # scripts/env
20
- ".txt" # plain text
21
- }
22
-
23
- DEFAULT_EXCLUDE_TYPES: Set[str] = {
24
- "image", "video", "audio", "presentation",
25
- "spreadsheet", "archive", "executable"
26
- }
27
-
28
- # never want these—even if they sneak into ALLOWED
29
- _unallowed = set(get_media_exts(DEFAULT_EXCLUDE_TYPES)) | {'.bak', '.shp', '.cpg', '.dbf', '.shx','.geojson',".pyc",'.shx','.geojson','.prj','.sbn','.sbx'}
30
- DEFAULT_UNALLOWED_EXTS = {e for e in _unallowed if e not in DEFAULT_ALLOWED_EXTS}
31
-
32
- DEFAULT_EXCLUDE_DIRS: Set[str] = {
33
- "node_modules", "__pycache__", "backups", "backup", "backs", "trash", "depriciated", "old", "__init__"
34
- }
35
-
36
- DEFAULT_EXCLUDE_PATTERNS: Set[str] = {
37
- "__init__*", "*.tmp", "*.log", "*.lock", "*.zip","*~"
38
- }
1
+ from .imports import *
39
2
  def get_default_modular(obj,default=None,add=False,typ=set):
40
3
  if obj in [False,True,None]:
41
4
  if obj in [True,None]: