XSLTools

XSLForms/Prepare.py

377:7fb43f2befb0
2005-11-01 paulb [project @ 2005-11-01 18:58:15 by paulb] Added memory and storage unit initialisation.
     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 # Generic functions.    30     31 def _ensure_document(template_name, output_name, fn, *args, **kw):    32     if not os.path.exists(output_name) or \    33         os.path.getmtime(output_name) < os.path.getmtime(template_name):    34     35         fn(template_name, output_name, *args, **kw)    36     37 def _make_document(input_name, output_name, stylesheet_names, encoding=None, parameters=None):    38     stylesheets = [os.path.join(resource_dir, stylesheet_name) for stylesheet_name in stylesheet_names]    39     proc = XSLOutput.Processor(stylesheets, parameters=parameters or {})    40     input = libxml2dom.parse(input_name)    41     proc.send_output(open(output_name, "wb"), encoding, input)    42     43 # Web template functions.    44     45 def make_stylesheet(template_name, output_name, stylesheet_names=["PrepareMacro.xsl", "Prepare.xsl"], encoding=None):    46     47     """    48     Make an output stylesheet using the file with the given 'template_name',    49     producing a file with the given 'output_name'.    50     """    51     52     _make_document(template_name, output_name, stylesheet_names, encoding)    53     54 def make_stylesheet_fragment(template_name, output_name, element_id, stylesheet_names=["Extract.xsl"], encoding=None):    55     56     """    57     Make an output stylesheet using the file with the given 'template_name',    58     producing a file with the given 'output_name', capturing the fragment    59     identified by the given 'element_id'.    60     """    61     62     _make_document(template_name, output_name, stylesheet_names, encoding, parameters={"element-id" : element_id})    63     64 def ensure_stylesheet(template_name, output_name):    65     66     """    67     Ensure the presence of an output stylesheet, preparing it if necessary    68     using the file with the given 'template_name', producing a file with the    69     given 'output_name'.    70     """    71     72     _ensure_document(template_name, output_name, make_stylesheet)    73     74 def ensure_stylesheet_fragment(template_name, output_name, element_id):    75     76     """    77     Ensure the presence of an output stylesheet, preparing it if necessary    78     using the file with the given 'template_name', producing a file with the    79     given 'output_name', capturing the fragment identified by the given    80     'element_id'.    81     """    82     83     _ensure_document(template_name, output_name, make_stylesheet_fragment, element_id)    84     85 # Document initialisation functions.    86     87 def make_input_stylesheet(template_name, input_name, init_enumerations=1, stylesheet_names=["Schema.xsl", "Input.xsl"], encoding=None):    88     89     """    90     Make an input stylesheet using the file with the given 'template_name',    91     producing a file with the given 'input_name'. Such stylesheets are used to    92     ensure the general structure of an input document.    93     94     The optional 'init_enumerations' (defaulting to true) may be used to    95     indicate whether enumerations are to be initialised from external documents.    96     """    97     98     if init_enumerations:    99         init_enumerations_str = "yes"   100     else:   101         init_enumerations_str = "no"   102     _make_document(template_name, input_name, stylesheet_names, encoding, parameters={"init-enumerations" : init_enumerations_str})   103    104 def ensure_input_stylesheet(template_name, input_name, init_enumerations=1):   105    106     """   107     Ensure the presence of an input stylesheet, preparing it if necessary   108     using the file with the given 'template_name', producing a file with the   109     given 'input_name'.   110    111     The optional 'init_enumerations' (defaulting to true) may be used to   112     indicate whether enumerations are to be initialised from external documents.   113     """   114    115     _ensure_document(template_name, input_name, make_input_stylesheet, init_enumerations)   116    117 # Schema-related functions.   118    119 def make_schema(template_name, output_name, stylesheet_names=["Schema.xsl"], encoding=None):   120    121     """   122     Make a schema document using the file with the given 'template_name',   123     producing a file with the given 'output_name'.   124     """   125    126     _make_document(template_name, output_name, stylesheet_names, encoding)   127    128 # Qt Designer functions.   129    130 def make_qt_fragment(template_name, output_name, widget_name, stylesheet_names=["QtDesignerExtract.xsl"], encoding=None):   131     _make_document(template_name, output_name, stylesheet_names, encoding, parameters={"widget-name" : widget_name})   132    133 def ensure_qt_fragment(template_name, output_name, widget_name):   134     _ensure_document(template_name, output_name, make_qt_fragment, widget_name)   135    136 # Qt Designer Web functions.   137    138 def make_qt_template(template_name, output_name, stylesheet_names=["QtDesigner.xsl"], encoding=None):   139     _make_document(template_name, output_name, stylesheet_names, encoding)   140    141 def ensure_qt_template(template_name, output_name):   142     _ensure_document(template_name, output_name, make_qt_template)   143    144 def make_qt_stylesheet(template_name, output_name, stylesheet_names=["QtDesigner.xsl", "PrepareMacro.xsl", "Prepare.xsl"], encoding=None):   145     _make_document(template_name, output_name, stylesheet_names, encoding)   146    147 def ensure_qt_stylesheet(template_name, output_name):   148     _ensure_document(template_name, output_name, make_qt_stylesheet)   149    150 # Qt Designer Web functions for fragments.   151    152 def make_qt_template_fragment(template_name, output_name, widget_name, stylesheet_names=["QtDesignerExtract.xsl", "QtDesigner.xsl"], encoding=None):   153     _make_document(template_name, output_name, stylesheet_names, encoding, parameters={"widget-name" : widget_name})   154    155 def ensure_qt_template_fragment(template_name, output_name, widget_name):   156     _ensure_document(template_name, output_name, make_qt_template_fragment, widget_name)   157    158 def make_qt_stylesheet_fragment(template_name, output_name, widget_name,   159     stylesheet_names=["QtDesignerExtract.xsl", "QtDesigner.xsl", "PrepareMacro.xsl", "Prepare.xsl"], encoding=None):   160    161     _make_document(template_name, output_name, stylesheet_names, encoding, parameters={"widget-name" : widget_name})   162    163 def ensure_qt_stylesheet_fragment(template_name, output_name, widget_name):   164     _ensure_document(template_name, output_name, make_qt_stylesheet_fragment, widget_name)   165    166 # vim: tabstop=4 expandtab shiftwidth=4