Không phải là một câu trả lời đầy đủ, nhiều hơn hoặc ít hơn một điểm khởi đầu:
autodoc
dịch chỉ thị ô tô để chỉ thị python. Vì vậy, người ta có thể sử dụng các sự kiện autodoc để có được các chỉ thị python dịch.
Ví dụ, nếu bạn đã sau mymodule.py
:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
This is my module.
"""
def my_test_func(a, b=1):
"""This is my test function"""
return a + b
class MyClass(object):
"""This is my class"""
def __init__(x, y='test'):
"""The init of my class"""
self.x = float(x)
self.y = y
def my_method(self, z):
"""This is my method.
:param z: a number
:type z: float, int
:returns: the sum of self.x and z
:rtype: float
"""
return self.x + z
sphinx-apidoc
sẽ tạo
mymodule Module
===============
.. automodule:: mymodule
:members:
:undoc-members:
:show-inheritance:
Việc gia hạn sau đây (hoặc bổ sung để conf.py
):
NAMES = []
DIRECTIVES = {}
def get_rst(app, what, name, obj, options, signature,
return_annotation):
doc_indent = ' '
directive_indent = ''
if what in ['method', 'attribute']:
doc_indent += ' '
directive_indent += ' '
directive = '%s.. py:%s:: %s' % (directive_indent, what, name)
if signature: # modules, attributes, ... don't have a signature
directive += signature
NAMES.append(name)
rst = directive + '\n\n' + doc_indent + obj.__doc__ + '\n'
DIRECTIVES[name] = rst
def write_new_docs(app, exception):
txt = ['My module documentation']
txt.append('-----------------------\n')
for name in NAMES:
txt.append(DIRECTIVES[name])
print '\n'.join(txt)
with open('../doc_new/generated.rst', 'w') as outfile:
outfile.write('\n'.join(txt))
def setup(app):
app.connect('autodoc-process-signature', get_rst)
app.connect('build-finished', write_new_docs)
sẽ cung cấp cho bạn :
My module documentation
-----------------------
.. py:module:: mymodule
This is my module.
.. py:class:: mymodule.MyClass(x, y='test')
This is my class
.. py:method:: mymodule.MyClass.my_method(z)
This is my method.
:param z: a number
:type z: float, int
:returns: the sum of self.x and z
:rtype: float
.. py:function:: mymodule.my_test_func(a, b=1)
This is my test function
Tuy nhiên, không có sự kiện, khi bản dịch được hoàn thành, Vì vậy, việc xử lý tiếp theo được thực hiện bằng autodoc phải được điều chỉnh phù hợp với tài liệu tại đây.
có gì sai khi sử dụng tệp rst do autodoc tạo ra (vì vậy chỉ các trình tự động chuyển hướng không có định nghĩa miền py đầy đủ) và mở rộng chúng? – bmu
ipaddress đã có tài liệu mở rộng, vì vậy tôi không muốn phải sao chép và dán chúng và định dạng lại chúng bằng tay cho các tài liệu còn lại. – ncoghlan
Vậy tại sao bạn phải sao chép chúng? Bạn có thể viết tài liệu bổ sung của bạn 'giữa' các chỉ thị tự động và để cho nhân sư dịch nó, không cần sao chép. Xin lỗi có lẽ tôi không hiểu bạn (hoặc câu hỏi của bạn). – bmu