1SEQUENCE_TYPES = (list, tuple)
2DEFAULT_ENCODING = "utf-8"
3ICAL_TYPE = str | bytes
4
5
6def from_unicode(value: ICAL_TYPE, encoding="utf-8") -> bytes:
7 """Converts a value to bytes, even if it is already bytes.
8
9 Parameters:
10 value: The value to convert.
11 encoding: The encoding to use in the conversion.
12
13 Returns:
14 The bytes representation of the value.
15 """
16 if isinstance(value, bytes):
17 return value
18 if isinstance(value, str):
19 try:
20 return value.encode(encoding)
21 except UnicodeEncodeError:
22 return value.encode("utf-8", "replace")
23 else:
24 return value
25
26
27def to_unicode(value: ICAL_TYPE, encoding="utf-8-sig") -> str:
28 """Converts a value to Unicode, even if it is already a Unicode string.
29
30 Parameters:
31 value: The value to convert.
32 encoding: The encoding to use in the conversion.
33 """
34 if isinstance(value, str):
35 return value
36 if isinstance(value, bytes):
37 try:
38 return value.decode(encoding)
39 except UnicodeDecodeError:
40 return value.decode("utf-8-sig", "replace")
41 else:
42 return value
43
44
45def data_encode(
46 data: ICAL_TYPE | dict | list, encoding=DEFAULT_ENCODING
47) -> bytes | list[bytes] | dict:
48 """Encode all datastructures to the given encoding.
49
50 Currently Unicode strings, dicts, and lists are supported.
51
52 Parameters:
53 data: The datastructure to encode.
54 """
55 # https://stackoverflow.com/questions/1254454/fastest-way-to-convert-a-dicts-keys-values-from-unicode-to-str
56 if isinstance(data, str):
57 return data.encode(encoding)
58 if isinstance(data, dict):
59 return dict(map(data_encode, iter(data.items())))
60 if isinstance(data, (list, tuple)):
61 return list(map(data_encode, data))
62 return data
63
64
65__all__ = [
66 "DEFAULT_ENCODING",
67 "ICAL_TYPE",
68 "SEQUENCE_TYPES",
69 "data_encode",
70 "from_unicode",
71 "to_unicode",
72]