The base classes
All iCalendar Component classes and iCalendar Property classes inherit from ICalBaseClass
.
Then ICalBaseClass is extended by both Component and Property.
ICalBaseClass
This is the base class of all custom classes representing an iCal component or iCal property in our library.
ical_library.base_classes.Component and :class:Property
are the only ones inheriting this class directly, the
rest of the classes are inheriting from :class:Component
and :class:Property
based on whether they represent an
iCal component or iCal property.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
the actual name of this property or component. E.g. VEVENT, RRULE, VCUSTOMCOMPONENT, CUSTOMPROPERTY. |
required |
parent |
Optional[Component]
|
The Component this item is encapsulated by in the iCalendar data file. |
required |
Source code in ical_library/base_classes/base_class.py
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 |
|
name: str
property
Return the actual name of this property or component. E.g. VEVENT, RRULE, VCUSTOMCOMPONENT, CUSTOMPROPERTY.
We inherit this class, for the general Property and Component but also for the specific VEvent component and
the RRule property. Now what do we do with the x-comp
or iana-comp
components and x-prop
and iana-prop
properties? They also have an iCalendar name, e.g. VCUSTOMCOMPONENT. However, we can't specify them beforehand
as we simply can't cover all cases. Therefore, we use get_ical_name_of_class
to find and map all of our
pre-defined Components and Properties but we still specify the name for all custom components. So the rule of
thumb:
Use .name
on instantiated classes while we use .get_ical_name_of_class()
for non-instantiated classes.
parent: Optional[Component]
property
writable
Return the parent :class:Component
that contains this :class:Component
.
Returns:
Type | Description |
---|---|
Return the parent :class: |
get_ical_name_of_class()
classmethod
Return the name of a pre-defined property or pre-defined component. E.g. VEVENT, RRULE, COMPONENT, PROPERTY.
For a :class:Property
this would be the value at the start of the line. Example: a property with the name of
ABC;def=ghi:jkl
would be ABC
.
For a :class:Component
this would be the value at the start of the component after BEGIN. Example: a VEvent
starts with BEGIN:VEVENT
, hence this function would return VEVENT
.
Source code in ical_library/base_classes/base_class.py
56 57 58 59 60 61 62 63 64 65 66 |
|
Component
Bases: ICalBaseClass
This is the base class for any component (according to the RFC 5545 specification) in iCal-library.
Inside all components (so also all classes inheriting this class, e.g. VEvent) there are four kind of variables:
- variables that start with
_
. These are metadata of the class and not parsed as a property or component from the iCalendar data file. - variables that have a type of
List[x]
and a default value of List. These are child components/properties of the instance. These components/properties may or may not be required to be present in the iCal file. - variables that have a type of
Optional[List[x]]
. These are components/properties of the instance. They can be either optional or required and may occur multiple times in the iCal file. - variables that have a type of
Optional[x]
(and notOptional[List[x]]
). These are properties of the instance. They can be either optional or required, but may only occur once in the iCal file.
Any Component that is predefined according to the RFC 5545 should inherit this class, e.g. VCalendar, VEVENT. Only x-components or iana-components should instantiate the Component class directly.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
The actual name of this component instance. E.g. |
required |
parent |
Optional[Component]
|
The Component this item is encapsulated by in the iCalendar data file. |
None
|
Source code in ical_library/base_classes/component.py
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 |
|
children: List[Component]
property
Return all children components.
extra_child_components: Dict[str, List[Component]]
property
Return all children components that are considered as x-comp
or iana-comp
components.
extra_properties: Dict[str, List[Property]]
property
Return all properties that are considered as x-prop
or iana-prop
properties.
original_ical_text: str
property
Return the original iCAL text for your property from the RAW string list as if it is a property.
properties: Dict[str, Union[Property, List[Property]]]
property
Return all iCalendar properties of this component instance.
tree_root: VCalendar
property
Return the tree root which should always be a VCalendar object.
__add_child_without_setting_the_parent(child)
Just add a child component and do not also set the parent.
If the child is an undefined x-comp
or iana-comp
component, we add it to _extra_child_components.
If the child is defined, we add it to one of the other variables according to
:function:self._get_child_component_mapping()
.
Source code in ical_library/base_classes/component.py
142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
|
__enter__()
Enter the context manager. Check ComponentContext for more info.
Source code in ical_library/base_classes/component.py
78 79 80 81 |
|
__exit__(_type, _value, _tb)
Exit the context manager. Check ComponentContext for more info.
Source code in ical_library/base_classes/component.py
83 84 85 |
|
_extract_ical_class_from_args(var_name, a_type)
staticmethod
Given a_type, which is either a List or an Optional, return the subtype that is not None.
Note: When we execute get_args(some_type), we consider the result to be the subtypes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
var_name |
str
|
The variable name of the type we are dissecting. |
required |
a_type |
Union[Type[List], type(Union)]
|
The type we want to get the subtype of. |
required |
Returns:
Type | Description |
---|---|
Type
|
The subtype that is not equal to the NoneType. |
Source code in ical_library/base_classes/component.py
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
|
_extract_type_information(var_name, a_type, is_in_list)
staticmethod
Extract typing information for an instance variable of the component.
The type of the variable should either be (wrapping) a :class:Property
or a :class:Component
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
var_name |
str
|
The variable name of the type we are dissecting. |
required |
a_type |
Type
|
The type we want to extract a child class of :class: |
required |
is_in_list |
bool
|
Whether the child class of :class: |
required |
Returns:
Type | Description |
---|---|
Optional[Tuple[str, Tuple[str, Optional[Type[ICalBaseClass]], bool]]]
|
None if there is no child class of :class: |
Source code in ical_library/base_classes/component.py
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 |
|
_get_child_component_mapping()
cached
classmethod
Return the same mapping as :function:cls._get_var_mapping()
but only return variables related to
:class:Component
classes.
Source code in ical_library/base_classes/component.py
272 273 274 275 276 277 278 279 280 281 282 283 |
|
_get_init_method_for_var_mapping()
classmethod
We generate _get_var_mapping based on cls.__init__
. This var mapping is later used to list all properties,
all components but also all the types of the items. This is a function so that it can be overwritten for
the recurring components.
Source code in ical_library/base_classes/component.py
228 229 230 231 232 233 234 235 |
|
_get_property_mapping()
cached
classmethod
Return the same mapping as :function:cls._get_var_mapping()
but only return variables related to
:class:Property
classes. Example: {"RRULE": tuple("rrule", Type[RRule], False), ...}
Source code in ical_library/base_classes/component.py
259 260 261 262 263 264 265 266 267 268 269 270 |
|
_get_var_mapping()
cached
classmethod
Get a mapping of all variables of this class that do not start with _
.
Returns:
Type | Description |
---|---|
Mapping[str, Tuple[str, Type[ICalBaseClass], bool]]
|
A class mapping that maps the iCal name (e.g. VEVENT) to another tuple that contains the variable name, the child class of :class: |
Source code in ical_library/base_classes/component.py
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
|
_set_self_as_parent_for_ical_component(prop_or_comp)
Verifies the parent is not already set to a different component, if not sets the parent.
Source code in ical_library/base_classes/component.py
87 88 89 90 91 92 93 94 |
|
add_child(child)
Add a children component and set its parent.
If the child is an undefined x-comp
or iana-comp
component, we add it to _extra_child_components.
If the child is defined, we add it to one of the other variables according to
:function:self._get_child_component_mapping()
.
Source code in ical_library/base_classes/component.py
157 158 159 160 161 162 163 164 165 166 |
|
as_parent(value)
We set self as Parent for Properties and Components but also Properties and Components in lists.
Source code in ical_library/base_classes/component.py
96 97 98 99 100 101 102 103 104 |
|
get_property_ical_names()
cached
classmethod
Get all the variables for this component class that reference a :class:Property
in the typing information.
Source code in ical_library/base_classes/component.py
173 174 175 176 177 178 179 |
|
parse_component(lines, line_number)
Parse the raw lines representing this component (which was just instantiated).
Based on the first line that starts with BEGIN:
, we know using self._get_child_component_mapping() which
specific component type we should instantiate. We then add it to the current component instance as a child.
Then we parse line by line, if we find another BEGIN:
, we create another component instance and proceed
to calling :function:self.parse_component
for parsing all the lines related to that component. If we find
a property line (any line that doesn't start with BEGIN:
), we call :function:self.parse_property
which
then automatically adds it to the current instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lines |
List[str]
|
A list of all the lines in the iCalendar file. |
required |
line_number |
int
|
The line number at which this component starts. |
required |
Returns:
Type | Description |
---|---|
int
|
The line number at which this component ends. |
Source code in ical_library/base_classes/component.py
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 |
|
parse_property(line)
Parse a raw line containing a :class:Property
definition, instantiate the corresponding Property and set the
variable.
Based on the first part of the line (before the ; and :), we know using self._get_property_mapping() which
property type we should instantiate. Then, depending on whether the typing info of the property denoted it in
a List or not, it adds it to a list/instantiates the list, compared to simply setting it as the variable of
the :class:Component
instance.
Credits for the excellent regex parsing string go to @Jan Goyvaerts: https://stackoverflow.com/a/2482067/2277445
Parameters:
Name | Type | Description | Default |
---|---|---|---|
line |
str
|
The entire line that contains the property string (meaning multi-lines properties are already converted to a single line here). |
required |
Returns:
Type | Description |
---|---|
Property
|
The created Property instance based on the line that we set for the component. |
Source code in ical_library/base_classes/component.py
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 |
|
print_tree_structure(indent=0)
Print the tree structure of all components starting with this instance.
Source code in ical_library/base_classes/component.py
295 296 297 298 299 |
|
set_property(property_instance, property_map_info=None, property_map_was_checked=False)
Setting a property for a Component instance.
If the property_map_info
is equal to None, we either have not yet looked up all the properties or it is an
x-prop/iana-prop. This can be decided based on property_map_was_checked. This avoids extra (expensive) lookups
in our self._get_property_mapping.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
property_instance |
Property
|
The Property we wish to set. |
required |
property_map_info |
Optional[Tuple[str, Type[Property], bool]]
|
A tuple containing the variable name for this Component instance, the type of the Property and whether the variable can occur multiple times for the same property. If it equals None, this either means it is an x-prop/iana-prop or that the property_map was not checked yet. |
None
|
property_map_was_checked |
bool
|
Whether the |
False
|
Source code in ical_library/base_classes/component.py
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 |
|
ComponentContext
Component context is used to keep the current Component when Component is used as ContextManager.
You can use components as context: with Component() as my_component:
.
If you do this the context stores the Component and whenever a new component or property is created, it will use such stored Component as the parent Component.
Source code in ical_library/help_modules/component_context.py
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 |
|
get_current_component()
classmethod
Get the current context managed component.
Source code in ical_library/help_modules/component_context.py
37 38 39 40 |
|
pop_context_managed_component()
classmethod
Pop the current context managed component.
Source code in ical_library/help_modules/component_context.py
27 28 29 30 31 32 33 34 35 |
|
push_context_managed_component(component)
classmethod
Set the current context managed component.
Source code in ical_library/help_modules/component_context.py
20 21 22 23 24 25 |
|