Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/icalendar/prop/text.py: 63%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

52 statements  

1"""TEXT values from :rfc:`5545`.""" 

2 

3from typing import Any, ClassVar 

4 

5from icalendar.compatibility import Self 

6from icalendar.error import JCalParsingError 

7from icalendar.parser import Parameters, escape_char 

8from icalendar.parser_tools import DEFAULT_ENCODING, ICAL_TYPE, to_unicode 

9 

10 

11class vText(str): 

12 """Simple text.""" 

13 

14 default_value: ClassVar[str] = "TEXT" 

15 params: Parameters 

16 __slots__ = ("encoding", "params") 

17 

18 def __new__( 

19 cls, 

20 value: ICAL_TYPE, 

21 encoding: str = DEFAULT_ENCODING, 

22 /, 

23 params: dict[str, Any] | None = None, 

24 ) -> Self: 

25 value = to_unicode(value, encoding=encoding) 

26 self = super().__new__(cls, value) 

27 self.encoding = encoding 

28 self.params = Parameters(params) 

29 return self 

30 

31 def __repr__(self) -> str: 

32 return f"vText({self.to_ical()!r})" 

33 

34 def to_ical(self) -> bytes: 

35 return escape_char(self).encode(self.encoding) 

36 

37 @classmethod 

38 def from_ical(cls, ical: ICAL_TYPE) -> Self: 

39 return cls(ical) 

40 

41 @property 

42 def ical_value(self) -> str: 

43 """The string value of the text.""" 

44 return str(self) 

45 

46 from icalendar.param import ALTREP, GAP, LANGUAGE, RELTYPE, VALUE 

47 

48 def to_jcal(self, name: str) -> list: 

49 """The jCal representation of this property according to :rfc:`7265`.""" 

50 if name == "request-status": # TODO: maybe add a vRequestStatus class? 

51 return [name, {}, "text", self.split(";", 2)] 

52 return [name, self.params.to_jcal(), self.VALUE.lower(), str(self)] 

53 

54 @classmethod 

55 def examples(cls) -> list[Self]: 

56 """Examples of vText.""" 

57 return [cls("Hello World!")] 

58 

59 @classmethod 

60 def from_jcal(cls, jcal_property: list) -> Self: 

61 """Parse jCal from :rfc:`7265`. 

62 

63 Parameters: 

64 jcal_property: The jCal property to parse. 

65 

66 Raises: 

67 ~error.JCalParsingError: If the provided jCal is invalid. 

68 """ 

69 JCalParsingError.validate_property(jcal_property, cls) 

70 name = jcal_property[0] 

71 if name == "categories": 

72 from icalendar.prop import vCategory 

73 

74 return vCategory.from_jcal(jcal_property) 

75 string = jcal_property[3] # TODO: accept list or string but join with ; 

76 if name == "request-status": # TODO: maybe add a vRequestStatus class? 

77 JCalParsingError.validate_list_type(jcal_property[3], str, cls, 3) 

78 string = ";".join(jcal_property[3]) 

79 JCalParsingError.validate_value_type(string, str, cls, 3) 

80 return cls( 

81 string, 

82 params=Parameters.from_jcal_property(jcal_property), 

83 ) 

84 

85 @classmethod 

86 def parse_jcal_value(cls, jcal_value: Any) -> Self: 

87 """Parse a jCal value into a vText.""" 

88 JCalParsingError.validate_value_type(jcal_value, (str, int, float), cls) 

89 return cls(str(jcal_value)) 

90 

91 

92__all__ = ["vText"]