Stephen Gross
Stephen Gross

Reputation: 5724

Can pydoc generate subdirectories?

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?

Upvotes: 1

Views: 2248

Answers (1)

Alex Martelli
Alex Martelli

Reputation: 882591

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.

Upvotes: 1

Related Questions