forked from aamalev/aiohttp_apiset
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swagger_ui.py
executable file
·76 lines (63 loc) · 2.27 KB
/
swagger_ui.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
#!/usr/bin/env python
""" Installer swagger-ui
Author: Alexander Malev
"""
import urllib.request
import tempfile
import zipfile
import shutil
import os
import sys
VERSION = os.environ.get('SWAGGER_UI_VERSION', '2.2.10')
PACKAGE = os.environ.get('PACKAGE', 'aiohttp_apiset')
URL = 'https://github.com/swagger-api/swagger-ui/archive/v{}.zip'
DIR = os.path.dirname(__file__)
STATIC_DIR = os.path.join(DIR, PACKAGE, 'static', 'swagger-ui')
TEMPLATES_DIR = os.path.join(DIR, PACKAGE, 'templates', 'swagger-ui')
PREFIX = '{{static_prefix}}'
REPLACE_STRINGS = [
('http://petstore.swagger.io/v2/swagger.json', '{{url}}'),
('href="images', 'href="' + PREFIX + 'images'),
('src="images', 'src="' + PREFIX + 'images'),
("href='css", "href='" + PREFIX + 'css'),
("src='lib", "src='" + PREFIX + 'lib'),
("src='swagger-ui.js", "src='" + PREFIX + 'swagger-ui.min.js'),
('href="./', 'href="' + PREFIX),
('src="./', 'src="' + PREFIX),
]
def setup_ui(version=VERSION):
template_dir = os.path.join(TEMPLATES_DIR, version[0])
static_dir = os.path.join(STATIC_DIR, version[0])
template_ui = os.path.join(template_dir, 'index.html')
if os.environ.get('FAKE_UI'):
os.makedirs(static_dir)
os.makedirs(template_dir)
with open(template_ui, 'w'):
return
with tempfile.NamedTemporaryFile() as f:
with urllib.request.urlopen(URL.format(version)) as r:
f.write(r.read())
f.flush()
with tempfile.TemporaryDirectory() as d:
with zipfile.ZipFile(f.name) as z:
mask = 'swagger-ui-{}/dist'.format(version)
for member in z.namelist():
if member.startswith(mask):
z.extract(member, path=d)
shutil.move(os.path.join(d, mask), static_dir)
if not os.path.exists(template_dir):
os.makedirs(template_dir)
with open(os.path.join(static_dir, 'index.html'), 'rt') as source:
s = source.read()
for target, source in REPLACE_STRINGS:
s = s.replace(target, source)
with open(template_ui, 'wt') as f:
f.write(s)
def delete():
shutil.rmtree(TEMPLATES_DIR)
shutil.rmtree(STATIC_DIR)
if __name__ == '__main__':
if 'delete' in sys.argv:
delete()
else:
setup_ui()