2020import sys
2121from importlib import import_module
2222from os .path import exists
23+ from typing import Optional , Callable
24+ from unittest import mock
2325
2426import time
2527
@@ -35,7 +37,6 @@ class BotDebugger:
3537 EXAMPLE = """\n The message may look like:
3638 '{"source.network": "178.72.192.0/18", "time.observation": "2017-05-12T05:23:06+00:00"}' """
3739
38- load_configuration = utils .load_configuration
3940 logging_level = None
4041 output = []
4142 instance = None
@@ -65,13 +66,15 @@ def __init__(self, runtime_configuration, bot_id, run_subcommand=None, console_t
6566 # Set's the bot's default and initial value for the logging_level to the value we want
6667 bot .logging_level = self .logging_level
6768
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+ )
7173
7274 def run (self ) -> str :
7375 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 ()
7578 else :
7679 self .instance ._Bot__connect_pipelines ()
7780 if self .run_subcommand == "console" :
@@ -181,33 +184,33 @@ def arg2msg(self, msg):
181184 return msg
182185
183186 def leverageLogger (self , level ):
184- utils .load_configuration = BotDebugger .load_configuration_patch
185187 self .logging_level = level
186188 if self .instance :
187189 self .instance .logger .setLevel (level )
188190 for h in self .instance .logger .handlers :
189191 if isinstance (h , StreamHandler ):
190192 h .setLevel (level )
191193
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 :
194195 """
195196 Mock function for utils.load_configuration which ensures the logging level parameter is set to the value we want.
196197 If Runtime configuration is detected, the logging_level parameter is
197198 - inserted in all bot's parameters. bot_id is not accessible here, hence we add it everywhere
198199 - inserted in the global parameters (ex-defaults).
199200 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.
200201 """
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
211214
212215 def messageWizzard (self , msg ):
213216 self .instance .logger .error (msg )
0 commit comments