From 90c7ab8f28bd0805ba2f199da4e4a612b507d656 Mon Sep 17 00:00:00 2001 From: Oleg Strizhechenko Date: Wed, 18 May 2022 19:31:25 +0500 Subject: [PATCH] Refactoring: color usage is strictly follows --no-color flag now. --- netutils_linux_monitoring/base_top.py | 5 ++- netutils_linux_monitoring/colors.py | 49 ++++++++++------------- netutils_linux_monitoring/irqtop.py | 5 +-- netutils_linux_monitoring/link_rate.py | 7 ++-- netutils_linux_monitoring/network_top.py | 5 ++- netutils_linux_monitoring/softirqs.py | 10 ++--- netutils_linux_monitoring/softnet_stat.py | 28 ++----------- netutils_linux_tuning/rss_ladder.py | 2 +- 8 files changed, 42 insertions(+), 69 deletions(-) diff --git a/netutils_linux_monitoring/base_top.py b/netutils_linux_monitoring/base_top.py index ee0ae16..805218e 100644 --- a/netutils_linux_monitoring/base_top.py +++ b/netutils_linux_monitoring/base_top.py @@ -16,7 +16,7 @@ class BaseTop(object): current = None previous = None diff = None - header = Color.wrap('Press CTRL-C to exit...\n', Color.GREY) + header = None options = None file_arg = None file_value = None @@ -113,7 +113,7 @@ def spaces(self, number, sep=' '): def __repr_table__(self, table): if self.options.clear: - return BaseTop.header + str(table) + return self.header + str(table) return str(table) def default_init(self, topology=None): @@ -125,6 +125,7 @@ def default_post_optparse(self): if not self.topology: self.topology = Topology(fake=self.options.random) self.color = Color(self.topology, self.options.color) + self.header = self.color.wrap('Press CTRL-C to exit...\n', self.color.GREY) @abstractmethod def parse(self): diff --git a/netutils_linux_monitoring/colors.py b/netutils_linux_monitoring/colors.py index b0d8999..e40a809 100644 --- a/netutils_linux_monitoring/colors.py +++ b/netutils_linux_monitoring/colors.py @@ -6,23 +6,23 @@ class Color(object): """ Helper for highlighting a console tools """ try: - YELLOW = Fore.LIGHTYELLOW_EX - GREY = Fore.LIGHTBLACK_EX + CLS_YELLOW = Fore.LIGHTYELLOW_EX + CLS_GREY = Fore.LIGHTBLACK_EX except AttributeError: - YELLOW = Fore.YELLOW - GREY = Fore.CYAN + CLS_YELLOW = Fore.YELLOW + CLS_GREY = Fore.CYAN COLORS_NODE = { 0: Fore.GREEN, 1: Fore.RED, - 2: YELLOW, + 2: CLS_YELLOW, 3: Fore.BLUE, -1: Style.RESET_ALL, } COLORS_SOCKET = { 0: Fore.BLUE, - 1: YELLOW, + 1: CLS_YELLOW, 2: Fore.RED, 3: Fore.GREEN, -1: Style.RESET_ALL, @@ -30,40 +30,35 @@ class Color(object): COLOR_NONE = dict((key, "") for key in range(-1, 4)) - def __init__(self, topology, enabled=True): - self.enabled = enabled - self.topology = topology - if topology is not None: - self.color_scheme = self.__choose_color_scheme() + def __init__(self, topology, enabled): + self.enabled, self.topology = enabled, topology + self.RESET, self.RESET_ALL = Fore.RESET, Style.RESET_ALL + self.YELLOW, self.GREY = Color.CLS_YELLOW, Color.CLS_GREY + self.RED, self.BRIGHT = Fore.RED, Style.BRIGHT + self.color_scheme = self.__choose_color_scheme() def __choose_color_scheme(self): - if not self.enabled: - Style.BRIGHT = Style.RESET_ALL = Fore.RED = Fore.RESET = self.GREY = self.YELLOW = "" + if not self.enabled or not self.topology: + self.BRIGHT = self.RESET_ALL = self.RED = self.RESET = self.GREY = self.YELLOW = "" return self.COLOR_NONE if self.topology.layout_kind == 'NUMA': return self.COLORS_NODE return self.COLORS_SOCKET - @staticmethod - def wrap(word, color): + def wrap(self, word, color): """ wrap string in given color """ - return '{0}{1}{2}'.format(color, word, Style.RESET_ALL) + return '{0}{1}{2}'.format(color, word, self.RESET_ALL) - @staticmethod - def colorize(value, warning, error): - return Color.wrap(value, Fore.RED if value >= error else Color.YELLOW if value >= warning else Fore.RESET) + def colorize(self, value, warning, error): + return self.wrap(value, self.RED if value >= error else self.YELLOW if value >= warning else self.RESET) - @staticmethod - def bright(string): - return Color.wrap(string, Style.BRIGHT) + def bright(self, string): + return self.wrap(string, self.BRIGHT) - @staticmethod - def wrap_header(string): - return Color.wrap("# {0}\n".format(string), Style.BRIGHT) + def wrap_header(self, string): + return self.wrap("# {0}\n".format(string), self.BRIGHT) def colorize_cpu(self, cpu): - if not self.color_scheme: - self.color_scheme = self.__choose_color_scheme() if isinstance(cpu, str): cpu = int(cpu[3:]) return self.color_scheme.get(self.topology.layout.get(cpu)) diff --git a/netutils_linux_monitoring/irqtop.py b/netutils_linux_monitoring/irqtop.py index a2904e9..13ce082 100644 --- a/netutils_linux_monitoring/irqtop.py +++ b/netutils_linux_monitoring/irqtop.py @@ -59,10 +59,9 @@ def make_rows(self): output_lines.append(line) return output_lines, cpu_count - @staticmethod - def colorize_irq_per_cpu(irq_per_cpu): + def colorize_irq_per_cpu(self, irq_per_cpu): """ :returns: highlighted by warning/error irq string """ - return Color.colorize(irq_per_cpu, 40000, 80000) + return self.color.colorize(irq_per_cpu, 40000, 80000) def __repr__(self): output_lines, cpu_count = self.make_rows() diff --git a/netutils_linux_monitoring/link_rate.py b/netutils_linux_monitoring/link_rate.py index a94bbd0..880825b 100644 --- a/netutils_linux_monitoring/link_rate.py +++ b/netutils_linux_monitoring/link_rate.py @@ -73,9 +73,8 @@ def eval(self): def make_header(self): return ['Device'] + [stat.shortname for stat in self.stats] - @staticmethod - def colorize_stat(stat, value): - return Color.colorize(value, 1, 1) if 'errors' in stat.filename or 'dropped' in stat.filename else value + def colorize_stat(self, stat, value): + return self.color.colorize(value, 1, 1) if 'errors' in stat.filename or 'dropped' in stat.filename else value def colorize_stats(self, dev, repr_source): return [self.colorize_stat(stat, repr_source[dev][stat]) for stat in self.stats] @@ -86,7 +85,7 @@ def make_rows(self): repr_source = self.repr_source() for dev in self.options.devices: dev_node = self.pci.devices.get(dev) - dev_color = self.color.COLORS_NODE.get(dev_node) + dev_color = self.color.COLORS_NODE.get(dev_node) if self.options.color else "" _dev = self.color.wrap(dev, dev_color) yield [_dev] + self.colorize_stats(dev, repr_source) diff --git a/netutils_linux_monitoring/network_top.py b/netutils_linux_monitoring/network_top.py index cecf246..bfb824d 100644 --- a/netutils_linux_monitoring/network_top.py +++ b/netutils_linux_monitoring/network_top.py @@ -23,7 +23,7 @@ def __init__(self): } self.parse_options() self.topology = Topology(fake=self.options.random, lscpu_output=self.options.lscpu_output) - self.color = Color(self.topology) + self.color = Color(self.topology, self.options.color) for top in self.tops.values(): top.topology = self.topology top.color = self.color @@ -50,7 +50,7 @@ def tick(self): def __repr__(self): output = [ - BaseTop.header, + self.header, self.__repr_irq(), self.__repr_cpu(), self.__repr_dev(), @@ -71,6 +71,7 @@ def parse_options(self): top.options = self.options if hasattr(top, 'post_optparse'): top.post_optparse() + self.default_post_optparse() def __repr_dev(self): top = self.tops.get('link-rate') diff --git a/netutils_linux_monitoring/softirqs.py b/netutils_linux_monitoring/softirqs.py index 6c0d51b..ae97e2d 100644 --- a/netutils_linux_monitoring/softirqs.py +++ b/netutils_linux_monitoring/softirqs.py @@ -41,15 +41,13 @@ def __repr__(self): table = make_table(header, ['l', 'r', 'r'], rows) return self.__repr_table__(table) - @staticmethod - def colorize_net_rx(net_rx): + def colorize_net_rx(self, net_rx): """ :returns: highlighted by warning/error net_rx string """ - return Color.colorize(net_rx, 40000, 80000) + return self.color.colorize(net_rx, 40000, 80000) - @staticmethod - def colorize_net_tx(net_tx): + def colorize_net_tx(self, net_tx): """ :returns: highlighted by warning/error net_tx string """ - return Color.colorize(net_tx, 20000, 30000) + return self.color.colorize(net_tx, 20000, 30000) @staticmethod def __active_cpu_count__(data): diff --git a/netutils_linux_monitoring/softnet_stat.py b/netutils_linux_monitoring/softnet_stat.py index dceea35..927daa6 100644 --- a/netutils_linux_monitoring/softnet_stat.py +++ b/netutils_linux_monitoring/softnet_stat.py @@ -84,29 +84,9 @@ def make_header(): def make_rows(self): return [[ self.color.wrap('CPU{0}'.format(stat.cpu), self.color.colorize_cpu(stat.cpu)), - self.colorize_total(stat.total), - self.colorize_dropped(stat.dropped), - self.colorize_time_squeeze(stat.time_squeeze), - self.colorize_cpu_collision(stat.cpu_collision), + self.color.colorize(stat.total, 300000, 900000), + self.color.colorize(stat.dropped, 1, 1), + self.color.colorize(stat.time_squeeze, 1, 300), + self.color.colorize(stat.cpu_collision, 1, 1000), stat.received_rps ] for stat in self.repr_source()] - - @staticmethod - def colorize_total(total): - """ :returns: highlighted by warning/error total string """ - return Color.colorize(total, 300000, 900000) - - @staticmethod - def colorize_dropped(dropped): - """ :returns: highlighted by warning/error dropped string """ - return Color.colorize(dropped, 1, 1) - - @staticmethod - def colorize_time_squeeze(time_squeeze): - """ :returns: highlighted by warning/error time_squeeze string """ - return Color.colorize(time_squeeze, 1, 300) - - @staticmethod - def colorize_cpu_collision(cpu_collision): - """ :returns: highlighted by warning/error cpu_collision string """ - return Color.colorize(cpu_collision, 1, 1000) diff --git a/netutils_linux_tuning/rss_ladder.py b/netutils_linux_tuning/rss_ladder.py index 27a8f7c..e8dd2d5 100644 --- a/netutils_linux_tuning/rss_ladder.py +++ b/netutils_linux_tuning/rss_ladder.py @@ -65,7 +65,7 @@ def apply(self, decision): if len(set(cpus)) != len(cpus): warning = 'WARNING: some CPUs process multiple queues, consider reduce queue count for this network device' if self.options.color: - print_(self.color.wrap(warning, Color.YELLOW)) + print_(self.color.wrap(warning, self.color.YELLOW)) else: print_(warning) for irq, queue_name, socket_cpu in affinity: