summaryrefslogtreecommitdiff
path: root/src/python/Component.py
blob: 598e4977f1ccfcb9c28573c9d8c7d8fc98956a71 (plain)
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
#!/usr/bin/env python
#======================================================================
# FILE: Component.py
# CREATOR: eric
#
# (C) COPYRIGHT 2001, Eric Busboom <eric@civicknowledge.com>
# (C) COPYRIGHT 2001, Patrick Lewis <plewis@inetarena.com>
#
# This library is free software; you can redistribute it and/or modify
# it under the terms of either:
#
#   The LGPL as published by the Free Software Foundation, version
#   2.1, available at: https://www.gnu.org/licenses/lgpl-2.1.txt
#
# Or:
#
#   The Mozilla Public License Version 2.0. You may obtain a copy of
#   the License at https://www.mozilla.org/MPL/

# This library is free software; you can redistribute it and/or modify
# it under the terms of either:
#
#   The LGPL as published by the Free Software Foundation, version
#   2.1, available at: https://www.gnu.org/licenses/lgpl-2.1.html
#
# Or:
#
#   The Mozilla Public License Version 2.0. You may obtain a copy of
#   the License at https://www.mozilla.org/MPL/
#======================================================================

from LibicalWrap import *
from types import DictType, StringType, IntType
from Property import Property
from Collection import *
from Attendee import Attendee, Organizer
from Time import Time
from Duration import Duration
from Period import Period
import string

WrapperNULL = None

# Swig objects are natively unhashable, so we hash on the pointer val.
class SwigRefHash(dict):
    def __getitem__(self, k):
        return dict.__getitem__(self, int(k))

    def __setitem__(self, k, v):
        return dict.__setitem__(self, int(k), v)

    def __delitem__(self, k):
        dict.__delitem__(self, int(k))

    def has_key(self, k):
        return dict.has_key(self, int(k))

