Allow filter parameters to be passed "URI style"

This commit is contained in:
Sam Ruby 2006-10-22 17:48:37 -04:00
parent 9f69ac1c30
commit 0a5015d657
5 changed files with 40 additions and 6 deletions

View File

@ -32,12 +32,19 @@ types of advertisements that you may find in feeds.</p>
the form of a <code>planet:excerpt</code> element) to the feed itself. You the form of a <code>planet:excerpt</code> element) to the feed itself. You
can see examples of how parameters are passed to this program in either can see examples of how parameters are passed to this program in either
<a href="../tests/data/filter/excerpt-images.ini">excerpt-images</a> or <a href="../tests/data/filter/excerpt-images.ini">excerpt-images</a> or
<a href="../examples/opml-top100.ini">opml-top100.ini</a>.</p> <a href="../examples/opml-top100.ini">opml-top100.ini</a>.
Alternately parameters may be passed
<abbr title="Uniform Resource Identifier">URI</abbr> style, for example:
<a href="../tests/data/filter/excerpt-images2.ini">excerpt-images2</a>.
</p>
<p>The <a href="../filters/xpath_sifter.py">xpath sifter</a> is a variation of <p>The <a href="../filters/xpath_sifter.py">xpath sifter</a> is a variation of
the above, including or excluding feeds based on the presence (or absence) of the above, including or excluding feeds based on the presence (or absence) of
data specified by <a href="http://www.w3.org/TR/xpath20/">xpath</a> data specified by <a href="http://www.w3.org/TR/xpath20/">xpath</a>
expressions.</p> expressions. Again, parameters can be passed as
<a href="../tests/data/filter/xpath-sifter.ini">config options</a> or
<a href="../tests/data/filter/xpath-sifter2.ini">URI style</a>.
</p>
<h3>Notes</h3> <h3>Notes</h3>

View File

@ -13,6 +13,14 @@ def run(template_file, doc, mode='template'):
else: else:
dirs = planet.config.filter_directories() dirs = planet.config.filter_directories()
# parse out "extra" options
if template_file.find('?') < 0:
extra_options = {}
else:
import cgi
template_file, extra_options = template_file.split('?',1)
extra_options = dict(cgi.parse_qsl(extra_options))
# see if the template can be located # see if the template can be located
for template_dir in dirs: for template_dir in dirs:
template_resolved = os.path.join(template_dir, template_file) template_resolved = os.path.join(template_dir, template_file)
@ -43,6 +51,7 @@ def run(template_file, doc, mode='template'):
# Execute the shell module # Execute the shell module
options = planet.config.template_options(template_file) options = planet.config.template_options(template_file)
options.update(extra_options)
log.debug("Processing %s %s using %s", mode, log.debug("Processing %s %s using %s", mode,
os.path.realpath(template_resolved), module_name) os.path.realpath(template_resolved), module_name)
if mode == 'filter': if mode == 'filter':

View File

@ -0,0 +1,2 @@
[Planet]
filters = excerpt.py?omit=img

View File

@ -0,0 +1,2 @@
[Planet]
filters = xpath_sifter.py?require=//atom%3Acategory%5B%40term%3D%27two%27%5D

View File

@ -14,10 +14,16 @@ class FilterTests(unittest.TestCase):
imgsrc = dom.getElementsByTagName('img')[0].getAttribute('src') imgsrc = dom.getElementsByTagName('img')[0].getAttribute('src')
self.assertEqual('http://example.com.nyud.net:8080/foo.png', imgsrc) self.assertEqual('http://example.com.nyud.net:8080/foo.png', imgsrc)
def test_excerpt_images(self): def test_excerpt_images1(self):
testfile = 'tests/data/filter/excerpt-images.xml'
config.load('tests/data/filter/excerpt-images.ini') config.load('tests/data/filter/excerpt-images.ini')
self.verify_images()
def test_excerpt_images2(self):
config.load('tests/data/filter/excerpt-images2.ini')
self.verify_images()
def verify_images(self):
testfile = 'tests/data/filter/excerpt-images.xml'
output = open(testfile).read() output = open(testfile).read()
for filter in config.filters(): for filter in config.filters():
output = shell.run(filter, output, mode="filter") output = shell.run(filter, output, mode="filter")
@ -58,8 +64,15 @@ class FilterTests(unittest.TestCase):
self.assertEqual(u'before--after', self.assertEqual(u'before--after',
excerpt.firstChild.firstChild.nodeValue) excerpt.firstChild.firstChild.nodeValue)
def test_xpath_filter(self): def test_xpath_filter1(self):
config.load('tests/data/filter/xpath-sifter.ini') config.load('tests/data/filter/xpath-sifter.ini')
self.verify_xpath()
def test_xpath_filter2(self):
config.load('tests/data/filter/xpath-sifter2.ini')
self.verify_xpath()
def verify_xpath(self):
testfile = 'tests/data/filter/category-one.xml' testfile = 'tests/data/filter/category-one.xml'
output = open(testfile).read() output = open(testfile).read()
@ -89,7 +102,8 @@ try:
import libxml2 import libxml2
except: except:
logger.warn("libxml2 is not available => can't test xpath_sifter") logger.warn("libxml2 is not available => can't test xpath_sifter")
del FilterTests.test_xpath_filter del FilterTests.test_xpath_filter1
del FilterTests.test_xpath_filter2
except ImportError: except ImportError:
logger.warn("Popen is not available => can't test standard filters") logger.warn("Popen is not available => can't test standard filters")