summaryrefslogtreecommitdiff
path: root/calendar_cli
diff options
context:
space:
mode:
authorTobias Brox <tobias@redpill-linpro.com>2022-11-06 10:23:24 +0100
committerTobias Brox <tobias@redpill-linpro.com>2022-11-06 10:23:24 +0100
commit891359b85d305199a76283f412107359e8674b7d (patch)
tree924566a50b183bc311fce68b1d61699edbc1d97a /calendar_cli
parent68e2b2dfdaebd01a0bdd2b1f4d9f87f3b203a7fb (diff)
downloadcalendar-cli-891359b85d305199a76283f412107359e8674b7d.zip
reorganized the repository - should fix https://github.com/tobixen/calendar-cli/issues/94
Diffstat (limited to 'calendar_cli')
-rw-r--r--calendar_cli/__init__.py0
-rwxr-xr-xcalendar_cli/cal.py536
-rwxr-xr-xcalendar_cli/legacy.py1027
-rw-r--r--calendar_cli/metadata.py15
-rw-r--r--calendar_cli/template.py49
5 files changed, 1627 insertions, 0 deletions
diff --git a/calendar_cli/__init__.py b/calendar_cli/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/calendar_cli/__init__.py
diff --git a/calendar_cli/cal.py b/calendar_cli/cal.py
new file mode 100755
index 0000000..682ac1f
--- /dev/null
+++ b/calendar_cli/cal.py
@@ -0,0 +1,536 @@
+#!/usr/bin/env python
+
+"""https://github.com/tobixen/calendar-cli/ - high-level cli against caldav servers.
+
+Copyright (C) 2013-2021 Tobias Brox and other contributors.
+
+See https://www.gnu.org/licenses/gpl-3.0.en.html for license information.
+
+This is a new cli to be fully released in version 1.0, until then
+quite much functionality will only be available through the legacy
+calendar-cli. For discussions on the directions, see
+https://github.com/tobixen/calendar-cli/issues/88
+"""
+
+## This file should preferably just be a thin interface between public
+## python libraries and the command line. Logics that isn't strictly
+## tied to the cli as such but also does not fit into other libraries
+## may be moved out to a separate library file.
+
+## This file aims to be smaller than the old calendar-cli while
+## offering more featuores.
+
+from calendar_cli.metadata import metadata
+__version__ = metadata["version"]
+
+import click
+import os
+import caldav
+#import isodate
+import dateutil
+import dateutil.parser
+import datetime
+import re
+from icalendar import prop
+from calendar_cli.template import Template
+
+list_type = list
+
+## should make some subclasses of click.ParamType:
+
+## class DateOrDateTime - perhaps a subclass of click.DateTime, returns date
+## if no time is given (can probably just be subclassed directly from
+## click.DateTime?
+
+## class DurationOrDateTime - perhaps a subclass of the above, should attempt
+## to use pytimeparse if the given info is not a datetime.
+
+## See https://click.palletsprojects.com/en/8.0.x/api/#click.ParamType and
+## /usr/lib/*/site-packages/click/types.py on how to do this.
+
+## TODO: maybe find those attributes through the icalendar library? icalendar.cal.singletons, icalendar.cal.multiple, etc
+attr_txt_one = ['location', 'description', 'geo', 'organizer', 'summary', 'class', 'rrule']
+attr_txt_many = ['category', 'comment', 'contact', 'resources', 'parent', 'child']
+
+def parse_dt(input, return_type=None):
+ """Parse a datetime or a date.
+
+ If return_type is date, return a date - if return_type is
+ datetime, return a datetime. If no return_type is given, try to
+ guess if we should return a date or a datetime.
+
+ """
+ ret = dateutil.parser.parse(input)
+ if return_type is datetime.datetime:
+ return ret
+ elif return_type is datetime.date:
+ return ret.date()
+ elif ret.time() == datetime.time(0,0) and len(input)<12 and not '00:00' in input and not '0000' in input:
+ return ret.date()
+ else:
+ return ret
+
+def parse_add_dur(dt, dur):
+ """
+ duration may be something on the format 1s (one second), 3m (three minutes, not months), 3.5h, 1y1w, etc
+ or a ISO8601 duration (TODO: not supported yet). Return the dt plus duration
+ """
+ time_units = {
+ 's': 1, 'm': 60, 'h': 3600,
+ 'd': 86400, 'w': 604800
+ }
+ while dur:
+ rx = re.match(r'(\d+(?:\.\d+)?)([smhdw])(.*)', dur)
+ assert rx
+ i = float(rx.group(1))
+ u = rx.group(2)
+ dur = rx.group(3)
+ if u=='y':
+ dt = datetime.datetime.combine(datetime.date(dt.year+i, dt.month, dt.day), dt.time())
+ else:
+ dt = dt + datetime.timedelta(0, i*time_units[u])
+ return dt
+
+
+## TODO ... (and should be moved somewhere else?)
+def parse_timespec(timespec):
+ """parses a timespec and return two timestamps
+
+ The ISO8601 interval format, format 1, 2 or 3 as described at
+ https://en.wikipedia.org/wiki/ISO_8601#Time_intervals should be
+ accepted, though it may be dependent on
+ https://github.com/gweis/isodate/issues/77 or perhaps
+ https://github.com/dateutil/dateutil/issues/1184
+
+ The calendar-cli format (i.e. 2021-01-08 15:00:00+1h) should be accepted
+
+ Two timestamps should be accepted.
+
+ One timestamp should be accepted, and the second return value will be None.
+ """
+ ## calendar-cli format, 1998-10-03 15:00+2h
+ if '+' in timespec:
+ rx = re.match(r'(.*)\+((?:\d+(?:\.\d+)?[smhdwy])+)$', timespec)
+ if rx:
+ start = parse_dt(rx.group(1))
+ end = parse_add_dur(start, rx.group(2))
+ return (start, end)
+ try:
+ ## parse("2015-05-05 2015-05-05") does not throw the ParserError
+ if timespec.count('-')>3:
+ raise dateutil.parser.ParserError("Seems to be two dates here")
+ ret = parse_dt(timespec)
+ return (ret,None)
+ except dateutil.parser.ParserError:
+ split_by_space = timespec.split(' ')
+ if len(split_by_space) == 2:
+ return (parse_dt(split_by_space[0]), parse_dt(split_by_space[1]))
+ elif len(split_by_space) == 4:
+ return (parse_dt(f"{split_by_space[0]} {split_by_space[1]}"), parse_dt(f"{split_by_space[2]} {split_by_space[3]}"))
+ else:
+ raise ValueError(f"couldn't parse time interval {timespec}")
+
+ raise NotImplementedError("possibly a ISO time interval")
+
+@click.group()
+## TODO
+#@click.option('-c', '--config-file', type=click.File("rb"), default=f"{os.environ['HOME']}/.config/calendar.conf")
+#@click.option('--config-section', default="default")
+@click.option('--caldav-url', help="Full URL to the caldav server", metavar='URL')
+@click.option('--caldav-username', '--caldav-user', help="Full URL to the caldav server", metavar='URL')
+@click.option('--caldav-password', '--caldav-pass', help="Full URL to the caldav server", metavar='URL')
+@click.option('--calendar-url', help="Calendar id, path or URL", metavar='cal', multiple=True)
+@click.option('--calendar-name', help="Calendar name", metavar='cal', multiple=True)
+@click.pass_context
+def cli(ctx, **kwargs):
+ """
+ CalDAV Command Line Interface, in development.
+
+ This command will eventually replace calendar-cli.
+ It's not ready for consumption. Only use if you want to contribute/test.
+ """
+ ## The cli function will prepare a context object, a dict containing the
+ ## caldav_client, principal and calendar
+
+ ctx.ensure_object(dict)
+ ## TODO: add all relevant connection parameters for the DAVClient as options
+ ## TODO: logic to read the config file and edit kwargs from config file
+ ## TODO: delayed communication with caldav server (i.e. if --help is given to subcommand)
+ ## TODO: catch errors, present nice error messages
+ conn_params = {}
+ for k in kwargs:
+ if k.startswith('caldav_'):
+ conn_params[k[7:]] = kwargs[k]
+ client = caldav.DAVClient(**conn_params)
+ principal = client.principal()
+ calendars = []
+ for calendar_url in kwargs['calendar_url']:
+ calendars.append(principal.calendar(cal_id=calendar_url))
+ for calendar_name in kwargs['calendar_name']:
+ calendars.append(principal.calendar(name=calendar_name))
+ if not calendars:
+ calendars = principal.calendars()
+ ctx.obj['calendars'] = calendars
+
+@cli.command()
+@click.pass_context
+def test(ctx):
+ """
+ Will test that we can connect to the caldav server and find the calendars.
+ """
+ click.echo("Seems like everything is OK")
+
+def _set_attr_options_(func, verb):
+ """
+ decorator that will add options --set-category, --set-description etc
+ """
+ if verb:
+ verb1 = f"{verb}-"
+ else:
+ verb1 = ""
+ verb = "Select by "
+ for foo in attr_txt_one:
+ func = click.option(f"--{verb1}{foo}", help=f"{verb} ical attribute {foo}")(func)
+ for foo in attr_txt_many:
+ func = click.option(f"--{verb1}{foo}", help=f"{verb} ical attribute {foo}", multiple=True)(func)
+ return func
+
+def _abort(message):
+ click.echo(message)
+ raise click.Abort(message)
+
+def _set_attr_options(verb=""):
+ return lambda func: _set_attr_options_(func,verb)
+
+@cli.group()
+@click.option('--all/--none', default=None, help='Select all (or none) of the objects. Overrides all other selection options.')
+@click.option('--uid', multiple=True, help='select an object with a given uid (or select more object with given uids). Overrides all other selection options')
+@click.option('--abort-on-missing-uid/--ignore-missing-uid', default=False, help='Abort if (one or more) uids are not found (default: silently ignore missing uids). Only effective when used with --uid')
+@click.option('--todo/--notodo', default=None, help='select only todos (or no todos)')
+@click.option('--event/--noevent', default=None, help='select only todos (or no todos)')
+@click.option('--include-completed/--exclude-completed', default=False, help='select only todos (or no todos)')
+@_set_attr_options()
+@click.option('--start', help='do a time search, with this start timestamp')
+@click.option('--end', help='do a time search, with this end timestamp (or duration)')
+@click.option('--timespan', help='do a time search for this interval')
+@click.option('--sort-key', help='use this attributes for sorting. Templating can be used. Prepend with - for reverse sort', multiple=True)
+@click.option('--skip-parents/--include-parents', help="Skip parents if it's children is selected. Useful for finding tasks that can be started if parent depends on child", default=False)
+@click.option('--skip-children/--include-children', help="Skip children if it's parent is selected. Useful for getting an overview of the big picture if children are subtasks", default=False)
+@click.option('--limit', help='Number of objects to show', type=int)
+@click.option('--offset', help='SKip the first objects', type=int)
+@click.pass_context
+def select(ctx, all, uid, abort_on_missing_uid, sort_key, skip_parents, skip_children, limit, offset, **kwargs_):
+ """
+ select/search/filter tasks/events, for listing/editing/deleting, etc
+ """
+ objs = []
+ ctx.obj['objs'] = objs
+
+ ## TODO: move all search/filter/select logic to caldav library?
+
+ ## handle all/none options
+ if all is False: ## means --none.
+ return
+ if all:
+ for c in ctx.obj['calendars']:
+ objs.extend(c.objects())
+ return
+
+ kwargs = {}
+ for kw in kwargs_:
+ if kwargs_[kw] is not None and kwargs_[kw] != ():
+ kwargs[kw] = kwargs_[kw]
+
+ ## uid(s)
+ missing_uids = []
+ for uid_ in uid:
+ comp_filter=None
+ if kwargs_['event']:
+ comp_filter='VEVENT'
+ if kwargs_['todo']:
+ comp_filter='VTODO'
+ cnt = 0
+ for c in ctx.obj['calendars']:
+ try:
+ objs.append(c.object_by_uid(uid_, comp_filter=comp_filter))
+ cnt += 1
+ except caldav.error.NotFoundError:
+ pass
+ if not cnt:
+ missing_uids.append(uid_)
+ if abort_on_missing_uid and missing_uids:
+ _abort(f"Did not find the following uids in any calendars: {missing_uids}")
+ if uid:
+ return
+
+ if kwargs_['start']:
+ kwargs['start'] = parse_dt(kwargs['start'])
+ if kwargs_['end']:
+ rx = re.match(r'\+((\d+(\.\d+)?[smhdwy])+)', kwargs['end'])
+ if rx:
+ kwargs['end'] = parse_add_dur(kwargs['start'], rx.group(1))
+ else:
+ kwargs['end'] = parse_dt(kwargs['end'])
+ elif kwargs_['timespan']:
+ kwargs['start'], kwargs['end'] = parse_timespec(kwargs['timespan'])
+
+ for attr in attr_txt_many:
+ if len(kwargs_[attr])>1:
+ raise NotImplementedError(f"is it really needed to search for more than one {attr}?")
+ elif kwargs_[attr]:
+ kwargs[attr] = kwargs[attr][0]
+
+ ## TODO: special handling of parent and child! (and test for that!)
+
+ for c in ctx.obj['calendars']:
+ objs.extend(c.search(**kwargs))
+
+ if skip_children or skip_parents:
+ objs_by_uid = {}
+ for obj in objs:
+ objs_by_uid[obj.icalendar_instance.subcomponents[0]['uid']] = obj
+ for obj in objs:
+ rels = obj.icalendar_instance.subcomponents[0].get('RELATED-TO', [])
+ rels = rels if isinstance(rels, list_type) else [ rels ]
+ for rel in rels:
+ rel_uid = rel
+ rel_type = rel.params.get('REL-TYPE', None)
+ if ((rel_type == 'CHILD' and skip_parents) or (rel_type == 'PARENT' and skip_children)) and rel_uid in objs_by_uid and uid in objs_by_uid:
+ del objs_by_uid[uid]
+ if ((rel_type == 'PARENT' and skip_parents) or (rel_type == 'CHILD' and skip_children)) and rel_uid in objs_by_uid:
+ del objs_by_uid[rel_uid]
+ objs = objs_by_uid.values()
+
+ ## OPTIMIZE TODO: sorting the list multiple times rather than once is a bit of brute force, if there are several sort keys and long list of objects, we should sort once and consider all sort keys while sorting
+ ## TODO: Consider that an object may be expanded and contain lots of event instances. We will then need to expand the caldav.Event object into multiple objects, each containing one recurrance instance. This should probably be done on the caldav side of things.
+ for skey in reversed(sort_key):
+ ## If the key starts with -, sorting should be reversed
+ if skey[0] == '-':
+ reverse = True
+ skey=skey[1:]
+ else:
+ reverse = False
+ ## if the key contains {}, it should be considered to be a template
+ if '{' in skey:
+ fkey = lambda obj: Template(skey).format(**obj.icalendar_instance.subcomponents[0])
+ else:
+ fkey = lambda obj: obj.icalendar_instance.subcomponents[0][skey]
+ ctx.obj['objs'].sort(key=fkey, reverse=reverse)
+
+ ## OPTIMIZE TODO: this is also suboptimal, if ctx.obj is a very long list
+ if offset is not None:
+ ctx.obj['objs'] = ctx.obj['objs'][offset:]
+ if limit is not None:
+ ctx.obj['objs'] = ctx.obj['objs'][0:limit]
+
+@select.command()
+@click.option('--ics/--no-ics', default=False, help="Output in ics format")
+@click.option('--template', default="{DUE.dt:?{DTSTART.dt:?(date missing)?}?:%F %H:%M:%S}: {SUMMARY:?{DESCRIPTION:?(no summary given)?}?}")
+@click.pass_context
+def list(ctx, ics, template):
+ """
+ print out a list of tasks/events/journals
+ """
+ if ics:
+ if not ctx.obj['objs']:
+ return
+ icalendar = ctx.obj['objs'].pop(0).icalendar_instance
+ for obj in ctx.obj['objs']:
+ icalendar.subcomponents.extend(obj.icalendar_instance.subcomponents)
+ click.echo(icalendar.to_ical())
+ return
+ template=Template(template)
+ for obj in ctx.obj['objs']:
+ for sub in obj.icalendar_instance.subcomponents:
+ click.echo(template.format(**sub))
+
+@select.command()
+@click.pass_context
+def print_uid(ctx):
+ click.echo(ctx.obj['objs'][0].icalendar_instance.subcomponents[0]['UID'])
+
+@select.command()
+@click.option('--multi-delete/--no-multi-delete', default=None, help="Delete multiple things without confirmation prompt")
+@click.pass_context
+def delete(ctx, multi_delete, **kwargs):
+ """
+ delete the selected item(s)
+ """
+ objs = ctx.obj['objs']
+ if multi_delete is None and len(objs)>1:
+ multi_delete = click.confirm(f"OK to delete {len(objs)} items?")
+ if len(objs)>1 and not multi_delete:
+ _abort(f"Not going to delete {len(objs)} items")
+ for obj in objs:
+ obj.delete()
+
+@select.command()
+@click.option('--add-category', default=None, help="Delete multiple things without confirmation prompt", multiple=True)
+@click.option('--complete/--uncomplete', default=None, help="Mark task(s) as completed")
+@click.option('--complete-recurrence-mode', default='safe', help="Completion of recurrent tasks, mode to use - can be 'safe', 'thisandfuture' or '' (see caldav library for details)")
+@_set_attr_options(verb='set')
+@click.pass_context
+def edit(*largs, **kwargs):
+ return _edit(*largs, **kwargs)
+
+def _edit(ctx, add_category=None, complete=None, complete_recurrence_mode='safe', **kwargs):
+ """
+ Edits a task/event/journal
+ """
+ if 'recurrence_mode' in kwargs:
+ complete_recurrence_mode = kwargs.pop('recurrence_mode')
+ _process_set_args(ctx, kwargs)
+ for obj in ctx.obj['objs']:
+ ie = obj.icalendar_instance.subcomponents[0]
+ for arg in ctx.obj['set_args']:
+ if arg in ('child', 'parent'):
+ obj.set_relation(arg, ctx.obj['set_args'][arg])
+ else:
+ if arg in ie:
+ ie.pop(arg)
+ ie.add(arg, ctx.obj['set_args'][arg])
+ if add_category:
+ if 'categories' in ie:
+ cats = ie.pop('categories').cats
+ else:
+ cats = []
+ cats.extend(add_category)
+ ie.add('categories', cats)
+ if complete:
+ obj.complete(handle_rrule=complete_recurrence_mode, rrule_mode=complete_recurrence_mode)
+ elif complete is False:
+ obj.uncomplete()
+ obj.save()
+
+
+@select.command()
+@click.pass_context
+@click.option('--recurrence-mode', default='safe', help="Completion of recurrent tasks, mode to use - can be 'safe', 'thisandfuture' or '' (see caldav library for details)")
+def complete(ctx, **kwargs):
+ """
+ Mark tasks as completed (alias for edit --complete)
+ """
+ return _edit(ctx, complete=True, **kwargs)
+
+@select.command()
+@click.pass_context
+def calculate_panic_time(ctx, **kwargs):
+ raise NotImplementedError()
+
+@select.command()
+@click.pass_context
+def sum_hours(ctx, **kwargs):
+ raise NotImplementedError()
+
+## TODO: all combinations of --first-calendar, --no-first-calendar, --multi-add, --no-multi-add should be tested
+@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=None, help="Add things to multiple calendars")
+@click.option('--first-calendar/--no-first-calendar', default=None, help="Add things only to the first calendar found")
+@click.pass_context
+def add(ctx, **kwargs):
+ """
+ Save new objects on calendar(s)
+ """
+ if len(ctx.obj['calendars'])>1 and kwargs['multi_add'] is False:
+ _abort("Giving up: Multiple calendars given, but --no-multi-add is given")
+ ## TODO: crazy-long if-conditions can be refactored - see delete on how it's done there
+ if (kwargs['first_calendar'] or
+ (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'] is not None and
+ (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:
+ _abort("Giving up: Multiple calendars found/given, please specify which calendar you want to use")
+
+ ctx.obj['ical_fragment'] = "\n".join(kwargs['add_ical_line'])
+
+@add.command()
+@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()
+ if ctx.obj['ical_fragment']:
+ ical = ical.replace('\nEND:', f"{ctx.obj['ical_fragment']}\nEND:")
+ for c in ctx.obj['calendars']:
+ ## TODO: this may not be an event - should make a Calendar.save_object method
+ c.save_event(ical)
+
+def _process_set_args(ctx, kwargs):
+ ctx.obj['set_args'] = {}
+ for x in kwargs:
+ if kwargs[x] is None or kwargs[x]==():
+ continue
+ if x == 'set_rrule':
+ rrule = {}
+ for split1 in kwargs[x].split(';'):
+ k,v = split1.split('=')
+ rrule[k] = v
+ ctx.obj['set_args']['rrule'] = rrule
+ elif x == 'set_category':
+ ctx.obj['set_args']['categories'] = kwargs[x]
+ elif x.startswith('set_'):
+ ctx.obj['set_args'][x[4:]] = kwargs[x]
+ if 'summary' in kwargs:
+ ctx.obj['set_args']['summary'] = ctx.obj['set_args'].get('summary', '') + kwargs['summary']
+ if 'ical_fragment' in kwargs:
+ ctx.obj['set_args']['ics'] = kwargs['ical_fragment']
+
+@add.command()
+@click.argument('summary', nargs=-1)
+@_set_attr_options(verb='set')
+@click.pass_context
+def todo(ctx, **kwargs):
+ """
+ Creates a new task with given SUMMARY
+
+ Examples:
+
+ kal add todo "fix all known bugs in calendar-cli"
+ kal add todo --set-due=2050-12-10 "release calendar-cli version 42.0.0"
+ """
+ kwargs['summary'] = " ".join(kwargs['summary'])
+ _process_set_args(ctx, kwargs)
+ if not ctx.obj['set_args']['summary']:
+ _abort("denying to add a TODO with no summary given")
+ return
+ for cal in ctx.obj['calendars']:
+ todo = cal.save_todo(ical=ctx.obj['ical_fragment'], **ctx.obj['set_args'], no_overwrite=True)
+ click.echo(f"uid={todo.id}")
+
+@add.command()
+## TODO
+@click.argument('summary')
+@click.argument('timespec')
+@_set_attr_options(verb='set')
+@click.pass_context
+def event(ctx, timespec, **kwargs):
+ """
+ Creates a new event with given SUMMARY at the time specifed through TIMESPEC.
+
+ TIMESPEC is an ISO-formatted date or timestamp, optionally with a postfixed interval
+.
+ Examples:
+
+ kal add event "final bughunting session" 2004-11-25+5d
+ kal add event "release party" 2004-11-30T19:00+2h
+ """
+ _process_set_args(ctx, kwargs)
+ for cal in ctx.obj['calendars']:
+ (dtstart, dtend) = parse_timespec(timespec)
+ event = cal.save_event(dtstart=dtstart, dtend=dtend, **ctx.obj['set_args'], no_overwrite=True)
+ click.echo(f"uid={event.id}")
+
+def journal():
+ click.echo("soon you should be able to add journal entries to your calendar")
+ raise NotImplementedError("foo")
+
+if __name__ == '__main__':
+ cli()
diff --git a/calendar_cli/legacy.py b/calendar_cli/legacy.py
new file mode 100755
index 0000000..b7b61dc
--- /dev/null
+++ b/calendar_cli/legacy.py
@@ -0,0 +1,1027 @@
+#!/usr/bin/env python
+
+"""
+https://github.com/tobixen/calendar-cli/ - high-level cli against caldav servers.
+
+This is the legacy calendar-cli. A new interface is being built in cal.py - aka kal
+Feedback/wishes/discussions on the new cli can be held at
+https://github.com/tobixen/calendar-cli/issues/88
+
+Copyright (C) 2013-2021 Tobias Brox and other contributors.
+
+See https://www.gnu.org/licenses/gpl-3.0.en.html for license information.
+"""
+
+import argparse
+import tzlocal
+## we still need to use pytz, see https://github.com/collective/icalendar/issues/333
+#try:
+# import zoneinfo
+#except:
+# from backports import zoneinfo
+import pytz
+import time
+from datetime import datetime, timedelta, date
+from datetime import time as time_
+import dateutil.parser
+from dateutil.rrule import rrulestr
+from icalendar import Calendar,Event,Todo,Journal,Alarm
+import vobject
+import caldav
+import uuid
+import json
+import os
+import logging
+import sys
+import re
+import urllib3
+from getpass import getpass
+from six import PY3
+
+from calendar_cli.metadata import metadata
+__version__ = metadata["version"]
+
+UTC = pytz.utc
+#UTC = zoneinfo.ZoneInfo('UTC')
+
+def to_normal_str(text):
+ if PY3 and text and not isinstance(text, str):
+ text = text.decode('utf-8')
+ elif not PY3 and text and not isinstance(text, str):
+ text = text.encode('utf-8')
+ return text
+
+
+## ref https://github.com/tobixen/calendar-cli/issues/33, python3-compatibility
+try:
+ raw_input
+except NameError:
+ raw_input = input
+
+try:
+ unicode
+except NameError:
+ unicode = str
+
+def _date(ts):
+ """
+ helper function to get a date out of a Date or Datetime object.
+ """
+ if hasattr(ts, 'date'):
+ return ts.date()
+ return ts
+
+def _force_datetime(t, args):
+ """
+ date objects cannot be compared with timestamp objects, neither in
+ python2 nor python3. Silly. also, objects with time zone info
+ cannot be compared with timestamps without time zone info. and
+ both datetime.now() and datetime.utcnow() seems to be without
+ those bits. Silly.
+
+ This method should only be used in comparitions, never when
+ populating fields in an icalendar object. Events with dates
+ rather than timestamps are to be considered as full-day events,
+ so the difference is significant.
+ """
+ if type(t) == date:
+ t = datetime(t.year, t.month, t.day)
+ if t.tzinfo is None:
+ return t.replace(tzinfo=_tz(args.timezone))
+ return t
+
+def _now():
+ """
+ python datetime is ... crap!
+ """
+ return datetime.utcnow().replace(tzinfo=UTC)
+
+def _tz(timezone=None):
+ """
+ gives the local time zone if no time zone is given,
+ otherwise should return the timezone (or some canonical time zone object)
+ """
+ if timezone is None:
+ try:
+ ## should not be needed - but see
+ ## https://github.com/collective/icalendar/issues/333
+ return pytz.timezone(tzlocal.get_localzone_name())
+ except:
+ ## if the tzlocal version is old
+ return tzlocal.get_localzone()
+
+ elif not hasattr(timezone, 'utcoffset'):
+ ## See https://github.com/collective/icalendar/issues/333
+ #return zoneinfo.ZoneInfo(timezone)
+ return pytz.timezone(timezone)
+ else:
+ return timezone
+
+def _localize(ts, tz=None, from_tz=None, to_tz=None):
+ """
+ Should always return a non-native timestamp with timezone.
+ If from_tz or to_tz is None, assume ts is in local timezone.
+ if ts already has tiemzone, then it will trump from_tz
+ """
+ if not from_tz and not to_tz:
+ from_tz = tz
+ to_tz = tz
+ from_tz = _tz(from_tz)
+ to_tz = _tz(to_tz)
+ if not ts.tzinfo:
+ if hasattr(from_tz, 'localize'):
+ ts = from_tz.localize(ts)
+ else:
+ ts = ts.replace(tzinfo=tz)
+ return ts.astimezone(to_tz)
+
+## global constant
+## (todo: this doesn't really work out that well, leap seconds/days are not considered, and we're missing the month unit)
+## (todo: Sebastian Brox has made some other code using regexps and dateutil.relativedelta, should consider to steal his code)
+time_units = {
+ 's': 1, 'm': 60, 'h': 3600,
+ 'd': 86400, 'w': 604800, 'y': 31536000
+}
+
+vtodo_txt_one = ['location', 'description', 'geo', 'organizer', 'summary']
+vtodo_txt_many = ['categories', 'comment', 'contact', 'resources']
+vcal_txt_one = ['location', 'description']
+vcal_txt_many = []
+
+def niy(*args, **kwargs):
+ if 'feature' in kwargs:
+ raise NotImplementedError("This feature is not implemented yet: %(feature)s" % kwargs)
+ raise NotImplementedError
+
+def caldav_connect(args):
+ ## args.ssl_verify_cert is a string and can be a path or 'yes'/'no'.
+ ## the library expects a path or a boolean.
+ ## Translate 'yes' and 'no' to True and False, or pass the raw string:
+ ssl_verify_cert = {
+ 'yes': True,
+ 'no': False
+ }.get(args.ssl_verify_cert, args.ssl_verify_cert)
+ # Create the account
+ return caldav.DAVClient(url=args.caldav_url, username=args.caldav_user, password=args.caldav_pass, ssl_verify_cert=ssl_verify_cert, proxy=args.caldav_proxy)
+
+def parse_time_delta(delta_string):
+ # TODO: handle bad strings more gracefully
+ if len(delta_string) < 2 or delta_string[-1].lower() not in time_units:
+ raise ValueError("Invalid time delta: %s" % delta_string)
+ num = int(delta_string[:-1])
+ return timedelta(0, num*time_units[delta_string[-1].lower()])
+
+def find_calendar(caldav_conn, args):
+ if args.calendar_url:
+ if '/' in args.calendar_url:
+ return caldav.Calendar(client=caldav_conn, url=args.calendar_url)
+ else:
+ return caldav.Principal(caldav_conn).calendar(cal_id=args.calendar_url)
+ else:
+ ## Find default calendar
+ calendars = caldav.Principal(caldav_conn).calendars()
+ if not calendars:
+ sys.stderr.write("no calendar url given and no default calendar found - can't proceed. You will need to create a calendar first")
+ sys.exit(2)
+ if len(calendars) > 1:
+ sys.stderr.write("no calendar url given and several calendars found; assuming the primary is %s" % calendars[0].url)
+ return calendars[0]
+
+def _calendar_addics(caldav_conn, ics, uid, args):
+ """"
+ "Internal" method for adding a calendar object item to the caldav
+ server through a PUT. ASSUMES the ics conforms to rfc4791.txt
+ section 4.1 Handles --calendar-url and --icalendar from the args
+ """
+ if args.icalendar and args.nocaldav:
+ print(ics)
+ return
+
+ if args.icalendar or args.nocaldav:
+ 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
+
+ try:
+ c = find_calendar(caldav_conn, args)
+ ## unicode strings vs byte strings is a minefield in python3 ... so, re.search demands a string here ...
+ ics = to_normal_str(ics)
+ if re.search(r'^METHOD:[A-Z]+[\r\n]+',ics,flags=re.MULTILINE) and args.ignoremethod:
+ ics = re.sub(r'^METHOD:[A-Z]+[\r\n]+', '', ics, flags=re.MULTILINE)
+ print ("METHOD property found and ignored")
+ c.add_event(ics)
+ except caldav.lib.error.AuthorizationError:
+ print("Error logging in")
+ sys.exit(2)
+ """
+ Peter Havekes: This needs more checking. It works for me when connecting to O365
+
+ except caldav.lib.error.PutError as e:
+ if "200 OK" in str(e):
+ print("Duplicate")
+ else:
+ raise
+ """
+
+def calendar_addics(caldav_conn, args):
+ """
+ Takes an ics from external source and puts it into the calendar.
+
+ From the CalDAV RFC:
+
+ Calendar components in a calendar collection that have different UID
+ property values MUST be stored in separate calendar object resources.
+
+ This means the inbound .ics has to be split up into one .ics for
+ each event as long as the uid is different.
+ """
+ if args.file == '-':
+ input_ical = sys.stdin.read()
+ else:
+ with open(args.file, 'r') as f:
+ input_ical = f.read()
+
+ c = Calendar.from_ical(input_ical)
+
+ ## unfortunately we need to mess around with the object internals,
+ ## since the icalendar library doesn't offer methods out of the
+ ## hat for doing such kind of things
+ entries = c.subcomponents
+
+ ## Timezones should be duplicated into each ics, ref the RFC
+ timezones = [x for x in entries if x.name == 'VTIMEZONE']
+
+ ## Make a mapping from UID to the other components
+ uids = {}
+ for x in entries:
+ if x.name == 'VTIMEZONE' or not 'UID' in x:
+ continue
+ uid = x['UID'].to_ical()
+ uids[uid] = uids.get(uid, []) + [x]
+
+ for uid in uids:
+ c.subcomponents = timezones + uids[uid]
+ _calendar_addics(caldav_conn, c.to_ical(), uid, args)
+
+def interactive_config(args, config, remaining_argv):
+ import readline
+
+ new_config = False
+ section = 'default'
+ backup = {}
+ modified = False
+
+ print("Welcome to the interactive calendar configuration mode")
+ print("Warning - untested code ahead, raise issues at t-calendar-cli@tobixen.no or the github issue tracker")
+ print("It might be a good idea to read the documentation in parallel if running this for your first time")
+ if not config or not hasattr(config, 'keys'):
+ config = {}
+ print("No valid existing configuration found")
+ new_config = True
+ if config:
+ print("The following sections have been found: ")
+ print("\n".join(config.keys()))
+ if args.config_section and args.config_section != 'default':
+ section = args.config_section
+ else:
+ ## TODO: tab completion
+ section = raw_input("Chose one of those, or a new name / no name for a new configuration section: ")
+ if section in config:
+ backup = config[section].copy()
+ print("Using section " + section)
+ else:
+ section = 'default'
+
+ if not section in config:
+ config[section] = {}
+
+ for config_key in ('caldav_url', 'calendar_url', 'caldav_user', 'caldav_pass', 'caldav_proxy', 'ssl_verify_cert', 'language', 'timezone', 'inherits'):
+
+ if config_key == 'caldav_pass':
+ print("Config option caldav_pass - old value: **HIDDEN**")
+ value = getpass(prompt="Enter new value (or just enter to keep the old): ")
+ else:
+ print("Config option %s - old value: %s" % (config_key, config[section].get(config_key, '(None)')))
+ value = raw_input("Enter new value (or just enter to keep the old): ")
+
+ if value:
+ config[section][config_key] = value
+ modified = True
+
+ if not modified:
+ print("No configuration changes have been done")
+ else:
+ state = 'start'
+ while state == 'start':
+ options = []
+ if section:
+ options.append(('save', 'save configuration into section %s' % section))
+ if backup or not section:
+ options.append(('save_other', 'add this new configuration into a new section in the configuration file'))
+ if remaining_argv:
+ options.append(('use', 'use this configuration without saving'))
+ options.append(('abort', 'abort without saving'))
+ print("CONFIGURATION DONE ...")
+ for o in options:
+ print("Type %s if you want to %s" % o)
+ cmd = raw_input("Enter a command: ")
+ if cmd in ('use', 'abort'):
+ state = 'done'
+ if cmd in ('save', 'save_other'):
+ if cmd == 'save_other':
+ new_section = raw_input("New config section name: ")
+ config[new_section] = config[section]
+ if backup:
+ config[section] = backup
+ else:
+ del config[section]
+ section = new_section
+ try:
+ if os.path.isfile(args.config_file):
+ os.rename(args.config_file, "%s.%s.bak" % (args.config_file, int(time.time())))
+ with open(args.config_file, 'w') as outfile:
+ json.dump(config, outfile, indent=4)
+ except Exception as e:
+ print(e)
+ else:
+ print("Saved config")
+ state = 'done'
+
+
+
+
+ if args.config_section == 'default' and section != 'default':
+ config['default'] = config[section]
+ return config
+
+def create_alarm(message, relative_timedelta):
+ alarm = Alarm()
+ alarm.add('ACTION', 'DISPLAY')
+ alarm.add('DESCRIPTION', message)
+ alarm.add('TRIGGER', relative_timedelta, parameters={'VALUE':'DURATION'})
+ return alarm
+
+def calendar_add(caldav_conn, args):
+ cal = Calendar()
+ cal.add('prodid', '-//{author_short}//{product}//{language}'.format(language=args.language, **metadata))
+ cal.add('version', '2.0')
+ event = Event()
+ ## read timestamps from arguments
+ event_spec = args.event_time.split('+')
+ if len(event_spec)>3:
+ raise ValueError('Invalid event time "%s" - can max contain 2 plus-signs' % args.event_time)
+ elif len(event_spec)==3:
+ event_time = '%s+%s' % tuple(event_spec[0:2])
+ event_duration = event_spec[2]
+ elif len(event_spec)==2 and not event_spec[1][-1:] in time_units:
+ event_time = '%s+%s' % tuple(event_spec[0:2])
+ event_duration = '1h'
+ elif len(event_spec)==2:
+ event_time = '%s' % event_spec[0]
+ event_duration = event_spec[1]
+ else:
+ event_time = event_spec[0]
+ event_duration = '1h'
+ ## TODO: error handling
+ event_duration_secs = int(event_duration[:-1]) * time_units[event_duration[-1:]]
+ dtstart = dateutil.parser.parse(event_spec[0], ignoretz=True)
+ if (args.whole_day or
+ (event_duration_secs % (60*60*24) == 0 and
+ dtstart.time() == time_(0,0))):
+
+ ## allowing 1 second off due to leap seconds
+ if (event_duration_secs+1) % (60*60*24) > 2:
+ raise ValueError('Duration of whole-day event must be multiple of 1d')
+
+ duration = event_duration_secs//60//60//24
+ dtend = dtstart + timedelta(days=duration)
+ event.add('dtstart', _date(dtstart.date()))
+ event.add('dtend', _date(dtend.date()))
+ else:
+ dtstart = _localize(dtstart, args.timezone)
+ event.add('dtstart', dtstart)
+ ## TODO: handle duration and end-time as options. default 3600s by now.
+ event.add('dtend', dtstart + timedelta(0,event_duration_secs))
+ if (args.private):
+ event.add('class', 'PRIVATE')
+ event.add('dtstamp', _now())
+ uid = uuid.uuid1()
+ event.add('uid', str(uid))
+ for attr in vcal_txt_one + vcal_txt_many:
+ if attr == 'summary':
+ continue
+ val = getattr(args, 'set_'+attr)
+ if val:
+ event.add(attr, val)
+ event.add('summary', ' '.join(args.summary))
+ cal.add_component(event)
+ ## workaround for getting RFC-compliant ical data,
+ ## ref https://github.com/collective/icalendar/issues/272#issuecomment-640204031
+ ical_data = vobject.readOne(cal.to_ical().decode('utf-8')).serialize()
+ _calendar_addics(caldav_conn, ical_data, uid, args)
+ print("Added event with uid=%s" % uid)
+
+def calendar_delete(caldav_conn, args):
+ cal = find_calendar(caldav_conn, args)
+ if args.event_uid:
+ event = cal.event_by_uid(args.event_uid)
+ elif args.event_url:
+ event = cal.event_by_url(args.event_url)
+ else:
+ raise ValueError("Event deletion failed: either uid or url is needed")
+ event.delete()
+
+def journal_add(caldav_conn, args):
+ ## TODO: copied from todo_add, should probably be consolidated
+ cal = Calendar()
+ cal.add('prodid', '-//{author_short}//{product}//{language}'.format(language=args.language, **metadata))
+ cal.add('version', '2.0')
+ journal = Journal()
+ ## TODO: what does the cryptic comment here really mean, and why was the dtstamp commented out? dtstamp is required according to the RFC.
+ ## TODO: (cryptic old comment:) not really correct, and it breaks i.e. with google calendar
+ journal.add('dtstamp', datetime.now())
+ journal.add('dtstart', date.today())
+ journal.add('summary', ' '.join(args.summaryline))
+ uid = uuid.uuid1()
+ journal.add('uid', str(uid))
+ cal.add_component(journal)
+ _calendar_addics(caldav_conn, cal.to_ical(), uid, args)
+ print("Added journal item with uid=%s" % uid)
+ ## FULL STOP - should do some major refactoring before doing more work here!
+
+def todo_add(caldav_conn, args):
+ ## TODO: copied from calendar_add, should probably be consolidated
+ if args.icalendar or args.nocaldav:
+ niy(feature="add todo item by icalendar raw stdin data or create raw icalendar data to stdout")
+ if args.todo_uid:
+ uid = args.todo_uid
+ else:
+ uid = uuid.uuid1()
+ cal = Calendar()
+ cal.add('prodid', '-//{author_short}//{product}//{language}'.format(language=args.language, **metadata))
+ cal.add('version', '2.0')
+ todo = Todo()
+ todo.add('dtstamp', _now())
+
+ for setarg in ('due', 'dtstart'):
+ if getattr(args, 'set_'+setarg):
+ if type(getattr(args, 'set_'+setarg)) == str:
+ val = dateutil.parser.parse(getattr(args, 'set_'+setarg))
+ else:
+ val = getattr(args, 'set_'+setarg)
+ todo.add(setarg, val)
+ todo.add('uid', str(uid))
+ todo.add('summary', ' '.join(args.summaryline))
+ todo.add('status', 'NEEDS-ACTION')
+
+ if args.is_child:
+ for t in todo_select(caldav_conn, args):
+ todo.add('related-to', t.instance.vtodo.uid.value)
+ rt = t.instance.vtodo.add('related-to')
+ rt.params['RELTYPE']=['CHILD']
+ rt.value = str(uid)
+ t.save()
+
+ for attr in vtodo_txt_one:
+ if attr == 'summary':
+ continue
+ val = getattr(args, 'set_'+attr)
+ if val:
+ todo.add(attr, val)
+ ## TODO: this doesn't currently work quite the way we'd like it to
+ ## work (it adds to lines to the ical, and vobject cares only
+ ## about one of them), and if we do get it to work, we'd like to
+ ## refactor and get the same logic in the edit-function
+ for attr in vtodo_txt_many:
+ val = getattr(args, 'set_'+attr)
+ if val:
+ vals = val.split(',')
+ todo.add(attr, vals)
+
+ if args.alarm is not None:
+ alarm = create_alarm(' '.join(args.summaryline), parse_time_delta(args.alarm))
+ todo.add_component(alarm)
+
+ cal.add_component(todo)
+ _calendar_addics(caldav_conn, cal.to_ical(), uid, args)
+ print("Added todo item with uid=%s" % uid)
+
+def calendar_agenda(caldav_conn, args):
+ if args.nocaldav and args.icalendar:
+ niy(feature="Read events from stdin in ical format and list out in prettified format")
+
+ if args.nocaldav:
+ raise ValueError("Agenda with --nocaldav only makes sense together with --icalendar")
+
+ if args.from_time:
+ search_dtstart = dateutil.parser.parse(args.from_time)
+ search_dtstart = _localize(search_dtstart, args.timezone)
+ else:
+ search_dtstart = _now()
+ if args.to_time:
+ search_dtend = dateutil.parser.parse(args.to_time)
+ search_dtend = _localize(search_dtend, args.timezone)
+ elif args.agenda_mins:
+ search_dtend = search_dtstart + timedelta(minutes=args.agenda_mins)
+ elif args.agenda_days:
+ search_dtend = search_dtstart + timedelta(args.agenda_days)
+ ## TODO - error handling if search_dtend is not set above - but agenda_days have a default value, so that probably won't happen
+
+ ## TODO: time zone
+ events_ = find_calendar(caldav_conn, args).date_search(search_dtstart, search_dtend, expand=True)
+ events = []
+ if args.icalendar:
+ for ical in events_:
+ print(to_normal_str(ical.data).strip())
+ else:
+ for event_cal in events_:
+ tzinfo = _tz(args.timezone)
+ events__ = event_cal.instance.components()
+ for event in events__:
+ if event.name != 'VEVENT':
+ continue
+ dtstart = event.dtstart.value if hasattr(event, 'dtstart') else _now()
+ if not isinstance(dtstart, datetime):
+ dtstart = datetime(dtstart.year, dtstart.month, dtstart.day)
+ dtstart = _localize(dtstart, tzinfo)
+
+ events.append({'dtstart': dtstart, 'instance': event})
+
+ ## changed to use the "key"-parameter at 2019-09-18, as needed for python3.
+ ## this will probably cause regression on sufficiently old versions of python
+ events.sort(key=lambda a: a['dtstart'])
+ for event in events:
+ event['summary'] = "(no description)"
+ event['dtstart'] = event['dtstart'].strftime(args.timestamp_format)
+ for timeattr in ('dtcreated', 'dtend'):
+ if hasattr(event['instance'], timeattr):
+ event[timeattr] = getattr(event['instance'], timeattr).value
+ if hasattr(event[timeattr], 'strftime'):
+ if hasattr(event[timeattr], 'astimezone'):
+ event[timeattr] = event[timeattr].astimezone(_tz(args.timezone))
+ event[timeattr] = event[timeattr].strftime(args.timestamp_format)
+ else:
+ event[timeattr] = '-'
+ for textattr in vcal_txt_one:
+ if hasattr(event['instance'], textattr):
+ event[textattr] = getattr(event['instance'], textattr).value
+ else:
+ event[textattr] = '-'
+ for summary_attr in ('summary', 'location', 'description'):
+ if hasattr(event['instance'], summary_attr):
+ event['summary'] = getattr(event['instance'], summary_attr).value
+ break
+ event['uid'] = event['instance'].uid.value if hasattr(event['instance'], 'uid') else '<no uid>'
+ for attr in vcal_txt_one + ['summary']:
+ if isinstance(event[attr], unicode):
+ event[attr] = to_normal_str(event[attr])
+ print(args.event_template.format(**event))
+
+def create_calendar(caldav_conn, args):
+ cal_obj = caldav.Principal(caldav_conn).make_calendar(cal_id=args.cal_id)
+ if cal_obj:
+ print("Created a calendar with id " + args.cal_id)
+
+def create_tasklist(caldav_conn, args):
+ cal_obj = caldav.Principal(caldav_conn).make_calendar(cal_id=args.cal_id, supported_calendar_component_set=['VTODO'])
+ if cal_obj:
+ print("Created a task list with id " + args.tasklist_id)
+
+def todo_select(caldav_conn, args):
+ if args.top+args.limit+args.offset+args.offsetn and args.todo_uid:
+ raise ValueError("It doesn't make sense to combine --todo-uid with --top/--limit/--offset/--offsetn")
+ if args.todo_uid:
+ tasks = [ find_calendar(caldav_conn, args).todo_by_uid(args.todo_uid) ]
+ else:
+ ## TODO: we're fetching everything from the server, and then doing the filtering here. It would be better to let the server do the filtering, though that requires library modifications.
+ ## TODO: current release of the caldav library doesn't support the multi-key sort_keys attribute. The try-except construct should be removed at some point in the future, when caldav 0.5 is released.
+ try:
+ tasks = find_calendar(caldav_conn, args).todos(sort_keys=('isnt_overdue', 'hasnt_started', 'due', 'dtstart', 'priority'))
+ except:
+ tasks = find_calendar(caldav_conn, args).todos()
+ for attr in vtodo_txt_one + vtodo_txt_many: ## TODO: now we have _exact_ match on items in the the array attributes, and substring match on items that cannot be duplicated. Does that make sense? Probably not.
+ if getattr(args, attr):
+ tasks = [x for x in tasks if hasattr(x.instance.vtodo, attr) and getattr(args, attr) in getattr(x.instance.vtodo, attr).value]
+ if getattr(args, 'no'+attr):
+ tasks = [x for x in tasks if not hasattr(x.instance.vtodo, attr)]
+ if args.overdue:
+ tasks = [x for x in tasks if hasattr(x.instance.vtodo, 'due') and _force_datetime(x.instance.vtodo.due.value, args) < _force_datetime(datetime.now(), args)]
+ if args.hide_future:
+ tasks = [x for x in tasks if not(hasattr(x.instance.vtodo, 'dtstart') and _force_datetime(x.instance.vtodo.dtstart.value, args) > _force_datetime(datetime.now(), args))]
+ if args.hide_parents or args.hide_children:
+ tasks_by_uid = {}
+ for task in tasks:
+ tasks_by_uid[task.instance.vtodo.uid.value] = task
+ for task in tasks:
+ if hasattr(task.instance.vtodo, 'related_to'):
+ uid = task.instance.vtodo.uid.value
+ rel_uid = task.instance.vtodo.related_to.value
+ rel_type = task.instance.vtodo.related_to.params.get('RELTYPE', 'PARENT')
+ if ((rel_type == 'CHILD' and args.hide_parents) or (rel_type == 'PARENT' and args.hide_children)) and \
+ rel_uid in tasks_by_uid and uid in tasks_by_uid:
+ del tasks_by_uid[uid]
+ if ((rel_type == 'PARENT' and args.hide_parents) or (rel_type == 'CHILD' and args.hide_children)) and \
+ rel_uid in tasks_by_uid:
+ del tasks_by_uid[rel_uid]
+ tasks = [x for x in tasks if x.instance.vtodo.uid.value in tasks_by_uid]
+ if args.top+args.limit:
+ tasks = tasks[args.offset+args.offsetn:args.top+args.limit+args.offset+args.offsetn]
+ elif args.offset+args.offsetn:
+ tasks = tasks[args.offset+args.offsetn:]
+ return tasks
+
+def todo_edit(caldav_conn, args):
+ tasks = todo_select(caldav_conn, args)
+ for task in tasks:
+ ## TODO: code duplication - can we refactor this?
+ for attr in vtodo_txt_one:
+ if getattr(args, 'set_'+attr):
+ if not hasattr(task.instance.vtodo, attr):
+ task.instance.vtodo.add(attr)
+ getattr(task.instance.vtodo, attr).value = getattr(args, 'set_'+attr)
+ for attr in vtodo_txt_many:
+ if getattr(args, 'set_'+attr):
+ if not hasattr(task.instance.vtodo, attr):
+ task.instance.vtodo.add(attr)
+ getattr(task.instance.vtodo, attr).value = [ getattr(args, 'set_'+attr) ]
+ for attr in vtodo_txt_many:
+ if getattr(args, 'add_'+attr):
+ if not hasattr(task.instance.vtodo, attr):
+ task.instance.vtodo.add(attr)
+ getattr(task.instance.vtodo, attr).value = []
+ getattr(task.instance.vtodo, attr).value.append(getattr(args, 'add_'+attr))
+ if args.pdb:
+ import pdb; pdb.set_trace()
+ ## you may now access task.data to edit the raw ical, or
+ ## task.instance.vtodo to edit a vobject instance
+ task.save()
+
+
+def todo_postpone(caldav_conn, args):
+ if args.nocaldav:
+ raise ValueError("No caldav connection, aborting")
+ rel_skew = None
+ new_ts = None
+ if args.until.startswith('+'):
+ rel_skew = timedelta(seconds=int(args.until[1:-1])*time_units[args.until[-1]])
+ elif args.until.startswith('in'):
+ new_ts = _now()+timedelta(seconds=int(args.until[2:-1])*time_units[args.until[-1]])
+ else:
+ new_ts = dateutil.parser.parse(args.until)
+ if not new_ts.time():
+ new_ts = _date(new_ts)
+
+ tasks = todo_select(caldav_conn, args)
+ for task in tasks:
+ if new_ts:
+ attr = 'due' if args.due else 'dtstart'
+ if not hasattr(task.instance.vtodo, attr):
+ task.instance.vtodo.add(attr)
+ getattr(task.instance.vtodo, attr).value = new_ts
+ if rel_skew:
+ if not args.due and hasattr(task.instance.vtodo, 'dtstart'):
+ task.instance.vtodo.dtstart.value += rel_skew
+ elif hasattr(task.instance.vtodo, 'due'):
+ task.instance.vtodo.due.value += rel_skew
+ if hasattr(task.instance.vtodo, 'dtstart') and hasattr(task.instance.vtodo, 'due'):
+ if type(task.instance.vtodo.dtstart.value) != type(task.instance.vtodo.due.value):
+ ## RFC states they must be of the same type
+ if isinstance(task.instance.vtodo.dtstart.value, date):
+ task.instance.vtodo.due.value = _date(task.instance.vtodo.due.value)
+ else:
+ d = task.instance.vtodo.due.value
+ task.instance.vtodo.due.value = datetime(d.year, d.month, d.day)
+ ## RFC also states that due cannot be before dtstart (and that makes sense)
+ if _force_datetime(task.instance.vtodo.dtstart.value, args) > _force_datetime(task.instance.vtodo.due.value, args):
+ task.instance.vtodo.due.value = task.instance.vtodo.dtstart.value
+ task.save()
+
+def todo_list(caldav_conn, args):
+ if args.nocaldav and args.icalendar:
+ niy(feature="display a prettified tasklist based on stdin ical")
+ if args.nocaldav:
+ raise ValueError("Todo-listing with --nocaldav only makes sense together with --icalendar")
+ tasks = todo_select(caldav_conn, args)
+ if args.icalendar:
+ for ical in tasks:
+ print(to_normal_str(ical.data))
+ elif args.list_categories:
+ categories = set()
+ for task in tasks:
+ if hasattr(task.instance.vtodo, 'categories'):
+ categories.update(task.instance.vtodo.categories.value)
+ for c in categories:
+ print(c)
+ else:
+ for task in tasks:
+ t = {'instance': task}
+ t['dtstart'] = task.instance.vtodo.dtstart.value if hasattr(task.instance.vtodo,'dtstart') else date.today()
+ t['dtstart_passed_mark'] = '!' if _force_datetime(t['dtstart'], args) <= _now() else ' '
+ t['due'] = task.instance.vtodo.due.value if hasattr(task.instance.vtodo,'due') else date.today()+timedelta(args.default_due)
+ t['due_passed_mark'] = '!' if _force_datetime(t['due'], args) < _now() else ' '
+ for timeattr in ('dtstart', 'due'):
+ t[timeattr] = t[timeattr].strftime(args.timestamp_format)
+ for summary_attr in ('summary', 'location', 'description', 'url', 'uid'):
+ if hasattr(task.instance.vtodo, summary_attr):
+ t['summary'] = getattr(task.instance.vtodo, summary_attr).value
+ break
+ t['uid'] = task.instance.vtodo.uid.value
+ t['summary'] = to_normal_str(t['summary'])
+ print(args.todo_template.format(**t))
+
+def todo_complete(caldav_conn, args):
+ if args.nocaldav:
+ raise ValueError("No caldav connection, aborting")
+ tasks = todo_select(caldav_conn, args)
+ for task in tasks:
+ if hasattr(task.instance.vtodo, 'rrule'):
+ rrule = rrulestr(task.instance.vtodo.rrule.value)
+ try:
+ next = rrule.after(datetime.now())
+ except TypeError: ## pesky problem with comparition of timestamps with and without tzinfo
+ next = rrule.after(datetime.now(tz=tzlocal.get_localzone()))
+ if next:
+ ## new_task is to be completed and we keep the original task open
+ completed_task = task.copy()
+ remaining_task = task
+
+ ## the remaining task should have recurrence id set to next start time, and range THISANDFUTURE
+ if hasattr(remaining_task.instance.vtodo, 'recurrence_id'):
+ del remaining_task.instance.vtodo.recurrence_id
+ remaining_task.instance.vtodo.add('recurrence-id')
+ remaining_task.instance.vtodo.dtstart.value = next ## TODO: should be same type as dtstart (date or datetime)
+ remaining_task.instance.vtodo.recurrence_id.params['RANGE'] = [ 'THISANDFUTURE' ]
+ remaining_task.instance.vtodo.rrule
+ count_search = re.search('COUNT=(\d+)', completed_task.instance.vtodo.rrule.value)
+ if count_search:
+ remaining_task.instance.vtodo.rrule.value = re.replace('COUNT=(\d+)', 'COUNT=%d' % int(count_search.group(1))-1)
+ remaining_task.save()
+
+ ## the completed task should have recurrence id set to current time
+ ## count in rrule should decrease
+ completed_task.instance.vtodo.remove(completed_task.instance.vtodo.rrule)
+ if hasattr(completed_task.instance.vtodo, 'recurrence_id'):
+ del completed_task.instance.vtodo.recurrence_id
+ completed_task.instance.vtodo.add('recurrence-id')
+ completed_task.instance.vtodo.recurrence_id.value = datetime.now()
+ completed_task.instance.vtodo.dtstart.value = datetime.now()
+ completed_task.complete()
+
+ continue
+ task.complete()
+
+
+def todo_delete(caldav_conn, args):
+ if args.nocaldav:
+ raise ValueError("No caldav connection, aborting")
+ tasks = todo_select(caldav_conn, args)
+ for task in tasks:
+ task.delete()
+
+def config_section(config, section='default'):
+ if section in config and 'inherits' in config[section]:
+ ret = config_section(config, config[section]['inherits'])
+ else:
+ ret = {}
+ if section in config:
+ ret.update(config[section])
+ return ret
+
+def main():
+ """
+ the main function does (almost) nothing but parsing command line parameters
+ """
+# sys.stderr.write("""
+#The calendar-cli command is slowly being deprecated in favor of kal
+#Check https://github.com/tobixen/calendar-cli/issues/88
+#""")
+
+ ## This boilerplate pattern is from
+ ## http://stackoverflow.com/questions/3609852
+ ## We want defaults for the command line options to be fetched from the config file
+
+ # Parse any conf_file specification
+ # We make this parser with add_help=False so that
+ # it doesn't parse -h and print help.
+ conf_parser = argparse.ArgumentParser(
+ prog=metadata["product"],
+ description=__doc__, # printed with -h/--help
+ # Don't mess with format of description
+ formatter_class=argparse.RawDescriptionHelpFormatter,
+ # Turn off help, so we print all options in response to -h
+ add_help=False
+ )
+ conf_parser.add_argument("--config-file",
+ help="Specify config file", metavar="FILE", default=os.getenv('XDG_CONFIG_HOME', os.getenv('HOME', '~') + '/.config')+'/calendar.conf')
+ conf_parser.add_argument("--config-section",
+ help="Specify config section; allows several caldav servers to be configured in the same config file", default='default')
+ conf_parser.add_argument("--interactive-config",
+ help="Interactively ask for configuration", action="store_true")
+ args, remaining_argv = conf_parser.parse_known_args()
+ conf_parser.add_argument("--version", action='version', version='%%(prog)s %s' % metadata["version"])
+
+ config = {}
+
+ try:
+ with open(args.config_file) as config_file:
+ config = json.load(config_file)
+ except IOError:
+ ## File not found
+ logging.info("no config file found")
+ except ValueError:
+ if args.interactive_config:
+ logging.error("error in config file. Be aware that the current config file will be ignored and overwritten", exc_info=True)
+ else:
+ logging.error("error in config file. You may want to run --interactive-config or fix the config file", exc_info=True)
+
+ if args.interactive_config:
+ defaults = interactive_config(args, config, remaining_argv)
+ if not remaining_argv:
+ return
+ else:
+ defaults = config_section(config, args.config_section)
+ if not 'ssl_verify_cert' in defaults:
+ defaults['ssl_verify_cert'] = 'yes'
+ if not 'language' in defaults:
+ ## TODO: shouldn't this be lower case?
+ defaults['language'] = 'EN'
+
+ # Parse rest of arguments
+ # Don't suppress add_help here so it will handle -h
+ parser = argparse.ArgumentParser(
+ description=__doc__,
+ prog=metadata["product"],
+ # Inherit options from config_parser
+ parents=[conf_parser]
+ )
+ parser.set_defaults(**defaults)
+
+ ## Global options
+ parser.add_argument("--nocaldav", help="Do not connect to CalDAV server, but read/write icalendar format from stdin/stdout", action="store_true")
+ parser.add_argument("--icalendar", help="Read/write icalendar format from stdin/stdout", action="store_true")
+ parser.add_argument("--timezone", help="Timezone to use")
+ parser.add_argument('--language', help="language used")
+ parser.add_argument("--caldav-url", help="Full URL to the caldav server", metavar="URL")
+ parser.add_argument("--caldav-user", help="username to log into the caldav server", metavar="USER")
+ parser.add_argument("--caldav-pass", help="password to log into the caldav server", metavar="PASS")
+ parser.add_argument("--caldav-proxy", help="HTTP proxy server to use (if any)")
+ parser.add_argument("--file-pass", help="Absolute path to file containing the password")
+ parser.add_argument("--ssl-verify-cert", help="verification of the SSL cert - 'yes' to use the OS-provided CA-bundle, 'no' to trust any cert and the path to a CA-bundle")
+ parser.add_argument("--debug-logging", help="turn on debug logging", action="store_true")
+ 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)")
+ parser.add_argument("--ignoremethod", help="Ignores METHOD property if exists in the request. This violates RFC4791 but is sometimes appended by some calendar servers", action="store_true")
+ parser.set_defaults(print_help=parser.print_help)
+
+ ## TODO: check sys.argv[0] to find command
+ ## TODO: set up logging
+ subparsers = parser.add_subparsers(title='command')
+
+ ## Tasks
+ todo_parser = subparsers.add_parser('todo')
+ todo_parser.add_argument('--top', '-1', action='count', default=0)
+ todo_parser.add_argument('--offset', action='count', default=0)
+ todo_parser.add_argument('--offsetn', type=int, default=0)
+ todo_parser.add_argument('--limit', type=int, default=0)
+ todo_parser.add_argument('--todo-uid')
+ todo_parser.add_argument('--hide-parents', help='Hide the parent if you need to work on children tasks first (parent task depends on children tasks to be done first)', action='store_true')
+ todo_parser.add_argument('--hide-children', help='Hide the parent if you need to work on children tasks first (parent task depends on children tasks to be done first)', action='store_true')
+ todo_parser.add_argument('--overdue', help='Only show overdue tasks', action='store_true')
+ todo_parser.add_argument('--hide-future', help='Hide events with future dtstart', action='store_true')
+
+ for attr in vtodo_txt_one + vtodo_txt_many:
+ todo_parser.add_argument('--'+attr, help="for filtering tasks")
+
+ for attr in vtodo_txt_one + vtodo_txt_many:
+ todo_parser.add_argument('--no'+attr, help="for filtering tasks", action='store_true')
+
+ #todo_parser.add_argument('--priority', ....)
+ #todo_parser.add_argument('--sort-by', ....)
+ #todo_parser.add_argument('--due-before', ....)
+ todo_parser.set_defaults(print_help=todo_parser.print_help)
+ todo_subparsers = todo_parser.add_subparsers(title='tasks subcommand')
+ todo_create_parser = todo_subparsers.add_parser('createlist')
+ todo_create_parser.add_argument('tasklist_id')
+ todo_create_parser.set_defaults(func=create_tasklist)
+
+ todo_add_parser = todo_subparsers.add_parser('add')
+ todo_add_parser.add_argument('summaryline', nargs='+')
+ todo_add_parser.add_argument('--set-dtstart', default=date.today()+timedelta(1))
+ todo_add_parser.add_argument('--set-due', default=date.today()+timedelta(1))
+ todo_add_parser.add_argument('--is-child', help="the new task is a child-task of the selected task(s)", action='store_true')
+ for attr in vtodo_txt_one + vtodo_txt_many:
+ if attr != 'summary':
+ todo_add_parser.add_argument('--set-'+attr, help="Set "+attr)
+ # TODO: we probably want to be able to set or delete alarms in other situations, yes? generalize?
+ todo_add_parser.add_argument('--alarm', metavar='DURATION_BEFORE',
+ help="specifies a time at which a reminder should be presented for this task, " \
+ "relative to the start time of the task (as a timestamp delta)")
+ todo_add_parser.set_defaults(func=todo_add)
+
+ todo_list_parser = todo_subparsers.add_parser('list')
+ todo_list_parser.add_argument('--todo-template', help="Template for printing out the event", default="{dtstart}{dtstart_passed_mark} {due}{due_passed_mark} {summary}")
+ todo_list_parser.add_argument('--default-due', help="If a task has no due date set, list it with the due date set N days from today", type=int, default=14)
+ todo_list_parser.add_argument('--list-categories', help="Instead of listing the todo-items, list the unique categories used", action='store_true')
+ todo_list_parser.add_argument('--timestamp-format', help="strftime-style format string for the output timestamps", default="%Y-%m-%d (%a)")
+ todo_list_parser.set_defaults(func=todo_list)
+
+ todo_edit_parser = todo_subparsers.add_parser('edit')
+ for attr in vtodo_txt_one + vtodo_txt_many:
+ todo_edit_parser.add_argument('--set-'+attr, help="Set "+attr)
+ for attr in vtodo_txt_many:
+ todo_edit_parser.add_argument('--add-'+attr, help="Add an "+attr)
+ todo_edit_parser.add_argument('--pdb', help='Allow interactive edit through the python debugger', action='store_true')
+ todo_edit_parser.set_defaults(func=todo_edit)
+
+ todo_postpone_parser = todo_subparsers.add_parser('postpone')
+ todo_postpone_parser.add_argument('until', help="either a new date or +interval to add some interval to the existing time, or i.e. 'in 3d' to set the time to a new time relative to the current time. interval is a number postfixed with a one character unit (any of smhdwy). If the todo-item has a dtstart, this field will be modified, else the due timestamp will be modified. If both timestamps exists and dstart will be moved beyond the due time, the due time will be set to dtime.")
+ todo_postpone_parser.add_argument('--due', help="move the due, not the dtstart", action='store_true')
+ todo_postpone_parser.set_defaults(func=todo_postpone)
+
+ todo_complete_parser = todo_subparsers.add_parser('complete')
+ todo_complete_parser.set_defaults(func=todo_complete)
+
+ todo_delete_parser = todo_subparsers.add_parser('delete')
+ todo_delete_parser.set_defaults(func=todo_delete)
+
+ ## journal
+ journal_parser = subparsers.add_parser('journal')
+ journal_parser.set_defaults(print_help=journal_parser.print_help)
+ journal_subparsers = journal_parser.add_subparsers(title='journal subcommand')
+ journal_add_parser = journal_subparsers.add_parser('add')
+ journal_add_parser.add_argument('summaryline', nargs='+')
+ journal_add_parser.set_defaults(func=journal_add)
+
+ calendar_parser = subparsers.add_parser('calendar')
+ calendar_parser.set_defaults(print_help=calendar_parser.print_help)
+ calendar_subparsers = calendar_parser.add_subparsers(title='cal subcommand')
+
+ calendar_create_parser = calendar_subparsers.add_parser('create')
+ calendar_create_parser.add_argument('cal_id')
+ calendar_create_parser.set_defaults(func=create_calendar)
+
+ 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")
+ calendar_add_parser.add_argument('summary', nargs='+')
+ calendar_add_parser.set_defaults(func=calendar_add)
+ calendar_add_parser.add_argument('--whole-day', help='Whole-day event', action='store_true', default=False)
+ calendar_add_parser.add_argument('--private', help='Private event', action='store_true', default=False)
+
+ for attr in vcal_txt_one + vcal_txt_many:
+ calendar_add_parser.add_argument('--set-'+attr, help='Set '+attr)
+
+ calendar_addics_parser = calendar_subparsers.add_parser('addics')
+ calendar_addics_parser.add_argument('--file', help="ICS file to upload", default='-')
+ calendar_addics_parser.set_defaults(func=calendar_addics)
+
+ calendar_agenda_parser = calendar_subparsers.add_parser('agenda')
+ calendar_agenda_parser.add_argument('--from-time', help="Fetch calendar events from this timestamp. See the documentation for time specifications. Defaults to now")
+ calendar_agenda_parser.add_argument('--to-time', help="Fetch calendar until this timestamp")
+ calendar_agenda_parser.add_argument('--agenda-mins', help="Fetch calendar for so many minutes", type=int)
+ calendar_agenda_parser.add_argument('--agenda-days', help="Fetch calendar for so many days", type=int, default=7)
+ calendar_agenda_parser.add_argument('--event-template', help="Template for printing out the event. Defaults to '{dtstart} {summary}'", default="{dtstart} {summary}")
+ calendar_agenda_parser.add_argument('--timestamp-format', help="strftime-style format string for the output timestamps", default="%Y-%m-%d %H:%M (%a)")
+ calendar_agenda_parser.set_defaults(func=calendar_agenda)
+
+ calendar_delete_parser = calendar_subparsers.add_parser('delete')
+ calendar_delete_parser.add_argument('--event-uid')
+ calendar_delete_parser.add_argument('--event-url')
+ calendar_delete_parser.set_defaults(func=calendar_delete)
+
+ args = parser.parse_args(remaining_argv)
+
+ if args.debug_logging:
+ ## TODO: set up more proper logging in a more proper way
+ logging.getLogger().setLevel(logging.DEBUG)
+ caldav.log.setLevel(logging.DEBUG)
+ caldav.log.addHandler(logging.StreamHandler())
+
+ if args.file_pass:
+ with open(args.file_pass, 'r') as f:
+ args.caldav_pass = f.read().strip()
+
+ if not args.nocaldav:
+ if not args.calendar_url and not args.caldav_url:
+ sys.stderr.write("""
+missing mandatory arguments ... either calendar_url or caldav_url needs to be set
+Have you set up a config file? Read the doc or ...
+... use the --interactive-config option to create a config file
+""")
+ sys.exit(1)
+ caldav_conn = caldav_connect(args)
+ else:
+
+ caldav_conn = None
+
+ if args.ssl_verify_cert == 'no':
+ urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
+
+ if hasattr(args, 'func'):
+ return args.func(caldav_conn, args)
+ else:
+ ## We get here if a subcommand is not given - in that case we should print a friendly
+ ## help message. With python2 this goes automatically, with python3 we get here.
+ ## ref https://stackoverflow.com/a/22994500 subcommands are by default not required anymore
+ ## in python3. However, setting required=True gave a traceback rather than a friendly error message.
+ args.print_help()
+
+if __name__ == '__main__':
+ main()
diff --git a/calendar_cli/metadata.py b/calendar_cli/metadata.py
new file mode 100644
index 0000000..a81437f
--- /dev/null
+++ b/calendar_cli/metadata.py
@@ -0,0 +1,15 @@
+metadata = {
+ "version": "0.14.0.dev0",
+ "author": "Tobias Brox",
+ "author_short": "tobixen",
+ "copyright": "Copyright 2013-2022, Tobias Brox and contributors",
+ "license": "GPLv3+",
+ "license_url": "http://www.gnu.org/licenses/gpl-3.0-standalone.html",
+ "maintainer": "Tobias Brox",
+ "author_email": "t-calendar-cli@tobixen.no",
+ "status": "Development",
+ "product": "calendar_cli",
+ "name": "calendar_cli",
+ "description": "Simple command-line CalDav client, for adding and browsing calendar items, todo list items, etc.",
+ "url": "https://github.com/tobixen/calendar-cli",
+}
diff --git a/calendar_cli/template.py b/calendar_cli/template.py
new file mode 100644
index 0000000..47281d9
--- /dev/null
+++ b/calendar_cli/template.py
@@ -0,0 +1,49 @@
+"""String formatter that allows default values to be passed in the
+template string.
+
+This does not really belong in the calendar-cli package. I was
+googling a bit, and didn't find anything like this out there ... but
+I'm sure there must exist something like this?"""
+
+import datetime
+import string
+import re
+
+class NoValue():
+ def __getattr__(self, attr):
+ return self
+ def __getitem__(self, attr):
+ return self
+ def __str__(self):
+ return ""
+ def __format__(self, spec):
+ try:
+ return "".__format__(spec)
+ except:
+ return ""
+
+no_value = NoValue()
+
+class Template(string.Formatter):
+ def __init__(self, template):
+ self.template = template
+
+ def format(self, *pargs, **kwargs):
+ return super().format(self.template, *pargs, **kwargs)
+
+ def get_value(self, key, args, kwds):
+ try:
+ return string.Formatter.get_value(self, key, args, kwds)
+ except:
+ return no_value
+
+ def format_field(self, value, format_spec):
+ rx = re.match(r'\?([^\?]*)\?(.*)', format_spec)
+ if rx:
+ format_spec = rx.group(2)
+ if value is no_value:
+ value = rx.group(1)
+ try:
+ return string.Formatter.format_field(self, value, format_spec)
+ except:
+ return string.Formatter.format_field(self, value, "")