osbot-utils 1.42.0__py3-none-any.whl → 1.44.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,27 @@
1
+ import logging
2
+ from contextlib import contextmanager
3
+ import asyncio
4
+
5
+ @contextmanager
6
+ def async_invoke():
7
+ logger = logging.getLogger('asyncio')
8
+ level_original = logger.level
9
+ logger.level = logging.INFO # this will suppress the asyncio debug messages which where showing in tests
10
+ try:
11
+ original_loop = asyncio.get_event_loop()
12
+ except RuntimeError:
13
+ original_loop = None
14
+
15
+ loop = asyncio.new_event_loop()
16
+ asyncio.set_event_loop(loop)
17
+
18
+ try:
19
+ yield loop.run_until_complete
20
+ finally:
21
+ loop.close()
22
+ if original_loop is not None:
23
+ asyncio.set_event_loop(original_loop)
24
+ else:
25
+ asyncio.set_event_loop(None)
26
+
27
+ logger.level = level_original # restore the original log level
@@ -1,26 +1,28 @@
1
1
  import time
2
- from enum import Enum
3
- from queue import Queue, Empty
4
- from threading import Thread
5
- from typing import Any
6
-
7
- from osbot_utils.helpers.pubsub.schemas.Schema__Event import Schema__Event
8
- from osbot_utils.helpers.pubsub.schemas.Schema__Event__Message import Schema__Event__Message
9
- from osbot_utils.utils import Misc
10
- from osbot_utils.base_classes.Kwargs_To_Self import Kwargs_To_Self
11
- from osbot_utils.utils.Misc import random_text, wait_for, timestamp_utc_now, random_guid
12
-
13
- QUEUE_WAIT_TIMEOUT = 1.0 # todo: see if this value is a good one to use here
14
-
15
- class Event__Queue(Kwargs_To_Self):
16
- events : list
17
- event_class : type
18
- log_events : bool = False
19
- queue : Queue
20
- queue_name : str = random_text('event_queue')
21
- queue_timeout: float = QUEUE_WAIT_TIMEOUT
22
- running : bool
23
- thread : Thread = None
2
+ from queue import Queue, Empty
3
+ from threading import Thread
4
+ from osbot_utils.base_classes.Type_Safe import Type_Safe
5
+ from osbot_utils.helpers.pubsub.schemas.Schema__Event import Schema__Event
6
+ from osbot_utils.helpers.pubsub.schemas.Schema__Event__Message import Schema__Event__Message
7
+ from osbot_utils.utils.Misc import random_text, timestamp_utc_now, random_guid
8
+
9
+ TIMEOUT__THREAD_JOIN = 1.0 # todo: see if this value is a good one to use here
10
+ TIMEOUT__QUEUE_GET = 1.0
11
+ TIMEOUT__WAIT_FOR_QUEUE_COMPLETED = 0.05 # todo: see if this value is too aggressive (or if will be better to use a value like 0.1 or 0.5)
12
+
13
+
14
+ class Event__Queue(Type_Safe):
15
+ events : list
16
+ event_class : type
17
+ events_added : int
18
+ events_completed : int
19
+ events_failed : int
20
+ log_events : bool = False
21
+ queue : Queue
22
+ queue_name : str = random_text('event_queue')
23
+ queue_get_timeout : float = TIMEOUT__QUEUE_GET
24
+ running : bool
25
+ thread : Thread = None
24
26
 
25
27
 
26
28
  def __init__(self, **kwargs):
@@ -49,6 +51,7 @@ class Event__Queue(Kwargs_To_Self):
49
51
  event.timestamp = timestamp_utc_now()
50
52
  if not event.event_id:
51
53
  event.event_id = random_guid()
54
+ self.events_added += 1
52
55
  self.queue.put(event)
53
56
  return True
54
57
  return False
@@ -72,18 +75,23 @@ class Event__Queue(Kwargs_To_Self):
72
75
  return self
73
76
 
74
77
  def stop(self):
75
- self.running = False
78
+ self.running = False # will make the event loop stop at the next self.queue_get_timeout
76
79
  return self
77
80
 
81
+ def queue_size(self):
82
+ return self.queue.qsize()
83
+
78
84
  def run_thread(self):
79
85
  while self.running:
80
86
  try:
81
- event = self.queue.get(timeout=self.queue_timeout)
87
+ event = self.queue.get(timeout=self.queue_get_timeout)
82
88
  if isinstance(event, self.event_class):
