osbot-utils 1.23.0__py3-none-any.whl → 1.24.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.
@@ -0,0 +1,20 @@
1
+ import os
2
+
3
+ from osbot_utils.base_classes.Type_Safe import Type_Safe
4
+
5
+
6
+ class Temp_Env_Vars(Type_Safe):
7
+ env_vars : dict
8
+ original_env_vars: dict
9
+
10
+ def __enter__(self):
11
+ for key, value in self.env_vars.items():
12
+ self.original_env_vars[key] = os.environ.get(key) # Backup original environment variables and set new ones
13
+ os.environ[key] = value
14
+
15
+ def __exit__(self, exc_type, exc_value, traceback):
16
+ for key in self.env_vars: # Restore original environment variables
17
+ if self.original_env_vars[key] is None:
18
+ del os.environ[key]
19
+ else:
20
+ os.environ[key] = self.original_env_vars[key]
@@ -4,17 +4,23 @@ from osbot_utils.utils.Misc import random_filename
4
4
 
5
5
 
6
6
  class Temp_File:
7
- def __init__(self, contents='...', extension='tmp',file_name=None, ):
7
+ def __init__(self, contents='...', extension='tmp',file_name=None, return_file_path=False, create_file=True):
8
8
  self.tmp_file = file_name or random_filename(extension)
9
9
  self.tmp_folder = None
10
10
  self.file_path = None
11
11
  self.original_contents = contents
12
+ self.return_file_path = return_file_path
13
+ self.create_file = create_file
12
14
 
13
15
  def __enter__(self):
14
16
  self.tmp_folder = Files.temp_folder(prefix='temp_folder_')
15
17
  self.file_path = Files.path_combine(self.tmp_folder, self.tmp_file)
16
- file_create(self.file_path, self.original_contents)
17
- return self
18
+ if self.create_file:
19
+ file_create(self.file_path, self.original_contents)
20
+ if self.return_file_path:
21
+ return self.file_path
22
+ else:
23
+ return self
18
24
 
19
25
  def __exit__(self, type, value, traceback):
20
26
  file_delete (self.file_path)
osbot_utils/utils/Env.py CHANGED
@@ -82,7 +82,7 @@ def in_python_debugger():
82
82
  return True
83
83
  return False
84
84
 
85
- def load_dotenv(dotenv_path=None, override=False):
85
+ def load_dotenv(dotenv_path=None, override=False): # todo: add detection when we have already loaded the .env (so that we don't load it again)
86
86
  if dotenv_path: # If a specific dotenv path is provided, load from it
87
87
  env_load_from_file(dotenv_path, override)
88
88
  else:
@@ -211,7 +211,7 @@ def obj_base_classes_names(obj, show_module=False):
211
211
  names.append(base.__name__)
212
212
  return names
213
213
 
214
- def obj_data(target, name_width=30, value_width=100, show_private=False, show_internals=False, show_value_class=False, show_methods=False, only_show_methods=False):
214
+ def obj_data(target, convert_value_to_str=True, name_width=30, value_width=100, show_private=False, show_internals=False, show_value_class=False, show_methods=False, only_show_methods=False):
215
215
  result = {}
216
216
  if show_internals:
217
217
  show_private = True # show_private will skip all internals, so need to make sure it is True
