tags:

views:

91

answers:

1

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?

--Steve

A: 

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.

Alex Martelli
Ok, good to know. In this case, I think I will simply not muck about with overriding the module's behavior. But it's good to know!Does python take feature requests for pydoc? I suppose I could just submit the request somehow, eh?
Stephen Gross
Sure, you can submit a feature request to the python tracker, http://bugs.python.org/ (you'll need to register on the site to get an ID and password and log on with them to get more than read-only access to the tracker), better done after checking on the tracker that no such request was already entered.
Alex Martelli