diff --git a/scripts/timew-status-indicator b/scripts/timew-status-indicator index d9b3f4e..90773a8 100755 --- a/scripts/timew-status-indicator +++ b/scripts/timew-status-indicator @@ -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, @@ -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): @@ -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(): diff --git a/src/timew_status/__init__.py b/src/timew_status/__init__.py index 7025c10..bbebaf8 100644 --- a/src/timew_status/__init__.py +++ b/src/timew_status/__init__.py @@ -6,6 +6,8 @@ from .utils import ( CFG, + DEBUG, + get_delta_limits, get_state_icon, get_state_str, get_status, @@ -26,7 +28,9 @@ "__description__", "__version__", "CFG", + "DEBUG", "TAG", + "get_delta_limits", "get_state_icon", "get_state_str", "get_status", diff --git a/src/timew_status/utils.py b/src/timew_status/utils.py index 0356219..086e09a 100644 --- a/src/timew_status/utils.py +++ b/src/timew_status/utils.py @@ -1,5 +1,6 @@ """ """ +import os import subprocess import sys from datetime import timedelta @@ -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. @@ -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 @@ -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() @@ -193,3 +207,4 @@ def to_td(h): CFG, _ = Munch.toDict(get_config()) +DEBUG = os.getenv('DEBUG', default=None)