|
| 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 | +# i3status theme |
| 25 | +dnf_updates { |
| 26 | + format = "[\?if=security&color=bad DNF: {available}" |
| 27 | + format += "|\?color=available DNF: {available}]" |
| 28 | +} |
| 29 | +
|
| 30 | +# hide module when no available updates |
| 31 | +dnf_updates { |
| 32 | + format = "[\?if=available DNF [\?if=security&color=bad {available}" |
| 33 | + format += "|\?color=available {available}]]" |
| 34 | +} |
| 35 | +
|
| 36 | +# individual colorized updates |
| 37 | +dnf_updates { |
| 38 | + format = "[\?if=security SECURITY [\?color=tomato {security}]][\?soft ]" |
| 39 | + format += "[\?if=bugfix BUGFIX [\?color=limegreen {bugfix}]][\?soft ]" |
| 40 | + format += "[\?if=enhancement ENHANCEMENT [\?color=lightskyblue {enhancement}]][\?soft ]" |
| 41 | + format += "[\?if=unspecified OTHER [\?color=darkgray {unspecified}]]" |
| 42 | +} |
| 43 | +``` |
| 44 | +
|
| 45 | +@author tobes |
| 46 | +@license BSD |
| 47 | +
|
| 48 | +SAMPLE OUTPUT |
| 49 | +[{'full_text': 'DNF '}, {'full_text': '14', 'color': '#FF0000'}] |
| 50 | +
|
| 51 | +no_updates |
| 52 | +[{'full_text': 'DNF '}, {'full_text': '0', 'color': '#00FF00'}] |
| 53 | +""" |
| 54 | + |
| 55 | +from collections import Counter |
| 56 | +from json import loads |
| 57 | + |
| 58 | +ADVISORIES = ["available", "bugfix", "enhancement", "security", "unspecified"] |
| 59 | + |
| 60 | + |
| 61 | +class Py3status: |
| 62 | + """ """ |
| 63 | + |
| 64 | + # available configuration parameters |
| 65 | + cache_timeout = 600 |
| 66 | + format = "DNF [\?if=security&color=bad {available}|\?color=available {available}]" |
| 67 | + thresholds = [(0, "good"), (1, "degraded")] |
| 68 | + |
| 69 | + def post_config_hook(self): |
| 70 | + self.thresholds_init = self.py3.get_color_names_list(self.format, ADVISORIES) |
| 71 | + |
| 72 | + def _get_dnf_data(self): |
| 73 | + try: |
| 74 | + updates = loads(self.py3.command_output("dnf updateinfo list --json")) |
| 75 | + cached_until = self.cache_timeout |
| 76 | + defaults = 0 |
| 77 | + except self.py3.CommandError: |
| 78 | + updates = [] |
| 79 | + cached_until = 10 |
| 80 | + defaults = None |
| 81 | + |
| 82 | + temporary = ( |
| 83 | + dict.fromkeys(ADVISORIES, defaults) |
| 84 | + | Counter(x['type'] for x in updates) |
| 85 | + | {ADVISORIES[0]: len(updates)} |
| 86 | + ) |
| 87 | + |
| 88 | + return (temporary, cached_until) |
| 89 | + |
| 90 | + def dnf_updates(self): |
| 91 | + (dnf_data, cached_until) = self._get_dnf_data() |
| 92 | + |
| 93 | + for x in self.thresholds_init: |
| 94 | + self.py3.threshold_get_color(dnf_data[x], x) |
| 95 | + |
| 96 | + return { |
| 97 | + "cached_until": self.py3.time_in(cached_until), |
| 98 | + "full_text": self.py3.safe_format(self.format, dnf_data), |
| 99 | + } |
| 100 | + |
| 101 | + |
| 102 | +if __name__ == "__main__": |
| 103 | + """ |
| 104 | + Run module in test mode. |
| 105 | + """ |
| 106 | + from py3status.module_test import module_test |
| 107 | + |
| 108 | + module_test(Py3status) |
0 commit comments