Skip to content

Commit

Permalink
new: reset seat counter on stop after err, plus cleanup
Browse files Browse the repository at this point in the history
* abstract out delta_limits, hide some prints behind DEBUG

Signed-off-by: Stephen L Arnold <[email protected]>
  • Loading branch information
sarnold committed Aug 9, 2024
1 parent afe3b72 commit 9d3a7c0
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 14 deletions.
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ PyGObject references
* https://github.com/candidtim/vagrant-appindicator (old)


.. |CI| image:: https://github.com/sarnold/timew-addons/actions/workflows/main.yml/badge.svg
:target: https://github.com/sarnold/timew-addons/actions/workflows/main.yml
.. |CI| image:: https://github.com/sarnold/timew-addons/actions/workflows/ci.yml/badge.svg
:target: https://github.com/sarnold/timew-addons/actions/workflows/ci.yml
:alt: CI test status

.. |pre| image:: https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&amp;logoColor=white
Expand Down
9 changes: 8 additions & 1 deletion scripts/timew-status-indicator
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ from gi.repository import Gtk, Notify

from timew_status import (
CFG,
DEBUG,
TAG,
__version__,
get_delta_limits,
get_state_icon,
get_state_str,
get_status,
Expand Down Expand Up @@ -124,7 +126,9 @@ class Indicator:
Notify.Notification.new("Timew state", msg, None).show()
old_state = new_state
COUNT['SeatTick'] += SLEEP_SEC
print(f'My tag text: {TAG["text"]}')
if DEBUG:
print(f'My tag text: {TAG["text"]}')
print(f'My tick count: {current_tick_count}')
time.sleep(SLEEP_SEC)

def create_menu(self):
Expand Down Expand Up @@ -214,6 +218,9 @@ class Indicator:
if CFG['show_state_label']:
self.indicator.set_label('INACTIVE'.format().center(9), '99999999')
Notify.Notification.new("Timew status", svc_msg, None).show()
_, _, _, seat_limit = get_delta_limits()
if seat_limit < timedelta(seconds=COUNT['SeatTick']):
COUNT.clear()


def main():
Expand Down
4 changes: 4 additions & 0 deletions src/timew_status/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

from .utils import (
CFG,
DEBUG,
get_delta_limits,
get_state_icon,
get_state_str,
get_status,
Expand All @@ -26,7 +28,9 @@
"__description__",
"__version__",
"CFG",
"DEBUG",
"TAG",
"get_delta_limits",
"get_state_icon",
"get_state_str",
"get_status",
Expand Down
37 changes: 26 additions & 11 deletions src/timew_status/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""
"""
import os
import subprocess
import sys
from datetime import timedelta
Expand Down Expand Up @@ -39,6 +40,23 @@ def get_config(file_encoding='utf-8'):
return cfgobj, cfgfile


def get_delta_limits():
"""
Return config max/snooze limits as timedeltas. Everything comes from
static config values and gets padded with seconds.
:return: tuple of timedeltas
"""
day_sum = [CFG["day_max"] + ':00', CFG["day_snooze"] + ':00']
seat_sum = [CFG["seat_max"] + ':00', CFG["seat_snooze"] + ':00']
day_limit = sum(map(to_td, day_sum), timedelta()) # noqa:
seat_limit = sum(map(to_td, seat_sum), timedelta()) # noqa:
day_max = to_td(CFG["day_max"] + ':00')
seat_max = to_td(CFG["seat_max"] + ':00')

return day_max, day_limit, seat_max, seat_limit


def get_state_icon(state):
"""
Look up the state msg and return the icon name.
Expand Down Expand Up @@ -79,20 +97,14 @@ def get_state_str(cmproc, count):
:param count: seat time counter value
:type count: timedelta
"""
DAY_SUM = [CFG["day_max"] + ':00', CFG["day_snooze"] + ':00']
SEAT_SUM = [CFG["seat_max"] + ':00', CFG["seat_snooze"] + ':00']
DAY_LIMIT = sum(map(to_td, DAY_SUM), timedelta()) # noqa:
SEAT_LIMIT = sum(map(to_td, SEAT_SUM), timedelta()) # noqa:
DAY_MAX = to_td(CFG["day_max"] + ':00')
SEAT_MAX = to_td(CFG["seat_max"] + ':00')
DAY_MAX, DAY_LIMIT, SEAT_MAX, SEAT_LIMIT = get_delta_limits()

state = 'INACTIVE' if cmproc.returncode == 1 else 'ACTIVE'
msg = cmproc.stdout.decode('utf8')
lines = msg.splitlines()

for x in [x for x in lines if x.split(',')[0] == 'total']:
day_total = x.split(',')[1]
if day_total == '00:00:00':
return msg, state
if DAY_MAX < to_td(day_total) < DAY_LIMIT:
state = 'WARNING'
msg = f'WARNING: day max of {DAY_MAX} has been exceeded\n' + msg
Expand Down Expand Up @@ -173,10 +185,12 @@ def run_cmd(action='status', tag=None):
result = subprocess.run(cmd, capture_output=True)
if action == 'stop':
tag = parse_for_tag(result.stdout.decode())
print(f'{action} result tag: {tag}')
if DEBUG:
print(f'run_cmd {action} result tag: {tag}')
return result, tag
print(f'{action} return code: {result.returncode}')
print(f'{action} result msg: {result.stdout.decode().strip()}')
if DEBUG:
print(f'run_cmd {action} return code: {result.returncode}')
print(f'run_cmd {action} result msg: {result.stdout.decode().strip()}')

return result, result.stdout.decode().strip()

Expand All @@ -193,3 +207,4 @@ def to_td(h):


CFG, _ = Munch.toDict(get_config())
DEBUG = os.getenv('DEBUG', default=None)

0 comments on commit 9d3a7c0

Please sign in to comment.