83
89
  self.handle_event(event)
90
+ self.events_completed += 1
84
91
  except Empty:
85
92
  continue
86
93
  except Exception as e: # todo: add way to handle this (which are errors in the handle_event), may call an on_event_handler_exceptions method
94
+ self.events_failed += 1
87
95
  continue
88
96
 
89
97
  def wait_micro_seconds(self, value=10):
@@ -92,4 +100,28 @@ class Event__Queue(Kwargs_To_Self):
92
100
 
93
101
  def wait_for_thread_ends(self):
94
102
  self.thread.join()
95
- return self
103
+ return self
104
+
105
+ # todo see if there are valid use cases for wait_for_queue_empty , or the wait_for_queue_completed is the one that is always used
106
+ def wait_for_queue_empty(self, thread_join_timeout=TIMEOUT__THREAD_JOIN): # this will start a new thread to wait for the main queue to be empty
107
+ def wait_until_queue_empty():
108
+ while self.running:
109
+ if self.queue.empty():
110
+ break
111
+ time.sleep(0.01)
112
+
113
+ wait_thread = Thread(target=wait_until_queue_empty)
114
+ wait_thread.start()
115
+ wait_thread.join(timeout=thread_join_timeout)
116
+ return self.queue.empty()
117
+
118
+ def wait_for_queue_completed(self, thread_join_timeout=TIMEOUT__THREAD_JOIN): # this will start a new thread to wait for the main queue to be empty
119
+ def wait_until_queue_completed():
120
+ while self.running:
121
+ if self.queue.empty() and self.events_added == self.events_completed :
122
+ break
123
+ time.sleep(TIMEOUT__WAIT_FOR_QUEUE_COMPLETED)
124
+ wait_thread = Thread(target=wait_until_queue_completed)
125
+ wait_thread.start()
126
+ wait_thread.join(timeout=thread_join_timeout)
127
+ return self.queue.empty()
@@ -1,7 +1,4 @@
1
- from osbot_utils.base_classes.Kwargs_To_Self import Kwargs_To_Self
2
1
  from osbot_utils.helpers.pubsub.schemas.Schema__Event import Schema__Event
3
- from osbot_utils.utils.Misc import random_guid
4
-
5
2
 
6
3
  class Schema__Event__Disconnect(Schema__Event):
7
4
  event_type : str = 'disconnect'
@@ -0,0 +1,14 @@
1
+ import types
2
+ from osbot_utils.helpers.pubsub.schemas.Schema__Event import Schema__Event
3
+
4
+ class Schema__Event__Execute_Method(Schema__Event):
5
+ event_type : str = 'execute-method'
6
+ execution_result : object = None
7
+ method_target : types.MethodType
8
+ method_args : list
9
+ method_kwargs : dict
10
+
11
+
12
+ def execute(self):
13
+ self.execution_result = self.method_target(*self.method_args, **self.method_kwargs)
14
+ return self.execution_result
@@ -1,5 +1,4 @@
1
- from osbot_utils.utils.Dev import pprint
2
-
1
+ from osbot_utils.utils.Dev import pprint
3
2
  from osbot_utils.base_classes.Kwargs_To_Self import Kwargs_To_Self
4
3
 
5
4
  PRINT_MAX_STRING_LENGTH = 100
@@ -7,14 +6,12 @@ PRINT_PADDING__DURATION = 100
7
6
  PRINT_PADDING_PARENT_INFO = 60
8
7
 
9
8
  class Trace_Call__Config(Kwargs_To_Self):
10
- title : str
11
9
  capture_locals : bool = False
12
10
  capture_duration : bool
13
11
  capture_extra_data : bool
14
12
  capture_frame : bool = True
15
13
  capture_frame_stats : bool
16
14
  deep_copy_locals : bool
17
- trace_capture_lines : bool
18
15
  ignore_start_with : list
19
16
  print_padding_duration : int = PRINT_PADDING__DURATION
20
17
  print_padding_parent_info : int = PRINT_PADDING_PARENT_INFO
@@ -27,6 +24,7 @@ class Trace_Call__Config(Kwargs_To_Self):
27
24
  show_caller : bool
28
25
  show_method_class : bool = True
29
26
  show_source_code_path : bool
27
+ title : str
30
28
  trace_capture_all : bool
