From 8f48b074f0d600a09ca3a6615863e91a951b64d5 Mon Sep 17 00:00:00 2001 From: Johannes Raggam Date: Tue, 29 Jan 2013 23:48:58 +0100 Subject: [PATCH] * Don't force to pass a list to vDDDLists and allow setting individual RDATE and EXDATE values without having to wrap them in a list. * Fix encoding function to allow setting RDATE and EXDATE values and not to have bypass encoding with an icalendar property. --- CHANGES.rst | 8 ++++++++ src/icalendar/cal.py | 19 +++++++++++-------- src/icalendar/prop.py | 18 ++---------------- src/icalendar/tests/test_prop.py | 24 ++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 24 deletions(-) create mode 100644 src/icalendar/tests/test_prop.py diff --git a/CHANGES.rst b/CHANGES.rst index baff2e5..aaf1b8e 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -4,6 +4,14 @@ Changelog 3.3dev (unreleased) ------------------- +* Don't force to pass a list to vDDDLists and allow setting individual RDATE + and EXDATE values without having to wrap them in a list. + [thet] + +* Fix encoding function to allow setting RDATE and EXDATE values and not to + have bypass encoding with an icalendar property. + [thet] + * Allow setting of timezone for vDDDLists and support timezone properties for RDATE and EXDATE component properties. [thet] diff --git a/src/icalendar/cal.py b/src/icalendar/cal.py index 5e5e1d0..367a531 100644 --- a/src/icalendar/cal.py +++ b/src/icalendar/cal.py @@ -235,16 +235,19 @@ class Component(CaselessDict): """Conditional convertion of values. """ - if cond: - klass = types_factory.for_property(name) - _klass = klass(value) - if hasattr(value, 'params') and len(value.params.keys()) > 0: - _klass.params = value.params - return _klass - return value + if not cond: return value + klass = types_factory.for_property(name) + obj = klass(value) + if hasattr(value, 'params') and len(value.params.keys()) > 0: + # TODO: How can a python native value have params? + obj.params = value.params + return obj def set(self, name, value, encode=1): - if type(value) == ListType: + if encode and type(value) == ListType\ + and name.lower() not in ['rdate', 'exdate']: + # Individually convert each value to an ical type except rdate and + # exdate, which encoded to vDDDLists, which needs a list self[name] = [self._encode(name, v, encode) for v in value] else: self[name] = self._encode(name, value, encode) diff --git a/src/icalendar/prop.py b/src/icalendar/prop.py index 54f30c0..51f15e4 100644 --- a/src/icalendar/prop.py +++ b/src/icalendar/prop.py @@ -340,11 +340,6 @@ class vDDDLists: >>> str(dt_list[2]) '1996-04-04 01:00:00+00:00' - >>> dt_list = vDDDLists('19960402T010000Z') - Traceback (most recent call last): - ... - ValueError: Value MUST be a list (of date instances) - >>> dt_list = vDDDLists([]) >>> dt_list.to_ical() '' @@ -357,20 +352,11 @@ class vDDDLists: >>> dt_list.to_ical() '20000101T000000,20001111T000000' - Timezone support - >>> from icalendar import Event - >>> at = pytz.timezone('Europe/Vienna') - >>> dt = at.localize(datetime(2013,1,29)) - >>> event = Event() - >>> event.add('rdate', vDDDLists([dt]), encode=0) - >>> out = event.to_ical() - >>> assert("RDATE;TZID=Europe/Vienna:20130129T000000" in out) - """ def __init__(self, dt_list): - if not isinstance(dt_list, list): - raise ValueError('Value MUST be a list (of date instances)') + if not hasattr(dt_list, '__iter__'): + dt_list = [dt_list] vDDD = [] tzid = None for dt in dt_list: diff --git a/src/icalendar/tests/test_prop.py b/src/icalendar/tests/test_prop.py new file mode 100644 index 0000000..b3ed77c --- /dev/null +++ b/src/icalendar/tests/test_prop.py @@ -0,0 +1,24 @@ +import datetime +import unittest + +import icalendar +import pytz + + +class TestPropertyValues(unittest.TestCase): + + def test_vDDDLists_timezone(self): + e = icalendar.Event() + at = pytz.timezone('Europe/Vienna') + dt1 = at.localize(datetime.datetime(2013,1,1)) + dt2 = at.localize(datetime.datetime(2013,1,2)) + dt3 = at.localize(datetime.datetime(2013,1,3)) + e.add('rdate', [dt1, dt2]) + e.add('exdate', dt3) + out = e.to_ical() + + import pdb; pdb.set_trace() + self.assertTrue('RDATE;TZID=Europe/Vienna:20130101T000000,20130102T000000' in out) + self.assertTrue('EXDATE;TZID=Europe/Vienna:20130103T000000' in out) + + #'BEGIN:VEVENT\r\nDTSTART;TZID=Europe/Vienna;VALUE=DATE-TIME:20130101T000000\r\nRDATE;TZID=Europe/Vienna:20130101T000000,20130102T000000\r\nEXDATE;TZID=Europe/Vienna:20130103T000000\r\nEND:VEVENT\r\n'