summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTobias Brox <tobias@redpill-linpro.com>2022-01-02 01:28:53 +0100
committerTobias Brox <tobias@redpill-linpro.com>2022-01-02 01:28:53 +0100
commit1a2b0fcceb9427c652d4abfa872626684906ad5a (patch)
treec9212ef4540e9851faaf559cf6574e4b67513764
parent46f66948473dc0b0ef98525e03bdf73d43992b1d (diff)
downloadcalendar-cli-1a2b0fcceb9427c652d4abfa872626684906ad5a.zip
kal: milestone, ics data can be added to the calendar
-rw-r--r--NEXT_LEVEL.md2
-rwxr-xr-x[-rw-r--r--]cal.py44
-rw-r--r--tests/_setup_alias16
-rwxr-xr-xtests/test_calendar-cli.sh6
-rwxr-xr-xtests/tests.sh24
5 files changed, 73 insertions, 19 deletions
diff --git a/NEXT_LEVEL.md b/NEXT_LEVEL.md
index 2ed6c62..9ce4752 100644
--- a/NEXT_LEVEL.md
+++ b/NEXT_LEVEL.md
@@ -84,7 +84,7 @@ A calendar event could be "striked-out" if it has a child VJOURNAL entry. A tas
Add an event, task or journal entry:
```
-kal add event --config-section=private_calendar --set-location="Aker Brygge marina" "new years party" 2032-12-31T20:00+5h
+kal --config-section=private_calendar add --set-location="Aker Brygge marina" event 2032-12-31T20:00+5h "new years party"
kal add todo "Buy food for the new years party" --set-due=2032-12-30T20:00 --set-duration=1h
kal add journal "Captain's log" 2020-12-04 'Started from Świnoujście a bit after 03AM. Due to miscommunication, bad planning and language problems my crew member Bartek managed to throw the whole mooring rope to the sea (clearly the captains fault - I didnt explain the task "release one end of the rope, let it go into the sea and then pull it in" well enough, and he did ask "are you really sure about that?" before throwing both ends of the rope to the sea). Tail wind, between 8-16 knots relative windspeed, changed a bit between broad reach and butterfly. While going butterfly, due to a rather big wave we had an accidental jib, bad enough that the preventer rope broke off the cleat it was attached to (but luckily no damanges to the rig). There seems to be a minor salt water leakage by the rudder. Passed Falsterbo around 21, moored up in the guest harbour in Skanör around 22. Very quiet as it was way outside the season. Didnt find any obvious choice on the payment automat for harbor fee - eventually I payed SEK 100 for "tvättstuga". I got no access to the laundry room, so I decided that 100 SEK was an OK price for staying overnight with electricity off-season in Skanör.'
diff --git a/cal.py b/cal.py
index 77e7c94..b1bf140 100644..100755
--- a/cal.py
+++ b/cal.py
@@ -41,7 +41,9 @@ import caldav
@click.pass_context
def cli(ctx, **kwargs):
"""
- CalDAV Command Line Interface
+ CalDAV Command Line Interface, in development.
+
+ This command will eventually replace calendar-cli.
"""
## The cli function will prepare a context object, a dict containing the
@@ -65,29 +67,45 @@ def cli(ctx, **kwargs):
calendars.append(principal.calendar(name=calendar_name))
if not calendars:
calendars = principal.calendars()
- ctx.obj['client'] = client
- ctx.obj['principal'] = principal
ctx.obj['calendars'] = calendars
-@cli.group()
-@click.option('-l', '--add-ical-line', multiple=True)
-@click.pass_context
-def add(ctx, add_ical_line):
- click.echo("working on something here")
-
@cli.command()
@click.pass_context
def test(ctx):
"""
Will test that we can connect to the caldav server and find the calendars.
"""
- print("Seems like everything is OK")
+ click.echo("Seems like everything is OK")
+@cli.group()
+@click.option('-l', '--add-ical-line', multiple=True, help="extra ical data to be injected")
+@click.option('--multi-add/--no-multi-add', default=False, help="Add things to multiple calendars")
+@click.option('--first-calendar/--no-first-calendar', default=False, help="Add things to the first given calendar")
+@click.pass_context
+def add(ctx, **kwargs):
+ if (len(ctx.obj['calendars'])>1 and
+ not kwargs['multi_add'] and
+ not click.confirm(f"Multiple calendars given. Do you want to duplicate to {len(ctx.obj['calendars'])} calendars? (tip: use option --multi-add to avoid this prompt in the future)")):
+ calendar = ctx.obj['calendars'][0]
+ ## TODO: we need to make sure f"{calendar.name}" will always work or something
+ if (kwargs['first_calendar'] or
+ click.confirm(f"First calendar on the list has url {calendar.url} - should we add there? (tip: use --calendar-url={calendar.url} or --first_calendar to avoid this prompt in the future)")):
+ ctx.obj['calendars'] = [ calendar ]
+ else:
+ raise click.Abort()
+ click.echo("working on something here")
+ ctx.obj['ical_fragment'] = "\n".join(kwargs['add_ical_line'])
@add.command()
-def ical():
- click.echo("soon you should be able to add ical data to your calendar")
- raise NotImplementedError("foo")
+@click.pass_context
+@click.option('-d', '--ical-data', '--ical', help="ical object to be added")
+@click.option('-f', '--ical-file', type=click.File('rb'), help="file containing ical data")
+def ical(ctx, ical_data, ical_file):
+ if (ical_file):
+ ical = ical_file.read()
+ for c in ctx.obj['calendars']:
+ ## TODO: this may not be an event - should make a Calendar.save_object method
+ c.save_event(ical)
@add.command()
def todo():
diff --git a/tests/_setup_alias b/tests/_setup_alias
index 45a267c..0d5c302 100644
--- a/tests/_setup_alias
+++ b/tests/_setup_alias
@@ -1,3 +1,5 @@
+outfile=$(mktemp)
+
error() {
echo "$1"
echo "sleeping 3"
@@ -9,10 +11,18 @@ error() {
[ -z "$calendar_cli" ] && [ -x ../calendar-cli.py ] && calendar_cli=../calendar-cli.py
[ -z "$calendar_cli" ] && error "couldn't find ./calendar_cli.py nor ../calendar_cli.py"
+[ -z "$kal" ] && [ -x ./cal.py ] && kal=./cal.py
+[ -z "$kal" ] && [ -x ../cal.py ] && kal=../cal.py
+[ -z "$kal" ] && error "couldn't find ./cal.py nor ../cal.py"
+
calendar_cli() {
echo " $calendar_cli $@"
- output=$($calendar_cli "$@")
- [ -z "$output" ] || echo $output
+ output=$($calendar_cli "$@" | tee $outfile)
+ [ -z "$output" ] || [ -z "$QUIET" ] || echo $output
}
-## TODO: new kal command aka cal.py \ No newline at end of file
+kal() {
+ echo " $kal $@"
+ output=$($kal "$@" | tee $outfile)
+ [ -z "$output" ] || [ -z "$QUIET" ] || echo $output
+}
diff --git a/tests/test_calendar-cli.sh b/tests/test_calendar-cli.sh
index 6d22973..c2eae03 100755
--- a/tests/test_calendar-cli.sh
+++ b/tests/test_calendar-cli.sh
@@ -11,7 +11,7 @@ radicale_pid=$(jobs -l | perl -ne '/^\[\d+\]\+\s+(\d+)\s+Running/ && print $1')
if [ -n "$radicale_pid" ]
then
echo "## Radicale now running on pid $radicale_pid"
- calendar_cli="../calendar-cli --caldav-url=http://localhost:5232/ --caldav-user=testuser --calendar-url=/testuser/calendar-cli-test-calendar"
+ calendar_cli="../calendar-cli.py --caldav-url=http://localhost:5232/ --caldav-user=testuser --calendar-url=/testuser/calendar-cli-test-calendar"
kal="../cal.py --caldav-url=http://localhost:5232/ --caldav-user=testuser --calendar-url=/testuser/calendar-cli-test-calendar"
echo "## Creating a calendar"
$calendar_cli calendar create calendar-cli-test-calendar
@@ -20,7 +20,9 @@ then
## the calendar is created. Without the statement above, I'll
## just get 404 when running tests.
export calendar_cli
+ export kal
./tests.sh
+ sleep 600
kill $radicale_pid
sleep 0.3
else
@@ -43,7 +45,9 @@ if [ -n "$xandikos_pid" ]
then
echo "## Xandikos now running on pid $xandikos_pid"
calendar_cli="../calendar-cli --caldav-url=http://localhost:8080/ --caldav-user=user"
+ kal="../cal.py --caldav-url=http://localhost:8080/ --caldav-user=user"
export calendar_cli
+ export kal
./tests.sh
kill $xandikos_pid
else
diff --git a/tests/tests.sh b/tests/tests.sh
index f519447..a71f8b5 100755
--- a/tests/tests.sh
+++ b/tests/tests.sh
@@ -9,13 +9,15 @@ set -e
for path in . .. ./tests ../tests
do
setup="$path/_setup_alias"
- [ -x $setup ] && source $setup
+ [ -f $setup ] && source $setup
done
echo "## CLEANUP from earlier failed test runs"
+QUIET=true
for uid in $($calendar_cli calendar agenda --from-time=2010-10-09 --agenda-days=5 --event-template='{uid}') ; do calendar_cli calendar delete --event-uid=$uid ; done
calendar_cli todo --categories scripttest delete
+unset QUIET
########################################################################
## TEST CODE FOLLOWS
@@ -65,11 +67,27 @@ uid=$(echo $output | perl -ne '/uid=(.*)$/ && print $1')
echo "## fetching the full day event, in ics format"
calendar_cli --icalendar calendar agenda --from-time=2010-10-13 --agenda-days=1
+
echo "$output" | grep -q "whole day" || error "could not find the event"
echo "$output" | grep -q "20101010" || error "could not find the date"
echo "$output" | grep -q "20101010T" && error "a supposed whole day event was found to be with the time of day"
echo "OK: found the event"
+## saving the ics data
+tmpfile=$(mktemp)
+cat $outfile > $tmpfile
+
+echo "## cleanup, delete it"
+calendar_cli calendar delete --event-uid=$uid
+
+echo "## Same, using kal add ics"
+kal add ical --ical-file=$tmpfile
+#rm $tmpfile
+calendar_cli --icalendar calendar agenda --from-time=2010-10-13 --agenda-days=1
+echo "$output" | grep -q "whole day" || error "could not find the event"
+echo "$output" | grep -q "20101010" || error "could not find the date"
+echo "$output" | grep -q "20101010T" && error "a supposed whole day event was found to be with the time of day"
+echo "OK: found the event"
echo "## cleanup, delete it"
calendar_cli calendar delete --event-uid=$uid
@@ -177,3 +195,7 @@ calendar_cli todo --hide-parents --categories scripttest list
[ -z "$output" ] && echo "## OK: found no tasks now"
echo "## ALL TESTS COMPLETED! YAY!"
+
+
+rm $outfile
+