Skip to content

Commit

Permalink
Merge pull request #6 from dave-shawley/more-fun
Browse files Browse the repository at this point in the history
Fix parameter processing.
  • Loading branch information
dave-shawley committed Jun 30, 2017
2 parents f3f1354 + e2a03d0 commit a7090f8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
2 changes: 1 addition & 1 deletion requires/installation.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Sphinx>=1.4,<2
sphinxcontrib-httpdomain==1.5.0
sphinxcontrib-httpdomain>=1.5.0
5 changes: 5 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,9 @@ def read_requirements(name):
'Programming Language :: Python :: 3.4',
'Framework :: Sphinx :: Extension',
],
entry_points={
'distutils.commands': [
'swagger = sphinxswagger.command:BuildSwagger',
],
},
)
29 changes: 26 additions & 3 deletions sphinxswagger/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,24 @@
URI_TEMPLATE_RE = re.compile(r'\(\?P<([^>]*)>.*\)')


def _find_param_separator(tokens):
"""
Return the index of the param separator.
:param list tokens: list of tokens on the parameter line
:returns: integer index of the separator or :data:`None` if no
separator is found
:rtype: int
Different versions of sphinxcontrib-httpdomain/autotornado use different
renderings for HTTP parameters. For example ``name (type) -- text``
where the ``--`` might be a single hyphen or the unicode em-dash...
"""
idx = [i for i, v in enumerate(tokens) if v in ('\u2013', '--', '-')]
return idx[0] if idx else None


def write_swagger_file(app, exception):
"""
:param sphinx.application.Sphinx app:
Expand Down Expand Up @@ -179,11 +197,16 @@ def visit_field(self, node):
{'in': 'path', 'required': True})
value_node.walkabout(visitor)
self.endpoint.parameters.extend(visitor.parameters)
elif name == 'Query Parameters':
visitor = ParameterVisitor(self.document, {'in': 'query'})
value_node.walkabout(visitor)
self.endpoint.parameters.extend(visitor.parameters)
elif name == 'Request JSON Object':
visitor = ParameterVisitor(self.document)
value_node.walkabout(visitor)
self.endpoint.parameters.append({
'name': 'request-body', 'in': 'body', 'required': True,
'description': 'A serialized request body',
'schema': visitor.get_schema()})
elif name == 'Request JSON Array of Objects':
visitor = ParameterVisitor(self.document)
Expand Down Expand Up @@ -234,14 +257,14 @@ def visit_list_item(self, node):
'float': 'number',
'object': 'object',
'dict': 'object',
'bool': 'boolean',
}

visitor = ParagraphVisitor(self.document)
node[0].walkabout(visitor)
tokens = visitor.get_paragraph().split()

# name (type) -- description
idx = tokens.index('--')
idx = _find_param_separator(tokens)
try:
s, e = tokens.index('(', 0, idx), tokens.index(')', 0, idx)
name = ' '.join(tokens[:s])
Expand Down Expand Up @@ -282,7 +305,7 @@ def visit_list_item(self, node):
tokens[1] = '[' + tokens[1]
else:
code = tokens[0]
idx = tokens.index('--')
idx = _find_param_separator(tokens)
reason = ' '.join(tokens[1:idx])
description = ' '.join(tokens[idx+1:])
self.status_info[code] = {'reason': reason, 'description': description}
Expand Down

0 comments on commit a7090f8

Please sign in to comment.