Skip to content

Commit

Permalink
Refactoring: color usage is strictly follows --no-color flag now.
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleg Strizhechenko committed May 18, 2022
1 parent f54359c commit 90c7ab8
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 69 deletions.
5 changes: 3 additions & 2 deletions netutils_linux_monitoring/base_top.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand Down
49 changes: 22 additions & 27 deletions netutils_linux_monitoring/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,64 +6,59 @@
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,
}

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))
Expand Down
5 changes: 2 additions & 3 deletions netutils_linux_monitoring/irqtop.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
7 changes: 3 additions & 4 deletions netutils_linux_monitoring/link_rate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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)

Expand Down
5 changes: 3 additions & 2 deletions netutils_linux_monitoring/network_top.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -50,7 +50,7 @@ def tick(self):

def __repr__(self):
output = [
BaseTop.header,
self.header,
self.__repr_irq(),
self.__repr_cpu(),
self.__repr_dev(),
Expand All @@ -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')
Expand Down
10 changes: 4 additions & 6 deletions netutils_linux_monitoring/softirqs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
28 changes: 4 additions & 24 deletions netutils_linux_monitoring/softnet_stat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion netutils_linux_tuning/rss_ladder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 90c7ab8

Please sign in to comment.