commit 35031b1c2f8a59adad07a04c36c587ade1094bde
parent e34d380cff1565e471a004b4cb2bd9286b11be48
Author: Matthias Balk <matthias.balk@fotopuzzle.de>
Date: Tue, 25 Sep 2018 08:59:09 +0200
print events sorted by date
Diffstat:
3 files changed, 31 insertions(+), 22 deletions(-)
diff --git a/README.rst b/README.rst
@@ -38,4 +38,4 @@ $ ya3 <command>
Example
~~~~~~~
You may want to write something like this to your shell's rc file:
-ya3 print | sort
+ya3 print
diff --git a/commands.py b/commands.py
@@ -72,22 +72,32 @@ def _conv2local(dt):
return dt.astimezone(gettz())
-def print_events(icsfilename):
-
- with open(icsfilename) as f:
- for cal in vobject.readComponents(f):
- for event in cal.vevent_list:
- start = _conv2local(event.dtstart.value)
- end = _conv2local(event.dtend.value)
-
- try:
- location = " (" + event.location.value + ")"
- except AttributeError:
- location = ""
-
- if start >= datetime.now(gettz()) - config.MAX_AGE and \
- start < datetime.now(gettz()) + config.MAX_AHEAD:
- print('%s - %s %s%s' % (start.strftime('%Y-%m-%d %H:%M'),
- end.strftime('%H:%M'),
- event.summary.value,
- location))
+def print_events(icsfilenames):
+
+ def _is_in_range(event):
+ start = _conv2local(event.dtstart.value)
+ end = _conv2local(event.dtend.value)
+ return (start >= datetime.now(gettz()) - config.MAX_AGE and
+ start < datetime.now(gettz()) + config.MAX_AHEAD)
+
+ def _print(event):
+ start = _conv2local(event.dtstart.value)
+ end = _conv2local(event.dtend.value)
+
+ try:
+ location = " (" + event.location.value + ")"
+ except AttributeError:
+ location = ""
+
+ print('%s - %s %s%s' % (start.strftime('%Y-%m-%d %H:%M'),
+ end.strftime('%H:%M'),
+ event.summary.value,
+ location))
+
+ events = []
+ for filename in icsfilenames:
+ with open(filename) as f:
+ for cal in vobject.readComponents(f):
+ events.extend(filter(_is_in_range, cal.vevent_list))
+ for event in sorted(events, key=lambda ev: _conv2local(ev.dtstart.value)):
+ _print(event)
diff --git a/ya3 b/ya3
@@ -22,7 +22,6 @@ else:
if command == 'create' or command == 'add':
commands.create_event()
elif command == 'print':
- for filename in glob.glob(os.path.join(config.CAL_DIR, '*.ics')):
- commands.print_events(filename)
+ commands.print_events(glob.glob(os.path.join(config.CAL_DIR, '*.ics')))
else:
parser.print_help()