Debian Codegraph @

lib/dbtexmf/xslt/xsltproc.py

raw
1 #
2 # Basic wrapper for xsltproc. Maybe we should directly use the lixslt Python
3 # API.
4 #
5 import sys
6 import os
7 import logging
8 import re
9 from subprocess import call, Popen, PIPE
10
11 class XsltProc:
12 def __init__(self):
13 self.catalogs = os.getenv("SGML_CATALOG_FILES")
14 self.use_catalogs = 1
15 self.log = logging.getLogger("dblatex")
16 self.run_opts = ["--xinclude"]
17 # If --xincludestyle is supported we *must* use it to support external
18 # listings (see mklistings.xsl and pals)
19 if self._has_xincludestyle():
20 self.run_opts.append("--xincludestyle")
21
22 def get_deplist(self):
23 return ["xsltproc"]
24
25 def run(self, xslfile, xmlfile, outfile, opts=None, params=None):
26 cmd = ["xsltproc", "-o", os.path.basename(outfile)] + self.run_opts
27 if self.use_catalogs and self.catalogs:
28 cmd.append("--catalogs")
29 if params:
30 for param, value in params.items():
31 cmd += ["--param", param, "'%s'" % value]
32 if opts:
33 cmd += opts
34 cmd += [xslfile, xmlfile]
35 self.system(cmd)
36
37 def system(self, cmd):
38 self.log.debug(" ".join(cmd))
39 rc = call(cmd)
40 if rc != 0:
41 raise ValueError("xsltproc failed")
42
43 def _has_xincludestyle(self):
44 # check that with help output the option is there
45 p = Popen(["xsltproc"], stdout=PIPE)
46 data = p.communicate()[0]
47 if isinstance(data, bytes):
48 data = data.decode(sys.getdefaultencoding())
49 m = re.search("--xincludestyle", data, re.M)
50 if not(m):
51 return False
52 else:
53 return True
54
55
56 class Xslt(XsltProc):
57 "Plugin Class to load"