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

Commit

Permalink
Merge pull request #48 from NotJustPizza/master
Browse files Browse the repository at this point in the history
base_path parameter and relative pathes
  • Loading branch information
atlithorn authored May 12, 2020
2 parents c7e08fa + d686a31 commit 0bb07da
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
# flask-swagger
A Swagger 2.0 spec extractor for Flask

You can now specify base path for yml files:
```
app = Flask(__name__)
@app.route("/spec")
def spec():
base_path = os.path.join(app.root_path, 'docs')
return jsonify(swagger(app), from_file_keyword="swagger_from_file", base_path=base_path)
```
and use relative paths:
```
@app.route('/test', methods=['POST'])
def login():
"""
swagger_from_file: test.yml
"""
```

Install:

```shell
Expand Down
16 changes: 9 additions & 7 deletions flask_swagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
If a swagger yaml description is found in the docstrings for an endpoint
we add the endpoint to swagger specification output
- Added basic_path parameter
"""
import inspect
import yaml
import re
import os

from collections import defaultdict

Expand All @@ -17,7 +19,7 @@ def _sanitize(comment):
return comment.replace('\n', '<br/>') if comment else comment


def _find_from_file(full_doc, from_file_keyword):
def _find_from_file(full_doc, from_file_keyword, base_path):
"""
Finds a line in <full_doc> like
Expand All @@ -33,7 +35,7 @@ def _find_from_file(full_doc, from_file_keyword):
if len(parts) == 2 and parts[0].strip() == from_file_keyword:
path = parts[1].strip()
break

path = path if not path else os.path.join(base_path, path)
return path


Expand All @@ -44,12 +46,12 @@ def _doc_from_file(path):
return doc


def _parse_docstring(obj, process_doc, from_file_keyword):
def _parse_docstring(obj, process_doc, from_file_keyword, base_path):
first_line, other_lines, swag = None, None, None
full_doc = inspect.getdoc(obj)
if full_doc:
if from_file_keyword is not None:
from_file = _find_from_file(full_doc, from_file_keyword)
from_file = _find_from_file(full_doc, from_file_keyword, base_path)
if from_file:
full_doc_from_file = _doc_from_file(from_file)
if full_doc_from_file:
Expand Down Expand Up @@ -122,7 +124,7 @@ def _extract_array_defs(source):


def swagger(app, prefix=None, process_doc=_sanitize,
from_file_keyword=None, template=None):
from_file_keyword=None, template=None, base_path=""):
"""
Call this from an @app.route method like this
@app.route('/spec.json')
Expand Down Expand Up @@ -173,15 +175,15 @@ def spec():
for verb in rule.methods.difference(ignore_verbs):
verb = verb.lower()
if hasattr(endpoint, 'methods') \
and verb in map(lambda m: m.lower(), endpoint.methods) \
and verb in map(lambda m: m.lower(), endpoint.methods) \
and hasattr(endpoint.view_class, verb):
methods[verb] = getattr(endpoint.view_class, verb)
else:
methods[verb] = endpoint
operations = dict()
for verb, method in methods.items():
summary, description, swag = _parse_docstring(method, process_doc,
from_file_keyword)
from_file_keyword, base_path)
if swag is not None: # we only add endpoints with swagger data in the docstrings
defs = swag.get('definitions', [])
defs = _extract_definitions(defs)
Expand Down

0 comments on commit 0bb07da

Please sign in to comment.