Debian Codegraph @

lib/dbtexmf/xslt/xsltconf.py

raw
1 #
2 # Wrapper for xslt an engine command specified by an XML Configuration.
3 #
4 import os
5 import re
6 import logging
7 from subprocess import call, Popen, PIPE
8 from dbtexmf.core.commander import CommandRunner
9 from dbtexmf.core.error import signal_error
10 import dbtexmf.xslt
11
12 class XsltEngine:
13 _log = logging.getLogger("dblatex")
14
15 def __init__(self, param_format=""):
16 self.param_format = param_format
17 self.subcommand_with_params = None
18 self.command = CommandRunner(log=self._log)
19
20 def add_command(self, *args, **kwargs):
21 # Remember the subcommand that shall contain XSL parameters
22 sc = self.command.add_command(*args, **kwargs)
23 if "%(param_list)s" in args[0]:
24 self.subcommand_with_params = sc
25
26 def _run(self, xslfile, xmlfile, outfile):
27 rc = self.command.run(kw={"stylesheet": xslfile,
28 "xmlfile": xmlfile,
29 "output": outfile })
30 if rc != 0: signal_error(self, "")
31
32 def run(self, xslfile, xmlfile, outfile, opts=None, params=None):
33 if not(self.subcommand_with_params):
34 self._run(xslfile, xmlfile, outfile)
35 else:
36 # Temporary replace the parameter list in the command
37 args_pattern = self.subcommand_with_params.arguments
38 self.subcommand_with_params.arguments = \
39 self.param_args_format(args_pattern, params)
40 self._run(xslfile, xmlfile, outfile)
41 self.subcommand_with_params.arguments = args_pattern
42
43 def param_args_format(self, arguments, params):
44 args = [] + arguments
45 idx = args.index("%(param_list)s")
46 args.remove("%(param_list)s")
47
48 if not(params):
49 return args
50 if not(self.param_format):
51 self._log.error("Error: Unknown XSL Parameter format")
52 return args
53
54 param_args = []
55 for param, value in params.items():
56 param_args.append(self.param_format % {"param_name": param,
57 "param_value": value})
58
59 for i in range(0, len(param_args)):
60 args.insert(idx+i, param_args[i])
61
62 return args
63
64 class XsltCommandPool:
65 def __init__(self):
66 self.command_runs = []
67
68 def add_command_run(self, command_runner):
69 self.command_runs.append(command_runner)
70
71 def extend(self, other):
72 self.command_runs.extend(other.command_runs)
73
74 def prepend(self, other):
75 self.command_runs = other.command_runs + self.command_runs
76
77 def get_command_runs(self, **criterions):
78 # By default return all the items
79 founds = [] + self.command_runs
80 return founds
81
82
83 class XsltConfigRunner:
84 """
85 Module loaded when a user defined XSLT engine is run. It just selects
86 the first engine from the pool, and run it.
87 """
88 def __init__(self):
89 pool = dbtexmf.xslt.xslt_setup().command_pool
90 if pool:
91 commands = pool.get_command_runs()
92 if commands: self.command = commands[0]
93 else:
94 self.command = None
95
96 def get_deplist(self):
97 return []
98
99 def run(self, xslfile, xmlfile, outfile, opts=None, params=None):
100 if not(self.command):
101 raise ValueError("xsltconf: no command found")
102
103 self.command.run(xslfile, xmlfile, outfile,
104 opts=opts, params=params)
105
106
107 class Xslt(XsltConfigRunner):
108 "Plugin Class to load"