<py:match path="foo">
<?python
import os
href = select('@href').render()
SOMEWHERE = ... # what file contained the foo tag?
path = os.path.abspath(os.path.join(os.path.dirname(SOMEWHERE), href)
f = file(path,'r')
# (do something interesting with f)
?>
</py:match>
...
<foo href="../path/relative/to/this/template/abcd.xyz"/>
What should go as “somewhere” above? I want that href attribute to be relative to the file with the foo tag in it, like href attributes on other tags.
Alternatively, what file contained the py:match block? This is less good because it may be in a different directory from the file with the foo tag.
Even less good: I could supply the path of the file I’m rendering as a context argument from outside Genshi, but that might be in a different directory from both of the above.
You need to make sure that the driver program (i.e., the Python program that parses the input file) runs in the directory of the file containing the
footag. Otherwise, you need to pass down the relative path (i.e., how to get from the directory in which the reader runs to the directory of the file being read) as a context argument to your Python code and add it to theos.path.joincommand.With this setup (and using Genshi 0.6 installed on MacOS X 10.6.3 via the Fink package genshi-py26) the command
os.getcwd()returns the current working directory of the file containing thefootag.For such complicated path constructs I also strongly recommend to use
path=os.path.normpath(path), since you may not want such things to leak in your resulting HTML code.