bumblebee-status/bumblebee_status/modules/contrib/watson.py

72 lines
1.9 KiB
Python
Raw Permalink Normal View History

2020-04-19 14:23:41 +02:00
# pylint: disable=C0111,R0903
"""Displays the status of watson (time-tracking tool)
Requires the following executable:
* watson
Parameters:
* watson.format: Output format, defaults to "{project} [{tags}]"
Supported fields are: {project}, {tags}, {relative_start}, {absolute_start}
contributed by `bendardenne <https://github.com/bendardenne>`_ - many thanks!
2020-04-19 14:23:41 +02:00
"""
import logging
import re
import functools
2020-04-19 14:27:11 +02:00
import core.module
import core.widget
import core.input
import core.decorators
import util.cli
2020-04-19 14:27:11 +02:00
class Module(core.module.Module):
@core.decorators.every(minutes=60)
def __init__(self, config, theme):
super().__init__(config, theme, core.widget.Widget(self.text))
2020-04-19 14:27:11 +02:00
self.__tracking = False
self.__info = {}
self.__format = self.parameter("format", "{project} [{tags}]")
2020-04-19 14:27:11 +02:00
core.input.register(self, button=core.input.LEFT_MOUSE, cmd=self.toggle)
2020-04-19 14:23:41 +02:00
def toggle(self, widget):
2020-04-19 14:27:11 +02:00
if self.__tracking:
util.cli.execute("watson stop")
2020-04-19 14:23:41 +02:00
else:
util.cli.execute("watson restart")
2020-04-19 14:27:11 +02:00
self.__tracking = not self.__tracking
2020-04-19 14:23:41 +02:00
def text(self, widget):
2020-04-19 14:27:11 +02:00
if self.__tracking:
return self.__format.format(**self.__info)
2020-04-19 14:23:41 +02:00
else:
return "Paused"
2020-04-19 14:23:41 +02:00
2020-04-19 14:27:11 +02:00
def update(self):
output = util.cli.execute("watson status")
m = re.search(r"Project ([^\[\]]+)(?: \[(.+)\])? started (.+) \((.+)\)", output)
if m:
self.__tracking = True
self.__info = {
"project": m.group(1),
"tags": m.group(2) or "",
"relative_start": m.group(3),
"absolute_start": m.group(4),
}
else:
2020-04-19 14:27:11 +02:00
self.__tracking = False
2020-04-19 14:23:41 +02:00
return
def state(self, widget):
return "on" if self.__tracking else "off"
2020-04-19 14:23:41 +02:00
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4