31
29
  trace_capture_source_code : bool
32
30
  trace_capture_start_with : list
@@ -34,6 +32,7 @@ class Trace_Call__Config(Kwargs_To_Self):
34
32
  trace_enabled : bool = True
35
33
  trace_ignore_start_with : list
36
34
  trace_ignore_contains : list
35
+ trace_capture_lines : bool
37
36
  trace_show_internals : bool
38
37
  trace_up_to_depth : int
39
38
  with_duration_bigger_than : float
@@ -1,6 +1,6 @@
1
1
  import os
2
-
3
2
  from osbot_utils.base_classes.Type_Safe import Type_Safe
3
+ from osbot_utils.utils.Env import del_env, set_env
4
4
 
5
5
 
6
6
  class Temp_Env_Vars(Type_Safe):
@@ -16,12 +16,12 @@ class Temp_Env_Vars(Type_Safe):
16
16
  def set_vars(self):
17
17
  for key, value in self.env_vars.items():
18
18
  self.original_env_vars[key] = os.environ.get(key) # Backup original environment variables and set new ones
19
- os.environ[key] = value
19
+ set_env(key, value)
20
20
  return self
21
21
 
22
22
  def restore_vars(self):
23
23
  for key in self.env_vars: # Restore original environment variables
24
24
  if self.original_env_vars[key] is None:
25
- del os.environ[key]
25
+ del_env(key)
26
26
  else:
27
- os.environ[key] = self.original_env_vars[key]
27
+ set_env(key, self.original_env_vars[key])
@@ -0,0 +1,27 @@
1
+ import asyncio
2
+ import logging
3
+ import typing
4
+
5
+ def invoke_async_function(target: typing.Coroutine):
6
+ """Run an asynchronous coroutine in a new event loop."""
7
+ logger = logging.getLogger('asyncio')
8
+ level_original = logger.level
9
+ logger.level = logging.INFO # this will suppress the asyncio debug messages which where showing in tests
10
+ try:
11
+ original_loop = asyncio.get_event_loop()
12
+ except RuntimeError:
13
+ original_loop = None # No event loop was set
14
+
15
+ loop = asyncio.new_event_loop()
16
+ asyncio.set_event_loop(loop)
17
+ try:
18
+ return loop.run_until_complete(target)
19
+ finally:
20
+ loop.close()
21
+ # Restore the original event loop
22
+ if original_loop is not None:
23
+ asyncio.set_event_loop(original_loop)
24
+ else:
25
+ asyncio.set_event_loop(None)
26
+
27
+ logger.level = level_original # restore the original log level
osbot_utils/version CHANGED
@@ -1 +1 @@
1
- v1.42.0
1
+ v1.44.0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: osbot_utils
3
- Version: 1.42.0
3
+ Version: 1.44.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.42.0-blue)
25
+ ![Current Release](https://img.shields.io/badge/release-v1.44.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
 
@@ -6,6 +6,7 @@ osbot_utils/base_classes/Type_Safe.py,sha256=8uOxl1e12vf25MoBBW-hcbqfEpo1ih_Is_9
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
9
+ osbot_utils/context_managers/async_invoke.py,sha256=-ja3K8orLy8Of54CIYSK-zn443pOIDY2hnFBjVELrXc,829
9
10
  osbot_utils/context_managers/capture_duration.py,sha256=sdWij3UJzDf1gDCogCO9RDg6Vijh1bZeeLSi3QmZeYs,1345
10
11
  osbot_utils/context_managers/disable_root_loggers.py,sha256=XDMbKATcRbmUQSMKaOhk68DANVdhbBvDxj15iXjIkw0,1056
11
12
  osbot_utils/decorators/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
@@ -169,7 +170,7 @@ osbot_utils/helpers/html/Tag__Html.py,sha256=W_QFUF27vpR109g9rpca0zoQis1wkMjGuAt
169
170
  osbot_utils/helpers/html/Tag__Link.py,sha256=rQ-gZN8EkSv5x1S-smdjvFflwMQHACHQXiOdx0MFke8,437
170
171
  osbot_utils/helpers/html/Tag__Style.py,sha256=LPPlIN7GyMvfCUlbs2eXVMUr9jS0PX5M94A5Ig_jXIs,846
171
172
  osbot_utils/helpers/html/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
172
- osbot_utils/helpers/pubsub/Event__Queue.py,sha256=51QO-3bnyrzYa7cMOk5rZP1Pwk1Wne_HtqeQ0oFMHl0,3187
173
+ osbot_utils/helpers/pubsub/Event__Queue.py,sha256=H4Hhmj_G3GjTcPFj6y1nVFt64vCwGRVdgta0T0b_a4E,5041
173
174
  osbot_utils/helpers/pubsub/PubSub__Client.py,sha256=6K3l4H-Tc0DhktrxpYzLVur1uZ532pQsHWprLNRXFJE,2316
174
175
  osbot_utils/helpers/pubsub/PubSub__Room.py,sha256=3drJIAVSAzXB_0Q9dXxwhYWxNaEUaMiWiXLY9Mh02DM,481
175
176
  osbot_utils/helpers/pubsub/PubSub__Server.py,sha256=DjQ6PsNGmULdFodspdNktADcoIN3FbdvAitE52m89-w,3548
@@ -177,7 +178,8 @@ osbot_utils/helpers/pubsub/PubSub__Sqlite.py,sha256=3u1Gbpq22JDgDEexdyMzGvkQAHe9
177
178
  osbot_utils/helpers/pubsub/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
178
179
  osbot_utils/helpers/pubsub/schemas/Schema__Event.py,sha256=si12mqRRROI3HYGvjQvzPZ_NGw3TpmVZxGhzuWq5cus,402
179
180
  osbot_utils/helpers/pubsub/schemas/Schema__Event__Connect.py,sha256=fgq0G3_zx3UFk5knsotmEXvP1YvZwOLBtT9gow1U-qM,269
180
- osbot_utils/helpers/pubsub/schemas/Schema__Event__Disconnect.py,sha256=HX52d5koQkbM_Cwb6-hM9-lN7HkYQQ_JGW0fb1s8S10,275
181
+ osbot_utils/helpers/pubsub/schemas/Schema__Event__Disconnect.py,sha256=NUP-3skJyCRUqCDt2YSh5gQ0NIVMGL3huUnVp0hNQ-Q,160
182
+ osbot_utils/helpers/pubsub/schemas/Schema__Event__Execute_Method.py,sha256=srVEFo0bKRhEslsdr8x3hqXVDzx9_pBxD4l2zxbsvIQ,502
181
183
  osbot_utils/helpers/pubsub/schemas/Schema__Event__Join_Room.py,sha256=OJmBuaA5AKN6HFfaQDlN2dWG8xWAmExr-k3DyzL1JWg,293
182
184
  osbot_utils/helpers/pubsub/schemas/Schema__Event__Leave_Room.py,sha256=cNugRz-k_Jmb3z5899CgvN4CeQZh0NF7FqMBfvoToSE,295
183
185
  osbot_utils/helpers/pubsub/schemas/Schema__Event__Message.py,sha256=rt8W-DGitmR-SvmunSG8kbTH_mubE2PKMh2cJ3eJOyo,244
@@ -231,7 +233,7 @@ osbot_utils/helpers/ssh/SSH__Python.py,sha256=O2DAwkbXzwkis8lffoqIL2NPSfYcN44Mr8
231
233
  osbot_utils/helpers/ssh/TestCase__SSH.py,sha256=MD8sq0_kI4f6pEmEO0cLq2mQOhIqbP45ZxFJNG44Jg4,1773
232
234
  osbot_utils/helpers/ssh/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
233
235
  osbot_utils/helpers/trace/Trace_Call.py,sha256=SNaYju1KA6UKaCZcaDoPadvSahdl0vOX27unGl0-ZEM,8825
234
- osbot_utils/helpers/trace/Trace_Call__Config.py,sha256=bZyOIWs_4ufNVbpfiT8h_284JFHiFeBAJO15Q5ONaTg,3288
236
+ osbot_utils/helpers/trace/Trace_Call__Config.py,sha256=qd6HpF1olvlQ9BU3-gvxRdnP0l7w7noCXpF561gkyUk,3305
235
237
  osbot_utils/helpers/trace/Trace_Call__Graph.py,sha256=HCrXRKQI42DIQxxyFLcaosWiOcUyoITbeV17ICdXcXM,1156
236
238
  osbot_utils/helpers/trace/Trace_Call__Handler.py,sha256=hdgiawoC4K2DrMAHbz2706SfEcxPdX9kK0gjyWqjSzQ,12400
237
239
  osbot_utils/helpers/trace/Trace_Call__Print_Lines.py,sha256=cy7zLv0_JNxdOIQPfZk6J9bv6AkIW6O643w0ykClXbw,4820
@@ -253,7 +255,7 @@ osbot_utils/testing/Profiler.py,sha256=4em6Lpp0ONRDoDDCZsc_CdAOi_QolKOp4eA7KHN96
253
255
  osbot_utils/testing/Pytest.py,sha256=R3qdsIXGcNQcu7iobz0RB8AhbbHhc6t757tZoSZRrxA,730
254
256
  osbot_utils/testing/Stderr.py,sha256=ynf0Wle9NvgneLChzAxFBQ0QlE5sbri_fzJ8bEJMNkc,718
255
257
  osbot_utils/testing/Stdout.py,sha256=Gmxd_dOplXlucdSbOhYhka9sWP-Hmqb7ZuLs_JjtW7Y,592
256
- osbot_utils/testing/Temp_Env_Vars.py,sha256=CE_lk54QcibkQVtIj2DrQPS3k2LgQZUctU5b-WwWvbo,884
258
+ osbot_utils/testing/Temp_Env_Vars.py,sha256=mQsc_ogtx2TA_20Y5zPusUZGvk_EZv4PMZjAXElbtFw,933
257
259
  osbot_utils/testing/Temp_File.py,sha256=yZBL9MmcNU4PCQ4xlF4rSss4GylKoX3T_AJF-BlQhdI,1693
258
260
  osbot_utils/testing/Temp_Folder.py,sha256=Dbcohr2ciex6w-kB79R41Nuoa0pgpDbKtPGnlMmJ73k,5194
259
261
  osbot_utils/testing/Temp_Sys_Path.py,sha256=gOMD-7dQYQlejoDYUqsrmuZQ9DLC07ymPZB3zYuNmG4,256
@@ -284,12 +286,13 @@ osbot_utils/utils/Python_Logger.py,sha256=tx8N6wRKL3RDHboDRKZn8SirSJdSAE9cACyJkx
284
286
  osbot_utils/utils/Regex.py,sha256=0ubgp8HKsS3PNe2H6XlzMIcUuV7jhga3VkQVDNOJWuA,866
285
287
  osbot_utils/utils/Status.py,sha256=Yq4s0TelXgn0i2QjCP9V8mP30GabXp_UL-jjM6Iwiw4,4305
286
288
  osbot_utils/utils/Str.py,sha256=kxdY8ROX4FdJtCaMTfOc8fK_xcDICprNkefHu2MMNU4,2585
289
+ osbot_utils/utils/Threads.py,sha256=fgFrZZXY2G9yJmfQcOv76GB_8MhCrvoUdcbkAhYxDBI,909
287
290
  osbot_utils/utils/Toml.py,sha256=dqiegndCJF7V1YT1Tc-b0-Bl6QWyL5q30urmQwMXfMQ,1402
288
291
  osbot_utils/utils/Version.py,sha256=Ww6ChwTxqp1QAcxOnztkTicShlcx6fbNsWX5xausHrg,422
289
292
  osbot_utils/utils/Zip.py,sha256=Gt4K7Q9LlYeRxOVv8aYpaBLKxrYriCAEUa_R6YWEbMg,13903
290
293
  osbot_utils/utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
291
- osbot_utils/version,sha256=XEDmF2gY0SXKEjwvdngDk1U0o2yRWMAxnldMBdQDSOM,8
292
- osbot_utils-1.42.0.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
293
- osbot_utils-1.42.0.dist-info/METADATA,sha256=A5I-YaoHJocPTRXNoGBep9eP3FhVTSndWFE9TcAGCLE,1266
294
- osbot_utils-1.42.0.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
295
- osbot_utils-1.42.0.dist-info/RECORD,,
294
+ osbot_utils/version,sha256=kYAcIo_a_mU0Bi3gNZH2ByIptBxKn1unKhuICxW0kUo,8
295
+ osbot_utils-1.44.0.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
296
+ osbot_utils-1.44.0.dist-info/METADATA,sha256=BFGhr7opMQ7FIyJgQT_bpYO6vYmXIfSEz_VeBLLpRMc,1266
297
+ osbot_utils-1.44.0.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
298
+ osbot_utils-1.44.0.dist-info/RECORD,,