class Component(object):

    def __init__(self,ref=None,kind=None):

        if ref != None:
            self._ref = ref
        elif kind != None:
            self._ref = icalcomponent_new(
                icalcomponent_string_to_kind("VCALENDAR"))
            _kind = icalcomponent_string_to_kind(kind)
            inner = icalcomponent_new(_kind)

            icalcomponent_add_component(self._ref,inner);

        else:
            raise "Could not construct component with an unspecified kind"

        self.cached_props = SwigRefHash()
        self.cached_comps = SwigRefHash()

    def __del__(self):
        if self._ref != None and icalcomponent_get_parent(self._ref) != WrapperNULL:

            for k in self.cached_props.keys():
                del self.cached_props[k]

            icalcomponent_free(self._ref)
            self._ref = None

    def _prop_from_ref(self,p):

        if(p == None or p== WrapperNULL):
            return None;

        d = {}
        d['value'] = icalproperty_get_value_as_string(p)
        d['name'] = icalproperty_get_property_name(p)

        propkind = icalproperty_string_to_kind(d['name'])
        kind = icalproperty_kind_to_value_kind(propkind)
        d['value_type'] = icalvalue_kind_to_string(kind)
        d['ref'] = p


        #~ print p, Property(ref=p).name()
        if not self.cached_props.has_key(p):

            if d['value_type'] == 'DATE-TIME' or d['value_type'] == 'DATE':
                prop = Time(d,)
            elif d['value_type'] == 'PERIOD':
                prop = Period(d)
            elif d['value_type'] == 'DURATION':
                prop = Duration(d)
            elif d['name'] == 'ATTACH':
                prop = Attach(d)
            elif d['name'] == 'ATTENDEE':
                prop = Attendee(d)
            elif d['name'] == 'ORGANIZER':
                prop = Organizer(d)
            else:
                prop=Property(ref=p)

            self.cached_props[p] = prop

    def property(self, type):

       p = icallangbind_get_first_property(self._ref,type)

       if p !=WrapperNULL:
           self._prop_from_ref(p)
           return self.cached_props[p]

    def properties(self,type='ANY'):
        """
        Returns a list of Property instances, each representing a
        property of the type 'type.'
        """

        props = []

        p = icallangbind_get_first_property(self._ref,type)

        while p !=WrapperNULL and p != None:

            self._prop_from_ref(p) # Puts property in self.cached_props
            prop =  self.cached_props[p]
            props.append(prop)
            p = icallangbind_get_next_property(self._ref,type)

        return Collection(self,props)

    def add_property(self, prop):
        "Adds the property object to the component."

        if not isinstance(prop,Property):
            raise TypeError

        prop_p = prop.ref()

        if not prop_p:
            s = str(prop)
            prop_p = icalproperty_new_from_string(s)

            if prop_p == WrapperNULL:
                raise "Bad property string: " + s

            prop.ref(prop_p)

        if icalproperty_get_parent(prop_p)==WrapperNULL:
            icalcomponent_add_property(self._ref, prop_p)
        elif  icalproperty_get_parent(prop_p) != self._ref:
            raise "Property is already a child of another component"


    def remove_property(self,prop):

        if prop.ref() and self.cached_props.has_key(prop.ref()):

            del self.cached_props[prop.ref()]
            icalcomponent_remove_property(self._ref,prop.ref())

    def components(self,type='ANY'):
        comps = []

        kind = icalcomponent_string_to_kind(type)
        c = icalcomponent_get_first_component(self._ref,kind);

        while c != WrapperNULL and c != None:

            if not self.cached_comps.has_key(c):

                self.cached_comps[c] = Component(c)

            comp = self.cached_comps[c]
            comps.append(comp)
            c = icalcomponent_get_next_component(self._ref,kind);

        return ComponentCollection(self, comps)

    def inner_component(self):

        inner = icalcomponent_get_inner(self._ref)

        if inner != WrapperNULL or inner is None:
            return NewComponent(inner)

    def add_component(self, comp):
        "Adds a child component."

        if not isinstance(comp,Component):
            raise ValueError("Expected a Component")

        if icalcomponent_get_parent(comp._ref) != WrapperNULL:
           raise "Failed to add child component. Child already has a parent";

        icalcomponent_add_component(self._ref,comp._ref)

    def remove_component(self, comp):
        "Removes a child component"

        if not isinstance(comp,Component):
            raise ValueError("Expected a Component")

        icalcomponent_remove_component(self._ref,comp._ref)

    def as_ical_string(self):
        return self.__str__()

    def __str__(self):

        return icalcomponent_as_ical_string(self._ref)

    def name(self):
        k = icalcomponent_isa(self._ref)
        return icalcomponent_kind_to_string(k)

    def ref(self):
        """ Returns the internal reference to the libical icalproperty """
        return self._ref

def CloneComponent(c):
    "Clones a string or C icalcomponent into the right component object."

    wasStr=0 # Were we passed a string or an icalcomponent?

    if isinstance(c, Component):
        comp = icalparser_parse_string(c.as_ical_string())
    elif isinstance (c, StringType) and  string.find(c,"icalcomponent") == -1:
        comp = icalparser_parse_string(c)
    else:
        comp = c

    if comp == None or comp == WrapperNULL:
        raise ValueError("Expected a libical reference or an iCal string")

    kind = icalcomponent_isa(comp)
    kindStr = icalcomponent_kind_to_string(kind)

    if kindStr == 'VCALENDAR':
        inner = icalcomponent_get_inner(comp)
        kind = icalcomponent_isa(inner)
        kindStr = icalcomponent_kind_to_string(kind)

    if kindStr == 'VEVENT':
        newComp = Event(comp)
    elif kindStr == 'VTODO':
        newComp = Todo(comp)
    elif kindStr == 'VJOURNAL':
        newComp = Journal(comp)
    else:
        newComp = Component(comp)

    # I don't think I need to free the component created when passed a string,
    # as it wasn't created with a _new function.

    return newComp


