1 #!/usr/bin/env python 2 3 """ 4 Preparation of templating stylesheets. 5 6 Copyright (C) 2005 Paul Boddie <paul@boddie.org.uk> 7 8 This library is free software; you can redistribute it and/or 9 modify it under the terms of the GNU Lesser General Public 10 License as published by the Free Software Foundation; either 11 version 2.1 of the License, or (at your option) any later version. 12 13 This library is distributed in the hope that it will be useful, 14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 Lesser General Public License for more details. 17 18 You should have received a copy of the GNU Lesser General Public 19 License along with this library; if not, write to the Free Software 20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 21 """ 22 23 from XSLTools import XSLOutput 24 import libxml2dom 25 import os 26 27 resource_dir = os.path.join(os.path.split(__file__)[0], "XSL") 28 29 # Web template functions. 30 31 def make_stylesheet(template_name, output_name, stylesheet_names=["PrepareMacro.xsl", "Prepare.xsl"], encoding=None): 32 33 """ 34 Make an output stylesheet using the file with the given 'template_name', 35 producing a file with the given 'output_name'. 36 """ 37 38 stylesheets = [os.path.join(resource_dir, stylesheet_name) for stylesheet_name in stylesheet_names] 39 proc = XSLOutput.Processor(stylesheets) 40 template = libxml2dom.parse(template_name) 41 proc.send_output(open(output_name, "wb"), encoding, template) 42 43 def make_stylesheet_fragment(template_name, output_name, element_id, stylesheet_name="Extract.xsl", encoding=None): 44 45 """ 46 Make an output stylesheet using the file with the given 'template_name', 47 producing a file with the given 'output_name', capturing the fragment 48 identified by the given 'element_id'. 49 """ 50 51 proc = XSLOutput.Processor([os.path.join(resource_dir, stylesheet_name)], parameters={"element-id" : element_id}) 52 template = libxml2dom.parse(template_name) 53 proc.send_output(open(output_name, "wb"), encoding, template) 54 55 def ensure_stylesheet(template_name, output_name): 56 57 """ 58 Ensure the presence of an output stylesheet, preparing it if necessary 59 using the file with the given 'template_name', producing a file with the 60 given 'output_name'. 61 """ 62 63 if not os.path.exists(output_name) or \ 64 os.path.getmtime(output_name) < os.path.getmtime(template_name): 65 66 make_stylesheet(template_name, output_name) 67 68 def ensure_stylesheet_fragment(template_name, output_name, element_id): 69 70 """ 71 Ensure the presence of an output stylesheet, preparing it if necessary 72 using the file with the given 'template_name', producing a file with the 73 given 'output_name', capturing the fragment identified by the given 74 'element_id'. 75 """ 76 77 if not os.path.exists(output_name) or \ 78 os.path.getmtime(output_name) < os.path.getmtime(template_name): 79 80 make_stylesheet_fragment(template_name, output_name, element_id) 81 82 # Document initialisation functions. 83 84 def make_input_stylesheet(template_name, input_name, stylesheet_names=["Schema.xsl", "Input.xsl"], encoding=None): 85 86 """ 87 Make an input stylesheet using the file with the given 'template_name', 88 producing a file with the given 'input_name'. Such stylesheets are used to 89 ensure the general structure of an input document. 90 """ 91 92 stylesheets = [os.path.join(resource_dir, stylesheet_name) for stylesheet_name in stylesheet_names] 93 proc = XSLOutput.Processor(stylesheets) 94 template = libxml2dom.parse(template_name) 95 proc.send_output(open(input_name, "wb"), encoding, template) 96 97 def ensure_input_stylesheet(template_name, input_name): 98 99 """ 100 Ensure the presence of an input stylesheet, preparing it if necessary 101 using the file with the given 'template_name', producing a file with the 102 given 'input_name'. 103 """ 104 105 if not os.path.exists(input_name) or \ 106 os.path.getmtime(input_name) < os.path.getmtime(template_name): 107 108 make_input_stylesheet(template_name, input_name) 109 110 # Qt Designer functions. 111 112 def make_qt_fragment(template_name, output_name, widget_name, stylesheet_name="QtDesignerExtract.xsl", encoding=None): 113 proc = XSLOutput.Processor([os.path.join(resource_dir, stylesheet_name)], parameters={"widget-name" : widget_name}) 114 template = libxml2dom.parse(template_name) 115 proc.send_output(open(output_name, "wb"), encoding, template) 116 117 def ensure_qt_fragment(template_name, output_name, widget_name): 118 if not os.path.exists(output_name) or \ 119 os.path.getmtime(output_name) < os.path.getmtime(template_name): 120 121 make_qt_fragment(template_name, output_name, widget_name) 122 123 # vim: tabstop=4 expandtab shiftwidth=4