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

36 statements  

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

2 

3from __future__ import annotations 

4 

5import base64 

6 

7from icalendar.prop import vBinary, vText, vUri 

8 

9 

10class Image: 

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

12 

13 Value Type: 

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

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

16 

17 Description: 

18 This property specifies an image for an iCalendar 

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

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

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

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

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

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

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

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

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

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

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

30 on the image in the calendar user agent. 

31 

32 Arguments: 

33 uri: The URI of the image. 

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

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

36 altrep: Link target of the image. 

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

38 

39 """ 

40 

41 @classmethod 

42 def from_property_value(cls, value: vUri | vBinary | vText): 

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

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

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

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

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

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

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

50 elif isinstance(value, vBinary): 

51 params["b64data"] = value.obj 

52 elif value_type == "BINARY": 

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

54 else: 

55 raise TypeError( 

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

57 ) 

58 params.update( 

59 { 

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

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

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

63 } 

64 ) 

65 return cls(**params) 

66 

67 def __init__( 

68 self, 

69 uri: str | None = None, 

70 b64data: str | None = None, 

71 fmttype: str | None = None, 

72 altrep: str | None = None, 

73 display: str | None = None, 

74 ): 

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

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

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

78 if uri is None and b64data is None: 

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

80 self.uri = uri 

81 self.b64data = b64data 

82 self.fmttype = fmttype 

83 self.altrep = altrep 

84 self.display = display 

85 

86 @property 

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

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

89 if self.b64data is None: 

90 return None 

91 return base64.b64decode(self.b64data) 

92 

93 

94__all__ = ["Image"]