Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/icalendar/prop/uri.py: 79%
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
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
1"""URI values from :rfc:`5545`."""
3from typing import Any, ClassVar
4from xml.etree.ElementTree import Element, SubElement
6from icalendar.compatibility import Self
7from icalendar.error import JCalParsingError
8from icalendar.parser import Parameters
9from icalendar.parser.xcal.base import XCalParser
10from icalendar.parser.xcal.string import _to_valid_xml_string
11from icalendar.parser.xcal.wrapper import from_xcal_wrapper
12from icalendar.parser_tools import DEFAULT_ENCODING, to_unicode
15class vUri(str):
16 """URI
18 Value Name:
19 URI
21 Purpose:
22 This value type is used to identify values that contain a
23 uniform resource identifier (URI) type of reference to the
24 property value.
26 Format Definition:
27 This value type is defined by the following notation:
29 .. code-block:: text
31 uri = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
33 Description:
34 This value type might be used to reference binary
35 information, for values that are large, or otherwise undesirable
36 to include directly in the iCalendar object.
38 Property values with this value type MUST follow the generic URI
39 syntax defined in [RFC3986].
41 When a property parameter value is a URI value type, the URI MUST
42 be specified as a quoted-string value.
44 Examples:
45 The following is a URI for a network file:
47 .. code-block:: ics
49 http://example.com/my-report.txt
51 .. code-block:: pycon
53 >>> from icalendar.prop import vUri
54 >>> uri = vUri.from_ical('http://example.com/my-report.txt')
55 >>> uri
56 vUri('http://example.com/my-report.txt')
57 >>> uri.uri
58 'http://example.com/my-report.txt'
59 """
61 default_value: ClassVar[str] = "URI"
62 params: Parameters
63 __slots__ = ("params",)
65 def __new__(
66 cls,
67 value: str,
68 encoding: str = DEFAULT_ENCODING,
69 /,
70 params: dict[str, Any] | None = None,
71 ) -> Self:
72 value = to_unicode(value, encoding=encoding)
73 if "\r" in value or "\n" in value:
74 raise ValueError(
75 f"A URI value may not contain CR or LF characters: {value!r}"
76 )
77 self = super().__new__(cls, value)
78 self.params = Parameters(params)
79 return self
81 def to_ical(self) -> bytes:
82 return self.encode(DEFAULT_ENCODING)
84 @classmethod
85 def from_ical(cls, ical: str | bytes) -> Self:
86 try:
87 return cls(ical)
88 except Exception as e:
89 raise ValueError(f"Expected , got: {ical}") from e
91 @classmethod
92 def examples(cls) -> list[Self]:
93 """Examples of vUri."""
94 return [cls("http://example.com/my-report.txt")]
96 def to_jcal(self, name: str) -> list:
97 """The jCal representation of this property according to :rfc:`7265`."""
98 return [name, self.params.to_jcal(), self.VALUE.lower(), str(self)]
100 @classmethod
101 def from_jcal(cls, jcal_property: list) -> Self:
102 """Parse jCal from :rfc:`7265`.
104 Parameters:
105 jcal_property: The jCal property to parse.
107 Raises:
108 ~error.JCalParsingError: If the provided jCal is invalid.
109 """
110 JCalParsingError.validate_property(jcal_property, cls)
111 return cls(
112 jcal_property[3],
113 params=Parameters.from_jcal_property(jcal_property),
114 )
116 @property
117 def ical_value(self) -> str:
118 """The URI."""
119 return self.uri
121 @property
122 def uri(self) -> str:
123 """The URI."""
124 return str(self)
126 def __repr__(self) -> str:
127 """repr(self)"""
128 return f"{self.__class__.__name__}({self.uri!r})"
130 from icalendar.param import FMTTYPE, GAP, LABEL, LANGUAGE, LINKREL, RELTYPE, VALUE
132 def to_xcal(self, element: Element) -> None:
133 """Add the xCal representation of this property according to :rfc:`6321`."""
134 self.params.to_xcal(element)
135 element = SubElement(element, self.default_value.lower())
136 element.text = _to_valid_xml_string(self)
138 @classmethod
139 @from_xcal_wrapper
140 def from_xcal(cls, parser: XCalParser, params: Parameters) -> Self:
141 """Parse xCal from :rfc:`6321`.
143 Parameters:
144 xml: The XML to parse or a parser.
146 Raises:
147 ~error.XCalParsingError: If the provided xCal is invalid.
148 """
149 element = parser.parse_tag(cls.default_value)
150 return cls(element.get_xsd_string(), params=params)
153__all__ = ["vUri"]