osbot-utils 1.38.0__py3-none-any.whl → 1.40.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.
@@ -10,6 +10,7 @@ from decimal import Decimal
10
10
  from enum import Enum, EnumMeta
11
11
  from typing import List
12
12
  from osbot_utils.base_classes.Type_Safe__List import Type_Safe__List
13
+ from osbot_utils.helpers.Random_Guid import Random_Guid
13
14
  from osbot_utils.utils.Dev import pprint
14
15
  from osbot_utils.utils.Json import json_parse
15
16
  from osbot_utils.utils.Misc import list_set
@@ -256,15 +257,18 @@ class Type_Safe:
256
257
  def deserialize_from_dict(self, data):
257
258
  for key, value in data.items():
258
259
  if hasattr(self, key) and isinstance(getattr(self, key), Type_Safe):
259
- getattr(self, key).deserialize_from_dict(value) # Recursive call for complex nested objects
260
+ getattr(self, key).deserialize_from_dict(value) # Recursive call for complex nested objects
260
261
  else:
261
- if hasattr(self, '__annotations__'): # can only do type safety checks if the class does not have annotations
262
+ if hasattr(self, '__annotations__'): # can only do type safety checks if the class does not have annotations
262
263
  if obj_is_attribute_annotation_of_type(self, key, EnumMeta): # Handle the case when the value is an Enum
263
264
  enum_type = getattr(self, '__annotations__').get(key)
264
265
  if type(value) is not enum_type: # If the value is not already of the target type
265
266
  value = enum_from_value(enum_type, value) # Try to resolve the value into the enum
266
-
267
- setattr(self, key, value) # Direct assignment for primitive types and other structures
267
+ elif obj_is_attribute_annotation_of_type(self, key, Decimal): # handle Decimals # todo: refactor these special cases into a separate method to class
268
+ value = Decimal(value)
269
+ elif obj_is_attribute_annotation_of_type(self, key, Random_Guid): # handle Random_Guid objects
270
+ value = Random_Guid(value)
271
+ setattr(self, key, value) # Direct assignment for primitive types and other structures
268
272
 
269
273
  return self
270
274
 
@@ -26,6 +26,7 @@ def cache_on_self(function: T) -> T:
26
26
  def wrapper(*args, **kwargs):
27
27
  if len(args) == 0 or inspect.isclass(type(args[0])) is False:
28
28
  raise Exception("In Method_Wrappers.cache_on_self could not find self")
29
+ # todo: fix bug that happens when the value of reload_cache is set to False
29
30
  if 'reload_cache' in kwargs: # if the reload parameter is set to True
30
31
  reload_cache = True # set reload to True
31
32
  del kwargs['reload_cache'] # remove the reload parameter from the kwargs
@@ -8,11 +8,18 @@ class Temp_Env_Vars(Type_Safe):
8
8
  original_env_vars: dict
9
9
 
10
10
  def __enter__(self):
11
+ return self.set_vars()
12
+
13
+ def __exit__(self, exc_type, exc_value, traceback):
14
+ self.restore_vars()
15
+
16
+ def set_vars(self):
11
17
  for key, value in self.env_vars.items():
12
18
  self.original_env_vars[key] = os.environ.get(key) # Backup original environment variables and set new ones
13
19
  os.environ[key] = value
20
+ return self
14
21
 
15
- def __exit__(self, exc_type, exc_value, traceback):
22
+ def restore_vars(self):
16
23
  for key in self.env_vars: # Restore original environment variables
17
24
  if self.original_env_vars[key] is None:
18
25
  del os.environ[key]
osbot_utils/utils/Json.py CHANGED
@@ -3,6 +3,8 @@ import os
3
3
 
4
4
  from osbot_utils.utils.Misc import str_lines, str_md5, str_sha256
5
5
  from osbot_utils.utils.Files import file_create_gz, file_create, load_file_gz, file_contents, file_lines, file_lines_gz
6
+ from osbot_utils.utils.Zip import str_to_gz, gz_to_str
7
+
6
8
 
7
9
  def json_dumps(python_object, indent=4, pretty=True, sort_keys=False, default=str, raise_exception=False):
8
10
  if python_object:
@@ -39,6 +41,15 @@ def json_sha_256(target):
39
41
  return str_sha256(json_dumps(target))
40
42
 
41
43
 
