33import re
44from collections import defaultdict
55import sys
6+ import inspect
67
78from flask import current_app , render_template , render_template_string
89from 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