osbot-utils 2.69.0__py3-none-any.whl → 2.70.0__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.
- osbot_utils/helpers/safe_str/Safe_Str__File__Name.py +3 -1
- osbot_utils/type_safe/Type_Safe__Primitive.py +6 -0
- osbot_utils/utils/Files.py +17 -4
- osbot_utils/utils/Zip.py +7 -3
- osbot_utils/version +1 -1
- {osbot_utils-2.69.0.dist-info → osbot_utils-2.70.0.dist-info}/METADATA +2 -2
- {osbot_utils-2.69.0.dist-info → osbot_utils-2.70.0.dist-info}/RECORD +9 -9
- {osbot_utils-2.69.0.dist-info → osbot_utils-2.70.0.dist-info}/LICENSE +0 -0
- {osbot_utils-2.69.0.dist-info → osbot_utils-2.70.0.dist-info}/WHEEL +0 -0
@@ -1,8 +1,10 @@
|
|
1
1
|
import re
|
2
2
|
from osbot_utils.helpers.safe_str.Safe_Str import Safe_Str
|
3
3
|
|
4
|
+
TYPE_SAFE_STR__FILE__NAME__REGEX = re.compile(r'[^a-zA-Z0-9_\-. ]')
|
5
|
+
|
4
6
|
class Safe_Str__File__Name(Safe_Str):
|
5
|
-
regex =
|
7
|
+
regex = TYPE_SAFE_STR__FILE__NAME__REGEX
|
6
8
|
allow_empty = False
|
7
9
|
trim_whitespace = True
|
8
10
|
allow_all_replacement_char = False
|
@@ -29,6 +29,12 @@ class Type_Safe__Primitive:
|
|
29
29
|
return super().__eq__(other)
|
30
30
|
return False # Different types → not equal
|
31
31
|
|
32
|
+
def __enter__(self): # support context manager
|
33
|
+
return self
|
34
|
+
|
35
|
+
def __exit__(self, exc_type, exc_val, exc_tb):
|
36
|
+
return
|
37
|
+
|
32
38
|
def __ne__(self, other):
|
33
39
|
return not self.__eq__(other)
|
34
40
|
|
osbot_utils/utils/Files.py
CHANGED
@@ -1,5 +1,8 @@
|
|
1
1
|
import os
|
2
|
-
from typing import Union
|
2
|
+
from typing import Union, List
|
3
|
+
|
4
|
+
from osbot_utils.helpers.safe_str.Safe_Str__File__Path import Safe_Str__File__Path
|
5
|
+
|
3
6
|
|
4
7
|
class Files:
|
5
8
|
@staticmethod
|
@@ -72,16 +75,25 @@ class Files:
|
|
72
75
|
return glob.glob(path_pattern, recursive=recursive)
|
73
76
|
|
74
77
|
@staticmethod
|
75
|
-
def files(path, pattern= '*', only_files=True):
|
78
|
+
def files(path, pattern= '*', only_files=True, include_path=True) -> List[Safe_Str__File__Path]:
|
79
|
+
|
76
80
|
from pathlib import Path
|
77
81
|
|
78
|
-
result
|
82
|
+
result = []
|
79
83
|
for file in Path(path).rglob(pattern):
|
80
84
|
if only_files and is_not_file(file):
|
81
85
|
continue
|
82
|
-
|
86
|
+
file_path = file.as_posix()
|
87
|
+
if include_path is False:
|
88
|
+
file_path = str(file.relative_to(path))
|
89
|
+
result.append(Safe_Str__File__Path(file_path))
|
90
|
+
|
83
91
|
return sorted(result)
|
84
92
|
|
93
|
+
@staticmethod
|
94
|
+
def files__virtual_paths(path, pattern='*', only_files=True):
|
95
|
+
return Files.files(path, pattern=pattern, only_files=only_files, include_path=False)
|
96
|
+
|
85
97
|
@staticmethod
|
86
98
|
def files_names(files : list, check_if_exists=True):
|
87
99
|
result = []
|
@@ -628,6 +640,7 @@ filter_parent_folder = Files.filter_parent_folder
|
|
628
640
|
files_find = Files.find
|
629
641
|
files_recursive = Files.files_recursive
|
630
642
|
files_list = Files.files
|
643
|
+
files_list__virtual_paths = Files.files__virtual_paths
|
631
644
|
files_names = Files.files_names
|
632
645
|
|
633
646
|
find_files = Files.files
|
osbot_utils/utils/Zip.py
CHANGED
@@ -79,6 +79,8 @@ def zip_bytes__add_files(zip_bytes, files_to_add):
|
|
79
79
|
file_contents = file_contents.encode('utf-8')
|
80
80
|
elif not isinstance(file_contents, bytes):
|
81
81
|
continue
|
82
|
+
if file_path.startswith(('/', '\\')): # Strip leading slash or backslash to make the path relative
|
83
|
+
file_path = file_path.lstrip('/\\')
|
82
84
|
zf.writestr(file_path, file_contents)
|
83
85
|
|
84
86
|
return zip_buffer.getvalue()
|
@@ -135,7 +137,7 @@ def zip_bytes__replace_files(zip_bytes, files_to_replace):
|
|
135
137
|
def zip_bytes__unzip(zip_bytes, target_folder=None):
|
136
138
|
target_folder = target_folder or temp_folder() # Use the provided target folder or create a temporary one
|
137
139
|
zip_buffer = io.BytesIO(zip_bytes) # Create a BytesIO buffer from the zip bytes
|
138
|
-
with zipfile.ZipFile(zip_buffer, 'r') as zf:
|
140
|
+
with zipfile.ZipFile(zip_buffer, 'r') as zf: # Open the zip file from the buffer
|
139
141
|
zf.extractall(target_folder) # Extract all files to the target folder
|
140
142
|
return target_folder # Return the path of the target folder
|
141
143
|
|
@@ -200,16 +202,18 @@ def zip_folder_to_file (root_dir, target_file):
|
|
200
202
|
zip_file = zip_folder(root_dir)
|
201
203
|
return file_move(zip_file, target_file)
|
202
204
|
|
203
|
-
def zip_folder_to_bytes(root_dir): # todo add unit test
|
205
|
+
def zip_folder_to_bytes(root_dir, files_to_ignore:list=None): # todo add unit test
|
204
206
|
zip_buffer = io.BytesIO() # Create a BytesIO buffer to hold the zipped file
|
205
207
|
with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED) as zf: # Create a ZipFile object with the buffer as the target
|
206
208
|
for foldername, subfolders, filenames in os.walk(root_dir): # Walk the root_dir and add all files and folders to the zip file
|
207
209
|
for filename in filenames:
|
210
|
+
if files_to_ignore and filename in files_to_ignore:
|
211
|
+
continue
|
208
212
|
absolute_path = os.path.join(foldername, filename) # Create the complete filepath
|
209
213
|
arcname = os.path.relpath(absolute_path, root_dir) # Define the arcname, which is the name inside the zip file
|
210
214
|
zf.write(absolute_path, arcname) # Add the file to the zip file
|
211
215
|
zip_buffer.seek(0) # Reset buffer position
|
212
|
-
return zip_buffer
|
216
|
+
return zip_buffer.getvalue()
|
213
217
|
|
214
218
|
def zip_files(base_folder, file_pattern="*.*", target_file=None):
|
215
219
|
base_folder = abspath(base_folder)
|
osbot_utils/version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
v2.
|
1
|
+
v2.70.0
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.3
|
2
2
|
Name: osbot_utils
|
3
|
-
Version: 2.
|
3
|
+
Version: 2.70.0
|
4
4
|
Summary: OWASP Security Bot - Utils
|
5
5
|
License: MIT
|
6
6
|
Author: Dinis Cruz
|
@@ -23,7 +23,7 @@ Description-Content-Type: text/markdown
|
|
23
23
|
|
24
24
|
Powerful Python util methods and classes that simplify common apis and tasks.
|
25
25
|
|
26
|
-