def NewComponent(c):
    "Converts a string or C icalcomponent into the right component object."

    wasStr=0 # Were we passed a string or an icalcomponent?

    if isinstance (c, StringType) and  string.find(c,"icalcomponent") == -1:
        comp = icalparser_parse_string(c)
    else:
        comp = c

    if comp == None or comp == WrapperNULL:
        raise ValueError("Expected a libical reference or an iCal string")

    kind = icalcomponent_isa(comp)
    kindStr = icalcomponent_kind_to_string(kind)

    if kindStr == 'VEVENT':
        newComp = Event(comp)
    elif kindStr == 'VTODO':
        newComp = Todo(comp)
    elif kindStr == 'VJOURNAL':
        newComp = Journal(comp)
    else:
        newComp = Component(comp)

    # I don't think I need to free the component created when passed a string,
    # as it wasn't created with a _new function.

    return newComp


class GenericComponent(Component):

    def __init__(self,ref=None,kind=None):

        if ref != None:
            Component.__init__(self, ref=ref) # Call from subclasses
        elif type != None:
            Component.__init__(self, kind=kind) # Call from subclasses
        else:
            raise ValueError("Expected either a icalcomponent reference or a kind string")


        self._recurrence_set=None

    def _singular_property(self, name, value_type, value=None,
                           property_obj=None, enumerated_values=None):
        """Sets or gets the value of a method which exists once per Component.

        This is a constructor method for properties without a strictly defined
        object."""

        # Depending on the property name, this routine will either
        # operate on the VCALENDAR container or on the inner VEVENT,
        # VTODO, or VJOURNAL

        if name in ['METHOD','PRODID','CALSCALE','VERSION']:
            comp = self
        else:
            comp = self.inner_component()

        curr_properties = comp.properties(name)

        # Get the value
        if value==None:
            if not curr_properties:
                return None
            elif len(curr_properties) == 1:
                return curr_properties[0]
            else:
                raise ValueError, "too many properties of type %s" % propType

        # Set the value
        else:
            # Check if value is in enumerated_values
            if enumerated_values:
                value = string.upper(value)
                if value not in enumerated_values:
                    raise ValueError, "%s is not one of %s" \
                          % (value, enumerated_values)

            # Create the new property
            if property_obj:
                if not isinstance(value, property_obj):
                    # Create a special property_obj property
                    if property_obj == Time:
                        p = Time(value, name)
                        ## p.value_type(value_type)
                    elif property_obj == Duration:
                        p = Duration(value)
                    else:
                        p = property_obj()
                        ## p.value_type(value_type)
                        p.value(value)
                else:
                    p = value # value is already a property_obj
            else:
                # Create a generic property
                p = Property(name)
                ## p.value_type(value_type)
                p.value(value)

            if len(curr_properties) == 1:
                comp.remove_property(curr_properties[0])
            elif curr_properties:
                raise ValueError, "too many properties of type %s" % propType

            comp.add_property(p)

    # METHOD, PRODID, CALSCALE and VERSION are properties of the
    # VCALENDAR, not the inner component

    def method(self, v=None):
        "Sets or returns the value of the METHOD property."
        return self._singular_property("METHOD", "TEXT", v)

    def prodid(self, v=None):
        "Sets or returns the value of the PRODID property."
        return self._singular_property("PRODID", "TEXT", v)

    def calscale(self, v=None):
        "Sets or returns the value of the CALSCALE property."
        return self._singular_property("CALSCALE", "TEXT", v)

    def version(self, v=None):
        "Sets or returns the value of the Version property."
        return self._singular_property("VERSION", "TEXT", v)

    # The remaining properties are all in the inner component

    def clone(self):
        "Returns a copy of the object."
        return CloneComponent(self)

    def class_prop(self, v=None):  # Class is a reserved word
        "Sets or returns the value of the CLASS property."
        if v!=None:
            v = string.upper(v)
        return self._singular_property('CLASS', 'TEXT', v)

    def created(self, v=None):
        """Sets or returns the value of the CREATED property.

        Usage:
        created(time_obj)           # Set the value using a Time object
        created('19970101T123000Z') # Set using an iCalendar string
        created(982362522)          # Set using seconds
        created()                   # Return an iCalendar string
        """
        return self._singular_property("CREATED", "DATE-TIME", v, Time)

    def description(self, v=None):
        "Sets or returns the value of the DESCRIPTION property."
        return self._singular_property("DESCRIPTION", "TEXT", v)

    def dtstamp(self, v=None):
        """Sets or returns the value of the DTSTAMP property.

        Usage:
        dtstamp(time_obj)          # Set the value using a Time object
        dtstamp('19970101T123000Z')# Set using an iCalendar string
        dtstamp(982362522)         # Set using seconds
        dtstamp()                  # Return an iCalendar string
        """
        return self._singular_property("DTSTAMP", "DATE-TIME", v, Time)

    def dtstart(self, v=None):
        """Sets or returns the value of the DTSTART property.

        Usage:
        dtstart(time_obj)           # Set the value using a Time object
        dtstart('19970101T123000Z') # Set the value as an iCalendar string
        dtstart(982362522)          # Set the value using seconds (time_t)
        dtstart()                   # Return the time as an iCalendar string
        """
        return self._singular_property("DTSTART", "DATE-TIME", v, Time)

    def last_modified(self, v=None):
        """Sets or returns the value of the LAST-MODIFIED property.

        Usage:
        last_modified(time_obj)          # Set the value using a Time object
        last_modified('19970101T123000Z')# Set using an iCalendar string
        last_modified(982362522)         # Set using seconds
        last_modified()                  # Return an iCalendar string
        """
        return self._singular_property("LAST-MODIFIED", "DATE-TIME", v, Time)

    def organizer(self, v=None):
        """Sets or gets the value of the ORGANIZER property.

        Usage:
        organizer(orgObj)              # Set value using an organizer object
        organizer('MAILTO:jd@not.com') # Set value using a CAL-ADDRESS string
        organizer()                    # Return a CAL-ADDRESS string
        """
        return self._singular_property('ORGANIZER', 'CAL-ADDRESS', v,
                                       Organizer)

    def recurrence_id(self, v=None):
        """Sets or gets the value for the RECURRENCE-ID property.

        Usage:
        recurrence_id(recIdObj)             # Set using a Recurrence_Id object
        recurrence_id("19700801T133000")    # Set using an iCalendar string
        recurrence_id(8349873494)           # Set using seconds from epoch
        recurrence_id()                     # Return an iCalendar string
        """
        return self._singular_property('RECURRENCE-ID', 'DATE-TIME', v,
                                       Recurrence_Id)

    def sequence(self, v=None):
        """Sets or gets the SEQUENCE value of the Event.

        Usage:
        sequence(1)     # Set the value using an integer
        sequence('2')   # Set the value using a string containing an integer
        sequence()      # Return an integer
        """
        if isinstance(v, StringType):
            v = int(str)
        return self._singular_property('SEQUENCE', 'INTEGER', v)

    def summary(self, v=None):
        "Sets or gets the SUMMARY value of the Event."
        return self._singular_property('SUMMARY', 'TEXT', v)

    def uid(self, v=None):
        "Sets or gets the UID of the Event."
        return self._singular_property('UID', 'TEXT', v)

    def url(self, v=None):
        """Sets or returns the URL property."""
        return self._singular_property('URL', 'URI', v)

    ####
    # Not quite sure if this is how we want to handle recurrence rules, but
    # this is a start.

    def recurrence_set(self):
        "Returns the Events RecurrenceSet object."
        if self._recurrence_set == None:  # i.e haven't initialized one
            self._recurrence_set = RecurrenceSet()
        return self._recurrence_set

    ###
    # Alarm interface.  Returns an ComponentCollection.

    def alarms(self, values=None):
        """Sets or returns ALARM components.

        Examples:
        alarms((alarm1,))   # Set using Alarm component
        alarms()            # Returns an ComponentCollection of all Alarms
        """
        if values!=None:
            for alarm in values:
                self.add_component(alarm)
        else:
            return self.components('VALARM')

    ####
    # Methods that deal with Properties that can occur multiple times are
    # below.  They use the Collection class to return their Properties.

    def _multiple_properties(self, name, value_type, values,
                             property_obj=None):
        "Processes set/get for Properties that can have multiple instances."

        comp = self.inner_component()

        # Set value
        if values!=None:
            if not isinstance(values, TupleType) \
               and not isinstance(values, ListType):
                raise TypeError, "%s is not a tuple or list."

            # Delete old properties
            for p in comp.properties(name):
                comp.remove_property(p)

            for v in values:
                if property_obj:   # Specialized properties
                    if not isinstance(v, property_obj): # Make new object
                        new_prop = property_obj()
                        new_prop.value(v)
                    else:                            # Use existing object
                        new_prop = v
                else:                  # Generic properties
                    new_prop=Property(name)
                    # new_prop.value_type(value_type)
                    new_prop.value(v)

                comp.add_property(new_prop)

        # Get value
        else:
            return Collection(self, comp.properties(name))

    def attachments(self, values=None):
        """Sets or returns a Collection of Attach properties.

        'values' can be a sequence containing URLs (strings) and/or file-ish
        objects.
        """
        return self._multiple_properties("ATTACH", "", value, Attach)

    def attendees(self, value=None):
        """Sets attendees or returns a Collection of Attendee objects.

        If setting the attendees, pass a sequence as the argument.
        Examples:
        # Set using Attendee objects
        attendees((attObj1, attObj2))
        # Set using a CAL-ADDRESS string
        attendees(['MAILTO:jdoe@somewhere.com'])
        # Set using a combination of Attendee objects and strings
        attendees(['MAILTO:jdoe@somewhere.com', attObj1])
        # Returns a list of Attendee objects
        attendees()

        When setting the attendees, any previous Attendee objects in the Event
        are overwritten.  If you want to add to the Attendees, one way to do it
        is:

        attendees().append(Attendee('MAILTO:jdoe@nothere.com'))
        """
        return self._multiple_properties("ATTENDEE", "", value, Attendee)

    def categories(self, value=None):
        """Sets categories or returns a Collection of CATEGORIES properties.

        If setting the categories, pass a sequence as the argument.
        Examples:
        # Set using string[s]
        categories(('APPOINTMENT', 'EDUCATION'))
        # Returns a list of Category properties
        categories()

        When setting the attendees, any previous category Properties in the
        Event are overwritten.  If you want to add to the categories, one way
        to do it is:

        new_cat=Property('CATEGORIES')
        new_cat.value_type('TEXT')
        new_cat.value('PERSONAL')
        categories().append(new_cat)
        """
        return self._multiple_properties("CATEGORIES", "TEXT", value)

    def comments(self, value=None):
        "Sets or returns a Collection of COMMENT properties."
        return self._multiple_properties('COMMENT', 'TEXT', value)

    def contacts(self, value=None):
        "Sets or returns a Collection of CONTACT properties."
        return self._multiple_properties('CONTACT', 'TEXT', value)

    def related_tos(self, value=None):
        "Sets or returns a Collection of RELATED-TO properties."
        return self._multiple_properties('RELATED-TO', 'TEXT', value)

    def x_properties(self, name, value=None):
        "Sets or returns a Collection of X- properties."
        return self._multiple_properties(name, 'TEXT', value)

