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.
master
Bogdan Cordier 2 years ago
parent
commit
d243696854
1 changed files with 36 additions and 5 deletions
  1. 36
    5
      ical_fusion.py

+ 36
- 5
ical_fusion.py View File

@@ -6,10 +6,14 @@
6 6
 #
7 7
 # Distributed under terms of the MIT license.
8 8
 
9
+import datetime
9 10
 from icalendar import Calendar
11
+from pytz import timezone
12
+from dateutil.parser import parse
10 13
 from tkinter import Tk, filedialog, Listbox, Button, Entry, StringVar, \
11 14
     LabelFrame, BooleanVar, Frame, ttk, END, Checkbutton, messagebox
12 15
 
16
+local_timezone = timezone('Europe/Paris')
13 17
 ical_fields = ('SUMMARY', 'UID', 'LOCATION', 'CATEGORIES', 'DTSTART', 'DTEND')
14 18
 
15 19
 
@@ -56,15 +60,13 @@ class GUI:
56 60
         """
57 61
 
58 62
         if self.filter_type.get() in ('DTSTART', 'DTEND'):
59
-            self.filter_cond['values'] = ('BEFORE', 'AFTER', 'EQUAL TO')
63
+            self.filter_cond['values'] = ('BEFORE', 'AFTER')
60 64
 
61 65
         else:
62 66
             self.filter_cond['values'] = ('CONTAINS', 'EQUAL TO')
63 67
 
64 68
         self.filter_cond.current(0)
65 69
 
66
-
67
-
68 70
     def create_files_list_frame(self):
69 71
         files_list_frame = LabelFrame(self.root, text='Files to merge')
70 72
         self.FilesList = Listbox(files_list_frame)
@@ -99,17 +101,46 @@ class GUI:
99 101
             self.FilesList.insert(END, file)
100 102
 
101 103
     def filter(self, event):
104
+        """Check if condition is met for a given event field"""
105
+
102 106
         value = self.filter_value.get()
103 107
         field = self.filter_type.get()
104
-        if self.filter_cond.get() == 'CONTAINS':
108
+        condition = self.filter_cond.get()
109
+
110
+        if field in ('DTSTART', 'DTEND'):
111
+            try:
112
+                value = parse(value)
113
+            except ValueError:
114
+                messagebox.showerror('Wrong value',
115
+                                     'Value is not recognized as a date')
116
+            value = self.normalize_date(value)
117
+
118
+        if condition == 'CONTAINS':
105 119
             if value in event.get(field):
106 120
                 return True
107
-        if self.filter_cond.get() == 'EQUAL TO':
121
+        if condition == 'EQUAL TO':
108 122
             if value == event.get(field):
109 123
                 return True
124
+        if condition == 'BEFORE':
125
+            if value > self.normalize_date(event.get(field).dt):
126
+                return True
127
+        if condition == 'AFTER':
128
+            if value < self.normalize_date(event.get(field).dt):
129
+                return True
110 130
 
111 131
         return False
112 132
 
133
+    def normalize_date(self, date):
134
+        """Ensure that date is a datetime object and is offset aware."""
135
+
136
+        if not isinstance(date, datetime.datetime):
137
+            date = datetime.datetime(date.year, date.month, date.day)
138
+
139
+        if date.tzinfo is None or date.tzinfo.utcoffset(date) is None:
140
+            date = local_timezone.localize(date)
141
+
142
+        return date
143
+
113 144
     def join_files(self):
114 145
         if self.FilesList.get(0, END):
115 146
             ical = filedialog.asksaveasfilename(title='Save as...')

Loading…
Cancel
Save