gateway/modules/workflows/automation2/scheduleCron.py

34 lines
1.1 KiB
Python

# Copyright (c) 2025 Patrick Motsch
"""
Parse cron strings (5-field or 6-field) to APScheduler CronTrigger kwargs.
Frontend produces: "minute hour day month dow" (5-field) or "sec min hour day month dow" (6-field).
"""
import re
from typing import Any, Dict
def parse_cron_to_kwargs(cron: str) -> Dict[str, Any]:
"""
Parse cron string to kwargs for APScheduler CronTrigger.
Supports 5-field (minute hour day month day_of_week) and 6-field (sec min hour day month day_of_week).
Returns dict with: second, minute, hour, day, month, day_of_week.
"""
if not cron or not isinstance(cron, str):
raise ValueError("Invalid cron: empty or not string")
parts = cron.strip().split()
if len(parts) == 5:
minute, hour, day, month, day_of_week = parts
second = "0"
elif len(parts) == 6:
second, minute, hour, day, month, day_of_week = parts
else:
raise ValueError(f"Invalid cron format: expected 5 or 6 fields, got {len(parts)}")
return {
"second": second,
"minute": minute,
"hour": hour,
"day": day,
"month": month,
"day_of_week": day_of_week,
}