xpath sifter and category support
This commit is contained in:
parent
0c7666f6f3
commit
f1c3730511
23
filters/xpath_sifter.py
Normal file
23
filters/xpath_sifter.py
Normal file
@ -0,0 +1,23 @@
|
||||
import sys, libxml2
|
||||
|
||||
# parse options
|
||||
options = dict(zip(sys.argv[1::2],sys.argv[2::2]))
|
||||
|
||||
# parse entry
|
||||
doc = libxml2.parseDoc(sys.stdin.read())
|
||||
ctxt = doc.xpathNewContext()
|
||||
ctxt.xpathRegisterNs('atom','http://www.w3.org/2005/Atom')
|
||||
ctxt.xpathRegisterNs('xhtml','http://www.w3.org/1999/xhtml')
|
||||
|
||||
# process requirements
|
||||
if options.has_key('--require'):
|
||||
for xpath in options['--require'].split('\n'):
|
||||
if xpath and not ctxt.xpathEval(xpath): sys.exit(1)
|
||||
|
||||
# process exclusions
|
||||
if options.has_key('--exclude'):
|
||||
for xpath in options['--exclude'].split('\n'):
|
||||
if xpath and ctxt.xpathEval(xpath): sys.exit(1)
|
||||
|
||||
# if we get this far, the feed is to be included
|
||||
print doc
|
@ -110,6 +110,16 @@ def date(xentry, name, parsed):
|
||||
formatted = time.strftime(config.date_format(), parsed)
|
||||
xdate.setAttribute('planet:format', formatted)
|
||||
|
||||
def category(xentry, tag):
|
||||
xtag = xentry.ownerDocument.createElement('category')
|
||||
if tag.has_key('term') and tag.term:
|
||||
xtag.setAttribute('term', tag.get('term'))
|
||||
if tag.has_key('scheme') and tag.scheme:
|
||||
xtag.setAttribute('scheme', tag.get('scheme'))
|
||||
if tag.has_key('label') and tag.label:
|
||||
xtag.setAttribute('label', tag.get('label'))
|
||||
xentry.appendChild(xtag)
|
||||
|
||||
def author(xentry, name, detail):
|
||||
""" insert an author-like element into the entry """
|
||||
if not detail: return
|
||||
@ -160,6 +170,9 @@ def source(xsource, source, bozo):
|
||||
createTextElement(xsource, 'icon', source.get('icon', None))
|
||||
createTextElement(xsource, 'logo', source.get('logo', None))
|
||||
|
||||
for tag in source.get('tags',[]):
|
||||
category(xsource, tag)
|
||||
|
||||
author_detail = source.get('author_detail',{})
|
||||
if not author_detail.has_key('name') and source.has_key('planet_name'):
|
||||
author_detail['name'] = source['planet_name']
|
||||
@ -201,6 +214,9 @@ def reconstitute(feed, entry):
|
||||
date(xentry, 'updated', entry.get('updated_parsed',time.gmtime()))
|
||||
date(xentry, 'published', entry.get('published_parsed',None))
|
||||
|
||||
for tag in entry.get('tags',[]):
|
||||
category(xentry, tag)
|
||||
|
||||
author(xentry, 'author', entry.get('author_detail',None))
|
||||
for contributor in entry.get('contributors',[]):
|
||||
author(xentry, 'contributor', contributor)
|
||||
|
3
tests/data/filter/category-one.xml
Normal file
3
tests/data/filter/category-one.xml
Normal file
@ -0,0 +1,3 @@
|
||||
<entry xmlns="http://www.w3.org/2005/Atom">
|
||||
<category term="one"/>
|
||||
</entry>
|
3
tests/data/filter/category-two.xml
Normal file
3
tests/data/filter/category-two.xml
Normal file
@ -0,0 +1,3 @@
|
||||
<entry xmlns="http://www.w3.org/2005/Atom">
|
||||
<category term="two"/>
|
||||
</entry>
|
@ -1,4 +1,4 @@
|
||||
<entry xmlns="http://www.w3.org/2005/xhtml">
|
||||
<entry xmlns="http://www.w3.org/2005/Atom">
|
||||
<content>
|
||||
<div xmlns="http://www.w3.org/1999/xhtml">
|
||||
<img src="http://example.com/foo.png"/>
|
||||
|
6
tests/data/filter/xpath-sifter.ini
Normal file
6
tests/data/filter/xpath-sifter.ini
Normal file
@ -0,0 +1,6 @@
|
||||
[Planet]
|
||||
filters = xpath_sifter.py
|
||||
|
||||
[xpath_sifter.py]
|
||||
require:
|
||||
//atom:category[@term='two']
|
11
tests/data/reconstitute/category_label.xml
Normal file
11
tests/data/reconstitute/category_label.xml
Normal file
@ -0,0 +1,11 @@
|
||||
<!--
|
||||
Description: category label
|
||||
Expect: tags[0].label == 'Inbox'
|
||||
-->
|
||||
|
||||
<feed xmlns="http://www.w3.org/2005/Atom">
|
||||
<entry>
|
||||
<category label="Inbox"/>
|
||||
</entry>
|
||||
</feed>
|
||||
|
11
tests/data/reconstitute/category_scheme.xml
Normal file
11
tests/data/reconstitute/category_scheme.xml
Normal file
@ -0,0 +1,11 @@
|
||||
<!--
|
||||
Description: category scheme
|
||||
Expect: tags[0].scheme == 'http://example.com/categories'
|
||||
-->
|
||||
|
||||
<feed xmlns="http://www.w3.org/2005/Atom">
|
||||
<entry>
|
||||
<category scheme="http://example.com/categories"/>
|
||||
</entry>
|
||||
</feed>
|
||||
|
11
tests/data/reconstitute/category_term.xml
Normal file
11
tests/data/reconstitute/category_term.xml
Normal file
@ -0,0 +1,11 @@
|
||||
<!--
|
||||
Description: category term
|
||||
Expect: tags[0].term == 'inbox'
|
||||
-->
|
||||
|
||||
<feed xmlns="http://www.w3.org/2005/Atom">
|
||||
<entry>
|
||||
<category term="inbox"/>
|
||||
</entry>
|
||||
</feed>
|
||||
|
14
tests/data/reconstitute/source_category.xml
Normal file
14
tests/data/reconstitute/source_category.xml
Normal file
@ -0,0 +1,14 @@
|
||||
<!--
|
||||
Description: source category
|
||||
Expect: source.tags[0].term == 'inbox' and source.tags[0].scheme == 'http://example.com/categories' and source.tags[0].label == 'Inbox'
|
||||
-->
|
||||
|
||||
<feed xmlns="http://www.w3.org/2005/Atom">
|
||||
<entry>
|
||||
<source>
|
||||
<category term='inbox' label='Inbox'
|
||||
scheme="http://example.com/categories"/>
|
||||
</source>
|
||||
</entry>
|
||||
</feed>
|
||||
|
@ -58,6 +58,24 @@ class FilterTests(unittest.TestCase):
|
||||
self.assertEqual(u'before--after',
|
||||
excerpt.firstChild.firstChild.nodeValue)
|
||||
|
||||
def test_xpath_filter(self):
|
||||
config.load('tests/data/filter/xpath-sifter.ini')
|
||||
testfile = 'tests/data/filter/category-one.xml'
|
||||
|
||||
output = open(testfile).read()
|
||||
for filter in config.filters():
|
||||
output = shell.run(filter, output, mode="filter")
|
||||
|
||||
self.assertEqual('', output)
|
||||
|
||||
testfile = 'tests/data/filter/category-two.xml'
|
||||
|
||||
output = open(testfile).read()
|
||||
for filter in config.filters():
|
||||
output = shell.run(filter, output, mode="filter")
|
||||
|
||||
self.assertNotEqual('', output)
|
||||
|
||||
try:
|
||||
from subprocess import Popen, PIPE
|
||||
sed=Popen(['sed','--version'],stdout=PIPE,stderr=PIPE)
|
||||
|
Loading…
Reference in New Issue
Block a user