@@ -226,11 +226,12 @@ def obj_data(target, name_width=30, value_width=100, show_private=False, show_in
226
226
  continue
227
227
  if only_show_methods:
228
228
  value = inspect.signature(value)
229
- if value !=None and type(value) not in [bool, int, float]:
230
- value = str(value).encode('unicode_escape').decode("utf-8")
231
- value = str_unicode_escape(value)
232
- value = str_max_width(value, value_width)
233
- name = str_max_width(name, name_width)
229
+ if value is not None and type(value) not in [bool, int, float]:
230
+ if convert_value_to_str:
231
+ value = str(value).encode('unicode_escape').decode("utf-8")
232
+ value = str_unicode_escape(value)
233
+ value = str_max_width(value, value_width)
234
+ name = str_max_width(name, name_width) # todo: look at the side effects of running this for all (at the moment if we do that we break the test_cache_on_self test)
234
235
  result[name] = value
235
236
  return result
236
237
 
osbot_utils/utils/Toml.py CHANGED
@@ -1,5 +1,7 @@
1
1
  import sys
2
2
 
3
+ from osbot_utils.utils.Files import file_create, file_contents
4
+
3
5
  if sys.version_info >= (3, 11):
4
6
  import tomllib
5
7
  else:
@@ -27,7 +29,19 @@ def dict_to_toml(data, indent_level=0):
27
29
 
28
30
  return toml_str
29
31
 
30
- def toml_to_dict(toml_string):
32
+ def toml_dict_to_file(toml_file, data):
33
+ str_toml = dict_to_toml(data)
34
+ return file_create(toml_file, str_toml)
35
+
36
+ def toml_dict_from_file(toml_file):
37
+ str_toml = file_contents(toml_file)
38
+ return toml_to_dict(str_toml)
39
+
40
+
41
+ def toml_to_dict(str_toml):
31
42
  if tomllib is None:
32
43
  raise NotImplementedError("TOML parsing is not supported in Python versions earlier than 3.11")
33
- return tomllib.loads(toml_string)
44
+ return tomllib.loads(str_toml)
45
+
46
+
47
+ toml_file_load = toml_dict_from_file
osbot_utils/version CHANGED
@@ -1 +1 @@
1
- v1.23.0
1
+ v1.24.0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: osbot_utils
3
- Version: 1.23.0
3
+ Version: 1.24.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.23.0-blue)
25
+ ![Current Release](https://img.shields.io/badge/release-v1.24.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
 
@@ -249,7 +249,8 @@ osbot_utils/testing/Profiler.py,sha256=4em6Lpp0ONRDoDDCZsc_CdAOi_QolKOp4eA7KHN96
249
249
  osbot_utils/testing/Pytest.py,sha256=R3qdsIXGcNQcu7iobz0RB8AhbbHhc6t757tZoSZRrxA,730
250
250
  osbot_utils/testing/Stderr.py,sha256=wi1gfjpsxnBK3aOl2jzCTWI-0En1HtPgEin97148_MQ,459
251
251
  osbot_utils/testing/Stdout.py,sha256=XQ9OlOW1aHXY1TiNu8O5b75RoDnoaX5RyMHml3OjlKw,459
252
- osbot_utils/testing/Temp_File.py,sha256=gL1BJ6aRNg2o4LeUJ4f3bxKc75iw1qp0WKP9T3rS_WU,1435
252
+ osbot_utils/testing/Temp_Env_Vars.py,sha256=oFuaegBlaV0aySkPe1Nzf-mdIKN03oTUKNfKijGz__M,751
253
+ osbot_utils/testing/Temp_File.py,sha256=yZBL9MmcNU4PCQ4xlF4rSss4GylKoX3T_AJF-BlQhdI,1693
253
254
  osbot_utils/testing/Temp_Folder.py,sha256=wZQhCi5Cy0dQQAXuu3_jArDz5T94Q_JX68GENU6nTMo,4793
254
255
  osbot_utils/testing/Temp_Sys_Path.py,sha256=gOMD-7dQYQlejoDYUqsrmuZQ9DLC07ymPZB3zYuNmG4,256
255
256
  osbot_utils/testing/Temp_Web_Server.py,sha256=0A-gZsd0_3wRj2YuBEOWyV2rhT6dcS2BlArngPXGTtk,3186
@@ -262,7 +263,7 @@ osbot_utils/utils/Assert.py,sha256=u9XLgYn91QvNWZGyPi29SjPJSXRHlm9andIn3NJEVog,1
262
263
  osbot_utils/utils/Call_Stack.py,sha256=MAq_0vMxnbeLfCe9qQz7GwJYaOuXpt3qtQwN6wiXsU0,6595
263
264
  osbot_utils/utils/Csv.py,sha256=oHLVpjRJqrLMz9lubMCNEoThXWju5rNTprcwHc1zq2c,1012
264
265
  osbot_utils/utils/Dev.py,sha256=HibpQutYy_iG8gGV8g1GztxNN4l29E4Bi7UZaVL6-L8,1203
265
- osbot_utils/utils/Env.py,sha256=pVKv_vJn0CU0DW6rj5hv5cfI2vupQZFptloSiaGjd8Y,4903
266
+ osbot_utils/utils/Env.py,sha256=CrvDcpY9NEuvyvG_QHcv1oSfw8BKXN6MqVEbPF_OqFU,5008
266
267
  osbot_utils/utils/Exceptions.py,sha256=KyOUHkXQ_6jDTq04Xm261dbEZuRidtsM4dgzNwSG8-8,389
267
268
  osbot_utils/utils/Files.py,sha256=q4_JeNWZygM8T0_frpTGvZjtISC1ZyJTOr3bD9pCKUE,19599
268
269
  osbot_utils/utils/Functions.py,sha256=0E6alPJ0fJpBiJgFOWooCOi265wSRyxxXAJ5CELBnso,3498
@@ -272,18 +273,18 @@ osbot_utils/utils/Json.py,sha256=UNaBazuH1R40fsHjpjuK8kmAANmUHoK9Q0PUeYmgPeY,625
272
273
  osbot_utils/utils/Json_Cache.py,sha256=mLPkkDZN-3ZVJiDvV1KBJXILtKkTZ4OepzOsDoBPhWg,2006
273
274
  osbot_utils/utils/Lists.py,sha256=CLEjgZwAixJAFlubWEKjnUUhUN85oqvR7UqExVW7rdY,5502
274
275
  osbot_utils/utils/Misc.py,sha256=ljscBemI5wOhfkl1BVpsqshacTOCKkOisV4er9xPCWM,16640
275
- osbot_utils/utils/Objects.py,sha256=SNtQ1nJnqihwTcmIf_Hg9P3V7fHjxcVHSzC7vKKI34Q,14419
276
+ osbot_utils/utils/Objects.py,sha256=WFH3oeXR1CU03oyzXfcIlktcoXQuKw9cIJn8xTs02AE,14654
276
277
  osbot_utils/utils/Png.py,sha256=V1juGp6wkpPigMJ8HcxrPDIP4bSwu51oNkLI8YqP76Y,1172
277
278
  osbot_utils/utils/Process.py,sha256=lr3CTiEkN3EiBx3ZmzYmTKlQoPdkgZBRjPulMxG-zdo,2357
278
279
  osbot_utils/utils/Python_Logger.py,sha256=7IPB6gMw-G9HWdgyG_k6d7CE71P55-SAudKgDVxraMs,12756
279
280
  osbot_utils/utils/Status.py,sha256=Yq4s0TelXgn0i2QjCP9V8mP30GabXp_UL-jjM6Iwiw4,4305
280
281
  osbot_utils/utils/Str.py,sha256=kxdY8ROX4FdJtCaMTfOc8fK_xcDICprNkefHu2MMNU4,2585
281
- osbot_utils/utils/Toml.py,sha256=-bg9srF7ef31UCNFa0aL3CDeDKp14XeUVwRMQzMb_dI,1074
282
+ osbot_utils/utils/Toml.py,sha256=dqiegndCJF7V1YT1Tc-b0-Bl6QWyL5q30urmQwMXfMQ,1402
282
283
  osbot_utils/utils/Version.py,sha256=Ww6ChwTxqp1QAcxOnztkTicShlcx6fbNsWX5xausHrg,422
283
284
  osbot_utils/utils/Zip.py,sha256=YFahdBguVK71mLdYy4m7mqVAQ5al-60QnTmYK-txCfY,6784
284
285
  osbot_utils/utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
285
- osbot_utils/version,sha256=53VeZ9pdur3uOW0jZ0xgOrWe1NDomr_JWgbBLtChg_g,8
286
- osbot_utils-1.23.0.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
287
- osbot_utils-1.23.0.dist-info/METADATA,sha256=6nl1O4DPerXYoazuL2vLDMJx4FjQNzvFPxKuLuSvImE,1266
288
- osbot_utils-1.23.0.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
289
- osbot_utils-1.23.0.dist-info/RECORD,,
286
+ osbot_utils/version,sha256=ZD3YCx9CcqVfYvN3EZrH5YNt6_M4up_qb295gQk6gwg,8
287
+ osbot_utils-1.24.0.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
288
+ osbot_utils-1.24.0.dist-info/METADATA,sha256=E-QECitRSsPhAR9aTUqtH7_7eM2y2PIG3ezGzStkr2Q,1266
289
+ osbot_utils-1.24.0.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
290
+ osbot_utils-1.24.0.dist-info/RECORD,,