From: David G. <go...@py...> - 2011-09-06 13:47:26
|
On Fri, Sep 2, 2011 at 13:19, BRIAN, Paul <pau...@bu...> wrote: > 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' You're doing it wrong. Don't bother with the little details, they're handled for you automatically if you use the included convenience code. (If you don't, you're on your own. See the included code for how to do it right.) # code begins here import docutils.examples input = u""" ==== ==== test feed ==== ==== A B C D ==== ====""" doc, pub = docutils.examples.internals(input_string=input) print doc.pformat() # code ends Result: <document source="<string>"> <table> <tgroup cols="2"> <colspec colwidth="4"> <colspec colwidth="4"> <thead> <row> <entry> <paragraph> test <entry> <paragraph> feed <tbody> <row> <entry> <paragraph> A <entry> <paragraph> B <row> <entry> <paragraph> C <entry> <paragraph> D > 2. Anyway, short of rolling my way through the docutils tree (which > might be the thing, It is the thing. > but I am not sure where to go) Now traverse the doc. > 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? See docutils.nodes.Node.walk & .walkabout for built-in traversal code. See docutils.nodes.*Visitor classes for base classes, and writers for fleshed-out examples. > And if I have to write a tree -> dict, do you accept patches? Your use case seems very specialized. I doubt if it would be generally useful. If you think it would be useful, please post it to your blog, or to the Python Cookbook (http://code.activestate.com/recipes/langs/python/), etc. -- David Goodger <http://python.net/~goodger> |