summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTobias Brox <tobias@redpill-linpro.com>2014-03-07 23:28:55 +0100
committerTobias Brox <tobias@redpill-linpro.com>2014-03-07 23:28:55 +0100
commit06fd72260d003c9c01c98eccfeb0dbc6e8b87872 (patch)
tree648224b963c6b3019f36d06c5222872fc82c8723
parent9bc2572fd579902773323cdc36e727685a7879ba (diff)
downloadcalendar-cli-06fd72260d003c9c01c98eccfeb0dbc6e8b87872.zip
library exchange
-rw-r--r--README.md4
-rwxr-xr-xcalendar-cli.py47
2 files changed, 20 insertions, 31 deletions
diff --git a/README.md b/README.md
index a0f675c..63bd746 100644
--- a/README.md
+++ b/README.md
@@ -11,7 +11,7 @@ GUIs and Web-UIs are nice for some purposes, but I really find the command line
* Minor stuff that are repeated often. Writing something like "todo add make a calendar-cli system" or "cal add 'tomorrow 15:40+2h' doctor appointment" is just very much faster than navigating into some web calendar interface and add an item there.
* Things that are outside the scope of the UI. Here is one of many tasks I'd like to do: "go through the work calendar, find all new calendar events that are outside office hours, check up with the personal calendar if there are potential conflicts, add some information at the personal calendar if appropriate", and vice versa - it has to be handled very manually if doing it through any normal calendar application as far as I know, but if having some simple CLI or python library I could easily make some interactive script that would help me doing the operation above.
-I've been looking a bit around, all I could find was cadaver and CalDAVClientLibrary. Both of those seems to be a bit shortcoming; they seem to miss the iCalendar parsing/generation. CalDAVClientLibrary is already a python library, and there also exist python libraries for iCalendar parsing/generation, so all that seems to be missing is the "glue" between those libraries.
+I've been looking a bit around, all I could find was cadaver and CalDAVClientLibrary. Both of those seems to be a bit shortcoming; they seem to miss the iCalendar parsing/generation. CalDAVClientLibrary is already a python library, and there also exist python libraries for iCalendar parsing/generation, so all that seems to be missing is the "glue" between those libraries. Or, eventually, not. After some problems I decided to ditch CalDAVClientLibrary.
Synopsis
--------
@@ -117,3 +117,5 @@ Status
2013-09-28: version 0.02 - possible to add a calendar item to the caldav server
2013-10-02: version 0.03 - support for configuration file
2013-10-05: version 0.04 - no need to specify URL for the default calendar
+2013-12 - 2014-03: helped cyrilrbt on making a new release of the caldav library
+2014-03-07: version 0.05 - rewrote parts of the tool to using the caldav library. Nice!!!
diff --git a/calendar-cli.py b/calendar-cli.py
index 8aa29ae..4985e25 100755
--- a/calendar-cli.py
+++ b/calendar-cli.py
@@ -1,4 +1,4 @@
-#!/usr/bin/python2.7
+#!/usr/bin/python2
## (the icalendar library is not ported to python3?)
@@ -8,15 +8,14 @@ import pytz
from datetime import datetime, timedelta
import dateutil.parser
from icalendar import Calendar,Event
-from caldavclientlibrary.client.account import CalDAVAccount
-from caldavclientlibrary.protocol.url import URL
+import caldav
import uuid
import json
import os
import logging
import sys
-__version__ = "0.05"
+__version__ = "0.5.1"
__author__ = "Tobias Brox"
__author_short__ = "tobixen"
__copyright__ = "Copyright 2013, Tobias Brox"
@@ -34,27 +33,18 @@ def niy(*args, **kwargs):
def caldav_connect(args):
# Create the account
- splits = urlparse.urlsplit(args.caldav_url)
- ssl = (splits.scheme == "https")
- return CalDAVAccount(splits.netloc, ssl=ssl, user=args.caldav_user, pswd=args.caldav_pass, root=(splits.path or '/'), principal=None, logging=args.debug_logging)
+ return caldav.DAVClient(url=args.caldav_url, username=args.caldav_user, password=args.caldav_pass)
-def find_calendar_url(caldav_conn, args):
+## TODO
+def find_calendar(caldav_conn, args):
if args.calendar_url:
- splits = urlparse.urlsplit(args.calendar_url)
- if splits.path.startswith('/') or splits.scheme:
- ## assume fully qualified URL or absolute path
- calendar = args.calendar_url
+ if '/' in args.calendar_url:
+ return caldav.Calendar(client=caldav_conn, url=args.calendar_url)
else:
- ## assume relative path
- calendar = args.caldav_url + args.calendar_url
+ return caldav.Principal(caldav_conn).calendar(name=args.calendar_url)
else:
## Find default calendar
- calendar = caldav_conn.getPrincipal().listCalendars()[0].path
-
- ## Unique file name
- if not calendar.endswith('/'):
- calendar += '/'
- return calendar
+ return caldav.Principal(caldav_conn).calendars()[0]
def _calendar_addics(caldav_conn, ics, uid, args):
""""
@@ -70,8 +60,8 @@ def _calendar_addics(caldav_conn, ics, uid, args):
raise ValueError("Nothing to do/invalid option combination for 'calendar add'-mode; either both --icalendar and --nocaldav should be set, or none of them")
return
- url = URL(find_calendar_url(caldav_conn, args) + str(uid) + '.ics')
- caldav_conn.session.writeData(url, ics, 'text/calendar', method='PUT')
+ c = find_calendar(caldav_conn, args)
+ c.add_event(ics)
def calendar_addics(caldav_conn, args):
"""
@@ -148,16 +138,13 @@ def calendar_agenda(caldav_conn, args):
dtend = dtstart + timedelta(1,0)
## TODO: time zone
## No need with "expand" - as for now the method below throws away the expanded data :-( We get a list of URLs, and then we need to do a get on each and one of them ...
- collection = caldav_conn.session.queryCollection(URL(find_calendar_url(caldav_conn, args)), timerange=True, start=dtstart.strftime("%FT%H%M%S"), end=dtend.strftime("%FT%H%M%S"), expand=False)
- ret_ical = []
- for url in collection:
- ret_ical.append(caldav_conn.session.readData(url))
+ events = caldav_conn.date_search(dtstart, dtend)
if args.icalendar:
- for ical in ret_ical:
- print ical
+ for ical in events:
+ print ical.data
else:
import pdb; pdb.set_trace()
- ret_ical = [Calendar.from_ical(c[0]) for c in ret_ical]
+ events = [Calendar.from_ical(event.data) for event in events]
niy("parse calendar events and print them in a nice format")
def main():
@@ -217,7 +204,7 @@ def main():
subparsers = parser.add_subparsers(title='command')
calendar_parser = subparsers.add_parser('calendar')
- calendar_parser.add_argument("--calendar-url", help="URL for calendar to be used (may be absolute or relative to caldav URL)")
+ calendar_parser.add_argument("--calendar-url", help="URL for calendar to be used (may be absolute or relative to caldav URL, or just the name of the calendar)")
calendar_subparsers = calendar_parser.add_subparsers(title='subcommand')
calendar_add_parser = calendar_subparsers.add_parser('add')
calendar_add_parser.add_argument('event_time', help="Timestamp and duration of the event. See the documentation for event_time specifications")