Blame lib/dbtexmf/xslt/xsltproc.py

Packit 0f19cf
#
Packit 0f19cf
# Basic wrapper for xsltproc. Maybe we should directly use the lixslt Python
Packit 0f19cf
# API.
Packit 0f19cf
#
Packit Service f3de8e
import sys
Packit 0f19cf
import os
Packit 0f19cf
import logging
Packit 0f19cf
import re
Packit 0f19cf
from subprocess import call, Popen, PIPE
Packit 0f19cf
Packit 0f19cf
class XsltProc:
Packit 0f19cf
    def __init__(self):
Packit 0f19cf
        self.catalogs = os.getenv("SGML_CATALOG_FILES")
Packit 0f19cf
        self.use_catalogs = 1
Packit 0f19cf
        self.log = logging.getLogger("dblatex")
Packit 0f19cf
        self.run_opts = ["--xinclude"]
Packit 0f19cf
        # If --xincludestyle is supported we *must* use it to support external
Packit 0f19cf
        # listings (see mklistings.xsl and pals)
Packit 0f19cf
        if self._has_xincludestyle():
Packit 0f19cf
            self.run_opts.append("--xincludestyle")
Packit 0f19cf
Packit 0f19cf
    def get_deplist(self):
Packit 0f19cf
        return ["xsltproc"]
Packit 0f19cf
Packit 0f19cf
    def run(self, xslfile, xmlfile, outfile, opts=None, params=None):
Packit 0f19cf
        cmd = ["xsltproc", "-o", os.path.basename(outfile)] + self.run_opts
Packit 0f19cf
        if self.use_catalogs and self.catalogs:
Packit 0f19cf
            cmd.append("--catalogs")
Packit 0f19cf
        if params:
Packit 0f19cf
            for param, value in params.items():
Packit 0f19cf
                cmd += ["--param", param, "'%s'" % value]
Packit 0f19cf
        if opts:
Packit 0f19cf
            cmd += opts
Packit 0f19cf
        cmd += [xslfile, xmlfile]
Packit 0f19cf
        self.system(cmd)
Packit 0f19cf
Packit 0f19cf
    def system(self, cmd):
Packit 0f19cf
        self.log.debug(" ".join(cmd))
Packit 0f19cf
        rc = call(cmd)
Packit 0f19cf
        if rc != 0:
Packit 0f19cf
            raise ValueError("xsltproc failed")
Packit 0f19cf
Packit 0f19cf
    def _has_xincludestyle(self):
Packit 0f19cf
        # check that with help output the option is there
Packit 0f19cf
        p = Popen(["xsltproc"], stdout=PIPE)
Packit 0f19cf
        data = p.communicate()[0]
Packit Service f3de8e
        if isinstance(data, bytes):
Packit Service f3de8e
            data = data.decode(sys.getdefaultencoding())
Packit 0f19cf
        m = re.search("--xincludestyle", data, re.M)
Packit 0f19cf
        if not(m):
Packit 0f19cf
            return False
Packit 0f19cf
        else:
Packit 0f19cf
            return True
Packit 0f19cf
Packit 0f19cf
Packit 0f19cf
class Xslt(XsltProc):
Packit 0f19cf
    "Plugin Class to load"