Skip to content
This repository is currently being migrated. It's locked while the migration is in progress.

Commit e3d1e58

Browse files
committed
Merge pull request #16 from deginner/master
templates and command line interface
2 parents 3b06dc2 + 5c9c2df commit e3d1e58

File tree

4 files changed

+72
-5
lines changed

4 files changed

+72
-5
lines changed

README.md

+21
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,27 @@ def spec():
100100

101101
Swagger-UI is the reason we embarked on this mission to begin with, flask-swagger does not however include Swagger-UI. Simply follow the awesome documentation over at https://github.com/swagger-api/swagger-ui and point your [swaggerUi.url](https://github.com/swagger-api/swagger-ui#swaggerui) to your new flask-swagger endpoint and enjoy.
102102

103+
## flaskswagger Command
104+
This package now comes with a very simple command line interface: flaskswagger. This command can be used to build and update swagger specs for your flask apps from the command line or at build time.
105+
106+
```
107+
flaskswagger -h
108+
usage: flaskswagger [-h] [--template TEMPLATE] [--out-dir OUT_DIR] app
109+
110+
positional arguments:
111+
app the flask app to swaggerify
112+
113+
optional arguments:
114+
-h, --help show this help message and exit
115+
--template TEMPLATE template spec to start with
116+
--out-dir OUT_DIR the directory to output to
117+
```
118+
119+
For example, this can be used to build a swagger spec which can be served from your static directory. In the example below, we use the manually created swagger.json.manual as a template, and output to the `static/` directory.
120+
121+
```
122+
flaskswagger server:app --template static/swagger.json.manual --out-dir static/
123+
```
103124

104125
Acknowledgements
105126

build_swagger_spec.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import argparse
2+
import json
3+
import pkg_resources
4+
from flask_swagger import swagger
5+
6+
parser = argparse.ArgumentParser()
7+
parser.add_argument('app', help='the flask app to swaggerify')
8+
#parser.add_argument('--definitions', help='json definitions file')
9+
parser.add_argument('--template', help='template spec to start with')
10+
parser.add_argument('--out-dir', default=None, help='the directory to output to')
11+
args = parser.parse_args()
12+
13+
def run():
14+
template = args.template
15+
app = pkg_resources.EntryPoint.parse("x=%s" % args.app).load(False)
16+
if template is not None:
17+
with open(template, 'r') as f:
18+
spec = swagger(app, template=json.loads(f.read()))
19+
else:
20+
spec = swagger(app)
21+
if args.out_dir is None:
22+
print json.dumps(spec, indent=4)
23+
else:
24+
with open("%s/swagger.json" % args.out_dir, 'w') as f:
25+
f.write(json.dumps(spec, indent=4))
26+
f.close()
27+
28+
run()
29+

flask_swagger.py

+14-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
from collections import defaultdict
1414

15+
1516
def _sanitize(comment):
1617
return comment.replace('\n', '<br/>') if comment else comment
1718

@@ -87,7 +88,7 @@ def _extract_array_defs(source):
8788
return defs
8889

8990

90-
def swagger(app, process_doc=_sanitize):
91+
def swagger(app, process_doc=_sanitize, template=None):
9192
"""
9293
Call this from an @app.route method like this
9394
@app.route('/spec.json')
@@ -103,8 +104,8 @@ def spec():
103104
104105
Keyword arguments:
105106
process_doc -- text sanitization method, the default simply replaces \n with <br>
107+
template -- The spec to start with and update as flask-swagger finds paths.
106108
"""
107-
108109
output = {
109110
"swagger": "2.0",
110111
"info": {
@@ -114,6 +115,17 @@ def spec():
114115
"paths": defaultdict(dict),
115116
"definitions": defaultdict(dict)
116117
}
118+
# set defaults from template
119+
if template is not None:
120+
for tkey, tval in template.items():
121+
if tkey == 'definitions':
122+
for k, v in tval.items():
123+
output['definitions'][k] = v
124+
if tkey == 'paths':
125+
for k, v in tval.items():
126+
output['paths'][k] = v
127+
else:
128+
output[tkey] = tval
117129

118130
paths = output['paths']
119131
definitions = output['definitions']

setup.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@
77
long_description = file.read()
88

99
setup(name='flask-swagger',
10-
version='0.2.10',
10+
version='0.2.11',
1111
url='https://github.com/gangverk/flask-swagger',
1212
description='Extract swagger specs from your flask project',
1313
author='Atli Thorbjornsson',
1414
license='MIT',
15-
py_modules=['flask_swagger'],
15+
py_modules=['flask_swagger', 'build_swagger_spec'],
1616
long_description=long_description,
17-
install_requires=['Flask>=0.10', 'PyYAML>=3.0'])
17+
install_requires=['Flask>=0.10', 'PyYAML>=3.0'],
18+
entry_points = """
19+
[console_scripts]
20+
flaskswagger = build_swagger_spec:run
21+
"""
22+
)

0 commit comments

Comments
 (0)