Open GoogleCodeExporter opened 9 years ago
I have figured this out. After studying the source code, in particular the
author sections, I noticed that self.in<varname> were set on _start blocks that
had the same child node keys, and then removed in _end blocks. Then you can
test for the existence of those self.in<varname> in the child element
processing and using a simple if/else you can add the values to your context in
the correct place. Here is the code I ended up with. Feedback welcome.
def _start_wp_postmeta(self, attrsD):
context = self._getContext()
self.inpostmeta = 1
context.setdefault('wp_postmeta', [])
context['wp_postmeta'].append(feedparser.FeedParserDict())
def _end_wp_postmeta(self):
self.inpostmeta = 0
def _start_wp_commentmeta(self, attrsD):
context = self._getContext()
self.incommentmeta = 1
context.setdefault('wp_commentmeta', [])
context['wp_commentmeta'].append(feedparser.FeedParserDict())
def _end_wp_commentmeta(self):
self.incommentmeta = 0
def _start_wp_meta_key(self, attrsD):
context = self._getContext()
context.setdefault('wp_meta_key', [])
self.push('wp_meta_key', 1) # new
context['wp_meta_key'].append(attrsD)
def _end_wp_meta_key(self):
wp_meta_key = self.pop('wp_meta_key')
context = self._getContext()
if self.inpostmeta:
context['wp_postmeta'][-1]['wp_meta_key'] = wp_meta_key
elif self.incommentmeta:
context['wp_commentmeta'][-1]['wp_meta_key'] = wp_meta_key
def _start_wp_meta_value(self, attrsD):
context = self._getContext()
context.setdefault('wp_meta_value', [])
self.push('wp_meta_value', 1) # new
context['wp_meta_value'].append(attrsD)
def _end_wp_meta_value(self):
wp_meta_value = self.pop('wp_meta_value')
context = self._getContext()
if self.inpostmeta:
context['wp_postmeta'][-1]['wp_meta_value'] = wp_meta_value
elif self.incommentmeta:
context['wp_commentmeta'][-1]['wp_meta_value'] = wp_meta_value
feedparser._FeedParserMixin._start_wp_postmeta = _start_wp_postmeta
feedparser._FeedParserMixin._end_wp_postmeta = _end_wp_postmeta
feedparser._FeedParserMixin._start_wp_commentmeta = _start_wp_commentmeta
feedparser._FeedParserMixin._end_wp_commentmeta = _end_wp_commentmeta
feedparser._FeedParserMixin._start_wp_meta_key = _start_wp_meta_key
feedparser._FeedParserMixin._end_wp_meta_key = _end_wp_meta_key
feedparser._FeedParserMixin._start_wp_meta_value = _start_wp_meta_value
feedparser._FeedParserMixin._end_wp_meta_value = _end_wp_meta_value
Original comment by robertln...@gmail.com
on 21 Oct 2014 at 4:58
Copy and paste went a bit nutso in the last method. Here it is again:
def _end_wp_meta_value(self):
wp_meta_value = self.pop('wp_meta_value')
context = self._getContext()
if self.inpostmeta:
context['wp_postmeta'][-1]['wp_meta_value'] = wp_meta_value
elif self.incommentmeta:
context['wp_commentmeta'][-1]['wp_meta_value'] = wp_meta_value
Original comment by robertln...@gmail.com
on 21 Oct 2014 at 5:01
Original issue reported on code.google.com by
robertln...@gmail.com
on 20 Oct 2014 at 11:20