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