File: relation.py

package info (click to toggle)
python-ical 9.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,448 kB
  • sloc: python: 13,877; sh: 9; makefile: 5
file content (69 lines) | stat: -rw-r--r-- 2,276 bytes parent folder | download
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
"""Implementation of the RELATED-TO property."""

import enum
from dataclasses import dataclass
from typing import Any
import logging

try:
    from pydantic.v1 import root_validator
except ImportError:
    from pydantic import root_validator # type: ignore[no-redef]

from .data_types import DATA_TYPE
from ical.parsing.property import ParsedProperty, ParsedPropertyParameter
from .parsing import parse_parameter_values


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."""


@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 = root_validator(pre=True, allow_reuse=True)(
        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"]])]