From 7f715be7bf9f2b3ae3def815c09edc8b94f886da Mon Sep 17 00:00:00 2001 From: nicobo Date: Mon, 25 Jan 2021 23:25:07 +0100 Subject: [PATCH] ~ Fixes #49 (JSON status printed twice with Python package/docker) : explicit sys.exit(0) is now made ~ default verbosity set to WARNING as it's the convention --- nicobot/askbot.py | 18 +++++++++--------- nicobot/bot.py | 2 +- nicobot/transbot.py | 14 ++++++++------ 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/nicobot/askbot.py b/nicobot/askbot.py index ef12757..f3d14de 100755 --- a/nicobot/askbot.py +++ b/nicobot/askbot.py @@ -42,7 +42,7 @@ class Config: 'patterns': [], 'stealth': False, 'timeout': None, - 'verbosity': "INFO", + 'verbosity': "WARNING", }) @@ -84,7 +84,7 @@ class AskBot(Bot): self.status['events'].append(status_message) self.responses_count = self.responses_count + 1 - logging.debug("<<< %s", message) + logging.info("<<< %s", message) # If we reached the last message or if we exceeded it (possible if we received several answers in a batch) if self.max_count>0 and self.responses_count >= self.max_count: @@ -137,8 +137,6 @@ def run( args=sys.argv[1:] ): """ A convenient CLI to play with this bot. - - TODO Put generic arguments in bot.py and inherit from it (should probably provide a parent ArgumentParser) """ # config will be the final, merged configuration @@ -188,13 +186,15 @@ def run( args=sys.argv[1:] ): status_args[k] = '(obfuscated)' status_result = bot.run() status = { 'args':vars(config), 'result':status_result } - # NOTE ensure_ascii=False + encode('utf-8').decode() is not mandatory but allows printing plain UTF-8 strings rather than \u... or \x... - # NOTE default=repr is mandatory because some objects in the args are not serializable - print( json.dumps(status,skipkeys=True,ensure_ascii=False,default=repr).encode('utf-8').decode(), file=sys.stdout, flush=True ) - # Still returns the full status for simpler handling in Python + # Returns the full status to this module can be called CLI-style return status +# Like run(), but also prints the final status to stdout if __name__ == '__main__': - run() + status = run(sys.argv[1:]) + # NOTE ensure_ascii=False + encode('utf-8').decode() is not mandatory but allows printing plain UTF-8 strings rather than \u... or \x... + # NOTE default=repr is mandatory because some objects in the args are not serializable + print( json.dumps(status,skipkeys=True,ensure_ascii=False,default=repr).encode('utf-8').decode(), file=sys.stdout, flush=True ) + sys.exit(0) diff --git a/nicobot/bot.py b/nicobot/bot.py index 5def7de..0c8bb43 100644 --- a/nicobot/bot.py +++ b/nicobot/bot.py @@ -77,7 +77,7 @@ class ArgsHelper: 'config_dir': os.getcwd(), 'input_file': sys.stdin, 'stealth': False, - 'verbosity': "INFO", + 'verbosity': "WARNING", }) diff --git a/nicobot/transbot.py b/nicobot/transbot.py index 91ecd29..da48e0a 100755 --- a/nicobot/transbot.py +++ b/nicobot/transbot.py @@ -74,7 +74,7 @@ class Config: 'signal_stealth': False, 'stealth': False, 'username': None, - 'verbosity': "INFO" + 'verbosity': "WARNING" }) @@ -691,13 +691,15 @@ def run( args=sys.argv[1:] ): status_args[k] = '(obfuscated)' status_result = bot.run() status = { 'args':vars(config), 'result':status_result } - # NOTE ensure_ascii=False + encode('utf-8').decode() is not mandatory but allows printing plain UTF-8 strings rather than \u... or \x... - # NOTE default=repr is mandatory because some objects in the args are not serializable - print( json.dumps(status,skipkeys=True,ensure_ascii=False,default=repr).encode('utf-8').decode(), file=sys.stdout, flush=True ) - # Still returns the full status for simpler handling in Python + # Returns the full status to this module can be called CLI-style return status +# Like run(), but also prints the final status to stdout if __name__ == '__main__': - run() + status = run(sys.argv[1:]) + # NOTE ensure_ascii=False + encode('utf-8').decode() is not mandatory but allows printing plain UTF-8 strings rather than \u... or \x... + # NOTE default=repr is mandatory because some objects in the args are not serializable + print( json.dumps(status,skipkeys=True,ensure_ascii=False,default=repr).encode('utf-8').decode(), file=sys.stdout, flush=True ) + sys.exit(0)