|
| 1 | +""" |
| 2 | +Display number of pending updates for Fedora Linux. |
| 3 | +
|
| 4 | +Configuration parameters: |
| 5 | + cache_timeout: refresh interval for this module (default 600) |
| 6 | + format: display format for this module |
| 7 | + (default "DNF [\?if=security&color=bad {available}|\?color=available {available}]") |
| 8 | + thresholds: specify color thresholds to use |
| 9 | + (default [(0, 'good'), (1, 'degraded')]) |
| 10 | +
|
| 11 | +Format placeholders: |
| 12 | + {available} number of pending available updates |
| 13 | + {bugfix} number of pending bugfix updates |
| 14 | + {enhancement} number of pending enhancement updates |
| 15 | + {security} number of pending security updates |
| 16 | + {unspecified} number of pending unspecified updates |
| 17 | +
|
| 18 | +Color thresholds: |
| 19 | + format: |
| 20 | + `xxx`: print a color based on the value of `xxx` placeholder |
| 21 | +
|
| 22 | +Examples: |
| 23 | +``` |
| 24 | +# individual colorized updates |
| 25 | +dnf_updates { |
| 26 | + format = "[\?if=security&color=tomato SECURITY {security}][\?soft ]" |
| 27 | + format += "[\?if=bugfix&color=limegreen BUGFIX {bugfix}][\?soft ]" |
| 28 | + format += "[\?if=enhancement&color=lightskyblue ENHANCEMENT {enhancement}][\?soft ]" |
| 29 | + format += "[\?if=unspecified&color=darkgray OTHER {unspecified}]" |
| 30 | +} |
| 31 | +``` |
| 32 | +
|
| 33 | +@author tobes |
| 34 | +@license BSD |
| 35 | +
|
| 36 | +SAMPLE OUTPUT |
| 37 | +[{'full_text': 'DNF '}, {'full_text': '14', 'color': '#FF0000'}] |
| 38 | +
|
| 39 | +no_updates |
| 40 | +[{'full_text': 'DNF '}, {'full_text': '0', 'color': '#00FF00'}] |
| 41 | +""" |
| 42 | + |
| 43 | +from collections import Counter |
| 44 | +from json import loads |
| 45 | + |
| 46 | +ADVISORIES = ["available", "bugfix", "enhancement", "security", "unspecified"] |
| 47 | + |
| 48 | + |
| 49 | +class Py3status: |
| 50 | + """ """ |
| 51 | + |
| 52 | + # available configuration parameters |
| 53 | + cache_timeout = 600 |
| 54 | + format = "DNF [\?if=security&color=bad {available}|\?color=available {available}]" |
| 55 | + thresholds = [(0, "good"), (1, "degraded")] |
| 56 | + |
| 57 | + def post_config_hook(self): |
| 58 | + self.thresholds_init = self.py3.get_color_names_list(self.format, ADVISORIES) |
| 59 | + |
| 60 | + def dnf_updates(self): |
| 61 | + updates = loads(self.py3.command_output("dnf updateinfo list --json")) |
| 62 | + dnf_data = ( |
| 63 | + dict.fromkeys(ADVISORIES, 0) |
| 64 | + | Counter(x['type'] for x in updates) |
| 65 | + | {"available": len(updates)} |
| 66 | + ) |
| 67 | + |
| 68 | + for x in self.thresholds_init: |
| 69 | + self.py3.threshold_get_color(dnf_data[x], x) |
| 70 | + |
| 71 | + return { |
| 72 | + "cached_until": self.py3.time_in(self.cache_timeout), |
| 73 | + "full_text": self.py3.safe_format(self.format, dnf_data), |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | +if __name__ == "__main__": |
| 78 | + """ |
| 79 | + Run module in test mode. |
| 80 | + """ |
| 81 | + from py3status.module_test import module_test |
| 82 | + |
| 83 | + module_test(Py3status) |
0 commit comments