class Event(GenericComponent):
    "The iCalendar Event object."

    def __init__(self,ref=None):
        if ref != None:
            GenericComponent.__init__(self, ref=ref)
        else:
            GenericComponent.__init__(self, kind='VEVENT')

    def component_type(self):
        "Returns the type of component for the object."
        return "VEVENT"

    def dtend(self, v=None):
        """Sets or returns the value of the DTEND property.

        Usage:
        dtend(time_obj)             # Set the value using a Time object
        dtend('19970101T123000Z')   # Set the value as an iCalendar string
        dtend(982362522)            # Set the value using seconds (time_t)
        dtend()                     # Return the time as an iCalendar string

        If the dtend value is being set and duration() has a value, the
        duration property will be removed.
        """
        if v != None:
            duration = self.properties('DURATION')
            for d in duration:          # Clear DURATION properties
                self.remove_property(d)
        return self._singular_property("DTEND", "DATE-TIME", v, Time)

    def duration(self, v=None):
        """Sets or returns the value of the duration property.

        Usage:
        duration(dur_obj)       # Set the value using a Duration object
        duration("P3DT12H")     # Set value as an iCalendar string
        duration(3600)          # Set duration using seconds
        duration()              # Return duration as an iCalendar string

        If the duration value is being set and dtend() has a value, the dtend
        property will be removed.
        """

        if v != None:
            dtend = self.properties('DTEND')
            for d in dtend:
                self.remove_property(d)  # Clear DTEND properties
        return self._singular_property("DURATION", "DURATION", v, Duration)

    def status(self, v=None):
        "Sets or returns the value of the STATUS property."

        # These values are only good for VEVENT components (i.e. don't copy
        # & paste into VTODO or VJOURNAL
        valid_values=('TENTATIVE', 'CONFIRMED', 'CANCELLED')
        return self._singular_property("STATUS", "TEXT", v,
                                       enumerated_values=valid_values)

    def geo(self, v=None):
        """Sets or returns the value of the GEO property.

        Usage:
        geo(value) or
        geo()           # Returns the icalendar string

        'value' is either a icalendar GEO string or a sequence with two 'float'
        numbers.

        Examples:
        geo('40.232;-115.9531')     # Set value using string
        geo((40.232, -115.9531))    # Set value using a sequence
        geo()                       # Returns "40.232;-115.9531"

        To get the GEO property represented as a tuple and numbers instead of
        the iCalendar string, use geo_get_tuple().
        """

        if isinstance(v, ListType) or isinstance(v, TupleType):
            v = "%s;%s" % (float(v[0]), float(v[1]))
        return self._singular_property("GEO", "FLOAT", v)

    def geo_get_tuple(self):
        """Returns the GEO property as a tuple."""

        geo = self.geo()
        geo = split(geo, ';')
        return float(geo[0]), float(geo[1])

    def location(self, v=None):
        """Sets or returns the LOCATION property."""
        return self._singular_property("LOCATION", "TEXT", v)

    def transp(self, v=None):
        """Sets or returns the TRANSP property."""
        ok_values = ('OPAQUE', 'TRANSPARENT')
        return self._singular_property('TRANSP', 'TEXT', v,
                                       enumerated_values=ok_values)

    def resources(self, v=None):
        pass

