2010/8/17 Matt Williams <li...@milliams.com>:
> While parts of this can obviously be done with plain Python, I was
> wondering how it should be done within the framework of Sphinx. This
> is particularly needed in order to re-evaluate the roles when the file
> has changed.

Just attach your cache to the environment object "app.env":

def init_doxylink(app):
    tag_file_mtime = os.path.getmtime(tagfile)
    if not hasattr(app.env, 'doxylink_cache'):
        # no cache present, initialize it
        app.env.doxylink_cache = DoxyLinkCache(tag_file_mtime)
    elif tag_file_mtime > app.env.doxylink_cache.tag_file_mtime:
        # tag file has been modified since cache creation
        app.env.doxylink_cache = DoxyLinkCache(tag_file_mtime)

def setup(app):
    app.connect('builder-inited', init_doxylink)

Sphinx pickles the whole environment object, so you get caching for
free for any object attached to the environment.  You just need to
create the cache, in case sphinx has a fresh environment (e.g. built
after configuration changes, "make clean" or so), and to verify, if
the cache is up to date.  This is done in "init_doxylink()", which is
run, when the builder is initialized.  This is what
sphinx.ext.interspinx does.

Of course, all access to the tag files must be done through the cache.
 How the cache could be implemented, depends on the way your extension
works.  collections.defaultdict generally makes a nice base class for
caches.  You could for instance use signatures as keys, normalize them
internally, and re-implement __missing__ to look the signature up in
the cache file:

class DoxyLinkCache(collections.defaultdict):
    def __getitem__(self, signature):
        return collections.defaultdict.__getitem__(self, normalize(signature))

    def __missing__(self, signature):
        # look up normalized signature in the tag file

HTH
Sebastian Wiesner

-- 
You received this message because you are subscribed to the Google Groups 
"sphinx-dev" group.
To post to this group, send email to sphinx-...@googlegroups.com.
To unsubscribe from this group, send email to 
sphinx-dev+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sphinx-dev?hl=en.

Reply via email to