| 1 |
# |
| 2 |
# Simple converter from text configuration to XML configuration |
| 3 |
# |
| 4 |
from __future__ import print_function |
| 5 |
|
| 6 |
import sys |
| 7 |
import os |
| 8 |
import xml.etree.ElementTree as ET |
| 9 |
import xml.dom.minidom |
| 10 |
|
| 11 |
package_base = os.path.join(os.path.dirname(sys.argv[0]), "..") |
| 12 |
sys.path.append(os.path.realpath(os.path.join(package_base, "lib"))) |
| 13 |
|
| 14 |
from dbtexmf.core.txtparser import TextConfig |
| 15 |
|
| 16 |
class XmlMapper: |
| 17 |
""" |
| 18 |
Make a correspondence beetween text keywords to equivalent XML nodes |
| 19 |
and how to convert the keyword data to XML content (fill_method) |
| 20 |
""" |
| 21 |
def __init__(self, fill_method, *args): |
| 22 |
self.tags = args |
| 23 |
self.value = None |
| 24 |
self.fill_method = fill_method |
| 25 |
|
| 26 |
def format(self, dir, value): |
| 27 |
self.value = value |
| 28 |
return [self] |
| 29 |
|
| 30 |
|
| 31 |
class TextXmlMapper(TextConfig): |
| 32 |
""" |
| 33 |
Use the original class to parse the text config file the same way, |
| 34 |
but give to this class the role to map the config data to XML equivalent |
| 35 |
""" |
| 36 |
conf_mapping = { |
| 37 |
'TexInputs' : XmlMapper("text", 'latex','texinputs'), |
| 38 |
'TexPost' : XmlMapper("mod_or_file", 'latex','texpost'), |
| 39 |
'FigPath' : XmlMapper("file", 'imagedata','figpath'), |
| 40 |
'XslParam' : XmlMapper("file", 'xslt','stylesheet'), |
| 41 |
'TexStyle' : XmlMapper("mod_or_file", 'latex','texstyle'), |
| 42 |
'Options' : XmlMapper("text", 'options') |
| 43 |
} |
| 44 |
|
| 45 |
class XmlConfig: |
| 46 |
""" |
| 47 |
Build an XML configuration file from an XML mapping |
| 48 |
""" |
| 49 |
xmlns = "http://dblatex.sourceforge.net/config" |
| 50 |
def __init__(self, config_dir=""): |
| 51 |
self.root = ET.Element("config") |
| 52 |
self.root.set("xmlns", self.xmlns) |
| 53 |
self.tree = ET.ElementTree(self.root) |
| 54 |
ET.register_namespace("", self.xmlns) |
| 55 |
self.config_dir = config_dir |
| 56 |
|
| 57 |
def add_element(self, xmldesc): |
| 58 |
xmlnode = self.root |
| 59 |
for tag in xmldesc.tags[:-1]: |
| 60 |
element = xmlnode.find(tag) |
| 61 |
if not(element is None): |
| 62 |
xmlnode = element |
| 63 |
else: |
| 64 |
xmlnode = ET.SubElement(xmlnode, tag) |
| 65 |
|
| 66 |
last_node = ET.SubElement(xmlnode, xmldesc.tags[-1]) |
| 67 |
self.fill_node(last_node, xmldesc.value, xmldesc.fill_method) |
| 68 |
|
| 69 |
def write(self, stream): |
| 70 |
data = ET.tostring(self.root) |
| 71 |
doc = xml.dom.minidom.parseString(data) |
| 72 |
stream.write(doc.toprettyxml(indent=" ")) |
| 73 |
|
| 74 |
def fill_node(self, xmlnode, value, fill_method): |
| 75 |
fill_calls = { |
| 76 |
"file": self.fill_file, |
| 77 |
"text": self.fill_text, |
| 78 |
"mod_or_file": self.fill_mod_or_file |
| 79 |
} |
| 80 |
fill_function = fill_calls.get(fill_method, None) |
| 81 |
if fill_function: fill_function(xmlnode, value) |
| 82 |
|
| 83 |
def fill_file(self, xmlnode, value): |
| 84 |
xmlnode.set("fileref", value) |
| 85 |
|
| 86 |
def fill_mod(self, xmlnode, value): |
| 87 |
xmlnode.set("use", value) |
| 88 |
|
| 89 |
def fill_text(self, xmlnode, value): |
| 90 |
xmlnode.text = value |
| 91 |
|
| 92 |
def fill_mod_or_file(self, xmlnode, value): |
| 93 |
if os.path.isabs(value) and os.path.isfile(value): |
| 94 |
self.fill_file(xmlnode, value) |
| 95 |
elif os.path.isfile(os.path.join(self.config_dir, value)): |
| 96 |
self.fill_file(xmlnode, value) |
| 97 |
else: |
| 98 |
self.fill_mod(xmlnode, value) |
| 99 |
|
| 100 |
|
| 101 |
if __name__ == "__main__": |
| 102 |
from optparse import OptionParser |
| 103 |
parser = OptionParser(usage="%s <config_input.txt> <config_output.xml>" \ |
| 104 |
% sys.argv[0]) |
| 105 |
|
| 106 |
(options, args) = parser.parse_args() |
| 107 |
|
| 108 |
if len(args) != 2: |
| 109 |
print("Invalid argument count: expected 2", file=sys.stderr) |
| 110 |
parser.parse_args(["-h"]) |
| 111 |
|
| 112 |
txt_file = args[0] |
| 113 |
xml_file = args[1] |
| 114 |
|
| 115 |
txt_config = TextXmlMapper() |
| 116 |
txt_config.fromfile(txt_file) |
| 117 |
|
| 118 |
xml_config = XmlConfig(os.path.dirname(txt_file)) |
| 119 |
for xmldesc in txt_config.options(): |
| 120 |
xml_config.add_element(xmldesc) |
| 121 |
|
| 122 |
f = open(xml_file, "w") |
| 123 |
xml_config.write(f) |
| 124 |
f.close() |
| 125 |
|