"""liturgy.py — Western (Gregorian) liturgical calendar for the rice tools. Pure date math, no I/O. Colors are expressed as ACCENT ROLE NAMES (gold, iris, love, ...) — the caller resolves them through ricefx's active theme, so purple is Rose Pine iris on rose-pine and Tokyo Night purple on tokyo-night without this module knowing about themes at all. """ from __future__ import annotations import datetime as _dt from collections import namedtuple Season = namedtuple("Season", "id name roles") Feast = namedtuple("Feast", "id name emblem verse roles") def easter(year: int) -> _dt.date: """Gregorian computus (Anonymous/Meeus algorithm).""" a = year % 19 b, c = divmod(year, 100) d, e = divmod(b, 4) f = (b + 8) // 25 g = (b - f + 1) // 3 h = (19 * a + b - d - g + 15) % 30 i, k = divmod(c, 4) l = (32 + 2 * e + 2 * i - h - k) % 7 m = (a + 11 * h + 22 * l) // 451 month, day = divmod(h + l - 7 * m + 114, 31) return _dt.date(year, month, day + 1) SEASONS = { "advent": Season("advent", "Advent", ("iris", "subtle")), "christmastide": Season("christmastide", "Christmastide", ("gold", "text")), "ordinary": Season("ordinary", "Ordinary Time", ("love", "gold")), "lent": Season("lent", "Lent", ("iris", "muted")), "holy_week": Season("holy_week", "Holy Week", ("love", "muted")), "eastertide": Season("eastertide", "Eastertide", ("gold", "foam")), } FEAST_DEFS = { "christmas": Feast("christmas", "Christmas", "star", "For unto you is born this day a Saviour — Luke 2:11", ("gold", "text")), "epiphany": Feast("epiphany", "Epiphany", "star", "We have seen his star in the east — Matthew 2:2", ("gold", "iris")), "ash_wednesday": Feast("ash_wednesday", "Ash Wednesday", "orthodox", "Dust thou art, and unto dust shalt thou return — Genesis 3:19", ("iris", "muted")), "palm_sunday": Feast("palm_sunday", "Palm Sunday", "jerusalem", "Blessed is he that cometh in the name of the Lord — Mark 11:9", ("pine", "foam")), "good_friday": Feast("good_friday", "Good Friday", "thorns", "It is finished — John 19:30", ("love", "muted")), "easter_day": Feast("easter_day", "Easter", "chi", "He is not here: for he is risen — Matthew 28:6", ("gold", "foam")), "ascension": Feast("ascension", "Ascension", "chi", "He was received up into heaven — Mark 16:19", ("foam", "text")), "pentecost": Feast("pentecost", "Pentecost", "dove", "And they were all filled with the Holy Ghost — Acts 2:4", ("love", "gold")), "all_saints": Feast("all_saints", "All Saints", "orthodox", "Blessed are the pure in heart — Matthew 5:8", ("gold", "text")), } def _advent1(year: int) -> _dt.date: """Fourth Sunday before Christmas (the Sunday STRICTLY before Dec 25, minus three more weeks).""" xmas = _dt.date(year, 12, 25) dow = xmas.isoweekday() % 7 # Sunday -> 0 last_sun = xmas - _dt.timedelta(days=dow or 7) return last_sun - _dt.timedelta(days=21) def _feast_dates(year: int) -> dict[str, _dt.date]: e = easter(year) return { "christmas": _dt.date(year, 12, 25), "epiphany": _dt.date(year, 1, 6), "ash_wednesday": e - _dt.timedelta(days=46), "palm_sunday": e - _dt.timedelta(days=7), "good_friday": e - _dt.timedelta(days=2), "easter_day": e, "ascension": e + _dt.timedelta(days=39), "pentecost": e + _dt.timedelta(days=49), "all_saints": _dt.date(year, 11, 1), } def season(d: _dt.date) -> Season: y = d.year if d >= _dt.date(y, 12, 25) or d <= _dt.date(y, 1, 5): return SEASONS["christmastide"] if d >= _advent1(y): return SEASONS["advent"] e = easter(y) ash = e - _dt.timedelta(days=46) palm = e - _dt.timedelta(days=7) pentecost = e + _dt.timedelta(days=49) if d < ash: return SEASONS["ordinary"] # Epiphanytide / Ordinary I if d < palm: return SEASONS["lent"] if d < e: return SEASONS["holy_week"] if d <= pentecost: return SEASONS["eastertide"] return SEASONS["ordinary"] # Ordinary II def feast(d: _dt.date) -> Feast | None: for fid, fd in _feast_dates(d.year).items(): if fd == d: return FEAST_DEFS[fid] return None