-
Notifications
You must be signed in to change notification settings - Fork 0
/
replacements.py
355 lines (275 loc) · 8.63 KB
/
replacements.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import base64
import re
import uuid
from typing import List, Callable, Dict, TypedDict, Any
class FunctionArguments(TypedDict, total=False):
args: List[Any]
kwargs: Dict[str, Any]
class Assignment(FunctionArguments, total=True):
name: str
type: str
replace_pattern = re.compile(r"\${[A-z_][A-z0-9_]*}")
data_retrievers: Dict[str, Callable[..., str]] = {}
def retriever(func: Callable[..., str]):
name = func.__name__
assert name.startswith("retriever_")
name = name[len("retriever_"):]
assert name not in data_retrievers, "no duplicate retriever names!"
data_retrievers[name] = func
return func
@retriever
def retriever_identity(s: str) -> str:
return s
@retriever
def retriever_base64uuid4() -> str:
return base64.urlsafe_b64encode(uuid.uuid4().bytes).strip(b"=").decode()
@retriever
def retriever_localfile(*args, **kwargs):
with open(*args, mode='r', **kwargs) as f:
return f.read()
@retriever
def retriever_fsspec(*args, **kwargs):
import fsspec
with fsspec.open(*args, **kwargs, mode='r').open() as f:
return f.read()
@retriever
def retriever_awssecret(region_name, secret_id):
import boto3
return (
boto3.client("secretsmanager", region_name=region_name)
.get_secret_value(SecretId=secret_id)
['SecretString']
)
@retriever
def retriever_env(name):
import os
res = os.getenv(name)
assert res is not None
return res
class Replacer:
def __init__(
self,
assignments: List[Assignment],
allow_missing: bool=False
) -> None:
"""
An object for replacing strings with other strings in json-like objects
Assignments is a list of dictionaries, each containing a `name`, a
`type`, and optionally `args`, `kwargs` and `default`.
If `allow_missing` is `False` (default), and a string to be replaced
has a "${name}" with no assignment to "name", a KeyError exception will
be thrown.
There are currently 6 implemented types:
- identity: returns the argument passed to it.
- localfile: passes the `args` and `kwargs` to `open` and then reads
the file object. The mode is always 'r'.
- fsspec: passes the `args` and `kwargs` to `fsspec.open`, opens
that, and reads. The mode is always 'r'. Requires fsspec to be
installed. fsspec has multiple protocols installed, e.g.
http(s), (s)ftp and zip. This can also be used for data on S3,
if s3fs is installed.
- awssecret: takes two arguments: `region_name` and `secret_id`.
Uses boto to call secretsmanager, and returns the returned
`SecretString`.
- env: takes a single `name` argument, and looks for this
environment variable. If the environment variable doesn't exist
and no default value was passed, an AssertionError will be
raised.
- base64uuid4; the base64 (the url safe "-_" variant) of a
`uuid.uuid4` call. Use this to create a unique id that can be
used in multiple derived replacements.
Example:
>>> Replacer([{
... "name": "name",
... "type": "identity",
... "args": ["World"]
... }])("Hello, ${name}!")
Hello, World!
There can be dependencies between the assignments. They are resolved
linearly using the list order:
>>> Replacer([
... {
... "name": "name",
... "type": "identity",
... "args": ["World"]
... },
... {
... "name": "greeting",
... "type": "identity",
... "args": ["Hello, ${name}!"]
... }
... ])("${greeting}")
Hello, World!
"""
self.variables: Dict[str, str] = {}
self.allow_missing = allow_missing
for assignment in assignments:
assignment = self.replace(assignment)
variable = assignment['name']
data_retriever = data_retrievers[assignment['type']]
try:
self.variables[variable] = data_retriever(
*assignment.get('args', []),
**assignment.get('kwargs', {})
)
except KeyboardInterrupt:
raise
except Exception as e:
if "default" in assignment:
self.variables[variable] = assignment["default"]
else:
raise
def replace(self, s):
if isinstance(s, dict):
return {self.replace(k): self.replace(v) for k, v in s.items()}
elif isinstance(s, (list, tuple)):
return [self.replace(v) for v in s]
elif not isinstance(s, str):
return s
names = set(n[2:-1] for n in re.findall(replace_pattern, s))
for name in names:
if self.allow_missing and name not in self.variables:
continue
s = s.replace(f"${{{name}}}", self.variables[name])
return s
__call__ = replace
tests: List[Callable[[], None]] = []
def test(func: Callable[[], None]) -> Callable[[], None]:
tests.append(func)
return func
def runtests():
for test in tests:
test()
@test
def test_empty():
assert Replacer([])("hello") == "hello"
@test
def test_allow_missing():
try:
Replacer([])("${hello}")
except KeyError:
pass
else:
raise Exception("allow_missing incorrect behavior")
assert Replacer([], allow_missing=True)("${hello}") == "${hello}"
@test
def test_identity_args():
replacer = Replacer([{
"name": "hello",
"type": "identity",
"args": ["world"]
}])
assert replacer("hello") == "hello"
assert replacer("${hello}") == "world"
@test
def test_identity_kwargs():
replacer = Replacer([{
"name": "hello",
"type": "identity",
"kwargs": {"s": "world"}
}])
assert replacer("hello") == "hello"
assert replacer("${hello}") == "world"
@test
def test_dependency():
replacer = Replacer([
{
"name": "name",
"type": "identity",
"args": ["World"]
},
{
"name": "greeting",
"type": "identity",
"args": ["Hello, ${name}!"]
}
])
assert replacer("${greeting}") == "Hello, World!"
@test
def test_localfile():
import tempfile
with tempfile.NamedTemporaryFile('w') as f:
f.write("Hello, World!")
f.flush()
replacer = Replacer([
{
"name": "a",
"type": "localfile",
"args": [f.name]
}
])
assert replacer("${a}") == "Hello, World!"
@test
def test_fsspec():
import tempfile
with tempfile.NamedTemporaryFile('w') as f:
f.write("Hello, World!")
f.flush()
replacer = Replacer([
{
"name": "a",
"type": "fsspec",
"args": [f.name]
}
])
assert replacer("${a}") == "Hello, World!"
@test
def test_fsspec_google():
replacer = Replacer([
{
"name": "a",
"type": "fsspec",
"args": ["https://www.google.com"]
}
])
assert "google" in replacer("${a}")
@test
def test_default():
replacer = Replacer([{
"name": "name",
"type": "localfile",
"args": "/best/path/ever/yo",
"default": "World"
}])
assert replacer("Hello, ${name}!") == "Hello, World!"
@test
def test_base64uuid4():
replacer = Replacer([{
"name": "myuuid",
"type": "base64uuid4"
}])
assert len(replacer("${myuuid}")) == 22
@test
def test_env():
import os
os.environ["shimi"] = "Hello"
replacer = Replacer([
{
"name": "greeting",
"type": "env",
"args": ["shimi"]
},
{
"name": "name",
"type": "env",
"args": ["noshimi"],
"default": "World"
}
])
assert replacer("${greeting}, ${name}!") == "Hello, World!"
@test
def test_no_env():
try:
Replacer([
{
"name": "a",
"type": "env",
"args": ["noshimi"]
}
])
except AssertionError:
pass
else:
raise Exception("missing env didn't raise")
if __name__ == "__main__":
runtests()