|
27
27
|
[](https://codecov.io/gh/owasp-sbot/OSBot-Utils)
|
28
28
|
|
29
29
|
|
@@ -262,7 +262,7 @@ osbot_utils/helpers/safe_int/Safe_UInt__Percentage.py,sha256=Ck-jiu6NK57Y3ruAjIJ
|
|
262
262
|
osbot_utils/helpers/safe_int/Safe_UInt__Port.py,sha256=uISrh8VKXiEQULQ1POU9YK8Di6z_vr0HWjCTpjA0YaY,482
|
263
263
|
osbot_utils/helpers/safe_int/__init__.py,sha256=kMU2WMsdQmayBEZugxnJV_wRW3O90bc118sx6iIm_mQ,310
|
264
264
|
osbot_utils/helpers/safe_str/Safe_Str.py,sha256=qR2RDh5cehP_wDDeV5TQahNBwmfcizPFyStALBKD5dw,3509
|
265
|
-
osbot_utils/helpers/safe_str/Safe_Str__File__Name.py,sha256=
|
265
|
+
osbot_utils/helpers/safe_str/Safe_Str__File__Name.py,sha256=9Evl4P45GCyyR2ywze29GzmqWhyTCcgI-o2CItMsOHo,358
|
266
266
|
osbot_utils/helpers/safe_str/Safe_Str__File__Path.py,sha256=K0yBcvH_Ncqiw7tMqjGqaNyWQh1Zs9qxZ-TR8nEIAow,550
|
267
267
|
osbot_utils/helpers/safe_str/Safe_Str__Hash.py,sha256=IpYdYwXey5WZWa6QfysmsiDP-4L-wP-_EKbkeXhFRH4,1252
|
268
268
|
osbot_utils/helpers/safe_str/Safe_Str__Text.py,sha256=QxuWqF8hNYdOPDn3Yac86h_1ZaX-THbTBDakberiJcs,313
|
@@ -377,7 +377,7 @@ osbot_utils/type_safe/Type_Safe__Base.py,sha256=UTMipTL6mXoetAEUCI5hs8RqXp4NDYOv
|
|
377
377
|
osbot_utils/type_safe/Type_Safe__Dict.py,sha256=QB200L5eNWT3FnUv8sm5kncj1wXJsJ9uRycNFl9xb6Y,3077
|
378
378
|
osbot_utils/type_safe/Type_Safe__List.py,sha256=y_lp7Ai0HfQCqC8Bxn0g6_M9MP5lPOXy5Dhkuj2fJvQ,1891
|
379
379
|
osbot_utils/type_safe/Type_Safe__Method.py,sha256=K100jAq1wmskuXcTd_9vt-XodSKJkDowJselmnDFCN8,16643
|
380
|
-
osbot_utils/type_safe/Type_Safe__Primitive.py,sha256
|
380
|
+
osbot_utils/type_safe/Type_Safe__Primitive.py,sha256=-GBUcuFBBBKrVRSf7cmjPRbAxo7vnL4tOvr9ktmbVj4,3829
|
381
381
|
osbot_utils/type_safe/Type_Safe__Set.py,sha256=j12fc8cbd9-s_a13ysaz723rNEW4Dt6hObCd0S-AjIg,1432
|
382
382
|
osbot_utils/type_safe/Type_Safe__Tuple.py,sha256=Kx7C4YfHybRbMmVMcmV6yFLi4T48pb592vEZfjjyLxo,1710
|
383
383
|
osbot_utils/type_safe/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
@@ -414,7 +414,7 @@ osbot_utils/utils/Csv.py,sha256=oHLVpjRJqrLMz9lubMCNEoThXWju5rNTprcwHc1zq2c,1012
|
|
414
414
|
osbot_utils/utils/Dev.py,sha256=eaQ87ZcMRRcxgzA-f7OO8HjjWhbE6L_edwvXiwFZvIQ,1291
|
415
415
|
osbot_utils/utils/Env.py,sha256=rBksAy6k-J5oAJp-S_JedVlcj1b2VK8V3zsQbacopMc,6076
|
416
416
|
osbot_utils/utils/Exceptions.py,sha256=KyOUHkXQ_6jDTq04Xm261dbEZuRidtsM4dgzNwSG8-8,389
|
417
|
-
osbot_utils/utils/Files.py,sha256=
|
417
|
+
osbot_utils/utils/Files.py,sha256=Zg8TV8RpKv3ytnZvvT17DWeEJCisSkO8zzyP_Twhcww,23449
|
418
418
|
osbot_utils/utils/Functions.py,sha256=VoTrAbCHt6hulz6hVz3co8w2xoOS8wE04wyHc5_cC1c,3671
|
419
419
|
osbot_utils/utils/Http.py,sha256=pLDwq0Jd4Zmpps0gEzXTbeycSFRXMN8W-DprNpYq9A0,8189
|
420
420
|
osbot_utils/utils/Int.py,sha256=PmlUdU4lSwf4gJdmTVdqclulkEp7KPCVUDO6AcISMF4,116
|
@@ -432,10 +432,10 @@ osbot_utils/utils/Str.py,sha256=KQVfh0o3BxJKVm24yhAhgIGH5QYfzpP1G-siVv2zQws,3301
|
|
432
432
|
osbot_utils/utils/Threads.py,sha256=YI1T382AtJpHVsa-BK7SycmEYhnkqHIiYyK_5HSmUtw,4329
|
433
433
|
osbot_utils/utils/Toml.py,sha256=Rxl8gx7mni5CvBAK-Ai02EKw-GwtJdd3yeHT2kMloik,1667
|
434
434
|
osbot_utils/utils/Version.py,sha256=Ww6ChwTxqp1QAcxOnztkTicShlcx6fbNsWX5xausHrg,422
|
435
|
-
osbot_utils/utils/Zip.py,sha256=
|
435
|
+
osbot_utils/utils/Zip.py,sha256=mG42lgTY0tnm14T3P1-DSAIZKkTiYoO3odZ1aOUdc1I,14394
|
436
436
|
osbot_utils/utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
437
|
-
osbot_utils/version,sha256=
|
438
|
-
osbot_utils-2.
|
439
|
-
osbot_utils-2.
|
440
|
-
osbot_utils-2.
|
441
|
-
osbot_utils-2.
|
437
|
+
osbot_utils/version,sha256=0cHMv8cBSyT1AgEUuKq6G2WmqYGDEd5ktNMn-AhcMBI,8
|
438
|
+
osbot_utils-2.70.0.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
|
439
|
+
osbot_utils-2.70.0.dist-info/METADATA,sha256=-9hKyVUnDUX9X8z-OuMG0sETWVC4RPkILyfo93EC5fQ,1329
|
440
|
+
osbot_utils-2.70.0.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
|
441
|
+
osbot_utils-2.70.0.dist-info/RECORD,,
|
File without changes
|
File without changes
|