20
20
import sys
21
21
from importlib import import_module
22
22
from os .path import exists
23
+ from typing import Optional , Callable
24
+ from unittest import mock
23
25
24
26
import time
25
27
@@ -35,7 +37,6 @@ class BotDebugger:
35
37
EXAMPLE = """\n The message may look like:
36
38
'{"source.network": "178.72.192.0/18", "time.observation": "2017-05-12T05:23:06+00:00"}' """
37
39
38
- load_configuration = utils .load_configuration
39
40
logging_level = None
40
41
output = []
41
42
instance = None
@@ -65,13 +66,15 @@ def __init__(self, runtime_configuration, bot_id, run_subcommand=None, console_t
65
66
# Set's the bot's default and initial value for the logging_level to the value we want
66
67
bot .logging_level = self .logging_level
67
68
68
- self .instance = bot (bot_id , disable_multithreading = True ,
69
- standalone = True , # instruct the bot to call SystemExit exception at the end or in case of errors
70
- )
69
+ with mock .patch .object (utils , 'get_runtime' , self .generate_get_runtime (self .logging_level )):
70
+ self .instance = bot (bot_id , disable_multithreading = True ,
71
+ standalone = True , # instruct the bot to call SystemExit exception at the end or in case of errors
72
+ )
71
73
72
74
def run (self ) -> str :
73
75
if not self .run_subcommand :
74
- self .instance .start ()
76
+ with mock .patch .object (utils , 'get_runtime' , self .generate_get_runtime (self .logging_level )):
77
+ self .instance .start ()
75
78
else :
76
79
self .instance ._Bot__connect_pipelines ()
77
80
if self .run_subcommand == "console" :
@@ -181,33 +184,33 @@ def arg2msg(self, msg):
181
184
return msg
182
185
183
186
def leverageLogger (self , level ):
184
- utils .load_configuration = BotDebugger .load_configuration_patch
185
187
self .logging_level = level
186
188
if self .instance :
187
189
self .instance .logger .setLevel (level )
188
190
for h in self .instance .logger .handlers :
189
191
if isinstance (h , StreamHandler ):
190
192
h .setLevel (level )
191
193
192
- @staticmethod
193
- def load_configuration_patch (configuration_filepath : str , * args , ** kwargs ) -> dict :
194
+ def generate_get_runtime (self , logging_level : Optional [str ] = None ) -> Callable :
194
195
"""
195
196
Mock function for utils.load_configuration which ensures the logging level parameter is set to the value we want.
196
197
If Runtime configuration is detected, the logging_level parameter is
197
198
- inserted in all bot's parameters. bot_id is not accessible here, hence we add it everywhere
198
199
- inserted in the global parameters (ex-defaults).
199
200
Maybe not everything is necessary, but we can make sure the logging_level is just everywhere where it might be relevant, also in the future.
200
201
"""
201
- config = BotDebugger .load_configuration (configuration_filepath = configuration_filepath , * args , ** kwargs )
202
- if BotDebugger .logging_level and configuration_filepath == RUNTIME_CONF_FILE :
203
- for bot_id in config .keys ():
204
- if bot_id == "global" :
205
- config [bot_id ]["logging_level" ] = BotDebugger .logging_level
206
- else :
207
- config [bot_id ]['parameters' ]["logging_level" ] = BotDebugger .logging_level
208
- if "global" not in config :
209
- config ["global" ] = {"logging_level" : BotDebugger .logging_level }
210
- return config
202
+ def new_get_runtime (* args , ** kwargs ):
203
+ config = utils .load_configuration (RUNTIME_CONF_FILE , * args , ** kwargs )
204
+ if logging_level :
205
+ for bot_id in config .keys ():
206
+ if bot_id == "global" :
207
+ config [bot_id ]["logging_level" ] = logging_level
208
+ else :
209
+ config [bot_id ]['parameters' ]["logging_level" ] = logging_level
210
+ if "global" not in config :
211
+ config ["global" ] = {"logging_level" : logging_level }
212
+ return config
213
+ return new_get_runtime
211
214
212
215
def messageWizzard (self , msg ):
213
216
self .instance .logger .error (msg )
0 commit comments