Skip to content

Customize category prompts through the .toml configuration file. #8

@rebron1900

Description

@rebron1900

I forked this repository and intended to implement custom reminders for categories through the following modifications, but an error occurred in the "Run tests" stage of Github Action.

D:\a\_temp\a36ab23e-0012-4baa-939f-a3b63fe4030f.sh: line 1: venv/bin/activate: No such file or directory
poetry run aw-notify --help  # Ensures that it at least starts
Usage: aw-notify [OPTIONS] COMMAND [ARGS]...

Options:
  -v, --verbose  Verbose logging.
  --testing      Enables testing mode.
  --help         Show this message and exit.

Commands:
  checkin  Send a summary notification.
  start    Start the notification service.
make typecheck
make[1]: Entering directory 'D:/a/aw-notify/aw-notify'
poetry run mypy aw_notify --ignore-missing-imports
aw_notify\main.py:307: error: Item "Item" of "Item | Container" has no attribute "__iter__" (not iterable)  [union-attr]
aw_notify\main.py:308: error: Invalid index type "str" for "str"; expected type "SupportsIndex | slice[Any, Any, Any]"  [index]
aw_notify\main.py:309: error: Invalid index type "str" for "str"; expected type "SupportsIndex | slice[Any, Any, Any]"  [index]
aw_notify\main.py:310: error: Item "str" of "Any | str" has no attribute "get"  [union-attr]
aw_notify\main.py:311: error: Item "str" of "Any | str" has no attribute "get"  [union-attr]
Found 5 errors in 1 file (checked 3 source files)
make[1]: *** [Makefile:11: typecheck] Error 1
make[1]: Leaving directory 'D:/a/aw-notify/aw-notify'
make: *** [Makefile:8: test] Error 2

config.toml

[[alerts]]
category = "All"
thresholds = ["1:00:00", "2:00:00", "4:00:00", "6:00:00", "8:00:00"]
label = "All"

[[alerts]]
category = "Twitter"
thresholds = ["0:15:00", "0:30:00", "1:00:00"]
label = "🐦 Twitter"

[[alerts]]
category = "Youtube"
thresholds = ["0:15:00", "0:30:00", "1:00:00"]
label = "📺 Youtube"

[[alerts]]
category = "Work"
thresholds = ["0:15:00", "0:30:00", "1:00:00", "2:00:00", "4:00:00"]
label = "💼 Work"
positive = true

Added Python code

import toml
from datetime import timedelta

def load_alerts_from_config(file_path: str) -> list[CategoryAlert]:
    """从 TOML 文件加载警报配置。"""
    config = toml.load(file_path)
    
    alerts = []
    for alert_config in config.get('alerts', []):
        category = alert_config['category']
        thresholds = [timedelta(hours=int(th.split(':')[0]), minutes=int(th.split(':')[1]), seconds=int(th.split(':')[2])) for th in alert_config['thresholds']]
        label = alert_config.get('label', category)
        positive = alert_config.get('positive', False)
        
        alerts.append(CategoryAlert(category, thresholds, label, positive))
    
    return alerts

def threshold_alerts():
    """
    检查每个类别的已用时间,并在达到阈值时触发警报。
    """
    # 从配置文件加载警报
    alerts = load_alerts_from_config('config.toml')

    # 遍历一次以检查是否达到任何阈值
    for alert in alerts:
        alert.update()
        alert.check(silent=True)

    while True:
        for alert in alerts:
            alert.update()
            alert.check()
            status = alert.status()
            if status != getattr(alert, "last_status", None):
                logger.debug(f"新状态: {status}")
                setattr(alert, "last_status", status)

        # TODO: 使其可配置,可能增加默认值以节省资源
        sleep(10)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions