kcal-cli 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
kcal.py ADDED
@@ -0,0 +1,233 @@
1
+ #!/usr/bin/env python3
2
+ """kcal - non-interactive CLI for managing a single .ics calendar file."""
3
+
4
+ import argparse
5
+ import sys
6
+ import uuid
7
+ from datetime import date, datetime
8
+ from pathlib import Path
9
+
10
+ from icalendar import Calendar, Event
11
+
12
+
13
+ def load_calendar(path: Path) -> Calendar:
14
+ if path.exists():
15
+ return Calendar.from_ical(path.read_bytes())
16
+ cal = Calendar()
17
+ cal.add("prodid", "-//kcal//EN")
18
+ cal.add("version", "2.0")
19
+ return cal
20
+
21
+
22
+ def save_calendar(cal: Calendar, path: Path):
23
+ path.write_bytes(cal.to_ical())
24
+
25
+
26
+ def parse_dt(s: str) -> datetime:
27
+ for fmt in ("%Y-%m-%d %H:%M", "%Y-%m-%d %H:%M:%S", "%Y-%m-%dT%H:%M", "%Y-%m-%d"):
28
+ try:
29
+ return datetime.strptime(s, fmt)
30
+ except ValueError:
31
+ continue
32
+ raise ValueError(f"Cannot parse datetime: {s!r}")
33
+
34
+
35
+ def parse_date(s: str) -> date:
36
+ for fmt in ("%Y-%m-%d",):
37
+ try:
38
+ return datetime.strptime(s, fmt).date()
39
+ except ValueError:
40
+ continue
41
+ raise ValueError(f"Cannot parse date: {s!r}")
42
+
43
+
44
+ def cmd_add(args):
45
+ cal = load_calendar(args.file)
46
+ event = Event()
47
+ event.add("uid", str(uuid.uuid4()))
48
+ event.add("summary", args.summary)
49
+ if args.allday:
50
+ start_date = parse_date(args.start)
51
+ event.add("dtstart", start_date)
52
+ if args.end:
53
+ end_date = parse_date(args.end)
54
+ else:
55
+ from datetime import timedelta
56
+ end_date = start_date + timedelta(days=1)
57
+ event.add("dtend", end_date)
58
+ else:
59
+ event.add("dtstart", parse_dt(args.start))
60
+ event.add("dtend", parse_dt(args.end))
61
+ if args.description:
62
+ event.add("description", args.description)
63
+ if args.location:
64
+ event.add("location", args.location)
65
+ cal.add_component(event)
66
+ save_calendar(cal, args.file)
67
+ print(f"Added: {args.summary} (uid: {event['uid']})")
68
+
69
+
70
+ def cmd_list(args):
71
+ cal = load_calendar(args.file)
72
+ events = []
73
+ for component in cal.walk():
74
+ if component.name != "VEVENT":
75
+ continue
76
+ dtstart = component.get("dtstart")
77
+ if dtstart:
78
+ dtstart = dtstart.dt
79
+ events.append((dtstart, component))
80
+
81
+ if args.date:
82
+ target = parse_dt(args.date).date()
83
+ filtered = []
84
+ for dt, e in events:
85
+ if dt is None:
86
+ continue
87
+ if isinstance(dt, datetime):
88
+ if dt.date() == target:
89
+ filtered.append((dt, e))
90
+ elif isinstance(dt, date):
91
+ if dt == target:
92
+ filtered.append((dt, e))
93
+ events = filtered
94
+
95
+ def sort_key(x):
96
+ dt = x[0]
97
+ if dt is None:
98
+ return (0, datetime.min)
99
+ if isinstance(dt, datetime):
100
+ return (1, dt.replace(tzinfo=None) if dt.tzinfo else dt)
101
+ if isinstance(dt, date):
102
+ return (1, datetime(dt.year, dt.month, dt.day))
103
+ return (0, datetime.min)
104
+
105
+ events.sort(key=sort_key)
106
+
107
+ if not events:
108
+ print("No events found.")
109
+ return
110
+
111
+ for dt, e in events:
112
+ uid = str(e.get("uid", "???"))
113
+ summary = str(e.get("summary", "(no title)"))
114
+ is_allday = isinstance(dt, date) and not isinstance(dt, datetime)
115
+ if is_allday:
116
+ start = dt.strftime("%Y-%m-%d") + " (all day)"
117
+ end = ""
118
+ else:
119
+ start = dt.strftime("%Y-%m-%d %H:%M")
120
+ dtend = e.get("dtend")
121
+ end = " - " + dtend.dt.strftime("%H:%M") if dtend else ""
122
+ print(f" {start}{end} {summary} [uid: {uid[:8]}...]")
123
+
124
+
125
+ def cmd_delete(args):
126
+ cal = load_calendar(args.file)
127
+ new_cal = Calendar()
128
+ for k, v in cal.items():
129
+ new_cal.add(k, v)
130
+
131
+ found = False
132
+ for component in cal.walk():
133
+ if component.name == "VEVENT":
134
+ uid = str(component.get("uid", ""))
135
+ if uid.startswith(args.uid) or uid == args.uid:
136
+ found = True
137
+ continue
138
+ if component.name != "VCALENDAR":
139
+ new_cal.add_component(component)
140
+
141
+ if not found:
142
+ print(f"No event with uid starting with {args.uid!r}", file=sys.stderr)
143
+ sys.exit(1)
144
+
145
+ save_calendar(new_cal, args.file)
146
+ print(f"Deleted event {args.uid}")
147
+
148
+
149
+ def cmd_edit(args):
150
+ cal = load_calendar(args.file)
151
+ found = False
152
+ for component in cal.walk():
153
+ if component.name != "VEVENT":
154
+ continue
155
+ uid = str(component.get("uid", ""))
156
+ if not (uid.startswith(args.uid) or uid == args.uid):
157
+ continue
158
+ found = True
159
+ if args.summary:
160
+ del component["summary"]
161
+ component.add("summary", args.summary)
162
+ if args.allday:
163
+ if "dtstart" in component:
164
+ del component["dtstart"]
165
+ if "dtend" in component:
166
+ del component["dtend"]
167
+ start_date = parse_date(args.start) if args.start else None
168
+ if start_date:
169
+ component.add("dtstart", start_date)
170
+ from datetime import timedelta
171
+ end_date = parse_date(args.end) if args.end else start_date + timedelta(days=1)
172
+ component.add("dtend", end_date)
173
+ else:
174
+ if args.start:
175
+ del component["dtstart"]
176
+ component.add("dtstart", parse_dt(args.start))
177
+ if args.end:
178
+ del component["dtend"]
179
+ component.add("dtend", parse_dt(args.end))
180
+ if args.description:
181
+ if "description" in component:
182
+ del component["description"]
183
+ component.add("description", args.description)
184
+ if args.location:
185
+ if "location" in component:
186
+ del component["location"]
187
+ component.add("location", args.location)
188
+ break
189
+
190
+ if not found:
191
+ print(f"No event with uid starting with {args.uid!r}", file=sys.stderr)
192
+ sys.exit(1)
193
+
194
+ save_calendar(cal, args.file)
195
+ print(f"Updated event {args.uid}")
196
+
197
+
198
+ def main():
199
+ parser = argparse.ArgumentParser(prog="kcal", description="Manage a .ics calendar file")
200
+ parser.add_argument("--file", "-f", type=Path, required=True, help="Path to .ics file")
201
+ sub = parser.add_subparsers(dest="command", required=True)
202
+
203
+ p_add = sub.add_parser("add", help="Add an event")
204
+ p_add.add_argument("summary", help="Event title")
205
+ p_add.add_argument("--start", "-s", required=True, help="Start date/time (YYYY-MM-DD HH:MM, or YYYY-MM-DD for all-day)")
206
+ p_add.add_argument("--end", "-e", help="End date/time (optional for all-day events)")
207
+ p_add.add_argument("--allday", action="store_true", help="Create an all-day event (uses DATE values)")
208
+ p_add.add_argument("--description", "-d", help="Description")
209
+ p_add.add_argument("--location", "-l", help="Location")
210
+
211
+ p_list = sub.add_parser("list", help="List events")
212
+ p_list.add_argument("--date", help="Filter by date (YYYY-MM-DD)")
213
+
214
+ p_del = sub.add_parser("delete", help="Delete an event by UID (prefix match)")
215
+ p_del.add_argument("uid", help="UID or UID prefix")
216
+
217
+ p_edit = sub.add_parser("edit", help="Edit an event by UID")
218
+ p_edit.add_argument("uid", help="UID or UID prefix")
219
+ p_edit.add_argument("--summary", help="New title")
220
+ p_edit.add_argument("--start", "-s", help="New start date/time")
221
+ p_edit.add_argument("--end", "-e", help="New end date/time")
222
+ p_edit.add_argument("--allday", action="store_true", help="Convert to an all-day event")
223
+ p_edit.add_argument("--description", "-d", help="New description")
224
+ p_edit.add_argument("--location", "-l", help="New location")
225
+
226
+ args = parser.parse_args()
227
+ if args.command == "add" and not args.allday and not args.end:
228
+ parser.error("--end is required for non-allday events")
229
+ {"add": cmd_add, "list": cmd_list, "delete": cmd_delete, "edit": cmd_edit}[args.command](args)
230
+
231
+
232
+ if __name__ == "__main__":
233
+ main()
@@ -0,0 +1,99 @@
1
+ Metadata-Version: 2.4
2
+ Name: kcal-cli
3
+ Version: 0.1.0
4
+ Summary: Non-interactive CLI for managing local iCalendar (.ics) files
5
+ Author: hebbian
6
+ License-Expression: MIT
7
+ License-File: LICENSE
8
+ Keywords: calendar,cli,ical,ics,korganizer
9
+ Classifier: Development Status :: 4 - Beta
10
+ Classifier: Environment :: Console
11
+ Classifier: Intended Audience :: End Users/Desktop
12
+ Classifier: License :: OSI Approved :: MIT License
13
+ Classifier: Topic :: Office/Business :: Scheduling
14
+ Requires-Python: >=3.10
15
+ Requires-Dist: icalendar>=6.0.0
16
+ Description-Content-Type: text/markdown
17
+
18
+ # kcal
19
+
20
+ Non-interactive CLI for managing local iCalendar (`.ics`) files. Designed for scripting and AI agent integration — no GUI, no server, just direct `.ics` file manipulation.
21
+
22
+ ## Install
23
+
24
+ ```
25
+ pip install kcal-cli
26
+ ```
27
+
28
+ Or with uv:
29
+
30
+ ```
31
+ uv tool install kcal-cli
32
+ ```
33
+
34
+ ## Usage
35
+
36
+ All commands require `-f <path>` pointing to your `.ics` file.
37
+
38
+ ### List events
39
+
40
+ ```
41
+ kcal -f ~/calendar.ics list
42
+ kcal -f ~/calendar.ics list --date 2026-06-25
43
+ ```
44
+
45
+ ### Add a timed event
46
+
47
+ ```
48
+ kcal -f ~/calendar.ics add "Meeting" -s "2026-06-26 09:00" -e "2026-06-26 10:00"
49
+ ```
50
+
51
+ ### Add an all-day event
52
+
53
+ ```
54
+ kcal -f ~/calendar.ics add "Holiday" -s "2026-07-04" --allday
55
+ ```
56
+
57
+ For multi-day events, set `--end` to the day after the last day (iCal convention):
58
+
59
+ ```
60
+ kcal -f ~/calendar.ics add "Conference" -s "2026-08-01" -e "2026-08-04" --allday
61
+ ```
62
+
63
+ ### Edit an event
64
+
65
+ ```
66
+ kcal -f ~/calendar.ics edit 1cc4704e --summary "New title"
67
+ kcal -f ~/calendar.ics edit 1cc4704e --start "2026-06-26 10:00" --end "2026-06-26 11:00"
68
+ kcal -f ~/calendar.ics edit 1cc4704e --start "2026-06-26" --allday
69
+ ```
70
+
71
+ ### Delete an event
72
+
73
+ ```
74
+ kcal -f ~/calendar.ics delete 1cc4704e
75
+ ```
76
+
77
+ UID prefix matching — only the first few characters of the event UID are needed.
78
+
79
+ ## Options
80
+
81
+ | Flag | Description |
82
+ |------|-------------|
83
+ | `-f`, `--file` | Path to `.ics` file (required) |
84
+ | `-s`, `--start` | Start date/time (`YYYY-MM-DD HH:MM` or `YYYY-MM-DD`) |
85
+ | `-e`, `--end` | End date/time (optional for all-day events) |
86
+ | `--allday` | Create/convert to all-day event (uses `VALUE=DATE`) |
87
+ | `-d`, `--description` | Event description |
88
+ | `-l`, `--location` | Event location |
89
+
90
+ ## Notes
91
+
92
+ - Creates the `.ics` file if it doesn't exist
93
+ - All-day events use proper `VALUE=DATE` entries (not midnight-to-midnight)
94
+ - Works with KOrganizer, GNOME Calendar, Thunderbird, or any iCal-compatible app
95
+ - If your calendar app is open, you may need to refresh to see changes
96
+
97
+ ## License
98
+
99
+ MIT
@@ -0,0 +1,6 @@
1
+ kcal.py,sha256=cQt078juwY_IDPpqw1WnS5XyoPd9GvrHnug_Hsm5uWg,7972
2
+ kcal_cli-0.1.0.dist-info/METADATA,sha256=Afv0YZIf7RCqYudlY5SnOPiIwMEqein47YdBx168Kzk,2488
3
+ kcal_cli-0.1.0.dist-info/WHEEL,sha256=mffPy8wBnZQn2VnJUU5jE99KsxaSfiyMHV9Yt0aLVxs,87
4
+ kcal_cli-0.1.0.dist-info/entry_points.txt,sha256=uk_YmNCGdcuGn1SYv2FrawBTGJ6nQuABN71X73e_D6g,35
5
+ kcal_cli-0.1.0.dist-info/licenses/LICENSE,sha256=BH3mwC0hyeb9ypYMehVh8FZ6PjyoyfwNwSIqicW-Lkg,1064
6
+ kcal_cli-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.30.1
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ kcal = kcal:main
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 hebbian
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.