forked from googleapis/gapic-generator-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuri_sample.py
More file actions
78 lines (67 loc) · 2.35 KB
/
uri_sample.py
File metadata and controls
78 lines (67 loc) · 2.35 KB
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
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Any, Generator, Dict, List, Tuple
import re
def _sample_names() -> Generator[str, None, None]:
sample_num: int = 0
while True:
sample_num += 1
yield "sample{}".format(sample_num)
def add_field(obj, path, value):
"""Insert a field into a nested dict and return the (outer) dict.
Keys and sub-dicts are inserted if necessary to create the path.
e.g. if obj, as passed in, is {}, path is "a.b.c", and value is
"hello", obj will be updated to:
{'a':
{'b':
{
'c': 'hello'
}
}
}
Args:
obj: a (possibly) nested dict (parsed json)
path: a segmented field name, e.g. "a.b.c"
where each part is a dict key.
value: the value of the new key.
Returns:
obj, possibly modified
Raises:
AttributeError if the path references a key that is
not a dict.: e.g. path='a.b', obj = {'a':'abc'}
"""
segments = path.split('.')
leaf = segments.pop()
subfield = obj
for segment in segments:
subfield = subfield.setdefault(segment, {})
subfield[leaf] = value
return obj
def sample_from_path_fields(paths: List[Tuple[str, str]]) -> Dict[Any, Any]:
"""Construct a dict for a sample request object from a list of fields
and template patterns.
Args:
paths: a list of tuples, each with a (segmented) name and a pattern.
Returns:
A new nested dict with the templates instantiated.
"""
request: Dict[str, Any] = {}
sample_names = _sample_names()
for path, template in paths:
sample_value = re.sub(
r"(\*\*|\*)",
lambda n: next(sample_names), template if template else '*'
)
add_field(request, path, sample_value)
return request