Imported Upstream version 0.48
[platform/upstream/libical.git] / src / python / DerivedProperties.py
1 #!/usr/bin/env python 
2 # -*- Mode: python -*-
3 #======================================================================
4 # FILE: DerivedProperties.py
5 # CREATOR: eric 
6 #
7 # DESCRIPTION:
8 #   
9 #
10 #  $Id: DerivedProperties.py,v 1.4 2001-04-03 15:18:42 ebusboom Exp $
11 #  $Locker:  $
12 #
13 # (C) COPYRIGHT 2001, Eric Busboom <eric@softwarestudio.org>
14 # (C) COPYRIGHT 2001, Patrick Lewis <plewis@inetarena.com>  
15 #
16 # This program is free software; you can redistribute it and/or modify
17 # it under the terms of either: 
18 #
19 #    The LGPL as published by the Free Software Foundation, version
20 #    2.1, available at: http://www.fsf.org/copyleft/lesser.html
21 #
22 #  Or:
23 #
24 #    The Mozilla Public License Version 1.0. You may obtain a copy of
25 #    the License at http://www.mozilla.org/MPL/
26 #======================================================================
27
28 from Property import Property
29 from Time import Time
30 from Period import Period
31 from Duration import Duration
32
33 def RDate(arg):
34
35     class RDate_Time(Time):
36         def __init__(self,arg): Time.__init__(self,arg,"RDATE")
37     
38     class RDate_Period(Period):
39         def __init__(self,arg): Period.__init__(self,arg,"RDATE")
40
41     p = None
42     for c in [RDate_Time, RDate_Period]:
43         try: return c(arg)
44         except Property.ConstructorFailedError, d: pass
45     raise Property.ConstructorFailedError("Failed to construct RDATE from "+str(arg))
46
47
48 def Trigger(arg):        
49     class Trigger_Time(Time): 
50         def __init__(self,arg): Time.__init__(self,arg,"TRIGGER")
51     
52     class Trigger_Duration(Duration):
53         def __init__(self,arg): Duration.__init__(self,arg,"TRIGGER")
54
55     p = None
56     for c in [Trigger_Duration, Trigger_Time]:
57         try: return c(arg)
58         except Property.ConstructorFailedError, d: pass        
59     raise Property.ConstructorFailedError("Failed to construct TRIGGER from "+str(arg))
60
61
62
63 class Recurrence_Id(Time):
64     """Class for RECURRENCE-ID property.
65
66     Usage:
67     Reccurence_Id(dict)         # A normal property dictionary
68     Reccurence_Id("19960401")   # An iCalendar string
69     Reccurence_Id(8349873494)   # Seconds from epoch
70
71     If the 'dict' constructor is used, 'name' and 'value_type'
72     entries in dict are ignored and automatically set with the appropriate
73     values.
74     """
75
76     def __init__(self, dict={}):
77         Time.__init__(self, dict)
78         Property.name(self, 'RECURRENCE-ID')
79
80     def name(self):
81         return Property.name(self)
82
83     def _doParam(self, parameter, v):
84         if v!=None:
85             self[parameter]=v
86         return self[parameter]
87
88     # Enumerated parameters
89     def value_parameter(self, v=None):
90         """Sets or gets the VALUE parameter value.
91
92         The value passed should be either "DATE-TIME" or "DATE".  Setting this
93         parameter has no impact on the property's value_type.  Doing something
94         like:
95
96         rid=Recurrence_Id("19960401")    # Sets value & makes value_type="DATE"
97         rid.value_parameter("DATE-TIME") # Sets the parameter VALUE=DATE-TIME
98
99         Would be allowed (even though it is wrong), so pay attention.
100         Verifying the component will reveal the error.
101         """
102         if v!=None and v!="DATE" and v!="DATE-TIME":
103             raise ValueError, "%s is an invalid VALUE parameter value" % str(v)
104         self._doParam("VALUE", v)
105
106     def tzid(self, v=None):
107         "Sets or gets the TZID parameter value."
108         self._doParam("TZID", v)
109
110     def range_parameter(self, v=None): # 'range' is a builtin function
111         "Sets or gets the RANGE parameter value."
112         if v!=None and v!="THISANDPRIOR" and v!= "THISANDFUTURE":
113             raise ValueError, "%s is an invalid RANGE parameter value" % str(v)
114         self._doParam("RANGE", v)
115
116 class Attach(Property):
117     """A class representing an ATTACH property.
118
119     Usage:
120     Attach(uriString [, parameter_dict])
121     Attach(fileObj [, parameter_dict])
122     """
123
124     def __init__(self, value=None, parameter_dict={}):
125         Property.__init__(self, parameter_dict)
126         Property.name(self, 'ATTACH')
127         self.value(value)
128
129     def value(self, v=None):
130         "Returns or sets the value of the property."
131         if v != None:
132             if isinstance(v, StringType):  # Is a URI
133                 self._desc['value']=v
134                 Property.value_type(self, 'URI')
135             else:
136                 try:
137                     tempStr = v.read()
138                 except:
139                     raise TypeError,"%s must be a URL string or file-ish type"\
140                           % str(v)
141                 self._desc['value'] = base64.encodestring(tempStr)
142                 Property.value_type(self, 'BINARY')
143         else:
144             return self._desc['value']
145
146     def name(self):
147         "Returns the name of the property."
148         return Property.name(self)
149
150     def value_type(self):
151         return Property.value_type(self)
152
153     def fmttype(self, v=None):
154         "Gets or sets the FMTYPE parameter."
155         if v!= None:
156             self['FMTTYPE']=v
157         else:
158             return self['FMTTYPE']
159