| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
from collections import OrderedDict |
| 20 |
|
| 21 |
import urwid |
| 22 |
|
| 23 |
|
| 24 |
class SummaryTextList: |
| 25 |
MAX_LABEL_L = 12 |
| 26 |
|
| 27 |
def __init__(self, source, visible_sensors_list): |
| 28 |
self.source = source |
| 29 |
self.visible_summaries = OrderedDict() |
| 30 |
keys = list(self.source.get_summary().keys()) |
| 31 |
|
| 32 |
self.visible_summaries[keys[0]] = any(visible_sensors_list) |
| 33 |
|
| 34 |
for key, visible in zip(keys[1:], visible_sensors_list): |
| 35 |
self.visible_summaries[key] = visible |
| 36 |
|
| 37 |
|
| 38 |
self.summary_text_items = OrderedDict() |
| 39 |
|
| 40 |
@staticmethod |
| 41 |
def _format_display_val(val, alerts, suffixes, sensor_idx): |
| 42 |
"""Return urwid text markup for a summary value with optional suffix/color.""" |
| 43 |
text = str(val) |
| 44 |
if 0 <= sensor_idx < len(suffixes) and suffixes[sensor_idx]: |
| 45 |
text += " " + suffixes[sensor_idx] |
| 46 |
attr = alerts[sensor_idx] if 0 <= sensor_idx < len(alerts) else None |
| 47 |
return (attr, text) if attr else text |
| 48 |
|
| 49 |
def get_text_item_list(self): |
| 50 |
summery_text_list = [] |
| 51 |
alerts = self.source.get_sensor_alerts() |
| 52 |
suffixes = self.source.get_sensor_suffixes() |
| 53 |
summary_items = list(self.source.get_summary().items()) |
| 54 |
for item_idx, (key, val) in enumerate(summary_items): |
| 55 |
label_w = urwid.Text(str(key[0 : self.MAX_LABEL_L])) |
| 56 |
|
| 57 |
display_val = self._format_display_val(val, alerts, suffixes, item_idx - 1) |
| 58 |
value_w = urwid.Text(display_val, align="right") |
| 59 |
|
| 60 |
self.summary_text_items[key] = value_w |
| 61 |
col_w = urwid.Columns([("weight", 1.5, label_w), value_w]) |
| 62 |
|
| 63 |
is_visible = self.visible_summaries.setdefault(key, True) |
| 64 |
if is_visible: |
| 65 |
summery_text_list.append(col_w) |
| 66 |
|
| 67 |
return summery_text_list |
| 68 |
|
| 69 |
def update_visibility(self, visible_sensors): |
| 70 |
keys = list(self.visible_summaries.keys()) |
| 71 |
self.visible_summaries[keys[0]] = any(visible_sensors) |
| 72 |
|
| 73 |
for sensor, visible in zip(keys[1:], visible_sensors): |
| 74 |
self.visible_summaries[sensor] = visible |
| 75 |
|
| 76 |
def update(self): |
| 77 |
alerts = self.source.get_sensor_alerts() |
| 78 |
suffixes = self.source.get_sensor_suffixes() |
| 79 |
summary_items = list(self.source.get_summary().items()) |
| 80 |
for item_idx, (key, val) in enumerate(summary_items): |
| 81 |
if key in self.summary_text_items: |
| 82 |
display_val = self._format_display_val( |
| 83 |
val, alerts, suffixes, item_idx - 1 |
| 84 |
) |
| 85 |
self.summary_text_items[key].set_text(display_val) |
| 86 |
|
| 87 |
def get_is_available(self): |
| 88 |
return self.source.get_is_available() |