我试图从我的应用程序支持的所有URL的代码自动创建文档.我们使用完全客户端MVC,因此支持的每个URL本质上是UI的REST API.有没有一种从这些URL生成文档的简单方法?
我现在写了这个小模块,但我正在寻找更好的方法.如果类似的东西已经存在,我不想重新发明轮子.
更新:请注意,目的是为网站的消费者提供公共文档,而不是内部消费.在这方面,我们需要记录每个URL: - 响应是什么, - 接受哪些参数, - 如果URL响应GET/POST或两者,等等.
某些只是重定向到主页的URL(^ $)不应该被记录,所以我还需要一些排除机制.
from django.core.management.base import BaseCommand
from django.conf import settings
class Command(BaseCommand):
''' Generates documentation for the URLs. For each URL includes the documentation from
the callback implementing the URL and prints the default arguments along with the URL pattern and name'''
def handle(self, *args, **options):
url_module = None
exec 'import '+settings.ROOT_URLCONF+'; url_module='+settings.ROOT_URLCONF
doc = file('doc.html','w')
doc.write("<table border=1 cellspacing=0 cellpadding=5>")
doc.write("<tr><th>URL Pattern<th>Name<th>Documentation<th>Parameters")
for x in url_module.urlpatterns:
doc.write("<tr>")
doc.write("<td>")
doc.write(x._regex)
doc.write("<td>")
doc.write(str(x.name))
try:
documentation = str(x.callback.__doc__)
doc.write("<td><pre>")
doc.write(documentation)
doc.write("</pre>")
except:
doc.write("<td> Unable to find module")
doc.write("<td>")
doc.write(str(x.default_args))
doc.write("</table>")
doc.close()
Run Code Online (Sandbox Code Playgroud)
只是最后更新,以防有人感兴趣。
我们最终将我拥有的代码片段用于内部目的。我们决定冒险进入 Sphinx 并在项目结束时创建开发人员指南时使用它,当我们准备好公开测试版时,因为它需要一些时间和投资用于学习、后勤等。
| 归档时间: |
|
| 查看次数: |
787 次 |
| 最近记录: |