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