1"""Factory class for all the property types."""
2
3from __future__ import annotations
4
5from typing import ClassVar
6
7from icalendar.caselessdict import CaselessDict
8from icalendar.prop.adr import vAdr
9from icalendar.prop.binary import vBinary
10from icalendar.prop.boolean import vBoolean
11from icalendar.prop.cal_address import vCalAddress
12from icalendar.prop.categories import vCategory
13from icalendar.prop.dt import (
14 vDate,
15 vDatetime,
16 vDDDLists,
17 vDDDTypes,
18 vDuration,
19 vPeriod,
20 vTime,
21 vUTCOffset,
22)
23from icalendar.prop.float import vFloat
24from icalendar.prop.geo import vGeo
25from icalendar.prop.inline import vInline
26from icalendar.prop.n import vN
27from icalendar.prop.org import vOrg
28from icalendar.prop.recur import vFrequency, vRecur, vWeekday
29from icalendar.prop.text import vText
30from icalendar.prop.uid import vUid
31from icalendar.prop.unknown import vUnknown
32from icalendar.prop.uri import vUri
33from icalendar.prop.xml_reference import vXmlReference
34
35from .integer import vInt
36
37
38class TypesFactory(CaselessDict):
39 """Factory for all value types defined in :rfc:`5545` and subsequent.
40
41 The value and parameter names don't overlap. So one factory is enough for
42 both kinds.
43 """
44
45 _instance: ClassVar[TypesFactory | None] = None
46
47 def instance() -> TypesFactory:
48 """Return a singleton instance of this class."""
49 if TypesFactory._instance is None:
50 TypesFactory._instance = TypesFactory()
51 return TypesFactory._instance
52
53 def __init__(self, *args, **kwargs):
54 """Set keys to upper for initial dict"""
55 super().__init__(*args, **kwargs)
56 self.all_types = (
57 vBinary,
58 vBoolean,
59 vCalAddress,
60 vDDDLists,
61 vDDDTypes,
62 vDate,
63 vDatetime,
64 vDuration,
65 vFloat,
66 vFrequency,
67 vGeo,
68 vInline,
69 vInt,
70 vPeriod,
71 vRecur,
72 vText,
73 vTime,
74 vUTCOffset,
75 vUri,
76 vWeekday,
77 vCategory,
78 vAdr,
79 vN,
80 vOrg,
81 vUid,
82 vXmlReference,
83 vUnknown,
84 )
85 self["binary"] = vBinary
86 self["boolean"] = vBoolean
87 self["cal-address"] = vCalAddress
88 self["date"] = vDDDTypes
89 self["date-time"] = vDDDTypes
90 self["duration"] = vDDDTypes
91 self["float"] = vFloat
92 self["integer"] = vInt
93 self["period"] = vPeriod
94 self["recur"] = vRecur
95 self["text"] = vText
96 self["time"] = vTime
97 self["uri"] = vUri
98 self["utc-offset"] = vUTCOffset
99 self["geo"] = vGeo
100 self["inline"] = vInline
101 self["date-time-list"] = vDDDLists
102 self["categories"] = vCategory
103 self["adr"] = vAdr # RFC 6350 vCard
104 self["n"] = vN # RFC 6350 vCard
105 self["org"] = vOrg # RFC 6350 vCard
106 self["unknown"] = vUnknown # RFC 7265
107 self["uid"] = vUid # RFC 9253
108 self["xml-reference"] = vXmlReference # RFC 9253
109
110 #################################################
111 # Property types
112
113 # These are the default types
114 types_map = CaselessDict(
115 {
116 ####################################
117 # Property value types
118 # Calendar Properties
119 "calscale": "text",
120 "method": "text",
121 "prodid": "text",
122 "version": "text",
123 # Descriptive Component Properties
124 "attach": "uri",
125 "categories": "categories",
126 "class": "text",
127 # vCard Properties (RFC 6350)
128 "adr": "adr",
129 "n": "n",
130 "org": "org",
131 "comment": "text",
132 "description": "text",
133 "geo": "geo",
134 "location": "text",
135 "percent-complete": "integer",
136 "priority": "integer",
137 "resources": "text",
138 "status": "text",
139 "summary": "text",
140 # RFC 9253
141 # link should be uri, xml-reference or uid
142 # uri is likely most helpful if people forget to set VALUE
143 "link": "uri",
144 "concept": "uri",
145 "refid": "text",
146 # Date and Time Component Properties
147 "completed": "date-time",
148 "dtend": "date-time",
149 "due": "date-time",
150 "dtstart": "date-time",
151 "duration": "duration",
152 "freebusy": "period",
153 "transp": "text",
154 "refresh-interval": "duration", # RFC 7986
155 # Time Zone Component Properties
156 "tzid": "text",
157 "tzname": "text",
158 "tzoffsetfrom": "utc-offset",
159 "tzoffsetto": "utc-offset",
160 "tzurl": "uri",
161 # Relationship Component Properties
162 "attendee": "cal-address",
163 "contact": "text",
164 "organizer": "cal-address",
165 "recurrence-id": "date-time",
166 "related-to": "text",
167 "url": "uri",
168 "conference": "uri", # RFC 7986
169 "source": "uri",
170 "uid": "text",
171 # Recurrence Component Properties
172 "exdate": "date-time-list",
173 "exrule": "recur",
174 "rdate": "date-time-list",
175 "rrule": "recur",
176 # Alarm Component Properties
177 "action": "text",
178 "repeat": "integer",
179 "trigger": "duration",
180 "acknowledged": "date-time",
181 # Change Management Component Properties
182 "created": "date-time",
183 "dtstamp": "date-time",
184 "last-modified": "date-time",
185 "sequence": "integer",
186 # Miscellaneous Component Properties
187 "request-status": "text",
188 ####################################
189 # parameter types (luckily there is no name overlap)
190 "altrep": "uri",
191 "cn": "text",
192 "cutype": "text",
193 "delegated-from": "cal-address",
194 "delegated-to": "cal-address",
195 "dir": "uri",
196 "encoding": "text",
197 "fmttype": "text",
198 "fbtype": "text",
199 "language": "text",
200 "member": "cal-address",
201 "partstat": "text",
202 "range": "text",
203 "related": "text",
204 "reltype": "text",
205 "role": "text",
206 "rsvp": "boolean",
207 "sent-by": "cal-address",
208 "value": "text",
209 # rfc 9253 parameters
210 "label": "text",
211 "linkrel": "text",
212 "gap": "duration",
213 }
214 )
215
216 def for_property(self, name, value_param: str | None = None) -> type:
217 """Returns the type class for a property or parameter.
218
219 Parameters:
220 name: Property or parameter name
221 value_param: Optional ``VALUE`` parameter, for example,
222 "DATE", "DATE-TIME", or other string.
223
224 Returns:
225 The appropriate value type class.
226 """
227 # Special case: RDATE and EXDATE always use vDDDLists to support list values
228 # regardless of the VALUE parameter
229 if name.upper() in ("RDATE", "EXDATE"):
230 return self["date-time-list"]
231
232 # Only use VALUE parameter for known properties that support multiple value
233 # types (like DTSTART, DTEND, etc. which can be DATE or DATE-TIME)
234 # For unknown/custom properties, always use the default type from types_map
235 if value_param and name in self.types_map and value_param in self:
236 return self[value_param]
237 return self[self.types_map.get(name, "unknown")]
238
239 def to_ical(self, name, value):
240 """Encodes a named value from a primitive python type to an icalendar
241 encoded string.
242 """
243 type_class = self.for_property(name)
244 return type_class(value).to_ical()
245
246 def from_ical(self, name, value):
247 """Decodes a named property or parameter value from an icalendar
248 encoded string to a primitive python type.
249 """
250 type_class = self.for_property(name)
251 return type_class.from_ical(value)
252
253
254__all__ = ["TypesFactory"]