osbot-utils 2.69.0__py3-none-any.whl → 2.71.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.
@@ -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 = re.compile(r'[^a-zA-Z0-9_\-. ]')
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
@@ -1,6 +1,6 @@
1
1
  import os
2
2
  from osbot_utils.type_safe.Type_Safe import Type_Safe
3
- from osbot_utils.utils.Env import del_env, set_env
3
+ from osbot_utils.utils.Env import del_env, set_env
4
4
 
5
5
 
6
6
  class Temp_Env_Vars(Type_Safe):
@@ -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
 
@@ -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
- result.append(str(file)) # todo: see if there is a better way to do this conversion to string
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
@@ -2,7 +2,9 @@
2
2
  from types import SimpleNamespace
3
3
 
4
4
  class __(SimpleNamespace):
5
- pass
5
+
6
+ def __enter__(self) : return self
7
+ def __exit__(self, exc_type, exc_val, exc_tb): return False
6
8
 
7
9
  def base_classes(cls):
8
10
  if type(cls) is type:
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: # Open the zip file from the buffer
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.69.0
1
+ v2.71.0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: osbot_utils
3
- Version: 2.69.0
3
+ Version: 2.71.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
- ![Current Release](https://img.shields.io/badge/release-v2.69.0-blue)
26
+ ![Current Release](https://img.shields.io/badge/release-v2.71.0-blue)
27
27
  [![codecov](https://codecov.io/gh/owasp-sbot/OSBot-Utils/graph/badge.svg?token=GNVW0COX1N)](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=ncjkQ2hAhw0a3UulrCuQsA9ytrFwg5CT1XRJIGChMpY,289
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
@@ -357,7 +357,7 @@ osbot_utils/testing/Profiler.py,sha256=4em6Lpp0ONRDoDDCZsc_CdAOi_QolKOp4eA7KHN96
357
357
  osbot_utils/testing/Pytest.py,sha256=R3qdsIXGcNQcu7iobz0RB8AhbbHhc6t757tZoSZRrxA,730
358
358
  osbot_utils/testing/Stderr.py,sha256=ynf0Wle9NvgneLChzAxFBQ0QlE5sbri_fzJ8bEJMNkc,718
359
359
  osbot_utils/testing/Stdout.py,sha256=Gmxd_dOplXlucdSbOhYhka9sWP-Hmqb7ZuLs_JjtW7Y,592
360
- osbot_utils/testing/Temp_Env_Vars.py,sha256=3HK_miVX8fya7y9kYgkVwWmoF-PfJpUUOkwatFkK3QI,930
360
+ osbot_utils/testing/Temp_Env_Vars.py,sha256=C1jHGtessu4Q3_EJCkkOsQWSvKPFcilVFsCxYWKSDLI,927
361
361
  osbot_utils/testing/Temp_File.py,sha256=yZBL9MmcNU4PCQ4xlF4rSss4GylKoX3T_AJF-BlQhdI,1693
362
362
  osbot_utils/testing/Temp_Folder.py,sha256=Dbcohr2ciex6w-kB79R41Nuoa0pgpDbKtPGnlMmJ73k,5194
363
363
  osbot_utils/testing/Temp_Sys_Path.py,sha256=gOMD-7dQYQlejoDYUqsrmuZQ9DLC07ymPZB3zYuNmG4,256
@@ -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=CJ4LP2W5i9utSSzuiiJrwqvwdMv1DeQ6dIZICtYfLTY,3635
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=YwfXMeU1jfDzYvZJGy0bWHvvduTuTnftBNlAnpsXDzw,23013
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
@@ -422,7 +422,7 @@ osbot_utils/utils/Json.py,sha256=TvfDoXwOkWzWH-9KMnme5C7iFsMZOleAeue92qmkH6g,883
422
422
  osbot_utils/utils/Json_Cache.py,sha256=mLPkkDZN-3ZVJiDvV1KBJXILtKkTZ4OepzOsDoBPhWg,2006
423
423
  osbot_utils/utils/Lists.py,sha256=tPz5x5s3sRO97WZ_nsxREBPC5cwaHrhgaYBhsrffTT8,5599
424
424
  osbot_utils/utils/Misc.py,sha256=H_xexJgiTxB3jDeDiW8efGQbO0Zuy8MM0iQ7qXC92JI,17363
425
- osbot_utils/utils/Objects.py,sha256=HFYcTM8o53ongj_Ih5Iw4C043DdHnDg7cOCd20JDG70,13587
425
+ osbot_utils/utils/Objects.py,sha256=iyQ6RojK87iQRoGbh1twNHHy7wSrAro_l4jduly9Do0,13706
426
426
  osbot_utils/utils/Png.py,sha256=V1juGp6wkpPigMJ8HcxrPDIP4bSwu51oNkLI8YqP76Y,1172
427
427
  osbot_utils/utils/Process.py,sha256=lr3CTiEkN3EiBx3ZmzYmTKlQoPdkgZBRjPulMxG-zdo,2357
428
428
  osbot_utils/utils/Python_Logger.py,sha256=M9Oi62LxfnRSlCN8GhaiwiBINvcSdGy39FCWjyDD-Xg,12792
@@ -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=pR6sKliUY0KZXmqNzKY2frfW-YVQEVbLKiyqQX_lc-8,14052
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=mmT3TZTEYCy0jjpssvKXIUrmVMPAJu7HXXH98D1g9XE,8
438
- osbot_utils-2.69.0.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
439
- osbot_utils-2.69.0.dist-info/METADATA,sha256=GFekKv0EI34E7oUrhdWAUcTZQh0NWsvuh76sFgazH84,1329
440
- osbot_utils-2.69.0.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
441
- osbot_utils-2.69.0.dist-info/RECORD,,
437
+ osbot_utils/version,sha256=C1FnkT-V6vyUQrdGCxbq8JOwWMbcdmOn8wCb4q1aMUA,8
438
+ osbot_utils-2.71.0.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
439
+ osbot_utils-2.71.0.dist-info/METADATA,sha256=1JTIsnZagWuiMiblurRoyjA7L2AstKWf4a7jkFfTnc0,1329
440
+ osbot_utils-2.71.0.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
441
+ osbot_utils-2.71.0.dist-info/RECORD,,