class Todo(GenericComponent):
    "The iCalendar TODO component."

    def __init__(self,ref=None):
        if ref != None:
            GenericComponent.__init__(self, ref=ref)
        else:
            GenericComponent.__init__(self, kind='VTODO')


    def component_type(self):
        "Returns the type of component for the object."
        return "VTODO"

    def completed(self, value=None):
        return self._singular_property('COMPLETED', 'DATE-TIME', value, Time)

    def geo(self, value=None):
        if isinstance(value, ListType) or isinstance(value, TupleType):
            v = "%s;%s" % (float(value[0]), float(value[1]))
        return self._singular_property("GEO", "FLOAT", v)

    def location(self, value=None):
        return self._singular_property('LOCATION', 'TEXT', value)

    def percent(self, value=None):
        if value!=None:
            value = str(int(value))
        return self._singular_property('PERCENT', 'INTEGER', value)

    def status(self, value=None):
        if value!=None:
            value=string.upper(value)
        ok_values = ('NEEDS-ACTION', 'COMPLETED', 'IN-PROCESS', 'CANCELLED')
        return self._singular_property('STATUS', 'TEXT', value,
                                       enumerated_values=ok_values)

    def due(self, value=None):
        if value != None:
            duration = self.properties('DURATION')
            for d in duration:
                self.remove_property(d) # Clear DURATION properties
        return self._singular_property('DUE', 'DATE-TIME', value, Time)

    def duration(self, value=None):
        if value != None:
            due = self.properties('DUE')
            for d in due:
                self.remove_property(d)  # Clear DUE properties
        return self._singular_property("DURATION", "DURATION", value, Duration)

    def resources():
        pass

class Journal(GenericComponent):
    "The iCalendar JOURNAL component."

    def __init__(self):
        if ref != None:
            GenericComponent.__init__(self, ref=ref)
        else:
            GenericComponent.__init__(self, kind='VJOURNAL')

    def component_type(self):
        "Returns the type of component for the object."
        return "VJOURNAL"

    def status(self, v=None):
        if v!=None:
            v = string.upper(v)
        ok_values=('DRAFT', 'FINAL', 'CANCELLED')
        return self._singular_property('STATUS', 'TEXT', v,
                                       enumerated_values=ok_values)