Code Repositories xandikos / 9a90449
Fix filtering on property/component in extract_from_calendar. Jelmer Vernooń≥ 3 months ago
2 changed file(s) with 77 addition(s) and 12 deletion(s). Raw diff Collapse all Expand all
221221 raise NotImplementedError
222222
223223
224 def _extract_from_component(incomp, outcomp, requested):
225 for tag in requested:
226 if tag.tag == ('{%s}comp' % NAMESPACE):
227 for insub in incomp.subcomponents:
228 if insub.name == tag.get('name'):
229 outsub = component_factory[insub.name]()
230 outcomp.add_component(outsub)
231 _extract_from_component(insub, outsub, tag)
232 elif tag.tag == ('{%s}prop' % NAMESPACE):
233 outcomp[tag.get('name')] = incomp[tag.get('name')]
234 elif tag.tag == ('{%s}allprop' % NAMESPACE):
235 for propname in incomp:
236 outcomp[propname] = incomp[propname]
237 elif tag.tag == ('{%s}allcomp' % NAMESPACE):
238 for insub in incomp.subcomponents:
239 outsub = component_factory[insub.name]()
240 outcomp.add_component(outsub)
241 _extract_from_component(insub, outsub, tag)
242 else:
243 raise AssertionError('invalid element %r' % tag)
244
245
224246 def extract_from_calendar(incal, outcal, requested):
225247 """Extract requested components/properties from calendar.
226248
228250 :param outcal: Calendar to write to
229251 :param requested: <calendar-data> element with requested
230252 components/properties
231 :return: A Calendar
232253 """
233254 for tag in requested:
234255 if tag.tag == ('{%s}comp' % NAMESPACE):
235 for insub in incal.subcomponents:
236 if insub.name == tag.get('name'):
237 outsub = component_factory[insub.name]
238 outcal.add_component(outsub)
239 extract_from_calendar(insub, outsub, tag)
240 elif tag.tag == ('{%s}prop' % NAMESPACE):
241 outcal[tag.get('name')] = incal[tag.get('name')]
256 if incal.name == tag.get('name'):
257 _extract_from_component(incal, outcal, tag)
242258 elif tag.tag == ('{%s}expand' % NAMESPACE):
243259 # TODO(jelmer): https://github.com/jelmer/xandikos/issues/102
244260 raise NotImplementedError('expand is not yet implemented')
8484 expected_outcal = ICalendar.from_ical(outcal_str)
8585 outcal = ICalendar()
8686 caldav.extract_from_calendar(incal, outcal, self.requested)
87 self.assertEqual(outcal, expected_outcal)
87 self.assertEqual(
88 expected_outcal.to_ical().decode(),
89 outcal.to_ical().decode(),
90 ET.tostring(self.requested))
8891
8992 def test_comp(self):
9093 comp = ET.SubElement(self.requested, '{%s}comp' % caldav.NAMESPACE)
103106 """)
104107
105108 def test_comp_nested(self):
106 vcal_comp = ET.SubElement(self.requested, '{%s}comp' % caldav.NAMESPACE)
109 vcal_comp = ET.SubElement(
110 self.requested, '{%s}comp' % caldav.NAMESPACE)
107111 vcal_comp.set('name', 'VCALENDAR')
108112 vtodo_comp = ET.SubElement(vcal_comp, '{%s}comp' % caldav.NAMESPACE)
109113 vtodo_comp.set('name', 'VTODO')
133137 """)
134138
135139 def test_prop(self):
136 vcal_comp = ET.SubElement(self.requested, '{%s}comp' % caldav.NAMESPACE)
140 vcal_comp = ET.SubElement(
141 self.requested, '{%s}comp' % caldav.NAMESPACE)
137142 vcal_comp.set('name', 'VCALENDAR')
138143 vtodo_comp = ET.SubElement(vcal_comp, '{%s}comp' % caldav.NAMESPACE)
139144 vtodo_comp.set('name', 'VTODO')
140 completed_prop = ET.SubElement(vtodo_comp, '{%s}prop' % caldav.NAMESPACE)
145 completed_prop = ET.SubElement(
146 vtodo_comp, '{%s}prop' % caldav.NAMESPACE)
141147 completed_prop.set('name', 'COMPLETED')
142148 self.extractEqual("""\
143149 BEGIN:VCALENDAR
161167 END:VCALENDAR
162168 """, """\
163169 BEGIN:VCALENDAR
170 END:VCALENDAR
171 """)
172
173 def test_allprop(self):
174 vcal_comp = ET.SubElement(
175 self.requested, '{%s}comp' % caldav.NAMESPACE)
176 vcal_comp.set('name', 'VCALENDAR')
177 vtodo_comp = ET.SubElement(vcal_comp, '{%s}comp' % caldav.NAMESPACE)
178 vtodo_comp.set('name', 'VTODO')
179 ET.SubElement(vtodo_comp, '{%s}allprop' % caldav.NAMESPACE)
180 self.extractEqual("""\
181 BEGIN:VCALENDAR
182 BEGIN:VTODO
183 COMPLETED:20100829T234417Z
184 CREATED:20090606T042958Z
185 END:VTODO
186 END:VCALENDAR
187 """, """\
188 BEGIN:VCALENDAR
189 BEGIN:VTODO
190 COMPLETED:20100829T234417Z
191 CREATED:20090606T042958Z
192 END:VTODO
193 END:VCALENDAR
194 """)
195
196 def test_allcomp(self):
197 vcal_comp = ET.SubElement(
198 self.requested,
199 '{%s}comp' % caldav.NAMESPACE)
200 vcal_comp.set('name', 'VCALENDAR')
201 ET.SubElement(vcal_comp, '{%s}allcomp' % caldav.NAMESPACE)
202 self.extractEqual("""\
203 BEGIN:VCALENDAR
204 BEGIN:VTODO
205 COMPLETED:20100829T234417Z
206 CREATED:20090606T042958Z
207 END:VTODO
208 END:VCALENDAR
209 """, """\
210 BEGIN:VCALENDAR
211 BEGIN:VTODO
212 END:VTODO
164213 END:VCALENDAR
165214 """)
166215