| 1 |
import os |
| 2 |
import re |
| 3 |
import xml.etree.ElementTree as ET |
| 4 |
from .txtparser import texinputs_parse |
| 5 |
|
| 6 |
class BaseOption: |
| 7 |
def __init__(self, config, optname): |
| 8 |
self.config = config |
| 9 |
self.optname = optname |
| 10 |
self._value = None |
| 11 |
|
| 12 |
def optvalue(self): |
| 13 |
return self._value |
| 14 |
|
| 15 |
def get(self, what, default=None): |
| 16 |
return None |
| 17 |
|
| 18 |
def options(self): |
| 19 |
value = self.optvalue() |
| 20 |
if self.optname and value: |
| 21 |
return ["%s=%s" % (self.optname, value)] |
| 22 |
else: |
| 23 |
return [] |
| 24 |
|
| 25 |
def fromnode(self, xmlnode): |
| 26 |
self._value = xmlnode.text |
| 27 |
|
| 28 |
def modules(self): |
| 29 |
return {} |
| 30 |
|
| 31 |
class CommandConfig: |
| 32 |
def __init__(self, config, type="command"): |
| 33 |
self.config = config |
| 34 |
self.type = type |
| 35 |
self.args = [] |
| 36 |
self.stdin = None |
| 37 |
self.stdout = None |
| 38 |
self.shell = False |
| 39 |
|
| 40 |
def options(self): |
| 41 |
return self.args |
| 42 |
|
| 43 |
def modules(self): |
| 44 |
return {} |
| 45 |
|
| 46 |
def fromnode(self, xmlnode): |
| 47 |
self.stdin = xmlnode.get("input") |
| 48 |
self.stdout = xmlnode.get("output") |
| 49 |
self.shell = xmlnode.get("shell") |
| 50 |
args = (xmlnode.text or "").split() |
| 51 |
for arg in xmlnode: |
| 52 |
if arg.text: args.append(arg.text) |
| 53 |
args.extend((arg.tail or "").split()) |
| 54 |
self.args = args |
| 55 |
|
| 56 |
class TexStyle(BaseOption): |
| 57 |
def __init__(self, config, optname): |
| 58 |
BaseOption.__init__(self, config, optname) |
| 59 |
self.filepath = "" |
| 60 |
|
| 61 |
def optvalue(self): |
| 62 |
return self.filepath |
| 63 |
|
| 64 |
def fromnode(self, xmlnode): |
| 65 |
self.filepath = xmlnode.get("fileref") or xmlnode.get("use") |
| 66 |
|
| 67 |
class TexPath(BaseOption): |
| 68 |
def __init__(self, config, optname): |
| 69 |
BaseOption.__init__(self, config, optname) |
| 70 |
self.paths = [] |
| 71 |
|
| 72 |
def optvalue(self): |
| 73 |
return os.pathsep.join(self.paths) |
| 74 |
|
| 75 |
def fromnode(self, xmlnode): |
| 76 |
if not(xmlnode.text): return |
| 77 |
self.paths = texinputs_parse(xmlnode.text, self.config.basedir) |
| 78 |
|
| 79 |
class FilePath(BaseOption): |
| 80 |
def __init__(self, config, optname): |
| 81 |
BaseOption.__init__(self, config, optname) |
| 82 |
self.filepath = "" |
| 83 |
|
| 84 |
def optvalue(self): |
| 85 |
return self.filepath |
| 86 |
|
| 87 |
def fromnode(self, xmlnode): |
| 88 |
filepath = xmlnode.get("fileref") |
| 89 |
if not(filepath): |
| 90 |
return |
| 91 |
if not(os.path.isabs(filepath)): |
| 92 |
filepath = os.path.normpath(os.path.join(self.config.basedir, |
| 93 |
filepath)) |
| 94 |
self.filepath = filepath |
| 95 |
|
| 96 |
|
| 97 |
class ModuleConfig(BaseOption): |
| 98 |
def __init__(self, config, optname): |
| 99 |
BaseOption.__init__(self, config, optname) |
| 100 |
self.commands = [] |
| 101 |
self.extra_args = None |
| 102 |
self.module_name = "" |
| 103 |
self.module_file = "" |
| 104 |
|
| 105 |
def optvalue(self): |
| 106 |
return self.module_name or self.module_file |
| 107 |
|
| 108 |
def modules(self): |
| 109 |
if self.module_name: |
| 110 |
return {self.module_name: self} |
| 111 |
else: |
| 112 |
return {} |
| 113 |
|
| 114 |
def fromnode(self, xmlnode): |
| 115 |
ns = { "x": self.config.xmlns } |
| 116 |
self._handle_location(xmlnode) |
| 117 |
xmlopts = xmlnode.find("x:options", ns) |
| 118 |
xmlcmds = xmlnode.find("x:command", ns) |
| 119 |
xmlchain = xmlnode.find("x:commandchain", ns) |
| 120 |
if not(xmlchain is None): |
| 121 |
xmlcmds = xmlchain.findall("x:command", ns) |
| 122 |
for cmd in xmlcmds: |
| 123 |
args = CommandConfig(self.config) |
| 124 |
args.fromnode(cmd) |
| 125 |
self.commands.append(args) |
| 126 |
elif not(xmlcmds is None): |
| 127 |
args = CommandConfig(self.config) |
| 128 |
args.fromnode(xmlcmds) |
| 129 |
self.commands.append(args) |
| 130 |
elif not(xmlopts is None): |
| 131 |
# FIXME |
| 132 |
self.extra_args = CommandConfig(self.config, type="option") |
| 133 |
self.extra_args.fromnode(xmlopts) |
| 134 |
|
| 135 |
def _handle_location(self, xmlnode): |
| 136 |
self.module_name = xmlnode.get("use") |
| 137 |
self.module_file = xmlnode.get("fileref") |
| 138 |
if not(self.module_name) and self.module_file: |
| 139 |
p = FilePath(self.config, "") |
| 140 |
p.fromnode(xmlnode) |
| 141 |
self.module_file = p.filepath |
| 142 |
|
| 143 |
class ImageConverterConfig(ModuleConfig): |
| 144 |
def __init__(self, config, optname): |
| 145 |
ModuleConfig.__init__(self, config, optname) |
| 146 |
|
| 147 |
def __repr__(self): |
| 148 |
return self.module_name |
| 149 |
|
| 150 |
def fromnode(self, xmlnode): |
| 151 |
self.imgsrc = xmlnode.get("src") |
| 152 |
self.imgdst = xmlnode.get("dst") |
| 153 |
self.docformat = xmlnode.get("docformat") or "*" |
| 154 |
self.backend = xmlnode.get("backend") or "*" |
| 155 |
ModuleConfig.fromnode(self, xmlnode) |
| 156 |
name = "%s/%s/%s/%s" % (self.imgsrc, self.imgdst, |
| 157 |
self.docformat, self.backend) |
| 158 |
self.module_name = name |
| 159 |
|
| 160 |
class ImageFormatConfig(BaseOption): |
| 161 |
def __init__(self, config, optname): |
| 162 |
BaseOption.__init__(self, config, optname) |
| 163 |
self.imgsrc = "" |
| 164 |
self.imgdst = "" |
| 165 |
self.docformat = "" |
| 166 |
self.backend = "" |
| 167 |
|
| 168 |
def fromnode(self, xmlnode): |
| 169 |
self.imgsrc = xmlnode.get("src") |
| 170 |
self.imgdst = xmlnode.get("dst") |
| 171 |
self.docformat = xmlnode.get("docformat") or "*" |
| 172 |
self.backend = xmlnode.get("backend") or "*" |
| 173 |
|
| 174 |
class XsltEngineConfig(ModuleConfig): |
| 175 |
def __init__(self, config, optname): |
| 176 |
ModuleConfig.__init__(self, config, optname) |
| 177 |
|
| 178 |
def __repr__(self): |
| 179 |
return self.module_name |
| 180 |
|
| 181 |
def fromnode(self, xmlnode): |
| 182 |
self.param_format = xmlnode.get("param-format") |
| 183 |
ModuleConfig.fromnode(self, xmlnode) |
| 184 |
if not(self.module_name or self.module_file): |
| 185 |
self.module_name = "xsltconf" |
| 186 |
|
| 187 |
class XmlConfigGroup: |
| 188 |
node_parsers = {} |
| 189 |
|
| 190 |
def __init__(self, config): |
| 191 |
self.config = config |
| 192 |
self.tagname = "" |
| 193 |
self.infos = {} |
| 194 |
|
| 195 |
def get(self, tag, default=""): |
| 196 |
if default == "": default = BaseOption(self, "") |
| 197 |
return self.infos.get(tag, default) |
| 198 |
|
| 199 |
def _register(self, xmlnode, info): |
| 200 |
tag = self.strip_ns(xmlnode.tag) |
| 201 |
taglist = self.infos.get(tag, []) |
| 202 |
taglist.append(info) |
| 203 |
self.infos[tag] = taglist |
| 204 |
|
| 205 |
def strip_ns(self, tag): |
| 206 |
return self.config.strip_ns(tag) |
| 207 |
|
| 208 |
def options(self): |
| 209 |
opts = [] |
| 210 |
for parsers in self.infos.values(): |
| 211 |
for parser in parsers: |
| 212 |
opts.extend(parser.options()) |
| 213 |
return opts |
| 214 |
|
| 215 |
def modules(self): |
| 216 |
mods = {} |
| 217 |
for parsers in self.infos.values(): |
| 218 |
for parser in parsers: |
| 219 |
mods.update(parser.modules()) |
| 220 |
return mods |
| 221 |
|
| 222 |
def fromnode(self, xmlnode): |
| 223 |
self.tagname = xmlnode.tag |
| 224 |
for child in xmlnode: |
| 225 |
found = self.node_parsers.get(self.strip_ns(child.tag)) |
| 226 |
if found: |
| 227 |
optname, parser_cls = found |
| 228 |
parser = parser_cls(self.config, optname) |
| 229 |
parser.fromnode(child) |
| 230 |
self._register(child, parser) |
| 231 |
|
| 232 |
class LatexConfig(XmlConfigGroup): |
| 233 |
node_parsers = { |
| 234 |
"texinputs": ("--texinputs", TexPath), |
| 235 |
"bibinputs": ("--bib-path", TexPath), |
| 236 |
"bstinputs": ("--bst-path", TexPath), |
| 237 |
"texstyle": ("--texstyle", TexStyle), |
| 238 |
"indexstyle": ("--indexstyle", FilePath), |
| 239 |
"backend": ("--backend", ModuleConfig), |
| 240 |
"texpost": ("--texpost", ModuleConfig) |
| 241 |
} |
| 242 |
|
| 243 |
class XsltConfig(XmlConfigGroup): |
| 244 |
node_parsers = { |
| 245 |
"stylesheet": ("--xsl-user", FilePath), |
| 246 |
"engine": ("--xslt", XsltEngineConfig) |
| 247 |
} |
| 248 |
|
| 249 |
class ImageConfig(XmlConfigGroup): |
| 250 |
node_parsers = { |
| 251 |
"figpath": ("--fig-path", FilePath), |
| 252 |
"figformat": ("--fig-format", BaseOption), |
| 253 |
"converter": ("", ImageConverterConfig), |
| 254 |
"formatrule": ("", ImageFormatConfig) |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
class XmlConfig: |
| 259 |
""" |
| 260 |
Parses an XML configuration file and stores its data in |
| 261 |
configuration objects. |
| 262 |
""" |
| 263 |
node_parsers = { |
| 264 |
"latex": LatexConfig, |
| 265 |
"xslt": XsltConfig, |
| 266 |
"imagedata": ImageConfig, |
| 267 |
"options": CommandConfig |
| 268 |
} |
| 269 |
xmlns = "http://dblatex.sourceforge.net/config" |
| 270 |
root_tag = "config" |
| 271 |
|
| 272 |
def __init__(self): |
| 273 |
self.basedir = "" |
| 274 |
self.infos = {} |
| 275 |
|
| 276 |
def _register(self, xmlnode, info): |
| 277 |
self.infos[self.strip_ns(xmlnode.tag)] = info |
| 278 |
|
| 279 |
def get(self, tag, default=""): |
| 280 |
if default == "": default = BaseOption(self, "") |
| 281 |
return self.infos.get(tag, default) |
| 282 |
|
| 283 |
def options(self): |
| 284 |
opts = [] |
| 285 |
for parser in self.infos.values(): |
| 286 |
opts.extend(parser.options()) |
| 287 |
return opts |
| 288 |
|
| 289 |
def modules(self): |
| 290 |
mods = {} |
| 291 |
for parser in self.infos.values(): |
| 292 |
mods.update(parser.modules()) |
| 293 |
return mods |
| 294 |
|
| 295 |
def strip_ns(self, tag): |
| 296 |
return tag.replace("{%s}" % self.xmlns, "", 1) |
| 297 |
|
| 298 |
def fromfile(self, filename): |
| 299 |
self.basedir = os.path.dirname(os.path.realpath(filename)) |
| 300 |
|
| 301 |
document = ET.parse(filename) |
| 302 |
root = document.getroot() |
| 303 |
self._check_root(root.tag) |
| 304 |
for child in root: |
| 305 |
parser_cls = self.node_parsers.get(self.strip_ns(child.tag)) |
| 306 |
if parser_cls: |
| 307 |
parser = parser_cls(self) |
| 308 |
parser.fromnode(child) |
| 309 |
self._register(child, parser) |
| 310 |
|
| 311 |
def _check_root(self, root): |
| 312 |
xmlns, tag = self._split_xmlns(root) |
| 313 |
if tag != self.root_tag: |
| 314 |
raise ValueError("Expect the XML config root element being '%s'" % \ |
| 315 |
self.root_tag) |
| 316 |
if xmlns and xmlns != self.xmlns: |
| 317 |
raise ValueError("Invalid XML config xmlns: '%s'" % xmlns) |
| 318 |
|
| 319 |
def _split_xmlns(self, tag): |
| 320 |
m = re.match("{([^}]+)}(.*)", tag) |
| 321 |
if m: |
| 322 |
return m.group(1), m.group(2) |
| 323 |
else: |
| 324 |
return "", tag |
| 325 |
|