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
|
"""Implementation of the RELATED-TO property."""
import enum
from dataclasses import dataclass
from typing import Any, Self
import logging
from pydantic import model_validator
from ical.parsing.property import ParsedProperty, ParsedPropertyParameter
from .data_types import DATA_TYPE
from .parsing import parse_parameter_values
@DATA_TYPE.register("RELATIONSHIP-TYPE")
class RelationshipType(str, enum.Enum):
"""Type of hierarchical relationship associated with the calendar component."""
PARENT = "PARENT"
"""Parent relationship - Default."""
CHILD = "CHILD"
"""Child relationship."""
SIBBLING = "SIBBLING"
"""Sibling relationship."""
@classmethod
def __parse_property_value__(cls, prop: ParsedProperty) -> Self | None:
"""Parse value into enum."""
try:
return cls(prop.value)
except ValueError:
return None
@DATA_TYPE.register("RELATED-TO")
@dataclass
class RelatedTo:
"""Used to represent a relationship or reference between one calendar component and another."""
uid: str
"""The value of the related-to property is the persistent, globally unique identifier of another calendar component."""
reltype: RelationshipType = RelationshipType.PARENT
"""Indicate the type of hierarchical relationship associated with the calendar component specified by the uid."""
@classmethod
def __parse_property_value__(cls, prop: Any) -> dict[str, Any]:
"""Parse a rfc5545 int value."""
logging.info("prop=%s", prop)
if isinstance(prop, ParsedProperty):
data: dict[str, Any] = {"uid": prop.value}
for param in prop.params or ():
if len(param.values) > 1:
raise ValueError("Expected only one value for RELATED-TO parameter")
data[param.name] = param.values[0]
return data
return {"uid": prop}
_parse_parameter_values = model_validator(mode="before")(parse_parameter_values)
@classmethod
def __encode_property_value__(cls, model_data: dict[str, str]) -> str | None:
return model_data.pop("uid")
@classmethod
def __encode_property_params__(
cls, model_data: dict[str, Any]
) -> list[ParsedPropertyParameter]:
if "reltype" not in model_data:
return []
return [ParsedPropertyParameter(name="RELTYPE", values=[model_data["reltype"]])]
|