osbot-utils 1.92.0__py3-none-any.whl → 1.93.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.
@@ -3,7 +3,7 @@
3
3
 
4
4
  import sys
5
5
  import types
6
- from osbot_utils.utils.Objects import default_value # todo: remove test mocking requirement for this to be here (instead of on the respective method)
6
+ from osbot_utils.utils.Objects import default_value # todo: remove test mocking requirement for this to be here (instead of on the respective method)
7
7
 
8
8
  # Backport implementations of get_origin and get_args for Python 3.7
9
9
  if sys.version_info < (3, 8): # pragma: no cover
@@ -95,8 +95,12 @@ class Type_Safe:
95
95
  if value is not None:
96
96
  if type(value) is dict:
97
97
  value = convert_dict_to_value_from_obj_annotation(self, name, value)
98
- if type(value) in [int, str]: # for now only a small number of str and int classes are supported (until we understand the full implications of this)
98
+ elif type(value) in [int, str]: # for now only a small number of str and int classes are supported (until we understand the full implications of this)
99
99
  value = convert_to_value_from_obj_annotation (self, name, value)
100
+ else:
101
+ origin = get_origin(value)
102
+ if origin is not None:
103
+ value = origin
100
104
  check_1 = value_type_matches_obj_annotation_for_attr (self, name, value)
101
105
  check_2 = value_type_matches_obj_annotation_for_union_and_annotated(self, name, value)
