2020-04-10 16:58:19 +02:00
|
|
|
"""
|
|
|
|
Show progress for cp, mv, dd, ...
|
|
|
|
|
|
|
|
Parameters:
|
2020-04-10 16:58:54 +02:00
|
|
|
* progress.placeholder: Text to display while no process is running (defaults to 'n/a')
|
2020-04-10 16:58:19 +02:00
|
|
|
* progress.barwidth: Width of the progressbar if it is used (defaults to 8)
|
2020-04-10 16:58:54 +02:00
|
|
|
* progress.format: Format string (defaults to '{bar} {cmd} {arg}')
|
2020-05-06 12:57:38 +02:00
|
|
|
Available values are: {bar} {pid} {cmd} {arg} {percentage} {quantity} {speed} {time}
|
2020-04-10 16:58:54 +02:00
|
|
|
* progress.barfilledchar: Character used to draw the filled part of the bar (defaults to '#'), notice that it can be a string
|
|
|
|
* progress.baremptychar: Character used to draw the empty part of the bar (defaults to '-'), notice that it can be a string
|
2020-04-10 16:58:19 +02:00
|
|
|
|
|
|
|
Requires the following executable:
|
|
|
|
* progress
|
2020-05-08 20:58:35 +02:00
|
|
|
|
|
|
|
contributed by `remi-dupre <https://github.com/remi-dupre>`_ - many thanks!
|
2020-04-10 16:58:19 +02:00
|
|
|
"""
|
|
|
|
|
2020-04-10 17:11:06 +02:00
|
|
|
import core.module
|
|
|
|
import core.widget
|
2020-04-10 16:58:19 +02:00
|
|
|
|
2020-04-10 17:11:06 +02:00
|
|
|
import util.cli
|
|
|
|
import util.format
|
2020-04-10 16:58:19 +02:00
|
|
|
|
2020-04-10 17:11:06 +02:00
|
|
|
import re
|
2020-04-10 16:58:19 +02:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-04-10 17:11:06 +02:00
|
|
|
class Module(core.module.Module):
|
2020-04-26 16:39:24 +02:00
|
|
|
def __init__(self, config, theme):
|
|
|
|
super().__init__(config, theme, core.widget.Widget(self.get_progress_text))
|
2020-04-10 17:11:06 +02:00
|
|
|
self.__active = False
|
2020-04-10 16:58:19 +02:00
|
|
|
|
|
|
|
def get_progress_text(self, widget):
|
|
|
|
if self.update_progress_info(widget):
|
2020-05-03 11:15:52 +02:00
|
|
|
width = util.format.asint(self.parameter("barwidth", 8))
|
|
|
|
count = round((width * widget.get("per")) / 100)
|
|
|
|
filledchar = self.parameter("barfilledchar", "#")
|
|
|
|
emptychar = self.parameter("baremptychar", "-")
|
2020-04-10 16:58:19 +02:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
bar = "[{}{}]".format(filledchar * count, emptychar * (width - count))
|
|
|
|
|
|
|
|
str_format = self.parameter("format", "{bar} {cmd} {arg}")
|
2020-04-10 16:58:19 +02:00
|
|
|
return str_format.format(
|
2020-05-03 11:15:52 +02:00
|
|
|
bar=bar,
|
|
|
|
pid=widget.get("pid"),
|
|
|
|
cmd=widget.get("cmd"),
|
|
|
|
arg=widget.get("arg"),
|
|
|
|
percentage=widget.get("per"),
|
|
|
|
quantity=widget.get("qty"),
|
|
|
|
speed=widget.get("spd"),
|
|
|
|
time=widget.get("tim"),
|
2020-04-10 16:58:19 +02:00
|
|
|
)
|
|
|
|
else:
|
2020-05-03 11:15:52 +02:00
|
|
|
return self.parameter("placeholder", "n/a")
|
2020-04-10 16:58:19 +02:00
|
|
|
|
|
|
|
def update_progress_info(self, widget):
|
|
|
|
"""Update widget's informations about the copy"""
|
2020-05-03 11:15:52 +02:00
|
|
|
if not self.__active:
|
|
|
|
return
|
2020-04-10 16:58:19 +02:00
|
|
|
|
|
|
|
# These regex extracts following groups:
|
|
|
|
# 1. pid
|
|
|
|
# 2. command
|
|
|
|
# 3. arguments
|
|
|
|
# 4. progress (xx.x formated)
|
|
|
|
# 5. quantity (.. unit / .. unit formated)
|
|
|
|
# 6. speed
|
|
|
|
# 7. time remaining
|
2020-05-03 11:15:52 +02:00
|
|
|
extract_nospeed = re.compile(
|
2020-06-02 20:13:39 +02:00
|
|
|
r"\[ *(\d*)\] ([a-zA-Z]*) (.*)\n\t(\d*\.*\d*)% \((.*)\)\n.*"
|
2020-05-03 11:15:52 +02:00
|
|
|
)
|
|
|
|
extract_wtspeed = re.compile(
|
2020-06-02 20:13:39 +02:00
|
|
|
r"\[ *(\d*)\] ([a-zA-Z]*) (.*)\n\t(\d*\.*\d*)% \((.*)\) (\d*\.\d .*) remaining (\d*:\d*:\d*)\n.*"
|
2020-05-03 11:15:52 +02:00
|
|
|
)
|
2020-04-10 16:58:19 +02:00
|
|
|
|
|
|
|
try:
|
2020-05-03 11:15:52 +02:00
|
|
|
raw = util.cli.execute("progress -qW 0.1")
|
2020-04-10 16:58:19 +02:00
|
|
|
result = extract_wtspeed.match(raw)
|
|
|
|
|
|
|
|
if not result:
|
|
|
|
# Abord speed measures
|
2020-05-03 11:15:52 +02:00
|
|
|
raw = util.cli.execute("progress -q")
|
2020-04-10 16:58:19 +02:00
|
|
|
result = extract_nospeed.match(raw)
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
widget.set("spd", "???.? B/s")
|
|
|
|
widget.set("tim", "??:??:??")
|
2020-04-10 16:58:19 +02:00
|
|
|
else:
|
2020-05-03 11:15:52 +02:00
|
|
|
widget.set("spd", result.group(6))
|
|
|
|
widget.set("tim", result.group(7))
|
|
|
|
|
|
|
|
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))
|
2020-04-10 16:58:19 +02:00
|
|
|
return True
|
|
|
|
except Exception:
|
|
|
|
return False
|
|
|
|
|
2020-04-10 17:11:06 +02:00
|
|
|
def update(self):
|
2020-05-03 11:15:52 +02:00
|
|
|
self.__active = bool(util.cli.execute("progress -q"))
|
2020-04-10 17:11:06 +02:00
|
|
|
|
2020-04-10 16:58:19 +02:00
|
|
|
def state(self, widget):
|
2020-04-10 17:11:06 +02:00
|
|
|
if self.__active:
|
2020-05-03 11:15:52 +02:00
|
|
|
return "copying"
|
2021-11-05 14:00:34 +01:00
|
|
|
return ["pending", "mayhide"]
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-04-10 16:58:19 +02:00
|
|
|
|
2020-04-10 16:58:54 +02:00
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|