44
+ def json_to_gz(data):
45
+ value = json_dumps(data, pretty=False)
46
+ return str_to_gz(value)
47
+
48
+ def gz_to_json(gz_data):
49
+ data = gz_to_str(gz_data)
50
+ return json.loads(data)
51
+
52
+
42
53
 
43
54
  class Json:
44
55
 
@@ -295,8 +295,13 @@ def obj_attribute_annotation(target, attr_name):
295
295
 
296
296
  def obj_is_attribute_annotation_of_type(target, attr_name, expected_type):
297
297
  attribute_annotation = obj_attribute_annotation(target, attr_name)
298
- attribute_type = type(attribute_annotation)
299
- return attribute_type is expected_type
298
+ #attribute_type = type(attribute_annotation)
299
+ #return attribute_type is expected_type
300
+ if expected_type is attribute_annotation:
301
+ return True
302
+ if expected_type is type(attribute_annotation):
303
+ return True
304
+ return False
300
305
 
301
306
  def obj_is_type_union_compatible(var_type, compatible_types):
302
307
  origin = get_origin(var_type)
osbot_utils/utils/Zip.py CHANGED
@@ -221,6 +221,30 @@ def zip_files(base_folder, file_pattern="*.*", target_file=None):
221
221
 
222
222
  return target_file
223
223
 
224
+ # actions on strings
225
+
226
+ def bytes_to_gz(input_bytes):
227
+ buf = io.BytesIO() # Create an in-memory bytes buffer
228
+ with gzip.GzipFile(fileobj=buf, mode='wb') as gz_file: # Create a gzip file object that writes to the buffer
229
+ gz_file.write(input_bytes) # Write the input bytes to the gzip file object
230
+ return buf.getvalue() # Get the compressed data from the buffer
231
+
232
+ def str_to_gz(value):
233
+ buf = io.BytesIO() # Create an in-memory bytes buffer
234
+ with gzip.GzipFile(fileobj=buf, mode='wb') as gz_file: # Create a gzip file object that writes to the buffer
235
+ gz_file.write(value.encode('utf-8')) # Write the input string to the gzip file object
236
+ return buf.getvalue() # Get the compressed data from the buffer
237
+
238
+ def gz_to_bytes(gz_data):
239
+ buf = io.BytesIO(gz_data) # Create an in-memory bytes buffer with the gzip data
240
+ with gzip.GzipFile(fileobj=buf, mode='rb') as gz_file: # Create a gzip file object that reads from the buffer
241
+ return gz_file.read() # Read the decompressed data as bytes
242
+
243
+ def gz_to_str(gz_data):
244
+ buf = io.BytesIO(gz_data) # Create an in-memory bytes buffer with the gzip data
245
+ with gzip.GzipFile(fileobj=buf, mode='rb') as gz_file: # Create a gzip file object that reads from the buffer
246
+ return gz_file.read().decode('utf-8') # Read the decompressed data and decode it to a string
247
+
224
248
  ###########################
225
249
  # extra function's mappings
226
250
 
osbot_utils/version CHANGED
@@ -1 +1 @@
1
- v1.38.0
1
+ v1.40.0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: osbot_utils
3
- Version: 1.38.0
3
+ Version: 1.40.0
4
4
  Summary: OWASP Security Bot - Utils
5
5
  Home-page: https://github.com/owasp-sbot/OSBot-Utils
6
6
  License: MIT
@@ -22,7 +22,7 @@ Description-Content-Type: text/markdown
22
22
 
23
23
  Powerful Python util methods and classes that simplify common apis and tasks.
24
24
 
