[Zope-dev] Manuel Beta

Benji York benji at zope.com
Wed Jun 24 18:00:07 EDT 2009


On Wed, Jun 24, 2009 at 5:06 PM, Chris Withers<chris at simplistix.co.uk> wrote:
> One thing you may have solved already, but if not would make a fantastic
> addition is a plugin that writes matching sections to a file.
>
> So, rather than having the following in a doctest:
>
>>>> f = open(os.path.join('mytempdir','myfile.zcml'),'w')
>>>> f.write('''
>>>> <configure xmlns="http://namespaces.zope.org/zope">
>>>> blah blah blah
>>>> </configure>
>>>> '''.strip())
>>>> f.close()
>>>> zope.configuration.xmlconfig.file(join('mytempdir','myfile.zxml'))
>
> I could have:
>
> .. file:: myfile.zcml
>
> <configure xmlns="http://namespaces.zope.org/zope">
> blah blah blah
> </configure>

There's no super-nice way to do exactly that, but here's a pretty
reasonable version:

--- begin quote ---

Contents of myfile.zcml::

    <configure xmlns="http://namespaces.zope.org/zope">
    blah blah blah
    </configure>

.. -> config

    >>> import tmpfile
    >>> f = tmpfile.mkstemp()
    >>> f.write(config)
    >>> f.close()
    >>> zope.configuration.xmlconfig.string(config)

--- end quote ---

Or, a much nicer version that doesn't use a temporary file (note the
change from .file to .string) in the code:

--- begin quote ---

Contents of myfile.zcml::

    <configure xmlns="http://namespaces.zope.org/zope">
    blah blah blah
    </configure>

.. -> config

    >>> zope.configuration.xmlconfig.string(config)

--- end quote ---

> ...with bonus points for telling me how to get this to render nicely with
> Sphinx into a box with the file contents in it and a caption of the file
> name :-)

The contents will be in a box, but the file name would be up to you to
describe in prose (as shown).

I could imagine a plug-in that understood the Sphinx
"..  literalinclude:: example.file" directive and made the included
lines available in a variable.  That might be handy when you *really*
want to keep a file on-disk but still include part or all of it in a
document.
-- 
Benji York
Senior Software Engineer
Zope Corporation


More information about the Zope-Dev mailing list