Debian Codegraph @

EmailReminder/Yearly.py

raw symbols SCIP
1 # This file is part of Email-Reminder.
2 #
3 # Copyright (C) 2020 by Francois Marier
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <https://www.gnu.org/licenses/>.
17
18 import datetime
19
20 from EmailReminder.Event import EventModel
21 from EmailReminder.Utils import readXMLValue, writeXMLValue
22
23
24 def formatDate(year, month, day):
25 if not month or not day:
26 return ''
27
28 try:
29 if not year:
30 # 1980 is a leap year
31 return datetime.date(1980, int(month), int(day)).strftime('%m-%d')
32 return datetime.date(int(year), int(month), int(day)).isoformat()
33 except ValueError:
34 return ''
35
36
37 def parseDate(dateString):
38 try:
39 if len(dateString) == 5:
40 date = datetime.datetime.strptime(dateString, '%m-%d')
41 return {'year': None, 'month': date.month, 'day': date.day}
42 date = datetime.datetime.strptime(dateString, '%Y-%m-%d')
43 except ValueError:
44 date = datetime.datetime.today()
45
46 return {'year': date.year, 'month': date.month, 'day': date.day}
47
48
49 class YearlyModel(EventModel):
50 LABEL = 'Yearly Events'
51 TYPE = 'yearly'
52 COLUMN_NAMES = ["Event Name", "Event Date"]
53
54 def fillNewEvent(self, event):
55 super().fillNewEvent(event)
56
57 writeXMLValue(self.document, event, 'month', 1)
58 writeXMLValue(self.document, event, 'day', 15)
59
60 def getDataInternal(self, column, row):
61 parentData = super().getDataInternal(column, row)
62 if parentData:
63 return parentData
64 if column == 1:
65 event = self.events[row]
66 year = readXMLValue(event, 'year')
67 month = readXMLValue(event, 'month')
68 day = readXMLValue(event, 'day')
69 return formatDate(year, month, day)
70
71 return None
72
73 def setDataInternal(self, column, row, value):
74 if column == 1:
75 event = self.events[row]
76 date = parseDate(value)
77 writeXMLValue(self.document, event, 'year', date['year'])
78 writeXMLValue(self.document, event, 'month', date['month'])
79 writeXMLValue(self.document, event, 'day', date['day'])
80 else:
81 super().setDataInternal(column, row, value)