From e22f1bfb59f7ea7b719178e5930efaabe8709fc9 Mon Sep 17 00:00:00 2001 From: Romain Dorgueil Date: Fri, 19 May 2017 18:35:39 +0200 Subject: [PATCH] Settings for debug/profile and use them in console plugin. --- bonobo/ext/console/plugin.py | 19 ++++++++++--------- bonobo/settings.py | 16 ++++++++++++++++ 2 files changed, 26 insertions(+), 9 deletions(-) create mode 100644 bonobo/settings.py diff --git a/bonobo/ext/console/plugin.py b/bonobo/ext/console/plugin.py index 8884a73..8fd8cdf 100644 --- a/bonobo/ext/console/plugin.py +++ b/bonobo/ext/console/plugin.py @@ -19,6 +19,7 @@ import sys from colorama import Fore, Style +from bonobo import settings from bonobo.plugins import Plugin from bonobo.util.term import CLEAR_EOL, MOVE_CURSOR_UP @@ -27,7 +28,7 @@ from bonobo.util.term import CLEAR_EOL, MOVE_CURSOR_UP def memory_usage(): import os, psutil process = psutil.Process(os.getpid()) - return process.get_memory_info()[0] / float(2**20) + return process.memory_info()[0] / float(2**20) # @lru_cache(64) @@ -50,15 +51,14 @@ class ConsoleOutputPlugin(Plugin): self.prefix = '' def _write(self, graph_context, rewind): - profile, debug = False, False - if profile: + if settings.PROFILE: append = ( ('Memory', '{0:.2f} Mb'.format(memory_usage())), # ('Total time', '{0} s'.format(execution_time(harness))), ) else: append = () - self.write(graph_context, prefix=self.prefix, append=append, debug=debug, profile=profile, rewind=rewind) + self.write(graph_context, prefix=self.prefix, append=append, rewind=rewind) def run(self): if sys.stdout.isatty(): @@ -70,23 +70,24 @@ class ConsoleOutputPlugin(Plugin): self._write(self.context.parent, rewind=False) @staticmethod - def write(context, prefix='', rewind=True, append=None, debug=False, profile=False): + def write(context, prefix='', rewind=True, append=None): t_cnt = len(context) for i in context.graph.topologically_sorted_indexes: node = context[i] + name_suffix = '({})'.format(i) if settings.DEBUG else '' if node.alive: _line = ''.join( ( - ' ', Style.BRIGHT, '+', Style.RESET_ALL, ' ', node.name, '(', str(i), ') ', - node.get_statistics_as_string(debug=debug, profile=profile), Style.RESET_ALL, ' ', + ' ', Style.BRIGHT, '+', Style.RESET_ALL, ' ', node.name, name_suffix, ' ', + node.get_statistics_as_string(), Style.RESET_ALL, ' ', ) ) else: _line = ''.join( ( - ' ', Fore.BLACK, '-', ' ', node.name, '(', str(i), ') ', - node.get_statistics_as_string(debug=debug, profile=profile), Style.RESET_ALL, ' ', + ' ', Fore.BLACK, '-', ' ', node.name, name_suffix, ' ', node.get_statistics_as_string(), + Style.RESET_ALL, ' ', ) ) print(prefix + _line + '\033[0K') diff --git a/bonobo/settings.py b/bonobo/settings.py new file mode 100644 index 0000000..aec203d --- /dev/null +++ b/bonobo/settings.py @@ -0,0 +1,16 @@ +import os + + +def to_bool(s): + if len(s): + if s.lower() in ('f', 'false', 'n', 'no', '0'): + return False + return True + return False + + +# Debug mode. +DEBUG = to_bool(os.environ.get('BONOBO_DEBUG', 'f')) + +# Profile mode. +PROFILE = to_bool(os.environ.get('BONOBO_PROFILE', 'f'))