Browse Source

Add date filtering

- Available conditions : 'BEFORE', 'AFTER' ('EQUAL TO' removed for now)
- User input date is guessed by using dateutil parser.
- Input date or date from *.ics files are always converted to datetime
if not time is specified and made offset-aware based on local_timezone
variable defined (for now) on top of the file.
octogene 3 years ago
1 changed files with 36 additions and 5 deletions
  1. +36

+ 36
- 5 View File

@@ -6,10 +6,14 @@
# Distributed under terms of the MIT license.

import datetime
from icalendar import Calendar
from pytz import timezone
from dateutil.parser import parse
from tkinter import Tk, filedialog, Listbox, Button, Entry, StringVar, \
LabelFrame, BooleanVar, Frame, ttk, END, Checkbutton, messagebox

local_timezone = timezone('Europe/Paris')

@@ -56,15 +60,13 @@ class GUI:

if self.filter_type.get() in ('DTSTART', 'DTEND'):
self.filter_cond['values'] = ('BEFORE', 'AFTER', 'EQUAL TO')
self.filter_cond['values'] = ('BEFORE', 'AFTER')

self.filter_cond['values'] = ('CONTAINS', 'EQUAL TO')


def create_files_list_frame(self):
files_list_frame = LabelFrame(self.root, text='Files to merge')
self.FilesList = Listbox(files_list_frame)
@@ -99,17 +101,46 @@ class GUI:
self.FilesList.insert(END, file)

def filter(self, event):
"""Check if condition is met for a given event field"""

value = self.filter_value.get()
field = self.filter_type.get()
if self.filter_cond.get() == 'CONTAINS':
condition = self.filter_cond.get()

if field in ('DTSTART', 'DTEND'):
value = parse(value)
except ValueError:
messagebox.showerror('Wrong value',
'Value is not recognized as a date')
value = self.normalize_date(value)

if condition == 'CONTAINS':
if value in event.get(field):
return True
if self.filter_cond.get() == 'EQUAL TO':
if condition == 'EQUAL TO':
if value == event.get(field):
return True
if condition == 'BEFORE':
if value > self.normalize_date(event.get(field).dt):
return True
if condition == 'AFTER':
if value < self.normalize_date(event.get(field).dt):
return True

return False

def normalize_date(self, date):
"""Ensure that date is a datetime object and is offset aware."""

if not isinstance(date, datetime.datetime):
date = datetime.datetime(date.year, date.month,

if date.tzinfo is None or date.tzinfo.utcoffset(date) is None:
date = local_timezone.localize(date)

return date

def join_files(self):
if self.FilesList.get(0, END):
ical = filedialog.asksaveasfilename(title='Save as...')