pydoc可以生成子目录吗?

发布于 2024-07-29 22:30:25 字数 414 浏览 1 评论 0原文

有没有办法让 pydoc 的 writedocs() 函数为包创建子目录? 例如,假设我有以下模块要记录:

foo.py
dir/bar.py
dir/__init__.py

当我运行 pydoc.writedocs() 时,我得到以下文件:

foo.html
dir.bar.html

我想得到:

foo.html
dir/bar.html

有什么方法可以做到这一点吗?

Is there any way to get pydoc's writedocs() function to create subdirectories for packages? For instance, let's say I have the following modules to document:

foo.py
dir/bar.py
dir/__init__.py

When I run pydoc.writedocs(), I get the following files:

foo.html
dir.bar.html

I would like to get:

foo.html
dir/bar.html

Is there any way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉城已无爱 2024-08-05 22:30:25

pydoc.writedocs 只是循环调用 writedoc,它被记录(并实现)为“在当前目录中写入文件”。 我能看到的唯一出路是制作一个修改版本并强制它(即叹息,monkeypatching)进入模块,或者monkeypatching它的一些关键方面,即“打开”打开以写入它所要求的HTML文件的位置打开。 具体来说,在您的代码中,您可以执行以下操作:

import pydoc

def monkey_open(name, option):
  if option == 'w' and name.endswith('.html'):
    name_pieces = name.split('.')
    name_pieces[-2:] = '.'.join(name_pieces[-2:])
    name = '/'.join(name_pieces)
  return open(name, option)

pydoc.open = monkey_open

不是一个优雅或极其强大的解决方案,但“需要必须”... pydoc 的设计目的不是让您做您想做的事,因此需要“硬塞进去”一点。

pydoc.writedocs just loops calling writedoc, which is documented (and implemented) to "write a file in the current directory". The only way out that I can see is by making a modified version and forcing it (i.e., sigh, monkeypatching it) into the module, or monkeypatching some key aspect of it, namely where 'open' opens for writing the HTML files it's asked to open. Specifically, in your code, you could do something like:

import pydoc

def monkey_open(name, option):
  if option == 'w' and name.endswith('.html'):
    name_pieces = name.split('.')
    name_pieces[-2:] = '.'.join(name_pieces[-2:])
    name = '/'.join(name_pieces)
  return open(name, option)

pydoc.open = monkey_open

Not an elegant or extremely robust solution, but "needs must"... pydoc just isn't designed to allow you to do what you want, so the thing needs to be "shoehorned in" a bit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文