Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Tests

on:
push:
branches:
- main
pull_request:

jobs:
test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]

steps:
- name: Check out repository
uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: python -m pip install --upgrade pip && pip install -e ".[dev]"

- name: Run tests
run: pytest
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ __pycache__
build/
dist/
*.egg-info/
tmp/
tmp/
.codex*
.agents*
6 changes: 3 additions & 3 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
envstack
drawcal

Copyright (c) 2022-2025, Ryan Galloway (ryan@rsgalloway.com)
Copyright (c) 2022-2025, Bnbnotify
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
* Neither the name of Bnbnotify nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 changes: 23 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,24 @@ Python library for drawing simple monthly calendar images with events.

## Installation

The easiest way to install:
Install from PyPI:

```bash
$ pip install -U drawcal
```

Alternatively, to install with distman:
Build locally:

```bash
$ dist [-d]
$ python -m pip install --upgrade build
$ python -m build
```

Upload to PyPI:

```bash
$ python -m pip install --upgrade twine
$ python -m twine upload dist/*
```

## Quickstart
Expand All @@ -33,3 +41,15 @@ Python:
>>> from drawcal import draw_calendar
>>> draw_calendar(month, year, events=events, outfile=outfile)
```

## Events format

`drawcal` expects a JSON file containing a list of events, where each event is a
list of dates in `M/D/YYYY` format:

```json
[
["3/1/2025", "3/2/2025", "3/3/2025"],
["3/14/2025", "3/15/2025"]
]
```
4 changes: 2 additions & 2 deletions bin/drawcal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python
#!/usr/bin/env python3
#
# Copyright (c) 2022-2025, Ryan Galloway (ryan@rsgalloway.com)
# Copyright (c) 2022-2025, Bnbnotify
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
Expand Down
12 changes: 7 additions & 5 deletions lib/drawcal/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python
#
# Copyright (c) 2022-2025, Ryan Galloway (ryan@rsgalloway.com)
# Copyright (c) 2022-2025, Bnbnotify
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
Expand Down Expand Up @@ -34,11 +34,13 @@
"""

__prog__ = "drawcal"
__version__ = "0.5.6"
__version__ = "0.5.8"
__author__ = "ryan@rsgalloway.com"

import envstack

envstack.init(__prog__)
def draw_calendar(*args, **kwargs):
"""Lazily import the renderer so package metadata stays cheap to import."""

from drawcal.drawlib import draw_calendar
from drawcal.drawlib import draw_calendar as _draw_calendar

return _draw_calendar(*args, **kwargs)
8 changes: 6 additions & 2 deletions lib/drawcal/cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python
#
# Copyright (c) 2022-2025, Ryan Galloway (ryan@rsgalloway.com)
# Copyright (c) 2022-2025, Bnbnotify
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
Expand Down Expand Up @@ -37,7 +37,6 @@
from datetime import datetime, timedelta

from drawcal import __prog__, __version__
from drawcal.drawlib import draw_calendar
from drawcal.events import get_events, read_events

d = datetime.today()
Expand Down Expand Up @@ -92,6 +91,9 @@ def parse_args():

def main():
"""Main event loop."""
import envstack

envstack.init(__prog__)

args = parse_args()

Expand All @@ -100,6 +102,8 @@ def main():
else:
events = get_events(args.month, args.year)

from drawcal.drawlib import draw_calendar

draw_calendar(month=args.month, year=args.year, events=events, outfile=args.outfile)

return 0
Expand Down
2 changes: 1 addition & 1 deletion lib/drawcal/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python
#
# Copyright (c) 2022-2025, Ryan Galloway (ryan@rsgalloway.com)
# Copyright (c) 2022-2025, Bnbnotify
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
Expand Down
20 changes: 12 additions & 8 deletions lib/drawcal/drawlib.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python
#
# Copyright (c) 2022-2025, Ryan Galloway (ryan@rsgalloway.com)
# Copyright (c) 2022-2025, Bnbnotify
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
Expand Down Expand Up @@ -68,6 +68,13 @@ class colors:
title_text = "#101010" # month/year text color


def _text_size(draw, text, font):
"""Return text width/height across Pillow versions."""

left, top, right, bottom = draw.textbbox((0, 0), text, font=font)
return right - left, bottom - top


def draw_calendar(
month=today.month,
year=today.year,
Expand Down Expand Up @@ -106,7 +113,7 @@ def draw_calendar(
pad = 20

# make sure events is a list
if events == None:
if events is None:
events = []

# categorize and track dates
Expand All @@ -133,7 +140,7 @@ def draw_calendar(

# draw the month and year
font = ImageFont.truetype(config.ARIAL_TTF_FILE, size=15)
header_w, header_h = draw.textsize(header, font=font)
header_w, header_h = _text_size(draw, header, font)
draw.text(
((width - header_w) / 2, pad / 2), header, fill=colors.title_text, font=font
)
Expand Down Expand Up @@ -233,11 +240,8 @@ def draw_calendar(
continue

# handle each day in event
if first_day == col:
if first_day == curr_day:
s = 0
if last_day == col:
e = 22

# check-in
if first_day == curr_day:
checkin = True
Expand Down Expand Up @@ -332,7 +336,7 @@ def draw_calendar(

# draw days of the week and date numbers
col_font = ImageFont.truetype(config.ARIAL_TTF_FILE, size=12)
col_w, col_h = draw.textsize(col, font=col_font)
col_w, col_h = _text_size(draw, col, col_font)

# set date text color
if past_date:
Expand Down
37 changes: 27 additions & 10 deletions lib/drawcal/events.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python
#
# Copyright (c) 2022-2025, Ryan Galloway (ryan@rsgalloway.com)
# Copyright (c) 2022-2025, Bnbnotify
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
Expand Down Expand Up @@ -34,6 +34,7 @@
"""

import json
from calendar import monthrange
from datetime import datetime, timedelta

d = datetime.today()
Expand All @@ -47,17 +48,20 @@ def get_events(month=today.month, year=today.year):
from random import randint

events = []
last_day = monthrange(year, month)[1]
num_events = randint(2, 8)
i = 1

for _ in range(2, num_events):
event = []
for dd in range(i, randint(i + 2, i + randint(3, 8))):
if dd > last_day:
break
event.append(f"{month}/{dd}/{year}")
i += 1
if i >= 31:
if i > last_day:
break
if i >= 31:
if i > last_day:
break
i += randint(1, 10)
if event:
Expand All @@ -74,13 +78,26 @@ def get_events(month=today.month, year=today.year):
def read_events(filepath):
"""Returns JSON serialized events from a given filepath."""

events = []

try:
fp = open(filepath)
events = json.load(fp)
fp.close()
except Exception as e:
print(e)
with open(filepath, encoding="utf-8") as fp:
events = json.load(fp)
except OSError as exc:
raise OSError(f"unable to read events file: {filepath}") from exc
except json.JSONDecodeError as exc:
raise ValueError(f"invalid JSON in events file: {filepath}") from exc

if not isinstance(events, list):
raise ValueError("events file must contain a list of event lists")

for event in events:
if not isinstance(event, list):
raise ValueError("each event must be a list of date strings")
for day in event:
if not isinstance(day, str):
raise ValueError("each event date must be a string in M/D/YYYY format")
try:
datetime.strptime(day, "%m/%d/%Y")
except (TypeError, ValueError) as exc:
raise ValueError(f"invalid event date: {day}") from exc
Comment thread
rsgalloway marked this conversation as resolved.

return events
69 changes: 0 additions & 69 deletions lib/drawcal/test.py

This file was deleted.

Loading
Loading