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

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

42 statements  

1"""This contains the IMAGE property from :rfc:`7986`.""" 

2 

3from __future__ import annotations 

4 

5from typing import TYPE_CHECKING 

6 

7from icalendar.prop.binary import vBinary 

8from icalendar.prop.uri import vUri 

9 

10if TYPE_CHECKING: 

11 from icalendar.prop.text import vText 

12 

13 

14class Image: 

15 """An image as URI or BINARY according to :rfc:`7986`. 

16 

17 Value Type: 

18 URI or BINARY -- no default. The value MUST be data 

19 with a media type of "image" or refer to such data. 

20 

21 Description: 

22 This property specifies an image for an iCalendar 

23 object or a calendar component via a URI or directly with inline 

24 data that can be used by calendar user agents when presenting the 

25 calendar data to a user. Multiple properties MAY be used to 

26 specify alternative sets of images with, for example, varying 

27 media subtypes, resolutions, or sizes. When multiple properties 

28 are present, calendar user agents SHOULD display only one of them, 

29 picking one that provides the most appropriate image quality, or 

30 display none. The "DISPLAY" parameter is used to indicate the 

31 intended display mode for the image. The "ALTREP" parameter, 

32 defined in :rfc:`5545`, can be used to provide a "clickable" image 

33 where the URI in the parameter value can be "launched" by a click 

34 on the image in the calendar user agent. 

35 

36 Parameters: 

37 uri: The URI of the image. 

38 b64data: The data of the image, base64 encoded. 

39 fmttype: The format type, e.g. ``"image/png"``. 

40 altrep: Link target of the image. 

41 display: The display mode, e.g. ``"BADGE"``. 

42 

43 """ 

44 

45 @classmethod 

46 def from_property_value(cls, value: vUri | vBinary | vText) -> Image: 

47 """Create an Image from a property value.""" 

48 params: dict[str, str] = {} 

49 if not hasattr(value, "params"): 

50 raise TypeError("Value must be URI or BINARY.") 

51 try: 

52 value_type = value.params.get("VALUE", "").upper() 

53 except (AttributeError, TypeError) as err: 

54 raise TypeError("Value must have a valid params attribute.") from err 

55 if value_type == "URI" or isinstance(value, vUri): 

56 params["uri"] = str(value) 

57 elif isinstance(value, vBinary): 

58 params["b64data"] = value.base64data 

59 elif value_type == "BINARY": 

60 params["b64data"] = str(value) 

61 else: 

62 raise TypeError( 

63 f"The VALUE parameter must be URI or BINARY, not {value_type!r}." 

64 ) 

65 params.update( 

66 { 

67 "fmttype": value.params.get("FMTTYPE"), 

68 "altrep": value.params.get("ALTREP"), 

69 "display": value.params.get("DISPLAY"), 

70 } 

71 ) 

72 return cls(**params) 

73 

74 def __init__( 

75 self, 

76 uri: str | None = None, 

77 b64data: str | None = None, 

78 fmttype: str | None = None, 

79 altrep: str | None = None, 

80 display: str | None = None, 

81 ) -> None: 

82 """Create a new image according to :rfc:`7986`.""" 

83 if uri is not None and b64data is not None: 

84 raise ValueError("Image cannot have both URI and binary data (RFC 7986)") 

85 if uri is None and b64data is None: 

86 raise ValueError("Image must have either URI or binary data") 

87 self.uri = uri 

88 self.b64data = b64data 

89 self.fmttype = fmttype 

90 self.altrep = altrep 

91 self.display = display 

92 

93 @property 

94 def data(self) -> bytes | None: 

95 """Return the binary data, if available.""" 

96 if self.b64data is None: 

97 return None 

98 return vBinary.from_ical(self.b64data) 

99 

100 

101__all__ = ["Image"]