BRIAN, Paul | 2 Sep 2011 19:19

function or otherwise to convert a ReSt table into python dict

All

(Sorry this got longer than I planned)

I would like to ask a question of the list.  I have been a long time
front end user of docutils (thank you!)
but just realised I have a need for deeper digging.  Gmane and a quick
grep seem not to give me the answer.

I want to use docutils to allow me to store data as nice tabular format,
but be able to parse that string in docutils 
and get out a usable python object (not a docutils object).  For example
I would like to take

input = """
==== ====
test feed
==== ====
A     B
C     D
==== ===="""

and get

[
{'test':'A', 'feed':'B'},
{'test':'C', 'feed':'D'},
]

I have tried the following (I brute forced the optparse.Values.  FOr
bonus points can anyone tell me why 
document = docutils.utils.new_document('') errored?) ::

    #docutils testing

    from docutils.parsers.rst import Parser #this line seems to
kickstart an __init__ file???
    import docutils

    import optparse

    parser = docutils.parsers.rst.Parser()
    input = """
    ==== ====
    test feed
    ==== ====
    A     B
    C     D
    ==== ===="""

    # I am unable to create a new document with just defaults - it seems
to need ,v in the params,
    # and that optparse object needs the below as defaults (new_document
errors on creation unless those below exist

    v = optparse.Values()
    v.ensure_value('tab_width',4)
    v.ensure_value('report_level',1)
    v.ensure_value('halt_level',1)
    v.ensure_value('warning_stream',None)
    v.ensure_value('warning_stream',None)
    v.ensure_value('debug',False)
    v.ensure_value('error_encoding',False)
    v.ensure_value('error_encoding_error_handler',False)
    v.ensure_value('language_code','en')
    v.ensure_value('pep_references',False)
    v.ensure_value('rfc_references',False)
    document = docutils.utils.new_document('', v)
    parser.parse(input, document)

    print
document.children[0].children[0].children[3].children[1].children[0].ast
ext()

    #so it looks like I am writing a wrapper around the parser output,
but i would rather not...

    from docutils import nodes
    class myv(nodes.NodeVisitor):
        def default_visit(self):
            print self.document

    n = myv(document)
    document.walk(n)

    #AttributeError: Values instance has no attribute 'strict_visitor'
    ##AAARRGGHH!!!

1. How do I avoid the optparse force approach - the default just seems
to not work ::

  >>> document = docutils.utils.new_document('')
  >>> parser.parse(input, document)
  Traceback (most recent call last):
    File "<interactive input>", line 1, in <module>
    File
"C:\Python27\lib\site-packages\docutils-0.7-py2.7.egg\docutils\parsers\r
st\__init__.py", line 155, in parse
      inputstring, tab_width=document.settings.tab_width,
  AttributeError: Values instance has no attribute 'tab_width'

2. Anyway, short of rolling my way through the docutils tree (which
might be the thing, but I am not sure where to go)
how do I get out a dict?  Can I do what I am hoping to do, am I walking
through the tree correctly, and has someone already written it?

And if I have to write a tree -> dict, do you accept patches?

Paul Brian
ScrumMaster
07540 456 115
paul.brian <at> bupa.com 
   "Everything can be automated"

Visit www.bupa.com for the story of who we are, where we've come from and what we do.
Bupa House 15-19 Bloomsbury Way London WC1A 2BA 
Internet communications are not secure and therefore Bupa does not accept legal responsibility for the
contents of this message. Any views or opinions presented are solely those of the author and do not
necessarily represent those of Bupa. 
Bupa Insurance Limited, Bupa Insurance Services Limited and Goldsborough Estates Limited are
authorised and regulated by the Financial Services Authority.
For a list of Bupa's main UK trading companies visit www.bupa.co.uk/html/statements/trading_addresses.html

------------------------------------------------------------------------------
Special Offer -- Download ArcSight Logger for FREE!
Finally, a world-class log management solution at an even better 
price-free! And you'll get a free "Love Thy Logs" t-shirt when you
download Logger. Secure your free ArcSight Logger TODAY!
http://p.sf.net/sfu/arcsisghtdev2dev

Gmane