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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
|
#------------------------------------------------------------------------------
# Copyright (c) 2013-2025, Nucleic Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file LICENSE, distributed with this software.
#------------------------------------------------------------------------------
import re
from atom.api import Member
#------------------------------------------------------------------------------
# AST Nodes
#------------------------------------------------------------------------------
class Docstring(object):
def __init__(self, body):
self.body = body
def render(self):
lines = []
for item in self.body:
lines.extend(item.render())
return lines
class NormalLine(object):
def __init__(self, text):
self.text = text
def render(self):
return [self.text]
class Parameters(object):
def __init__(self, body):
self.body = body
def render(self):
lines = []
for item in self.body:
lines.extend(item.render())
return lines
class ParameterSpec(object):
def __init__(self, pname, ptype, description):
self.pname = pname
self.ptype = ptype
self.description = description
def render(self):
lines = []
lines.append(':param %s: %s' % (self.pname, self.description))
if self.ptype:
lines.append(':type %s: %s' % (self.pname, self.ptype))
return lines
class Returns(object):
def __init__(self, body):
self.body = body
def render(self):
lines = []
for item in self.body:
lines.extend(item.render())
return lines
class ReturnSpec(object):
def __init__(self, rtype, description):
self.rtype = rtype
self.description = description
def render(self):
lines = []
lines.append(':rtype: %s' % self.rtype)
lines.append(':returns: %s' % self.description)
return lines
#------------------------------------------------------------------------------
# Lexer
#------------------------------------------------------------------------------
dashed_line = 'dashed_line'
empty_line = 'empty_line'
indented_line = 'indented_line'
normal_line = 'normal_line'
parameters_header = 'parameters_header'
parameter_spec = 'parameter_spec'
returns_header = 'returns_header'
star_args = 'star_args'
star_star_kwargs = 'star_star_kwargs'
line_regexes = (
(parameters_header, re.compile(r'^Parameters$')),
(returns_header, re.compile(r'^Returns$')),
(star_args, re.compile(r'^\*([a-zA-Z_][a-zA-Z0-9_]*)$')),
(star_star_kwargs, re.compile(r'^\*\*([a-zA-Z_][a-zA-Z0-9_]*)$')),
(parameter_spec, re.compile(r'^([a-zA-Z_][a-zA-Z0-9_]*)\s*:\s*(.*)$')),
(dashed_line, re.compile(r'^-+$')),
(indented_line, re.compile(r'^(\s+)[^\s].*$')),
(normal_line, re.compile(r'^[^\s].*$')),
(empty_line, re.compile(r'^\s*$')),
)
def lex_line(line):
for token, rgx in line_regexes:
match = rgx.match(line)
if match is not None:
return (token, line, match)
raise ValueError('line "%s" failed to lex' % line)
#------------------------------------------------------------------------------
# Parser
#------------------------------------------------------------------------------
def peek(stack):
if len(stack) == 0:
return ''
return stack[-1][0]
def consume_blank_lines(stack):
while peek(stack) == empty_line:
stack.pop()
def extract_description(stack):
parts = []
while peek(stack) == indented_line:
token, line, match = stack.pop()
parts.append(line.strip())
return ' '.join(parts)
def p_normal(line, match, stack):
return NormalLine(line)
def p_parameters(line, match, stack):
if peek(stack) != dashed_line:
return p_normal(line, match, stack)
stack.pop()
body = []
consume_blank_lines(stack)
while True:
p = peek(stack)
if p == parameter_spec:
token, line, match = stack.pop()
consume_blank_lines(stack)
descr = extract_description(stack)
pname = match.group(1)
ptype = match.group(2)
spec = ParameterSpec(pname, ptype, descr)
body.append(spec)
consume_blank_lines(stack)
elif p == star_args or p == star_star_kwargs:
token, line, match = stack.pop()
consume_blank_lines(stack)
descr = extract_description(stack)
pname = match.group(1)
spec = ParameterSpec(pname, '', descr)
body.append(spec)
consume_blank_lines(stack)
else:
break
return Parameters(body)
def p_returns(line, match, stack):
if peek(stack) != dashed_line:
return p_normal(line, match, stack)
stack.pop()
body = []
consume_blank_lines(stack)
if peek(stack) == parameter_spec:
token, line, match = stack.pop()
consume_blank_lines(stack)
descr = extract_description(stack)
rtype = match.group(2)
spec = ReturnSpec(rtype, descr)
body.append(spec)
consume_blank_lines(stack)
return Returns(body)
DISPATCH_TABLE = {
dashed_line: p_normal,
empty_line: p_normal,
indented_line: p_normal,
normal_line: p_normal,
parameters_header: p_parameters,
parameter_spec: p_normal,
returns_header: p_returns,
star_args: p_normal,
star_star_kwargs: p_normal,
}
def parse(classified):
body = []
stack = classified[::-1]
while stack:
token, line, match = stack.pop()
item = DISPATCH_TABLE[token](line, match, stack)
body.append(item)
return Docstring(body)
#------------------------------------------------------------------------------
# Docstring Processors
#------------------------------------------------------------------------------
def process_function(app, name, obj, options, lines):
classified = map(lex_line, lines)
return parse(classified).render()
def process_attribute(app, name, obj, options, lines):
# if isinstance(obj, Member):
# # indent = ' '
# # name = type(obj).__name__
# # new = [indent + name, u'']
# # for line in lines:
# # new.append(indent + line)
# # lines = new
# pass
return lines
HANDLERS = {
'function': process_function,
'method': process_function,
'attribute': process_attribute,
}
def process_docstring(app, what, name, obj, options, lines):
handler = HANDLERS.get(what)
if handler is not None:
modified_lines = handler(app, name, obj, options, lines[:])
lines[:] = modified_lines
def setup(app):
app.connect('autodoc-process-docstring', process_docstring)
|