bumblebee-status/modules/contrib/pacman.py

77 lines
2.1 KiB
Python
Raw Normal View History

2020-04-24 16:40:21 +02:00
# pylint: disable=C0111,R0903
"""Displays update information per repository for pacman.
Parameters:
2020-04-24 16:40:39 +02:00
* pacman.sum: If you prefere displaying updates with a single digit (defaults to 'False')
2020-04-24 16:40:21 +02:00
Requires the following executables:
* fakeroot
* pacman
"""
import os
import threading
2020-04-24 16:46:31 +02:00
import core.module
import core.widget
import core.decorators
import util.cli
import util.format
2020-04-24 16:40:21 +02:00
# list of repositories.
# the last one should always be other
repos = ["core", "extra", "community", "multilib", "testing", "other"]
2020-04-24 16:40:21 +02:00
def get_pacman_info(widget, path):
cmd = "{}/../../bin/pacman-updates".format(path)
2020-04-24 16:46:31 +02:00
if not os.path.exists(cmd):
cmd = "/usr/share/bumblebee-status/bin/pacman-update"
2020-04-24 16:46:31 +02:00
result = util.cli.execute(cmd, ignore_errors=True)
2020-04-24 16:40:21 +02:00
count = len(repos) * [0]
2020-04-24 16:40:21 +02:00
for line in result.splitlines():
if line.startswith(("http", "rsync")):
for i in range(len(repos) - 1):
if "/" + repos[i] + "/" in line:
2020-04-24 16:40:21 +02:00
count[i] += 1
break
else:
result[-1] += 1
for i in range(len(repos)):
widget.set(repos[i], count[i])
core.event.trigger("update", [widget.module.id], redraw_only=True)
2020-04-24 16:40:21 +02:00
2020-04-24 16:46:31 +02:00
class Module(core.module.Module):
@core.decorators.every(minutes=30)
def __init__(self, config, theme):
super().__init__(config, theme, core.widget.Widget(self.updates))
2020-04-24 16:40:21 +02:00
def updates(self, widget):
if util.format.asbool(self.parameter("sum")):
2020-04-24 16:40:21 +02:00
return str(sum(map(lambda x: widget.get(x, 0), repos)))
return "/".join(map(lambda x: str(widget.get(x, 0)), repos))
2020-04-24 16:40:21 +02:00
2020-04-24 16:46:31 +02:00
def update(self):
2020-04-24 16:40:21 +02:00
path = os.path.dirname(os.path.abspath(__file__))
2020-04-24 16:46:31 +02:00
thread = threading.Thread(target=get_pacman_info, args=(self.widget(), path))
thread.start()
2020-04-24 16:40:21 +02:00
def state(self, widget):
weightedCount = sum(
map(lambda x: (len(repos) - x[0]) * widget.get(x[1], 0), enumerate(repos))
)
2020-04-24 16:40:21 +02:00
if weightedCount < 10:
return "good"
2020-04-24 16:40:21 +02:00
return self.threshold_state(weightedCount, 100, 150)
2020-04-24 16:40:21 +02:00
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4