abstract-utilities 0.2.2.495__py3-none-any.whl → 0.2.2.504__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- abstract_utilities/__init__.py +5 -9
- 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/env_utils/imports/imports.py +3 -2
- 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/imports/imports.py +3 -18
- abstract_utilities/file_utils/imports/module_imports.py +3 -6
- abstract_utilities/file_utils/src/type_checks.py +0 -1
- 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/imports/imports.py +1 -1
- abstract_utilities/import_utils/imports/module_imports.py +1 -1
- abstract_utilities/import_utils/src/__init__.py +1 -1
- abstract_utilities/import_utils/src/clean_imports.py +31 -5
- abstract_utilities/import_utils/src/dot_utils.py +9 -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/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 +56 -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 +2 -0
- abstract_utilities/path_utils/imports/imports.py +1 -0
- abstract_utilities/path_utils/imports/module_imports.py +6 -0
- abstract_utilities/path_utils/path_utils.py +715 -0
- abstract_utilities/path_utils.py +94 -2
- 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 +2 -4
- abstract_utilities/safe_utils/__init__.py +2 -0
- abstract_utilities/safe_utils/imports/__init__.py +3 -0
- abstract_utilities/safe_utils/imports/imports.py +1 -0
- abstract_utilities/safe_utils/imports/module_imports.py +2 -0
- abstract_utilities/safe_utils/safe_utils.py +130 -0
- abstract_utilities/ssh_utils/__init__.py +2 -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 +5 -0
- abstract_utilities/ssh_utils/imports/utils.py +189 -0
- abstract_utilities/ssh_utils/pexpect_utils.py +11 -18
- 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.495.dist-info → abstract_utilities-0.2.2.504.dist-info}/METADATA +1 -1
- abstract_utilities-0.2.2.504.dist-info/RECORD +229 -0
- abstract_utilities-0.2.2.495.dist-info/RECORD +0 -123
- {abstract_utilities-0.2.2.495.dist-info → abstract_utilities-0.2.2.504.dist-info}/WHEEL +0 -0
- {abstract_utilities-0.2.2.495.dist-info → abstract_utilities-0.2.2.504.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,715 @@
|
|
|
1
|
+
"""
|
|
2
|
+
path_utils.py
|
|
3
|
+
|
|
4
|
+
This module provides a collection of utility functions related to file and directory path management.
|
|
5
|
+
Its features include, but are not limited to:
|
|
6
|
+
|
|
7
|
+
Usage:
|
|
8
|
+
import abstract_utilities.path_utils as path_utils
|
|
9
|
+
|
|
10
|
+
- Determining appropriate file path separators based on the operating system.
|
|
11
|
+
- Joining paths using the correct file path separator.
|
|
12
|
+
- Fetching the current working directory and user's home folder.
|
|
13
|
+
- Checking if a path corresponds to a file or directory and if they exist.
|
|
14
|
+
- Fetching file sizes and determining the size of directories.
|
|
15
|
+
- Creating multiple nested directories if they do not exist.
|
|
16
|
+
- Retrieving creation time of files.
|
|
17
|
+
- Converting sizes to GB with options for truncation.
|
|
18
|
+
|
|
19
|
+
This module is part of the `abstract_utilities` package.
|
|
20
|
+
|
|
21
|
+
Author: putkoff
|
|
22
|
+
Date: 05/31/2023
|
|
23
|
+
Version: 0.1.2
|
|
24
|
+
"""
|
|
25
|
+
from .imports import *
|
|
26
|
+
|
|
27
|
+
def get_os_info():
|
|
28
|
+
"""
|
|
29
|
+
Get Operating System Information
|
|
30
|
+
|
|
31
|
+
This function retrieves information about the current operating system, including its name and bit size.
|
|
32
|
+
|
|
33
|
+
Returns:
|
|
34
|
+
- os_info (dict): A dictionary containing the operating system information.
|
|
35
|
+
Keys:
|
|
36
|
+
- "operating_system" (str): The name of the operating system (e.g., "Windows", "Linux", "Darwin").
|
|
37
|
+
- "bit_size" (str): The bit size of the operating system (e.g., "32bit", "64bit").
|
|
38
|
+
|
|
39
|
+
Example:
|
|
40
|
+
os_info = get_os_info()
|
|
41
|
+
print("Operating System:", os_info["operating_system"])
|
|
42
|
+
print("Bit Size:", os_info["bit_size"])
|
|
43
|
+
"""
|
|
44
|
+
os_name = platform.system()
|
|
45
|
+
bit_size = platform.architecture()[0]
|
|
46
|
+
return {"operating_system": os_name, "bit_size": bit_size}
|
|
47
|
+
def get_dirs(path):
|
|
48
|
+
"""
|
|
49
|
+
Get List of Immediate Subdirectories in a Path
|
|
50
|
+
|
|
51
|
+
This function uses the os.walk method to traverse through a directory tree and returns a list of immediate subdirectories
|
|
52
|
+
within the specified path.
|
|
53
|
+
|
|
54
|
+
Parameters:
|
|
55
|
+
- path (str): The path for which subdirectories need to be retrieved.
|
|
56
|
+
|
|
57
|
+
Returns:
|
|
58
|
+
- subdirectories (list): A list of immediate subdirectories within the specified path.
|
|
59
|
+
|
|
60
|
+
Example:
|
|
61
|
+
subdirs = get_dirs("/path/to/directory")
|
|
62
|
+
print("Immediate Subdirectories:", subdirs)
|
|
63
|
+
"""
|
|
64
|
+
from os import walk
|
|
65
|
+
for (dirpath, dirnames, filenames) in walk(path):
|
|
66
|
+
return dirnames
|
|
67
|
+
def sanitize_filename(name: str):
|
|
68
|
+
"""
|
|
69
|
+
Sanitize a filename by removing invalid characters.
|
|
70
|
+
|
|
71
|
+
Args:
|
|
72
|
+
name (str): Filename to sanitize.
|
|
73
|
+
|
|
74
|
+
Returns:
|
|
75
|
+
str: Sanitized filename.
|
|
76
|
+
"""
|
|
77
|
+
return re.sub(r'[\\/*?:"<>|]', "", name)
|
|
78
|
+
def get_directory(file_path: str) -> str:
|
|
79
|
+
"""
|
|
80
|
+
Extracts and returns the directory path from a given file path.
|
|
81
|
+
|
|
82
|
+
Args:
|
|
83
|
+
file_path (str): A string representing the file path.
|
|
84
|
+
|
|
85
|
+
Returns:
|
|
86
|
+
str: The directory path extracted from the file path.
|
|
87
|
+
"""
|
|
88
|
+
return file_path[:-len(get_base_name(file_path))]
|
|
89
|
+
|
|
90
|
+
def get_base_name(file_path: str) -> str:
|
|
91
|
+
"""
|
|
92
|
+
Extracts and returns the base name of a file from a given file path.
|
|
93
|
+
|
|
94
|
+
Args:
|
|
95
|
+
file_path (str): A string representing the file path.
|
|
96
|
+
|
|
97
|
+
Returns:
|
|
98
|
+
str: The base name of the file.
|
|
99
|
+
"""
|
|
100
|
+
return os.path.basename(file_path)
|
|
101
|
+
def split_text(string: str) -> tuple:
|
|
102
|
+
"""
|
|
103
|
+
Splits a string into its base name and extension and returns them as a tuple.
|
|
104
|
+
|
|
105
|
+
Args:
|
|
106
|
+
string (str): A string to be split, typically representing a file name.
|
|
107
|
+
|
|
108
|
+
Returns:
|
|
109
|
+
tuple: A tuple containing the base name and extension of the input string.
|
|
110
|
+
"""
|
|
111
|
+
return os.path.splitext(string)
|
|
112
|
+
def get_ext(file_path: str) -> str:
|
|
113
|
+
"""
|
|
114
|
+
Retrieves and returns the extension of a file from a given file path.
|
|
115
|
+
|
|
116
|
+
Args:
|
|
117
|
+
file_path (str): A string representing the file path.
|
|
118
|
+
|
|
119
|
+
Returns:
|
|
120
|
+
str: The extension of the file (including the dot).
|
|
121
|
+
"""
|
|
122
|
+
return split_text(get_base_name(file_path))[1]
|
|
123
|
+
def get_file_name(file_path: str) -> str:
|
|
124
|
+
"""
|
|
125
|
+
Retrieves and returns the base name of a file from a given file path.
|
|
126
|
+
|
|
127
|
+
Args:
|
|
128
|
+
file_path (str): A string representing the file path.
|
|
129
|
+
|
|
130
|
+
Returns:
|
|
131
|
+
str: The base name of the file (without extension).
|
|
132
|
+
"""
|
|
133
|
+
return split_text(get_base_name(file_path))[0]
|
|
134
|
+
def get_slash():
|
|
135
|
+
"""
|
|
136
|
+
Returns the appropriate file path separator depending on the current operating system.
|
|
137
|
+
"""
|
|
138
|
+
slash = '/' # Assume a Unix-like system by default
|
|
139
|
+
if slash not in get_current_path():
|
|
140
|
+
slash = '\\' # Use backslash for Windows systems
|
|
141
|
+
return slash
|
|
142
|
+
|
|
143
|
+
def simple_path_join(path_A:str, path_B:str):
|
|
144
|
+
"""
|
|
145
|
+
Join two paths using the appropriate file path separator.
|
|
146
|
+
|
|
147
|
+
Args:
|
|
148
|
+
path_A (str): The first path to join.
|
|
149
|
+
path_B (str): The second path to join.
|
|
150
|
+
|
|
151
|
+
Returns:
|
|
152
|
+
str: The joined path.
|
|
153
|
+
"""
|
|
154
|
+
return os.path.join(str(path_A), str(path_B))
|
|
155
|
+
|
|
156
|
+
def path_join(path_A, path_B=None):
|
|
157
|
+
"""
|
|
158
|
+
Joins two paths or a list of paths using the appropriate file path separator.
|
|
159
|
+
|
|
160
|
+
Args:
|
|
161
|
+
path_A (str or list): The first path or list of paths to join.
|
|
162
|
+
path_B (str, optional): The second path to join. Defaults to None.
|
|
163
|
+
|
|
164
|
+
Returns:
|
|
165
|
+
str: The joined path.
|
|
166
|
+
"""
|
|
167
|
+
if path_B is not None: # If path_B is provided, join path_A and path_B
|
|
168
|
+
return simple_path_join(path_A, path_B)
|
|
169
|
+
if isinstance(path_A, list): # If path_A is a list, join all paths in the list
|
|
170
|
+
path = path_A[0]
|
|
171
|
+
for k in range(1, len(path_A)):
|
|
172
|
+
path = simple_path_join(path, path_A[k])
|
|
173
|
+
return path
|
|
174
|
+
|
|
175
|
+
def if_not_last_child_join(path:str,child:str):
|
|
176
|
+
"""
|
|
177
|
+
Adds a child path to the given path if it's not already present at the end.
|
|
178
|
+
|
|
179
|
+
Args:
|
|
180
|
+
path (str): The parent path.
|
|
181
|
+
child (str): The child path to add.
|
|
182
|
+
|
|
183
|
+
Returns:
|
|
184
|
+
str: The updated path.
|
|
185
|
+
"""
|
|
186
|
+
if path.endswith(child):
|
|
187
|
+
return path
|
|
188
|
+
return simple_path_join(path, child)
|
|
189
|
+
|
|
190
|
+
def get_current_path():
|
|
191
|
+
"""
|
|
192
|
+
Returns the current working directory.
|
|
193
|
+
|
|
194
|
+
Returns:
|
|
195
|
+
str: The current working directory.
|
|
196
|
+
"""
|
|
197
|
+
return os.getcwd()
|
|
198
|
+
|
|
199
|
+
def get_home_folder():
|
|
200
|
+
"""
|
|
201
|
+
Returns the path to the home directory of the current user.
|
|
202
|
+
|
|
203
|
+
Returns:
|
|
204
|
+
str: The path to the home directory.
|
|
205
|
+
"""
|
|
206
|
+
return os.path.expanduser("~")
|
|
207
|
+
|
|
208
|
+
def is_file(path: str) -> bool:
|
|
209
|
+
"""Checks if the provided path is a file.
|
|
210
|
+
|
|
211
|
+
Args:
|
|
212
|
+
path (str): The path to check.
|
|
213
|
+
|
|
214
|
+
Returns:
|
|
215
|
+
bool: True if the path is a file, False otherwise.
|
|
216
|
+
"""
|
|
217
|
+
return os.path.isfile(path)
|
|
218
|
+
|
|
219
|
+
def update_global_variable(name: str, value) -> None:
|
|
220
|
+
"""Updates the global variable with the provided name and value.
|
|
221
|
+
|
|
222
|
+
Args:
|
|
223
|
+
name (str): The name of the global variable.
|
|
224
|
+
value: The value to assign to the global variable.
|
|
225
|
+
|
|
226
|
+
Returns:
|
|
227
|
+
None
|
|
228
|
+
"""
|
|
229
|
+
globals()[name] = value
|
|
230
|
+
|
|
231
|
+
def list_directory_contents(path: str) -> list:
|
|
232
|
+
"""Returns a list of directory contents or a list with a single file, if the path is a file.
|
|
233
|
+
|
|
234
|
+
Args:
|
|
235
|
+
path (str): The path of the directory or file.
|
|
236
|
+
|
|
237
|
+
Returns:
|
|
238
|
+
list: A list of directory contents or a list with a single file path.
|
|
239
|
+
"""
|
|
240
|
+
if is_file(path):
|
|
241
|
+
return [path]
|
|
242
|
+
elif is_valid_path(path):
|
|
243
|
+
return os.listdir(path)
|
|
244
|
+
return [path]
|
|
245
|
+
|
|
246
|
+
def trunc(a: float, x: int) -> float:
|
|
247
|
+
"""
|
|
248
|
+
Truncates a float number to a specific number of decimal places.
|
|
249
|
+
|
|
250
|
+
Args:
|
|
251
|
+
a (float): The number to truncate.
|
|
252
|
+
x (int): The number of decimal places to retain.
|
|
253
|
+
|
|
254
|
+
Returns:
|
|
255
|
+
float: The truncated float number.
|
|
256
|
+
"""
|
|
257
|
+
temp = str(a)
|
|
258
|
+
for i in range(len(temp)):
|
|
259
|
+
if temp[i] == '.':
|
|
260
|
+
try:
|
|
261
|
+
return float(temp[:i+x+1])
|
|
262
|
+
except:
|
|
263
|
+
return float(temp)
|
|
264
|
+
return float(temp)
|
|
265
|
+
|
|
266
|
+
def mkGb(k) -> float:
|
|
267
|
+
"""
|
|
268
|
+
Converts a value to Gigabytes (GB).
|
|
269
|
+
|
|
270
|
+
Args:
|
|
271
|
+
k (float): The value to convert to GB.
|
|
272
|
+
|
|
273
|
+
Returns:
|
|
274
|
+
float: The value converted to GB.
|
|
275
|
+
"""
|
|
276
|
+
return float(float(k)*(10**9))
|
|
277
|
+
|
|
278
|
+
def mkGbTrunk(k) -> float:
|
|
279
|
+
"""
|
|
280
|
+
Converts a value to Gigabytes (GB) and truncates the result to five decimal places.
|
|
281
|
+
|
|
282
|
+
Args:
|
|
283
|
+
k (float): The value to convert to GB.
|
|
284
|
+
|
|
285
|
+
Returns:
|
|
286
|
+
float: The value converted to GB and truncated to five decimal places.
|
|
287
|
+
"""
|
|
288
|
+
return trunc(mkGb(k), 5)
|
|
289
|
+
|
|
290
|
+
def mkGbTrunFroPathTot(k) -> float:
|
|
291
|
+
"""
|
|
292
|
+
Fetches the file size from a path, converts it to Gigabytes (GB) and truncates the result to five decimal places.
|
|
293
|
+
|
|
294
|
+
Args:
|
|
295
|
+
k (str): The file path.
|
|
296
|
+
|
|
297
|
+
Returns:
|
|
298
|
+
float: The file size converted to GB and truncated to five decimal places.
|
|
299
|
+
"""
|
|
300
|
+
return trunc(mkGb(s.path.getsize(k)), 5)
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
def get_abs_name_of_this():
|
|
304
|
+
"""
|
|
305
|
+
Returns the absolute name of the current module.
|
|
306
|
+
|
|
307
|
+
Returns:
|
|
308
|
+
Path: The absolute name of the current module.
|
|
309
|
+
"""
|
|
310
|
+
return os.path.abspath(__name__)
|
|
311
|
+
|
|
312
|
+
def createFolds(ls: list) -> None:
|
|
313
|
+
"""
|
|
314
|
+
Creates multiple directories.
|
|
315
|
+
|
|
316
|
+
Args:
|
|
317
|
+
ls (list): The list of directory paths to create.
|
|
318
|
+
"""
|
|
319
|
+
for k in range(len(ls)):
|
|
320
|
+
mkdirs(ls[k])
|
|
321
|
+
def makeAllDirs(path: str) -> str:
|
|
322
|
+
def make_list(obj):
|
|
323
|
+
if not isinstance(obj,list):
|
|
324
|
+
obj = [obj]
|
|
325
|
+
return obj
|
|
326
|
+
|
|
327
|
+
slash = get_slash()
|
|
328
|
+
path_parts = path.split(slash)
|
|
329
|
+
path=''
|
|
330
|
+
if path_parts:
|
|
331
|
+
last_part = path_parts[-1]
|
|
332
|
+
for i,part in enumerate(path_parts):
|
|
333
|
+
if part == '':
|
|
334
|
+
part = slash
|
|
335
|
+
path = os.path.join(path,part)
|
|
336
|
+
if not os.path.exists(path):
|
|
337
|
+
if '.' in part and abs(len(path_parts)-1) == i:
|
|
338
|
+
return path
|
|
339
|
+
os.makedirs(path)
|
|
340
|
+
else:
|
|
341
|
+
if os.path.isfile(path):
|
|
342
|
+
return path
|
|
343
|
+
return path
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
def mkdirs(path: str) -> str:
|
|
347
|
+
"""
|
|
348
|
+
Creates a directory and any necessary intermediate directories.
|
|
349
|
+
|
|
350
|
+
Args:
|
|
351
|
+
path (str): The directory path to create.
|
|
352
|
+
|
|
353
|
+
Returns:
|
|
354
|
+
str: The created directory path.
|
|
355
|
+
"""
|
|
356
|
+
os.makedirs(path, exist_ok=True)
|
|
357
|
+
return path
|
|
358
|
+
def make_dirs(*paths):
|
|
359
|
+
path = path_join(*paths)
|
|
360
|
+
if not os.path.isfile(path):
|
|
361
|
+
mkdirs(path)
|
|
362
|
+
return path
|
|
363
|
+
def file_exists(file_path: str) -> bool:
|
|
364
|
+
"""
|
|
365
|
+
Checks if a file exists at the specified path.
|
|
366
|
+
|
|
367
|
+
Args:
|
|
368
|
+
file_path (str): The path to the file.
|
|
369
|
+
|
|
370
|
+
Returns:
|
|
371
|
+
bool: True if the file exists, False otherwise.
|
|
372
|
+
"""
|
|
373
|
+
return os.path.exists(file_path)
|
|
374
|
+
|
|
375
|
+
def dir_exists(path: str) -> bool:
|
|
376
|
+
"""
|
|
377
|
+
Checks if a directory exists at the specified path.
|
|
378
|
+
|
|
379
|
+
Args:
|
|
380
|
+
path (str): The path to the directory.
|
|
381
|
+
|
|
382
|
+
Returns:
|
|
383
|
+
bool: True if the directory exists, False otherwise.
|
|
384
|
+
"""
|
|
385
|
+
return os.path.isdir(path)
|
|
386
|
+
def file_size(path:str):
|
|
387
|
+
if is_file(path):
|
|
388
|
+
return os.path.getsize(path)
|
|
389
|
+
return 0
|
|
390
|
+
def get_file_create_time(path):
|
|
391
|
+
return os.path.getctime(path)
|
|
392
|
+
def get_size(path: str) -> int:
|
|
393
|
+
"""
|
|
394
|
+
Calculates the size of a file or a directory.
|
|
395
|
+
|
|
396
|
+
Args:
|
|
397
|
+
path (str): The path of the file or directory.
|
|
398
|
+
|
|
399
|
+
Returns:
|
|
400
|
+
int: The size of the file or directory in bytes.
|
|
401
|
+
"""
|
|
402
|
+
total_size = file_size(path)
|
|
403
|
+
if dir_exists(path):
|
|
404
|
+
total_size = 0
|
|
405
|
+
for dirpath, dirnames, filenames in os.walk(path):
|
|
406
|
+
for file in filenames:
|
|
407
|
+
total_size += file_size(simple_path_join(dirpath, file))
|
|
408
|
+
return total_size
|
|
409
|
+
|
|
410
|
+
def get_total_size(folder_path: str) -> int:
|
|
411
|
+
"""
|
|
412
|
+
Calculates the total size of a directory and its subdirectories.
|
|
413
|
+
|
|
414
|
+
Args:
|
|
415
|
+
folder_path (str): The path of the directory.
|
|
416
|
+
|
|
417
|
+
Returns:
|
|
418
|
+
int: The total size of the directory and its subdirectories in bytes.
|
|
419
|
+
"""
|
|
420
|
+
total_size = 0
|
|
421
|
+
if os.path.exists(folder_path) and os.path.isdir(folder_path):
|
|
422
|
+
for item in os.listdir(folder_path):
|
|
423
|
+
item_path = os.path.join(folder_path, item)
|
|
424
|
+
total_size += get_size(item_path)
|
|
425
|
+
return total_size
|
|
426
|
+
|
|
427
|
+
def get_files(directory):
|
|
428
|
+
file_list = []
|
|
429
|
+
for root,dirs,files in os.walk(directory):
|
|
430
|
+
for file in files:
|
|
431
|
+
file_list.append(os.path.join(root,file))
|
|
432
|
+
return file_list
|
|
433
|
+
|
|
434
|
+
def get_folders(directory):
|
|
435
|
+
directory_list = []
|
|
436
|
+
for root,dirs,files in os.walk(directory):
|
|
437
|
+
for folder in dirs:
|
|
438
|
+
directory_list.append(os.path.join(root,folder))
|
|
439
|
+
return directory_list
|
|
440
|
+
|
|
441
|
+
def break_down_find_existing(path):
|
|
442
|
+
slash = get_slash()
|
|
443
|
+
test_path=''
|
|
444
|
+
found_path=None
|
|
445
|
+
for part in path.split(slash):
|
|
446
|
+
test_path=os.path.join(test_path,part)
|
|
447
|
+
if not os.path.exists(test_path):
|
|
448
|
+
return found_path
|
|
449
|
+
found_path = test_path
|
|
450
|
+
return found_path
|
|
451
|
+
|
|
452
|
+
def get_directory_items(directory):
|
|
453
|
+
if not os.path.isdir(directory):
|
|
454
|
+
return []
|
|
455
|
+
return os.listdir(directory)
|
|
456
|
+
|
|
457
|
+
def get_directory_files(directory):
|
|
458
|
+
if not os.path.isdir(directory):
|
|
459
|
+
return []
|
|
460
|
+
return get_files(directory)
|
|
461
|
+
|
|
462
|
+
def get_all_item_paths(directory):
|
|
463
|
+
directory_items = get_directory_items(directory)
|
|
464
|
+
item_paths = [os.path.join(directory,item) for item in directory_items if item]
|
|
465
|
+
return item_paths
|
|
466
|
+
|
|
467
|
+
def get_all_file_paths(directory):
|
|
468
|
+
item_paths = get_directory_files(directory)
|
|
469
|
+
return item_paths
|
|
470
|
+
|
|
471
|
+
def get_directory(directory):
|
|
472
|
+
if not os.path.isdir(directory):
|
|
473
|
+
os.makedirs(directory,exist_ok=True)
|
|
474
|
+
return directory
|
|
475
|
+
|
|
476
|
+
def create_directory(directory,path):
|
|
477
|
+
directory = os.path.join(directory,path)
|
|
478
|
+
return get_directory(directory)
|
|
479
|
+
|
|
480
|
+
|
|
481
|
+
def initialize_file(directory,basename):
|
|
482
|
+
directory = get_directory(directory)
|
|
483
|
+
file_path = create_directory(directory,basename)
|
|
484
|
+
return get_file_path(file_path)
|
|
485
|
+
|
|
486
|
+
def join_path(directory,basename):
|
|
487
|
+
file_path = os.path.join(directory,basename)
|
|
488
|
+
return file_path
|
|
489
|
+
|
|
490
|
+
def is_last_itter(i,*itters):
|
|
491
|
+
itter_len = len(itters)
|
|
492
|
+
if i+1 == itter_len:
|
|
493
|
+
return True
|
|
494
|
+
return False
|
|
495
|
+
|
|
496
|
+
def path_join(*paths, isfile=False):
|
|
497
|
+
final_path = os.path.join(*paths)
|
|
498
|
+
paths_len = len(paths)
|
|
499
|
+
for i, path in enumerate(paths):
|
|
500
|
+
if i == 0:
|
|
501
|
+
final_path = path # Note: Fixed bug; original code had `final_path = paths`
|
|
502
|
+
else:
|
|
503
|
+
final_path = os.path.join(final_path, path)
|
|
504
|
+
if isfile and is_last_itter(i, paths_len): # Note: `is_last_itter` is undefined; assuming it checks if last iteration
|
|
505
|
+
break
|
|
506
|
+
os.makedirs(final_path, exist_ok=True)
|
|
507
|
+
return final_path
|
|
508
|
+
|
|
509
|
+
def is_file(*paths):
|
|
510
|
+
item_path = os.path.join(*paths)
|
|
511
|
+
return os.path.isfile(item_path)
|
|
512
|
+
|
|
513
|
+
def is_dir(*paths):
|
|
514
|
+
item_path = os.path.join(*paths)
|
|
515
|
+
return os.path.isdir(item_path)
|
|
516
|
+
|
|
517
|
+
def is_path(*paths):
|
|
518
|
+
item_path = os.path.join(*paths)
|
|
519
|
+
return item_path if os.path.exists(item_path) else None
|
|
520
|
+
|
|
521
|
+
def get_all_directories(directory):
|
|
522
|
+
dir_list = os.listdir(directory)
|
|
523
|
+
directory_list = [item for item in dir_list if is_dir(directory,item)]
|
|
524
|
+
return directory_list
|
|
525
|
+
|
|
526
|
+
def get_all_files(directory=None):
|
|
527
|
+
directory = directory or os.getcwd()
|
|
528
|
+
dir_list = os.listdir(directory)
|
|
529
|
+
file_list = [item for item in dir_list if is_file(directory,item)]
|
|
530
|
+
return file_list
|
|
531
|
+
|
|
532
|
+
def get_all_items(directory):
|
|
533
|
+
dir_list = os.listdir(directory)
|
|
534
|
+
file_list = [item for item in dir_list if is_path(directory,item)]
|
|
535
|
+
return file_list
|
|
536
|
+
|
|
537
|
+
|
|
538
|
+
def get_dirlist(directory):
|
|
539
|
+
path = get_directory(directory)
|
|
540
|
+
if not path:
|
|
541
|
+
return path
|
|
542
|
+
dir_list=[]
|
|
543
|
+
if is_dir(path):
|
|
544
|
+
dir_list = os.listdir(path)
|
|
545
|
+
elif is_file(path):
|
|
546
|
+
dir_list = [os.path.basename(path)]
|
|
547
|
+
return dir_list
|
|
548
|
+
|
|
549
|
+
|
|
550
|
+
def is_directory_in_paths(path,directory):
|
|
551
|
+
return directory in path
|
|
552
|
+
|
|
553
|
+
def remove_directory(directory,paths=None):
|
|
554
|
+
paths = make_list_it(paths)
|
|
555
|
+
shutil.rmtree(audio_dir)
|
|
556
|
+
for path in paths:
|
|
557
|
+
remove_path(path=path)
|
|
558
|
+
def remove_path(path=None):
|
|
559
|
+
if path and os.path.exists(path):
|
|
560
|
+
if os.path.isdir(path):
|
|
561
|
+
remove_directory(path)
|
|
562
|
+
else:
|
|
563
|
+
os.remove(path)
|
|
564
|
+
def get_safe_dirname(path=None):
|
|
565
|
+
if path:
|
|
566
|
+
path_str = str(path)
|
|
567
|
+
return os.path.dirname(path_str)
|
|
568
|
+
def get_safe_basename(path=None):
|
|
569
|
+
if path:
|
|
570
|
+
path_str = str(path)
|
|
571
|
+
return os.path.basename(path_str)
|
|
572
|
+
def get_safe_splitext(path=None,basename=None):
|
|
573
|
+
basename = basename or get_safe_basename(path=path)
|
|
574
|
+
if basename:
|
|
575
|
+
basename_str = str(basename)
|
|
576
|
+
filename,ext = os.path.splitext(basename_str)
|
|
577
|
+
return filename,ext
|
|
578
|
+
def get_safe_filename(path=None,basename=None):
|
|
579
|
+
filename,_ = get_safe_splitext(path=path,basename=basename)
|
|
580
|
+
return filename
|
|
581
|
+
def get_safe_ext(path=None,basename=None):
|
|
582
|
+
_,ext = get_safe_splitext(path=path,basename=basename)
|
|
583
|
+
return ext
|
|
584
|
+
def raw_create_dirs(*paths):
|
|
585
|
+
"""Recursively create all directories along the given path."""
|
|
586
|
+
full_path = os.path.abspath(safe_join(*paths))
|
|
587
|
+
sub_parts = [p for p in full_path.split(os.sep) if p]
|
|
588
|
+
|
|
589
|
+
current_path = "/" if full_path.startswith(os.sep) else ""
|
|
590
|
+
for part in sub_parts:
|
|
591
|
+
current_path = safe_join(current_path, part)
|
|
592
|
+
os.makedirs(current_path, exist_ok=True)
|
|
593
|
+
return full_path
|
|
594
|
+
|
|
595
|
+
|
|
596
|
+
def create_dirs(directory, child=None):
|
|
597
|
+
"""Create directory and optional child path safely."""
|
|
598
|
+
full_path = os.path.abspath(safe_join(directory, child))
|
|
599
|
+
if not os.path.exists(full_path):
|
|
600
|
+
raw_create_dirs(full_path)
|
|
601
|
+
return full_path
|
|
602
|
+
|
|
603
|
+
|
|
604
|
+
def get_base_dir(directory=None):
|
|
605
|
+
"""Return given directory or _BASE_DIR fallback."""
|
|
606
|
+
return directory or _BASE_DIR
|
|
607
|
+
|
|
608
|
+
|
|
609
|
+
def create_base_path(directory=None, child=None):
|
|
610
|
+
"""Join base dir with child."""
|
|
611
|
+
directory = get_base_dir(directory)
|
|
612
|
+
return safe_join(directory, child)
|
|
613
|
+
|
|
614
|
+
|
|
615
|
+
def create_base_dir(directory=None, child=None):
|
|
616
|
+
"""Ensure existence of base directory path."""
|
|
617
|
+
full_path = create_base_path(directory, child)
|
|
618
|
+
if not os.path.exists(full_path):
|
|
619
|
+
raw_create_dirs(full_path)
|
|
620
|
+
return full_path
|
|
621
|
+
|
|
622
|
+
|
|
623
|
+
|
|
624
|
+
# --- Individual path checks --------------------------------------------------
|
|
625
|
+
def is_remote_file(path: str,*args, **kwargs) -> bool:
|
|
626
|
+
"""True if remote path is a file."""
|
|
627
|
+
return _remote_test(path, "-f", **kwargs)
|
|
628
|
+
|
|
629
|
+
|
|
630
|
+
def is_remote_dir(path: str,*args, **kwargs) -> bool:
|
|
631
|
+
"""True if remote path is a directory."""
|
|
632
|
+
return _remote_test(path, "-d", **kwargs)
|
|
633
|
+
|
|
634
|
+
|
|
635
|
+
def is_local_file(path: str) -> bool:
|
|
636
|
+
"""True if local path is a file."""
|
|
637
|
+
return os.path.isfile(path)
|
|
638
|
+
|
|
639
|
+
|
|
640
|
+
def is_local_dir(path: str) -> bool:
|
|
641
|
+
"""True if local path is a directory."""
|
|
642
|
+
return os.path.isdir(path)
|
|
643
|
+
|
|
644
|
+
|
|
645
|
+
# --- Unified interface -------------------------------------------------------
|
|
646
|
+
|
|
647
|
+
def is_file(path: str,*args,**kwargs) -> bool:
|
|
648
|
+
"""Determine if path is a file (works local or remote)."""
|
|
649
|
+
if get_user_pass_host_key(**kwargs):
|
|
650
|
+
return is_remote_file(path, **kwargs)
|
|
651
|
+
return is_local_file(path)
|
|
652
|
+
|
|
653
|
+
|
|
654
|
+
def is_dir(path: str, *args,**kwargs) -> bool:
|
|
655
|
+
"""Determine if path is a directory (works local or remote)."""
|
|
656
|
+
if get_user_pass_host_key(**kwargs):
|
|
657
|
+
return is_remote_dir(path, **kwargs)
|
|
658
|
+
return is_local_dir(path)
|
|
659
|
+
|
|
660
|
+
def is_exists(path: str, *args,**kwargs) -> bool:
|
|
661
|
+
if is_file(path,**kwargs):
|
|
662
|
+
return True
|
|
663
|
+
if is_dir(path,**kwargs):
|
|
664
|
+
return True
|
|
665
|
+
return False
|
|
666
|
+
# --- Optional: keep your original all-in-one wrapper ------------------------
|
|
667
|
+
def check_path_type(
|
|
668
|
+
path: str,
|
|
669
|
+
*args,
|
|
670
|
+
**kwargs
|
|
671
|
+
) -> str:
|
|
672
|
+
"""
|
|
673
|
+
Return 'file', 'directory', 'missing', or 'unknown'.
|
|
674
|
+
Uses isolated is_file/is_dir functions.
|
|
675
|
+
"""
|
|
676
|
+
if get_user_pass_host_key(**kwargs):
|
|
677
|
+
if is_remote_file(path,**kwargs):
|
|
678
|
+
return "file"
|
|
679
|
+
elif is_remote_dir(path,**kwargs):
|
|
680
|
+
return "directory"
|
|
681
|
+
else:
|
|
682
|
+
return "missing"
|
|
683
|
+
else:
|
|
684
|
+
if os.path.isfile(path):
|
|
685
|
+
return "file"
|
|
686
|
+
elif os.path.isdir(path):
|
|
687
|
+
return "directory"
|
|
688
|
+
elif not os.path.exists(path):
|
|
689
|
+
return "missing"
|
|
690
|
+
return "unknown"
|
|
691
|
+
|
|
692
|
+
def get_file_parts(path):
|
|
693
|
+
|
|
694
|
+
basename = get_safe_basename(path)
|
|
695
|
+
filename, ext = get_safe_splitext(basename=basename)
|
|
696
|
+
|
|
697
|
+
dirname = get_safe_dirname(path)
|
|
698
|
+
dirbase = get_safe_basename(dirname)
|
|
699
|
+
|
|
700
|
+
parent_dirname = get_safe_dirname(dirname)
|
|
701
|
+
parent_dirbase = get_safe_basename(parent_dirname)
|
|
702
|
+
|
|
703
|
+
super_dirname = get_safe_dirname(parent_dirname)
|
|
704
|
+
super_dirbase = get_safe_basename(super_dirname)
|
|
705
|
+
|
|
706
|
+
return {"dirname": dirname,
|
|
707
|
+
"basename": basename,
|
|
708
|
+
"filename": filename,
|
|
709
|
+
"ext": ext,
|
|
710
|
+
"dirbase":dirbase,
|
|
711
|
+
"parent_dirname":parent_dirname,
|
|
712
|
+
"parent_dirbase":parent_dirbase,
|
|
713
|
+
"super_dirname":super_dirname,
|
|
714
|
+
"super_dirbase":super_dirbase
|
|
715
|
+
}
|