25
- ![Current Release](https://img.shields.io/badge/release-v1.38.0-blue)
25
+ ![Current Release](https://img.shields.io/badge/release-v1.40.0-blue)
26
26
  [![codecov](https://codecov.io/gh/owasp-sbot/OSBot-Utils/graph/badge.svg?token=GNVW0COX1N)](https://codecov.io/gh/owasp-sbot/OSBot-Utils)
27
27
 
28
28
 
@@ -2,7 +2,7 @@ osbot_utils/__init__.py,sha256=DdJDmQc9zbQUlPVyTJOww6Ixrn9n4bD3ami5ItQfzJI,16
2
2
  osbot_utils/base_classes/Cache_Pickle.py,sha256=kPCwrgUbf_dEdxUz7vW1GuvIPwlNXxuRhb-H3AbSpII,5884
3
3
  osbot_utils/base_classes/Kwargs_To_Disk.py,sha256=HHoy05NC_w35WcT-OnSKoSIV_cLqaU9rdjH0_KNTM0E,1096
4
4
  osbot_utils/base_classes/Kwargs_To_Self.py,sha256=weFNsBfBNV9W_qBkN-IdBD4yYcJV_zgTxBRO-ZlcPS4,141
5
- osbot_utils/base_classes/Type_Safe.py,sha256=62eV2AnIAN-7VshhOpvWw0v_DZHJucLq10VQHjgCfX8,17089
5
+ osbot_utils/base_classes/Type_Safe.py,sha256=aTbN-8FgobuCPnUQxrWbKEYUg0_e0XjAEq6jTxs-k_M,17589
6
6
  osbot_utils/base_classes/Type_Safe__List.py,sha256=-80C9OhsK6iDR2dAG8yNLAZV0qg5x3faqvSUigFCMJw,517
7
7
  osbot_utils/base_classes/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
8
8
  osbot_utils/context_managers/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
@@ -18,7 +18,7 @@ osbot_utils/decorators/lists/index_by.py,sha256=BEbfd13l11zROhXAb0vkB_aZi9P2Zt4p
18
18
  osbot_utils/decorators/methods/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
19
19
  osbot_utils/decorators/methods/cache.py,sha256=IMmHBeR6qtaOfBNgZUeI1SVLoexQQy6vk1LDW-20_5w,1062
20
20
  osbot_utils/decorators/methods/cache_on_function.py,sha256=sDebxWjJnusb_w4R26OTYcmTF6CCeWrpesn-dgzEu8g,2694
21
- osbot_utils/decorators/methods/cache_on_self.py,sha256=zhE1YoYma8eyx9i7dpTmpdrVzf5CkBJRe9aYok9xlgs,3527
21
+ osbot_utils/decorators/methods/cache_on_self.py,sha256=FODnv36jAwNPs11j2fGg6u-4A7MdaeRd1ES6fyPgP78,3611
22
22
  osbot_utils/decorators/methods/cache_on_tmp.py,sha256=8wLnRAUUkHobu6-B2Q8aAE8jLeIu3b-CQuMtXcnIN9w,3102
23
23
  osbot_utils/decorators/methods/capture_exception.py,sha256=mTfjqIS_qvZLhX6_NF_fkD_EnMLZRXkUHqUaiAbqZkU,1160
24
24
  osbot_utils/decorators/methods/capture_status.py,sha256=5xklG0usO3hGTjedhrmIucXtUjPd2pkuvA5jsty0a5E,659
@@ -253,7 +253,7 @@ osbot_utils/testing/Profiler.py,sha256=4em6Lpp0ONRDoDDCZsc_CdAOi_QolKOp4eA7KHN96
253
253
  osbot_utils/testing/Pytest.py,sha256=R3qdsIXGcNQcu7iobz0RB8AhbbHhc6t757tZoSZRrxA,730
254
254
  osbot_utils/testing/Stderr.py,sha256=ynf0Wle9NvgneLChzAxFBQ0QlE5sbri_fzJ8bEJMNkc,718
255
255
  osbot_utils/testing/Stdout.py,sha256=Gmxd_dOplXlucdSbOhYhka9sWP-Hmqb7ZuLs_JjtW7Y,592
256
- osbot_utils/testing/Temp_Env_Vars.py,sha256=oFuaegBlaV0aySkPe1Nzf-mdIKN03oTUKNfKijGz__M,751
256
+ osbot_utils/testing/Temp_Env_Vars.py,sha256=CE_lk54QcibkQVtIj2DrQPS3k2LgQZUctU5b-WwWvbo,884
257
257
  osbot_utils/testing/Temp_File.py,sha256=yZBL9MmcNU4PCQ4xlF4rSss4GylKoX3T_AJF-BlQhdI,1693
258
258
  osbot_utils/testing/Temp_Folder.py,sha256=Dbcohr2ciex6w-kB79R41Nuoa0pgpDbKtPGnlMmJ73k,5194
259
259
  osbot_utils/testing/Temp_Sys_Path.py,sha256=gOMD-7dQYQlejoDYUqsrmuZQ9DLC07ymPZB3zYuNmG4,256
@@ -273,11 +273,11 @@ osbot_utils/utils/Files.py,sha256=z-egRBPDYp6AJkZxK-JnXOwz1OezgzEDG_fgSHVcCCM,22
273
273
  osbot_utils/utils/Functions.py,sha256=0E6alPJ0fJpBiJgFOWooCOi265wSRyxxXAJ5CELBnso,3498
274
274
  osbot_utils/utils/Http.py,sha256=WlXEfgT_NaiDVD7vCDUxy_nOm5Qf8x_L0A3zd8B5tX8,4706
275
275
  osbot_utils/utils/Int.py,sha256=PmlUdU4lSwf4gJdmTVdqclulkEp7KPCVUDO6AcISMF4,116
276
- osbot_utils/utils/Json.py,sha256=UNaBazuH1R40fsHjpjuK8kmAANmUHoK9Q0PUeYmgPeY,6254
276
+ osbot_utils/utils/Json.py,sha256=7COxBlZRnpxtpNqpmzMPYkcKTnCok-s686nT27oiKEQ,6489
277
277
  osbot_utils/utils/Json_Cache.py,sha256=mLPkkDZN-3ZVJiDvV1KBJXILtKkTZ4OepzOsDoBPhWg,2006
278
278
  osbot_utils/utils/Lists.py,sha256=tPz5x5s3sRO97WZ_nsxREBPC5cwaHrhgaYBhsrffTT8,5599
279
279
  osbot_utils/utils/Misc.py,sha256=nODZT6p44B4xYiIiqfEeKYEErQiKR9SGthhGtZWGhkI,16804
280
- osbot_utils/utils/Objects.py,sha256=qAWNLISL-gYTl1Ihj4fBSZ9I6n-p-YPUhRZu9YQwqWQ,15235
280
+ osbot_utils/utils/Objects.py,sha256=frOLTJsIhGye1j99StuIJSkOlPmPDNI3BaWWAr98MEc,15392
281
281
  osbot_utils/utils/Png.py,sha256=V1juGp6wkpPigMJ8HcxrPDIP4bSwu51oNkLI8YqP76Y,1172
282
282
  osbot_utils/utils/Process.py,sha256=lr3CTiEkN3EiBx3ZmzYmTKlQoPdkgZBRjPulMxG-zdo,2357
283
283
  osbot_utils/utils/Python_Logger.py,sha256=tx8N6wRKL3RDHboDRKZn8SirSJdSAE9cACyJkxrThZ8,12792
@@ -286,10 +286,10 @@ osbot_utils/utils/Status.py,sha256=Yq4s0TelXgn0i2QjCP9V8mP30GabXp_UL-jjM6Iwiw4,4
286
286
  osbot_utils/utils/Str.py,sha256=kxdY8ROX4FdJtCaMTfOc8fK_xcDICprNkefHu2MMNU4,2585
287
287
  osbot_utils/utils/Toml.py,sha256=dqiegndCJF7V1YT1Tc-b0-Bl6QWyL5q30urmQwMXfMQ,1402
288
288
  osbot_utils/utils/Version.py,sha256=Ww6ChwTxqp1QAcxOnztkTicShlcx6fbNsWX5xausHrg,422
289
- osbot_utils/utils/Zip.py,sha256=riPLKkZJxQjVu8lCm19cOTx5uiLPm1HreB9_BzNXi30,12209
289
+ osbot_utils/utils/Zip.py,sha256=Gt4K7Q9LlYeRxOVv8aYpaBLKxrYriCAEUa_R6YWEbMg,13903
290
290
  osbot_utils/utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
291
- osbot_utils/version,sha256=0nfWeSLY7TCuC1ABX0OW6t1UI9kbFtRJw6PqE6oH7xs,8
292
- osbot_utils-1.38.0.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
293
- osbot_utils-1.38.0.dist-info/METADATA,sha256=aOTN_LvtbeozSVNCqnnkyaHYPbny6vxZJJf2s7DknLs,1266
294
- osbot_utils-1.38.0.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
295
- osbot_utils-1.38.0.dist-info/RECORD,,
291
+ osbot_utils/version,sha256=fSti0HDCJC-iadQvZ1K8EEmTHQ3sSC7euwYRLBZVc4w,8
292
+ osbot_utils-1.40.0.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
293
+ osbot_utils-1.40.0.dist-info/METADATA,sha256=CB7ZKm1V-3vWrx5ZXtm2EYManGV9nvNP5Shf8rCNdjc,1266
294
+ osbot_utils-1.40.0.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
295
+ osbot_utils-1.40.0.dist-info/RECORD,,