Skip to content
Browse files

grabber: fix: built-in xslt function 'file-exists' prepends host dir.

  • Loading branch information...
1 parent 3cd23d5 commit ef6643d3ba82b76255173e227158e70c4b5d2d65 @knipknap committed Apr 12, 2012
Showing with 11 additions and 7 deletions.
  1. +1 −1 src/fresh/grabber/processors/XsltProcessor.py
  2. +7 −3 src/fresh/grabber/util.py
  3. +3 −3 src/fresh/grabber/xslttest.py
View
2 src/fresh/grabber/processors/XsltProcessor.py
@@ -62,7 +62,7 @@ def start(self, provider, host, conn, **kwargs):
# Transform.
doc = etree.parse(StringIO(input), base_url = path)
xsl = etree.parse(xslt_file)
- result = apply_xslt(xsl, doc)
+ result = apply_xslt(os.path.dirname(path), xsl, doc)
# Insert extra data into the resulting XML.
for path, expression in self.add:
View
10 src/fresh/grabber/util.py
@@ -14,6 +14,7 @@
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import re
import os
+from functools import partial
from lxml import etree
filename_strip_re = re.compile(r'[^a-z0-9_\-\.]', re.I)
@@ -26,8 +27,11 @@ def str2filename(string, suffix = '.txt'):
python_ns = etree.FunctionNamespace('localhost')
python_ns.prefix = 'py'
-python_ns['file-exists'] = lambda c, x: os.path.isfile(x)
+def _file_exists(base_dir, context, filename):
+ return os.path.isfile(os.path.join(base_dir, filename))
-def apply_xslt(xslt, doc):
- transform = etree.XSLT(xslt)
+def apply_xslt(host_dir, xslt, doc):
+ exists = partial(_file_exists, host_dir)
+ extensions = {('localhost', 'file-exists'): exists}
+ transform = etree.XSLT(xslt, extensions = extensions)
return transform(doc)
View
6 src/fresh/grabber/xslttest.py
@@ -19,8 +19,8 @@
except IndexError:
parser.error('please enter the name of the XML file')
-path = os.path.basename(xml_file)
-doc = etree.parse(xml_file, base_url = path)
+doc = etree.parse(xml_file, base_url = xml_file)
xsl = etree.parse(xslt_file)
-print apply_xslt(xsl, doc)
+path = os.path.dirname(xml_file)
+print apply_xslt(path, xsl, doc)

0 comments on commit ef6643d

Please sign in to comment.
Something went wrong with that request. Please try again.