Coverage for gws-app/gws/lib/xmlx/util.py: 92%
24 statements
« prev ^ index » next coverage.py v7.11.0, created at 2025-10-16 22:59 +0200
« prev ^ index » next coverage.py v7.11.0, created at 2025-10-16 22:59 +0200
1import gws.lib.datetimex as dtx
4def atom_to_string(s) -> tuple[str, bool]:
5 """Return a string representation of a primitive value.
7 Returns:
8 A tuple containing the string representation and a boolean indicating if the conversion was successful.
9 """
11 if s is None:
12 return '', True
14 if isinstance(s, str):
15 return s, True
17 if isinstance(s, (int, float, bool)):
18 return str(s).lower(), True
20 if isinstance(s, dtx.dt.datetime):
21 return dtx.to_iso_string(s, with_tz=':'), True
23 if isinstance(s, dtx.dt.date):
24 return dtx.to_iso_date_string(s), True
26 return '', False
29def escape_text(s: str) -> str:
30 """Escape special characters in a string for XML."""
32 s = s.replace('&', '&')
33 s = s.replace('>', '>')
34 s = s.replace('<', '<')
35 return s
38def escape_attribute(s: str) -> str:
39 """Escape special characters in a string for XML attributes."""
41 s = s.replace('&', '&')
42 s = s.replace('"', '"')
43 s = s.replace('>', '>')
44 s = s.replace('<', '<')
45 return s