-
Notifications
You must be signed in to change notification settings - Fork 6
/
ics_compare.py
executable file
·167 lines (150 loc) · 6.33 KB
/
ics_compare.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env python3
#
# Python tool to compare two iCalendar files (specifically for python-remind)
#
# Copyright (C) 2014-2021 Jochen Sprickerhof
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from argparse import ArgumentParser
from datetime import datetime
from vobject import iCalendar
from vobject.base import Component, readComponents
def compare(first_in: Component, second_in: Component, second_out: Component) -> None:
for (j, second) in enumerate(second_in.vevent_list):
found = False
for (i, first) in enumerate(first_in.vevent_list):
wrong = False
for attr in (
"summary",
"location",
"description",
"recurrence_id",
):
if (
hasattr(first, attr)
and first.contents.get(attr)
and first.contents.get(attr)[0].value
and not (
hasattr(second, attr)
and second.contents.get(attr)
and second.contents.get(attr)[0].value
and first.contents.get(attr)[0].value
== second.contents.get(attr)[0].value
)
):
wrong = True
break
# ignore timezone when comparing dates (not supported by remind)
if hasattr(first, "dtstart") and not (
hasattr(second, "dtstart")
and (
(
isinstance(first.dtstart.value, datetime)
and isinstance(second.dtstart.value, datetime)
and first.dtstart.value.timestamp()
== second.dtstart.value.timestamp()
)
or first.dtstart.value == second.dtstart.value
)
):
wrong = True
if hasattr(first, "dtend"):
if hasattr(second, "dtend") and (
(
isinstance(first.dtend.value, datetime)
and isinstance(second.dtend.value, datetime)
and first.dtend.value.timestamp()
!= second.dtend.value.timestamp()
)
or first.dtend.value != second.dtend.value
):
wrong = True
elif (
hasattr(second, "duration")
and first.dtend.value
!= second.dtstart.value + second.duration.value
):
wrong = True
elif not (hasattr(second, "dtend") or hasattr(second, "duration")):
wrong = True
if hasattr(first, "duration"):
if (
hasattr(second, "duration")
and first.duration.value != second.duration.value
):
wrong = True
elif (
hasattr(second, "dtend")
and first.duration.value
!= second.dtend.value - second.dtstart.value
):
wrong = True
elif not (hasattr(second, "dtend") or hasattr(second, "duration")):
wrong = True
if hasattr(first, "rruleset") and first.rruleset:
if (
hasattr(second, "rruleset")
and second.rruleset
and list(first.rruleset) != list(second.rruleset)
):
wrong = True
elif (
hasattr(second, "rdate")
and second.rdate.value
and list(first.rruleset) != second.rdate.value
):
wrong = True
elif not (hasattr(second, "rruleset") or hasattr(second, "rdate")):
wrong = True
if hasattr(first, "rdate") and first.rdate.value:
if (
hasattr(second, "rdate")
and second.rdate.value
and first.rdate.value != second.rdate.value
):
wrong = True
elif (
hasattr(second, "rruleset")
and second.rruleset
and first.rdate.value != list(second.rruleset)
):
wrong = True
elif not (hasattr(second, "rruleset") or hasattr(second, "rdate")):
wrong = True
if wrong:
continue
found = True
first_in.remove(first)
print(f"matching {i} to {j}")
if not found:
second_out.add(second)
def main() -> None:
parser = ArgumentParser(description="Compare two iCalendar files semantically")
parser.add_argument("first_input", help="First iCalendar file input")
parser.add_argument("second_input", help="Second iCalendar file input")
parser.add_argument("first_output", help="First iCalendar file output")
parser.add_argument("second_output", help="Second iCalendar file output")
args = parser.parse_args()
with open(args.first_input, encoding="utf-8") as infile:
first_cal = next(readComponents(infile))
with open(args.second_input, encoding="utf-8") as infile:
second_cal = next(readComponents(infile))
second_out = iCalendar()
compare(first_cal, second_cal, second_out)
with open(args.first_output, "w", encoding="utf-8") as outfile:
outfile.write(first_cal.serialize())
with open(args.second_output, "w", encoding="utf-8") as outfile:
outfile.write(second_out.serialize())
if __name__ == "__main__":
main()