bumblebee-status/bumblebee/modules/progress.py

87 lines
2.9 KiB
Python
Raw Normal View History

2018-09-07 16:17:09 +02:00
"""
Show progress for cp, mv, dd, ...
Parameters:
* progress.placeholder: Text to display while no process is running (defaults to "n/a")
* progress.barwidth: Width of the progressbar if it is used (defaults to 8)
* progress.format: Format string (defaults to "{bar} {cmd} {arg}")
2018-09-07 16:36:09 +02:00
Available values are: {bar} {pid} {cmd} {arg} {per} {qty} {spd}
2018-09-07 16:17:09 +02:00
Requires the following executable:
* progress
"""
import bumblebee.input
import bumblebee.output
import bumblebee.engine
import re
class Module(bumblebee.engine.Module):
def __init__(self, engine, config):
super(Module, self).__init__(engine, config,
bumblebee.output.Widget(full_text=self.get_progress_text)
)
def get_progress_text(self, widget):
if self.update_progress_info(widget):
width = self.parameter("barwidth", 8)
count = round((width * widget.get("per")) / 100)
bar = "[{}{}]".format(
"#" * count,
"--" * (width - count)
)
str_format = self.parameter("format", '{bar} {cmd} {arg}')
return "" + str_format.format(
bar = bar,
pid = widget.get("pid"),
cmd = widget.get("cmd"),
arg = widget.get("arg"),
per = widget.get("per"),
2018-09-07 16:36:09 +02:00
qty = widget.get("qty"),
spd = widget.get("spd"),
tim = widget.get("tim")
2018-09-07 16:17:09 +02:00
)
else:
return self.parameter("placeholder", 'n/a')
def update_progress_info(self, widget):
2018-09-07 16:36:09 +02:00
# These regex extracts following groups:
2018-09-07 16:17:09 +02:00
# 1. pid
# 2. command
# 3. arguments
# 4. progress (xx.x formated)
# 5. quantity (.. unit / .. unit formated)
2018-09-07 16:36:09 +02:00
# 6. speed
# 7. time remaining
2018-09-07 16:17:09 +02:00
extract_nospeed = re.compile("\[ *(\d*)\] ([a-zA-Z]*) (.*)\n\t(\d*\.*\d*)% \((.*)\)\n.*")
2018-09-07 16:36:09 +02:00
extract_wtspeed = re.compile('\[ *(\d*)\] ([a-zA-Z]*) (.*)\n\t(\d*\.*\d*)% \((.*)\) (\d*\.\d .*) remaining (\d*:\d*:\d*)\n.*')
2018-09-07 16:17:09 +02:00
try:
2018-09-07 16:36:09 +02:00
raw = bumblebee.util.execute("progress -qW 0.1")
result = extract_wtspeed.match(raw)
if not result:
# Abord speed measures
raw = bumblebee.util.execute("progress -q")
result = extract_nospeed.match(raw)
widget.set("spd", "??? b/s")
widget.set("tim", "??:??:??")
else:
widget.set("spd", result.group(6))
widget.set("tim", result.group(7))
2018-09-07 16:17:09 +02:00
widget.set("pid", int(result.group(1)))
widget.set("cmd", result.group(2))
widget.set("arg", result.group(3))
widget.set("per", float(result.group(4)))
widget.set("qty", result.group(5))
return True
except Exception:
return False