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