File: boolean.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 (28 lines) | stat: -rw-r--r-- 811 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
"""Library for parsing and encoding BOOLEAN values."""

from ical.parsing.property import ParsedProperty

from .data_types import DATA_TYPE


@DATA_TYPE.register("BOOLEAN")
class BooleanEncoder:
    """Encode a boolean ICS value."""

    @classmethod
    def __property_type__(cls) -> type:
        return bool

    @classmethod
    def __parse_property_value__(cls, prop: ParsedProperty) -> bool:
        """Parse an rfc5545 property into a boolean."""
        if prop.value == "TRUE":
            return True
        if prop.value == "FALSE":
            return False
        raise ValueError(f"Unable to parse value as boolean: {prop}")

    @classmethod
    def __encode_property_value__(cls, value: bool) -> str:
        """Serialize boolean as an ICS value."""
        return "TRUE" if value else "FALSE"