102
106
  if (check_1 is False and check_2 is None or
@@ -169,7 +173,7 @@ class Type_Safe:
169
173
  #todo: fix type safety bug that I believe is caused here
170
174
  if obj_is_type_union_compatible(var_type, IMMUTABLE_TYPES) is False: # if var_type is not something like Optional[Union[int, str]]
171
175
  if type(var_type) not in IMMUTABLE_TYPES:
172
- exception_message = f"variable '{var_name}' is defined as type '{var_type}' which is not supported by Kwargs_To_Self, with only the following immutable types being supported: '{IMMUTABLE_TYPES}'"
176
+ exception_message = f"variable '{var_name}' is defined as type '{var_type}' which is not supported by Type_Safe, with only the following immutable types being supported: '{IMMUTABLE_TYPES}'"
173
177
  raise ValueError(exception_message)
174
178
  if include_base_classes is False:
175
179
  break
@@ -304,6 +308,19 @@ class Type_Safe:
304
308
  setattr(self, key, value)
305
309
  return self
306
310
 
311
+ def deserialize_type__using_value(self, value):
312
+ if value:
313
+ try:
314
+ module_name, type_name = value.rsplit('.', 1)
315
+ if module_name == 'builtins' and type_name == 'NoneType': # Special case for NoneType (which serialises as builtins.* , but it actually in types.* )
316
+ value = types.NoneType
317
+ else:
318
+ module = __import__(module_name, fromlist=[type_name])
319
+ value = getattr(module, type_name)
320
+ except (ValueError, ImportError, AttributeError) as e:
321
+ raise ValueError(f"Could not reconstruct type from '{value}': {str(e)}")
322
+ return value
323
+
307
324
  def deserialize_dict__using_key_value_annotations(self, key, value):
308
325
  from osbot_utils.base_classes.Type_Safe__Dict import Type_Safe__Dict
309
326
 
@@ -353,7 +370,9 @@ class Type_Safe:
353
370
  raise ValueError(f"Attribute '{key}' not found in '{self.__class__.__name__}'")
354
371
  else:
355
372
  continue
356
- if obj_is_attribute_annotation_of_type(self, key, dict): # handle the case when the value is a dict
373
+ if obj_attribute_annotation(self, key) == type: # Handle type objects
374
+ value = self.deserialize_type__using_value(value)
375
+ elif obj_is_attribute_annotation_of_type(self, key, dict): # handle the case when the value is a dict
357
376
  value = self.deserialize_dict__using_key_value_annotations(key, value)
358
377
  elif obj_is_attribute_annotation_of_type(self, key, list): # handle the case when the value is a list
359
378
  attribute_annotation = obj_attribute_annotation(self, key) # get the annotation for this variable
@@ -423,15 +442,17 @@ def serialize_to_dict(obj):
423
442
  return obj
424
443
  elif isinstance(obj, Enum):
425
444
  return obj.name
445
+ elif isinstance(obj, type):
446
+ return f"{obj.__module__}.{obj.__name__}" # save the full type name
426
447
  elif isinstance(obj, list) or isinstance(obj, List):
427
448
  return [serialize_to_dict(item) for item in obj]
428
449
  elif isinstance(obj, dict):
429
450
  return {key: serialize_to_dict(value) for key, value in obj.items()}
430
451
  elif hasattr(obj, "__dict__"):
431
- data = {} # todo: look at a more advanced version which saved the type of the object, for example with {'__type__': type(obj).__name__}
452
+ data = {} # todo: look at a more advanced version which saved the type of the object, for example with {'__type__': type(obj).__name__}
432
453
  for key, value in obj.__dict__.items():
433
- if key.startswith('__') is False: # don't process internal variables (for example the ones set by @cache_on_self)
434
- data[key] = serialize_to_dict(value) # Recursive call for complex types
454
+ if key.startswith('__') is False: # don't process internal variables (for example the ones set by @cache_on_self)
455
+ data[key] = serialize_to_dict(value) # Recursive call for complex types
435
456
  return data
436
457
  else:
437
458
  raise TypeError(f"Type {type(obj)} not serializable")
@@ -1,10 +1,6 @@
1
- from enum import Enum
2
-
3
- from osbot_utils.base_classes.Kwargs_To_Self import Kwargs_To_Self
4
- from osbot_utils.graphs.mermaid.configs.Mermaid__Node__Config import Mermaid__Node__Config
5
- from osbot_utils.graphs.mermaid.models.Mermaid__Node__Shape import Mermaid__Node__Shape
6
- from osbot_utils.graphs.mgraph.MGraph__Node import MGraph__Node
7
- from osbot_utils.utils.Str import safe_str
1
+ from osbot_utils.graphs.mermaid.configs.Mermaid__Node__Config import Mermaid__Node__Config
2
+ from osbot_utils.graphs.mermaid.models.Mermaid__Node__Shape import Mermaid__Node__Shape
3
+ from osbot_utils.graphs.mgraph.MGraph__Node import MGraph__Node
8
4
 
9
5
  LINE_PADDING = ' '
10
6
 
@@ -1,7 +1,7 @@
1
1
  from typing import List
2
2
 
3
3
  from osbot_utils.base_classes.Kwargs_To_Self import Kwargs_To_Self
4
- from osbot_utils.graphs.mermaid.configs.Mermaid__Render__Config import Mermaid__Render__Config
4
+ from osbot_utils.graphs.mermaid.configs.Mermaid__Render__Config import Mermaid__Render__Config
5
5
  from osbot_utils.graphs.mermaid.models.Mermaid__Diagram_Direction import Diagram__Direction
6
6
  from osbot_utils.graphs.mermaid.models.Mermaid__Diagram__Type import Diagram__Type
7
7
 
@@ -1,8 +1,10 @@
1
1
  from osbot_utils.utils.Misc import random_id_short
2
2
  from osbot_utils.utils.Str import safe_id
3
3
 
4
+ SAFE_ID__MAX_LENGTH = 512
5
+
4
6
  class Safe_Id(str):
5
- def __new__(cls, value=None, max_length=36):
7
+ def __new__(cls, value=None, max_length=SAFE_ID__MAX_LENGTH):
6
8
  if value is None:
7
9
  value = safe_id(random_id_short('safe-id'))
8
10
  sanitized_value = safe_id(value, max_length=max_length)
osbot_utils/utils/Json.py CHANGED
@@ -193,6 +193,7 @@ json_save_file_gz = Json.save_file_gz
193
193
  json_save_file_pretty_gz = Json.save_file_pretty_gz
194
194
  json_save_tmp_file = Json.json_save_tmp_file
195
195
  str_to_json = Json.loads
196
+ str_from_json = json_dumps
196
197
 
197
198
  load_file_json = json_load_file
198
199
  load_file_json_gz = json_load_file_gz
@@ -359,8 +359,6 @@ def obj_attribute_annotation(target, attr_name):
359
359
 
360
360
  def obj_is_attribute_annotation_of_type(target, attr_name, expected_type):
361
361
  attribute_annotation = obj_attribute_annotation(target, attr_name)
362
- #attribute_type = type(attribute_annotation)
363
- #return attribute_type is expected_type
364
362
  if expected_type is attribute_annotation:
365
363
  return True
366
364
  if expected_type is type(attribute_annotation):
osbot_utils/utils/Str.py CHANGED
@@ -42,7 +42,7 @@ def safe_id(value, max_length=36):
42
42
  value = str(value)
43
43
 
44
44
  if len(value) > max_length:
45
- raise ValueError(f"Invalid ID: The ID must not exceed 36 characters (was {len(value)}).")
45
+ raise ValueError(f"Invalid ID: The ID must not exceed {max_length} characters (was {len(value)}).")
46
46
 
47
47
  sanitized_value = REGEX__SAFE_ID_REGEX.sub('_', value)
48
48
 
osbot_utils/version CHANGED
@@ -1 +1 @@
1
- v1.92.0
1
+ v1.93.0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: osbot_utils
3
- Version: 1.92.0
3
+ Version: 1.93.0
4
4
  Summary: OWASP Security Bot - Utils
5
5
  Home-page: https://github.com/owasp-sbot/OSBot-Utils
6
6
  License: MIT
@@ -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-v1.92.0-blue)
26
+ ![Current Release](https://img.shields.io/badge/release-v1.93.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
 
@@ -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=D1SzaB3Km-UrlucCE3boL5_dbTQBDn1-s1URbehRl8w,27754
5
+ osbot_utils/base_classes/Type_Safe.py,sha256=XzJd1tOEFFT67ycg5QaiXv5pFeWD8Fq4YDVQGGVYAjc,28990
6
6
  osbot_utils/base_classes/Type_Safe__Base.py,sha256=CFPYe8_i5vvTLyc7s8CXbY4n_dY6sqVfBY8w9Vo77ZA,5468
7
7
  osbot_utils/base_classes/Type_Safe__Dict.py,sha256=sfZcukhXUd9TS0PQpAk-gGLfZUJSC6BtMh6jF4Fn8Jw,1107
8
8
  osbot_utils/base_classes/Type_Safe__List.py,sha256=pXDzJJttpEQQ9oTdsw7BykMB4VIX2rZzi1ZrnCzMZ8M,650
@@ -41,8 +41,8 @@ osbot_utils/graphs/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuF
41
41
  osbot_utils/graphs/mermaid/Mermaid.py,sha256=G7--iIKm2C1z-tEB1qLNopwoW3_w4oR7Oq7-yA460mM,3164
42
42
  osbot_utils/graphs/mermaid/Mermaid__Edge.py,sha256=jwHxHJEAA49aO28T8nnJFxOfpWAZZaWKNT_krG1fwkQ,1893
43
43
  osbot_utils/graphs/mermaid/Mermaid__Graph.py,sha256=FRw17efZrdcKyXDKsyb1C8nswIAmljiUAyiF0FHIL4M,2854
44
- osbot_utils/graphs/mermaid/Mermaid__Node.py,sha256=j_AVfR3hnKAJH2Z3d17djvU7MfQP8B70Lh7Jv6y0tTs,3322
45
- osbot_utils/graphs/mermaid/Mermaid__Renderer.py,sha256=-5h_Xkaq3boKVWzPYPG_kUoe0SOlR0Tm4zVEXJ70wUk,2289
44
+ osbot_utils/graphs/mermaid/Mermaid__Node.py,sha256=e_DO1SGnf6Zdh4ZvcRBhonDicgKGu86VDN9gd9tP7Gc,3215
45
+ osbot_utils/graphs/mermaid/Mermaid__Renderer.py,sha256=Uoy_y43zeKqKUYj0V2FMK9CKFweutUY5r4IjsD0Z7RU,2293
46
46
  osbot_utils/graphs/mermaid/configs/Mermaid__Edge__Config.py,sha256=PaypnkaDQQhNBIxZqnAB5bxuJAZWh3SQtmHUPfIcDCQ,212
47
47
  osbot_utils/graphs/mermaid/configs/Mermaid__Node__Config.py,sha256=KZWd_uiIm-QIz_wPSDfXfWAWU_A2yxzMeq-h5wldVjQ,465
48
48
  osbot_utils/graphs/mermaid/configs/Mermaid__Render__Config.py,sha256=JGQR6kdiTQh45zFZYvqdEDMghFIWLnVYMZsdukYtGyw,216
@@ -72,7 +72,7 @@ osbot_utils/helpers/Python_Audit.py,sha256=shpZlluJwqJBAlad6xN01FkgC1TsQ48RLvR5Z
72
72
  osbot_utils/helpers/Random_Guid.py,sha256=COu9hcP51vzjk-ErECTFFaOWuOmW0eGJyMu8HXhaRXQ,382
73
73
  osbot_utils/helpers/Random_Guid_Short.py,sha256=YP_k5OLuYvXWGU2OEnQHk_OGViBQofTWKm3pUdQaJao,404
74
74
  osbot_utils/helpers/Random_Seed.py,sha256=14btja8LDN9cMGWaz4fCNcMRU_eyx49gas-_PQvHgy4,634
75
- osbot_utils/helpers/Safe_Id.py,sha256=iwIrWXfSARyr6JkihNhh1soOdjeCGVf3wkXSkSP8zDw,402
75
+ osbot_utils/helpers/Safe_Id.py,sha256=0wPGd9eLzaOCYTSg9yEOal1kPsc8OI9khHkqEw9VQOM,446
76
76
  osbot_utils/helpers/Str_ASCII.py,sha256=PRqyu449XnKrLn6b9Miii1Hv-GO5OAa1UhhgvlRcC2M,704
77
77
  osbot_utils/helpers/Timestamp_Now.py,sha256=k3-SUGYx2jLTXvgZYeECqPRJhVxqWPmW7co1l6r12jk,438
78
78
  osbot_utils/helpers/Type_Registry.py,sha256=Ajk3SyMSKDi2g9SJYUtTgg7PZkAgydaHcpbGuEN3S94,311
@@ -323,24 +323,24 @@ osbot_utils/utils/Files.py,sha256=yxteAcyhDcUy1_r9Eihx80V16lV_UAE6cvoOe2Dc7DU,23
323
323
  osbot_utils/utils/Functions.py,sha256=0E6alPJ0fJpBiJgFOWooCOi265wSRyxxXAJ5CELBnso,3498
324
324
  osbot_utils/utils/Http.py,sha256=Cm_-b2EgxKoQJ47ThZp-dgHCdeGv4UcCNLfTOH94-7s,7790
325
325
  osbot_utils/utils/Int.py,sha256=PmlUdU4lSwf4gJdmTVdqclulkEp7KPCVUDO6AcISMF4,116
326
- osbot_utils/utils/Json.py,sha256=0DZGlCU7Nqte5n0r7ctPXFybqA5MRfSrTz5zuK_6UFk,7095
326
+ osbot_utils/utils/Json.py,sha256=0t7Hwefx8bg4JiZVr-xIbWP3BAk6_ZsnY7iV5pnRLDQ,7137
327
327
  osbot_utils/utils/Json_Cache.py,sha256=mLPkkDZN-3ZVJiDvV1KBJXILtKkTZ4OepzOsDoBPhWg,2006
328
328
  osbot_utils/utils/Lists.py,sha256=tPz5x5s3sRO97WZ_nsxREBPC5cwaHrhgaYBhsrffTT8,5599
329
329
  osbot_utils/utils/Misc.py,sha256=H_xexJgiTxB3jDeDiW8efGQbO0Zuy8MM0iQ7qXC92JI,17363
330
- osbot_utils/utils/Objects.py,sha256=FVX0CyVY2g1iXIL4UA1dFzgbsA-GYrJ_cYlxosGxTdo,21713
330
+ osbot_utils/utils/Objects.py,sha256=nsheXk2t4sRQVJStsKYDgCj8wF3knQ8X1AtLO1M4SlM,21614
331
331
  osbot_utils/utils/Png.py,sha256=V1juGp6wkpPigMJ8HcxrPDIP4bSwu51oNkLI8YqP76Y,1172
332
332
  osbot_utils/utils/Process.py,sha256=lr3CTiEkN3EiBx3ZmzYmTKlQoPdkgZBRjPulMxG-zdo,2357
333
333
  osbot_utils/utils/Python_Logger.py,sha256=tx8N6wRKL3RDHboDRKZn8SirSJdSAE9cACyJkxrThZ8,12792
334
334
  osbot_utils/utils/Regex.py,sha256=MtHhk69ax7Nwu4CQZK7y4KXHZ6VREwEpIchuioB168c,960
335
335
  osbot_utils/utils/Status.py,sha256=Yq4s0TelXgn0i2QjCP9V8mP30GabXp_UL-jjM6Iwiw4,4305
336
- osbot_utils/utils/Str.py,sha256=Y05F46m6s3_H7KoPdeasc1LRaU7R4YifIbsHNQYDEeg,3275
336
+ osbot_utils/utils/Str.py,sha256=6TS_DyWDfNfjK9JrEC7cAkqRU-0U7zBFYZDTMQkKPG0,3285
337
337
  osbot_utils/utils/Threads.py,sha256=lnh4doZWYUIoWBZRU_780QPeAIKGDh7INuqmU8Fzmdc,3042
338
338
  osbot_utils/utils/Toml.py,sha256=Rxl8gx7mni5CvBAK-Ai02EKw-GwtJdd3yeHT2kMloik,1667
339
339
  osbot_utils/utils/Version.py,sha256=Ww6ChwTxqp1QAcxOnztkTicShlcx6fbNsWX5xausHrg,422
340
340
  osbot_utils/utils/Zip.py,sha256=pR6sKliUY0KZXmqNzKY2frfW-YVQEVbLKiyqQX_lc-8,14052
341
341
  osbot_utils/utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
342
- osbot_utils/version,sha256=HcQJfW9qetpTTCAJA7zp8wAdYNIGH7zJcl-GVVPylC4,8
343
- osbot_utils-1.92.0.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
344
- osbot_utils-1.92.0.dist-info/METADATA,sha256=BD8zNKY7lu6bJuUASVxE_R_j2ywMvZj6t32oFGOn9Sw,1317
345
- osbot_utils-1.92.0.dist-info/WHEEL,sha256=Nq82e9rUAnEjt98J6MlVmMCZb-t9cYE2Ir1kpBmnWfs,88
346
- osbot_utils-1.92.0.dist-info/RECORD,,
342
+ osbot_utils/version,sha256=fCgkUd9gyG9PiBtqwiUTRF7MK2o3-3SPgYURno8TnxU,8
343
+ osbot_utils-1.93.0.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
344
+ osbot_utils-1.93.0.dist-info/METADATA,sha256=FiD0UZFCc7gZG4TyyE4JteDBmpCFyBIeU0w72FsCCPI,1317
345
+ osbot_utils-1.93.0.dist-info/WHEEL,sha256=Nq82e9rUAnEjt98J6MlVmMCZb-t9cYE2Ir1kpBmnWfs,88
346
+ osbot_utils-1.93.0.dist-info/RECORD,,