1 #!/usr/bin/env python 2 3 """ 4 Preparation of templating stylesheets. 5 """ 6 7 import XSLOutput 8 import libxml2dom 9 import os 10 11 resource_dir = os.path.join(os.path.split(__file__)[0], "XSL") 12 13 def make_stylesheet(template_name, output_name, stylesheet_name="Prepare.xsl", encoding="utf-8"): 14 global resource_dir 15 proc = XSLOutput.Processor([os.path.join(resource_dir, stylesheet_name)]) 16 template = libxml2dom.parse(template_name) 17 proc.send_output(open(output_name, "wb"), encoding, template) 18 19 def make_stylesheet_fragment(template_name, output_name, element_id, stylesheet_name="Extract.xsl", encoding="utf-8"): 20 global resource_dir 21 proc = XSLOutput.Processor([os.path.join(resource_dir, stylesheet_name)], parameters={"element-id" : element_id}) 22 template = libxml2dom.parse(template_name) 23 proc.send_output(open(output_name, "wb"), encoding, template) 24 25 def ensure_stylesheet(template_name, output_name): 26 if not os.path.exists(output_name) or \ 27 os.path.getmtime(output_name) < os.path.getmtime(template_name): 28 29 make_stylesheet(template_name, output_name) 30 31 def ensure_stylesheet_fragment(template_name, output_name, element_id): 32 if not os.path.exists(output_name) or \ 33 os.path.getmtime(output_name) < os.path.getmtime(template_name): 34 35 make_stylesheet_fragment(template_name, output_name, element_id) 36 37 # vim: tabstop=4 expandtab shiftwidth=4