简体   繁体   English

在 Zope 2 中使用 Sphinx 的自动文档(文档字符串问题)

[英]Using Sphinx's autodoc with Zope 2 (docstring issue)

I'd like to use Sphinx to document my Zope 2 Product and it would be nice to also use the autodoc feature, which pulls informations out of docstrings in my modules.我想使用 Sphinx 来记录我的 Zope 2 产品,并且还可以使用 autodoc 功能,该功能从我的模块中的文档字符串中提取信息。 Now in Zope the docstrings are unfortunately used to designate a method as accessible through http-requests.现在,在 Zope 中,不幸的是,文档字符串用于指定可通过 http 请求访问的方法。 So all methods that should not be accessible through http have no docstring.因此,所有不应通过 http 访问的方法都没有文档字符串。

Is there a way to write the docstring in a way that it is recognized by Sphinx but not by Zope?有没有办法以Sphinx识别但Zope不识别的方式编写文档字符串?

Or: Is there a way to change the Zope docstring behaviour?或者:有没有办法改变 Zope 文档字符串的行为?

I can see 3 ways to do it, but none (None :-) is exactly what you want, except on relying on heavy machinery.我可以看到 3 种方法来做到这一点,但没有一种(无 :-) 正是您想要的,除了依靠重型机械。

  1. methods starting with _ aren't published, so it might be ok for you to do it:_开头的方法未发布,因此您可以这样做:

     class MyClass: def _method(self): "I have a docstring, but I won't be published" return 'done'
  2. you can set role to ACCESS_PRIVATE您可以将角色设置为ACCESS_PRIVATE

     from AccessControl.SecurityInfo import ACCESS_PRIVATE class MyClass: myMethod__roles__ = ACCESS_PRIVATE def myMethod(self): "I look like I'm published, but I'm not" return 'done'

    In my opinion, the closest to what you want to achieve, but be aware that the behaviour is different from a missing docstring: if the method is called while it has no docstring, the result is a NotFound exception, while in the ACCESS_PRIVATE case, the result is an Unauthorized exception.在我看来,最接近您想要实现的目标,但请注意,该行为与缺少文档字符串不同:如果在没有文档字符串的情况下调用该方法,则结果是NotFound异常,而在ACCESS_PRIVATE情况下,结果是未经授权的异常。

  3. You can replace the traversal method (I think) by implementing your own IBrowserPublisher .您可以通过实现自己的IBrowserPublisher来替换遍历方法(我认为)。 The default implementation is DefaultPublishTraverse in ZPublisher/BaseRequest.py .默认实现是ZPublisher/BaseRequest.py DefaultPublishTraverse You then have to make your own by copying it and tweak it so that it uses another flag than docstring for publication.然后,您必须通过复制并调整它来制作自己的文件,以便它使用除 docstring 之外的另一个标志进行发布。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM