Skip to main content

Pydantic models for cron

Project description

cron-pydantic

Pydantic models for cron schedules, jobs, and crontab files.

Build Status codecov License PyPI

Installation

pip install cron-pydantic

Usage

Define a crontab from Python or validate the equivalent YAML with CronConfiguration.model_validate:

from cron_pydantic import CronConfiguration

configuration = CronConfiguration.model_validate(
    {
        "environment": {"PATH": "/usr/local/bin:/usr/bin"},
        "job": {
            "backup": {
                "schedule": "0 2 * * *",
                "command": "/opt/bin/backup",
            },
            "cleanup": {
                "schedule": "@weekly",
                "command": "/opt/bin/cleanup",
                "enabled": False,
            },
        },
    }
)

print(configuration.to_cron())

This renders:

# Generated by cron-pydantic
PATH=/usr/local/bin:/usr/bin

# cron-pydantic: backup
0 2 * * * /opt/bin/backup
# cron-pydantic: cleanup
# @weekly /opt/bin/cleanup

Use system=True and provide each job's user to render /etc/crontab or /etc/cron.d format:

configuration = CronConfiguration.model_validate(
    {
        "system": True,
        "job": {
            "backup": {
                "schedule": "@daily",
                "user": "root",
                "command": "/opt/bin/backup",
            }
        },
    }
)

Parse existing content with CronConfiguration.from_cron, load YAML or crontab files with CronConfiguration.load, and write rendered content with configuration.write(path).

cron-pydantic does not install a crontab or modify the host scheduler. This keeps rendering and validation separate from deployment and avoids replacing unmanaged crontab entries.

Supported syntax

  • Five-field user crontab schedules
  • Six-field system crontab entries containing a user
  • Lists, ranges, steps, month and weekday names, and Cronie random ranges
  • @reboot, @yearly, @annually, @monthly, @weekly, @daily, @midnight, and @hourly
  • Environment assignments and disabled jobs

[!NOTE] This library was generated using copier from the Base Python Project Template repository.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cron_pydantic-0.1.0.tar.gz (11.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cron_pydantic-0.1.0-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

File details

Details for the file cron_pydantic-0.1.0.tar.gz.

File metadata

  • Download URL: cron_pydantic-0.1.0.tar.gz
  • Upload date:
  • Size: 11.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for cron_pydantic-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ccb5822aabd8b3dac8e4ea8f62ab4a3a3b01fdd2bbbf7f1e27b3becc48b19e9a
MD5 3f0ede5c8b7b54b77deeaca9d59ab1e0
BLAKE2b-256 c1f587905c499741dd7dfd91ab598ac4c8b62fc8f40ee585e623a5407985a687

See more details on using hashes here.

File details

Details for the file cron_pydantic-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: cron_pydantic-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 11.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for cron_pydantic-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f17023d128819e710cc2cbab309f9b62bdcc04052df3684ebb947afa75e22386
MD5 a537ad0bfb3f65374bb21ce4dfafcb55
BLAKE2b-256 494437a4d376d131ae406a061ba5247bf2109030f159a743a163e8078e810b7d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page