Debian Codegraph @

src/globus_sdk/_internal/remarshal.py

raw
1 """
2 This module provides internal helpers for remarshalling data from one shape
3 into another.
4
5 This may be as simple as converting strings from one form to another, or as
6 sophisticated as building a specific internal object in a configurable way.
7 """
8
9 from __future__ import annotations
10
11 import collections.abc
12 import sys
13 import typing as t
14 import uuid
15
16 from globus_sdk._missing import MISSING, MissingType
17
18 if sys.version_info >= (3, 10):
19 from typing import TypeAlias
20 else:
21 from typing_extensions import TypeAlias
22
23 T = t.TypeVar("T")
24 R = t.TypeVar("R")
25
26 # Omittable[T] means "T may be missing"
27 # NullableOmittable[T] means "T may be missing or null"
28 #
29 # in type systems this kind of construction is sometimes called "Optional" or "Maybe"
30 # but Python uses "Optional" to mean "T | None" and in the SDK, "None" means "null"
31 Omittable: TypeAlias[T] = t.Union[T, MissingType]
32 NullableOmittable: TypeAlias[T] = t.Union[Omittable[T], None]
33
34
35 def stringify(value: NullableOmittable[object]) -> NullableOmittable[str]:
36 """
37 Convert a value to a string, with handling for None and Missing.
38
39 :param value: The stringifiable object
40 """
41 if value is None:
42 return None
43 if value is MISSING:
44 return MISSING
45 return str(value)
46
47
48 @t.overload
49 def listify(value: None) -> None: ...
50 @t.overload
51 def listify(value: MissingType) -> MissingType: ...
52 @t.overload
53 def listify(value: t.Iterable[T]) -> list[T]: ...
54
55
56 def listify(value: NullableOmittable[t.Iterable[T]]) -> NullableOmittable[list[T]]:
57 """
58 Convert any iterable to a list, with handling for None and Missing.
59
60 :param value: The iterable of objects
61 """
62 if value is None:
63 return None
64 if value is MISSING:
65 return MISSING
66 if isinstance(value, list):
67 return value
68 return list(value)
69
70
71 def strseq_iter(
72 value: t.Iterable[str | uuid.UUID] | str | uuid.UUID,
73 ) -> t.Iterator[str]:
74 """
75 Iterate over one or more string/string-convertible values.
76
77 :param value: The stringifiable object or objects to iterate over
78
79 This function handles strings, which are themselves iterable,
80 by producing the string itself, not it's characters:
81
82 >>> list("foo")
83 ['f', 'o', 'o']
84 >>> list(strseq_iter("foo"))
85 ['foo']
86
87 It also accepts and converts UUIDs and iterables thereof:
88
89 >>> list(strseq_iter(UUID(int=0)))
90 ['00000000-0000-0000-0000-000000000000']
91 >>> list(strseq_iter([UUID(int=0), UUID(int=1)]))
92 ['00000000-0000-0000-0000-000000000000', '00000000-0000-0000-0000-000000000001']
93
94 This helps handle cases where a string is passed to a function expecting an iterable
95 of strings, as well as cases where an iterable of UUID objects is accepted for a
96 list of IDs, or something similar.
97 """
98 if isinstance(value, str):
99 yield value
100 elif isinstance(value, uuid.UUID):
101 yield str(value)
102 else:
103 for x in value:
104 yield str(x)
105
106
107 @t.overload
108 def strseq_listify(value: None) -> None: ...
109 @t.overload
110 def strseq_listify(value: MissingType) -> MissingType: ...
111
112
113 @t.overload
114 def strseq_listify(
115 value: t.Iterable[str | uuid.UUID] | str | uuid.UUID,
116 ) -> list[str]: ...
117
118
119 def strseq_listify(
120 value: NullableOmittable[t.Iterable[str | uuid.UUID] | str | uuid.UUID],
121 ) -> NullableOmittable[list[str]]:
122 """
123 A wrapper over strseq_iter which produces list outputs.
124 This method takes responsibility for checking for MISSING and None values.
125
126 Unlike strseq_iter, this may be the "last mile" remarshalling step before
127 data is actually passed to the network layer. Therefore, it makes sense for this
128 helper to handle (MISSING | None).
129
130 :param value: The stringifiable object or iterable of objects
131 """
132 if value is None:
133 return None
134 if value is MISSING:
135 return MISSING
136 return list(strseq_iter(value))
137
138
139 @t.overload
140 def list_map(value: None, mapped_function: t.Callable[[T], R]) -> None: ...
141
142
143 @t.overload
144 def list_map(
145 value: MissingType, mapped_function: t.Callable[[T], R]
146 ) -> MissingType: ...
147
148
149 @t.overload
150 def list_map(value: t.Iterable[T], mapped_function: t.Callable[[T], R]) -> list[R]: ...
151
152
153 def list_map(
154 value: NullableOmittable[t.Iterable[T]], mapped_function: t.Callable[[T], R]
155 ) -> NullableOmittable[list[R]]:
156 """
157 Like list(map()) but handles None|MISSING.
158
159 :param value: The iterable of objects over which to map
160 :param mapped_function: The function to map
161 """
162 if value is None:
163 return None
164 if value is MISSING:
165 return MISSING
166 return [mapped_function(element) for element in value]
167
168
169 @t.overload
170 def commajoin(value: MissingType) -> MissingType: ...
171 @t.overload
172 def commajoin(value: None) -> None: ...
173 @t.overload
174 def commajoin(value: str | uuid.UUID | t.Iterable[str | uuid.UUID]) -> str: ...
175
176
177 def commajoin(
178 value: NullableOmittable[str | uuid.UUID | t.Iterable[str | uuid.UUID]],
179 ) -> NullableOmittable[str]:
180 if value is None:
181 return None
182 if value is MISSING:
183 return MISSING
184 # note that this explicit handling of Iterable allows for objects to be
185 # passed to this function and be stringified by the `str()` call
186 if isinstance(value, collections.abc.Iterable):
187 return ",".join(strseq_iter(value))
188 return str(value)