1"""SKIP value type of RECUR from :rfc:`7529`."""
2
3from __future__ import annotations
4
5from typing import TYPE_CHECKING, Any
6from xml.etree.ElementTree import Element, SubElement
7
8from icalendar.compatibility import Self # noqa: TC001
9from icalendar.enums import Enum
10from icalendar.error import JCalParsingError, XCalParsingError
11from icalendar.parser.xcal.wrapper import from_xcal_wrapper
12from icalendar.prop.text import vText
13
14if TYPE_CHECKING:
15 from icalendar.parser.parameter import Parameters
16 from icalendar.parser.xcal.base import XCalParser
17
18
19class vSkip(vText, Enum):
20 """Skip values for RRULE.
21
22 These are defined in :rfc:`7529`.
23
24 OMIT is the default value.
25
26 Examples:
27
28 .. code-block:: pycon
29
30 >>> from icalendar import vSkip
31 >>> vSkip.OMIT
32 vSkip('OMIT')
33 >>> vSkip.FORWARD
34 vSkip('FORWARD')
35 >>> vSkip.BACKWARD
36 vSkip('BACKWARD')
37 """
38
39 OMIT = "OMIT"
40 FORWARD = "FORWARD"
41 BACKWARD = "BACKWARD"
42
43 __reduce_ex__ = Enum.__reduce_ex__
44
45 def __repr__(self):
46 return f"{self.__class__.__name__}({self._name_!r})"
47
48 @classmethod
49 def parse_jcal_value(cls, value: Any) -> Self:
50 """Parse a jCal value for vSkip.
51
52 Raises:
53 ~error.JCalParsingError: If the value is not a valid skip value.
54 """
55 JCalParsingError.validate_value_type(value, str, cls)
56 try:
57 return cls[value.upper()]
58 except KeyError as e:
59 raise JCalParsingError(
60 "The value must be a valid skip value.", cls, value=value
61 ) from e
62
63 @classmethod
64 @from_xcal_wrapper
65 def from_xcal_in_recur(cls, parser: XCalParser, _params: Parameters) -> Self:
66 """Parse xCal from :rfc:`6321`.
67
68 Parameters:
69 xml: The XML to parse or a parser.
70
71 Raises:
72 ~error.XCalParsingError: If the provided xCal is invalid.
73 """
74 child = parser.parse_tag()
75 text = child.get_xsd_token()
76 try:
77 return cls(text.upper())
78 except ValueError as e:
79 raise XCalParsingError(
80 "Expected OMIT|BACKWARD|FORWARD https://datatracker.ietf.org/doc/html/rfc7529#section-4.1",
81 text,
82 child,
83 ) from e
84
85 def to_xcal(self, element: Element) -> None:
86 """Add the xCal representation of this property according to :rfc:`6321`."""
87 self.params.to_xcal(element)
88 skip_element = SubElement(element, "skip")
89 skip_element.text = self.name
90
91
92__all__ = ["vSkip"]