Skip to content

Commit 6f6b659

Browse files
author
Arnaud Coomans
committed
Merge pull request #17 from lukeyeager/add-locations
Add location information for each route
2 parents 9248d17 + b1a771e commit 6f6b659

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

flask_autodoc/autodoc.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import re
44
from collections import defaultdict
55
import sys
6+
import inspect
67

78
from flask import current_app, render_template, render_template_string
89
from jinja2 import evalcontextfilter
@@ -25,6 +26,7 @@ class Autodoc(object):
2526
def __init__(self, app=None):
2627
self.app = app
2728
self.func_groups = defaultdict(set)
29+
self.func_locations = defaultdict(dict)
2830
if app is not None:
2931
self.init_app(app)
3032

@@ -66,15 +68,23 @@ def doc(self, groups=None):
6668
or multiple other groups as well, besides the 'all' group.
6769
"""
6870
def decorator(f):
71+
# Set group[s]
6972
if type(groups) is list:
7073
groupset = set(groups)
7174
else:
7275
groupset = set()
7376
if type(groups) is str:
7477
groupset.add(groups)
7578
groupset.add('all')
76-
7779
self.func_groups[f] = groupset
80+
81+
# Set location
82+
caller_frame = inspect.stack()[1]
83+
self.func_locations[f] = {
84+
'filename': caller_frame[1],
85+
'line': caller_frame[2],
86+
}
87+
7888
return f
7989
return decorator
8090

@@ -110,6 +120,7 @@ def generate(self, groups='all', sort=None):
110120
func = current_app.view_functions[rule.endpoint]
111121
arguments = rule.arguments if rule.arguments else ['None']
112122
func_groups = self.func_groups[func]
123+
location = self.func_locations.get(func, None)
113124

114125
if func_groups.intersection(groups_to_generate):
115126
links.append(
@@ -119,7 +130,8 @@ def generate(self, groups='all', sort=None):
119130
endpoint=rule.endpoint,
120131
docstring=func.__doc__,
121132
args=arguments,
122-
defaults=rule.defaults
133+
defaults=rule.defaults,
134+
location=location,
123135
)
124136
)
125137
if sort:

0 commit comments

Comments
 (0)