This repository was archived by the owner on Mar 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_base_specs.py
62 lines (51 loc) · 1.81 KB
/
update_base_specs.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
import click
BLOCKS = (
'paths',
'schemas',
'parameters',
'responses',
'other',
)
def block_start_name(block_name):
return '### ZC2 builtin {}'.format(block_name)
def block_end_name(block_name):
return '### end ZC2 builtin {}'.format(block_name)
def get_block_location(file, block_name):
""" Returns a (start, end) tuple """
return (
file.index(block_start_name(block_name)),
file.index(block_end_name(block_name))
)
def update_block(file, base_specs, block_name):
""" Uses array slices to splice in the block from ZC2 base specs """
try:
base_specs_block = get_block_location(base_specs, block_name)
except ValueError:
print("{} not found in base specs".format(block_name))
return file
try:
specs_block = get_block_location(file, block_name)
except ValueError:
print("{} not found in specs".format(block_name))
return file
print("Replacing {} block".format(block_name))
return file[:specs_block[0]] + \
base_specs[base_specs_block[0]:base_specs_block[1]] + \
file[specs_block[1]:]
@click.command()
@click.option('--spec', help="The file path of the specs to update")
@click.option('--base', default="./openapi.yaml", help="The base ZC2 specs to use")
def update_specs(spec, base):
""" Updates the parts of the spec which are from ZC2.
This will perform an in-place update of the API specs!
"""
with open(base) as base_specs_fp:
with open(spec) as file_fp:
base_specs = base_specs_fp.read()
file = file_fp.read()
for block_name in BLOCKS:
file = update_block(file, base_specs, block_name)
with open(spec, 'w') as file_fp:
file_fp.write(file)
if __name__ == "__main__":
update_specs()