paulb@202 | 1 | #!/usr/bin/env python |
paulb@202 | 2 | |
paulb@600 | 3 | """ |
paulb@600 | 4 | A list of tuples to XML document converter. |
paulb@600 | 5 | |
paulb@600 | 6 | Copyright (C) 2005 Paul Boddie <paul@boddie.org.uk> |
paulb@600 | 7 | |
paulb@600 | 8 | This program is free software; you can redistribute it and/or modify it under |
paulb@600 | 9 | the terms of the GNU Lesser General Public License as published by the Free |
paulb@600 | 10 | Software Foundation; either version 3 of the License, or (at your option) any |
paulb@600 | 11 | later version. |
paulb@600 | 12 | |
paulb@600 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paulb@600 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paulb@600 | 15 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
paulb@600 | 16 | details. |
paulb@600 | 17 | |
paulb@600 | 18 | You should have received a copy of the GNU Lesser General Public License along |
paulb@600 | 19 | with this program. If not, see <http://www.gnu.org/licenses/>. |
paulb@600 | 20 | """ |
paulb@202 | 21 | |
paulb@202 | 22 | import libxml2dom |
paulb@202 | 23 | from UserDict import UserDict |
paulb@202 | 24 | |
paulb@202 | 25 | class OrderedDict(UserDict): |
paulb@202 | 26 | |
paulb@202 | 27 | "A dictionary with keys in the order in which they were given." |
paulb@202 | 28 | |
paulb@202 | 29 | def __init__(self, *args): |
paulb@202 | 30 | UserDict.__init__(self, *args) |
paulb@202 | 31 | self._keys = self.data.keys() |
paulb@202 | 32 | |
paulb@202 | 33 | def __setitem__(self, key, value): |
paulb@202 | 34 | UserDict.__setitem__(self, key, value) |
paulb@202 | 35 | if key not in self._keys: |
paulb@202 | 36 | self._keys.append(key) |
paulb@202 | 37 | |
paulb@202 | 38 | def __delitem__(self, key): |
paulb@202 | 39 | UserDict.__delitem__(self, key) |
paulb@202 | 40 | del self._keys[key] |
paulb@202 | 41 | |
paulb@202 | 42 | def keys(self): |
paulb@202 | 43 | return self._keys |
paulb@202 | 44 | |
paulb@202 | 45 | def items(self): |
paulb@202 | 46 | l = [] |
paulb@202 | 47 | for key in self._keys: |
paulb@202 | 48 | l.append((key, self.data[key])) |
paulb@202 | 49 | return l |
paulb@202 | 50 | |
paulb@202 | 51 | def values(self): |
paulb@202 | 52 | return [value for key, value in self.items()] |
paulb@202 | 53 | |
paulb@202 | 54 | class Converter: |
paulb@202 | 55 | def __init__(self, ns=None, prefix="", doc=None, root=None): |
paulb@202 | 56 | self.ns = ns |
paulb@202 | 57 | self.prefix = prefix |
paulb@202 | 58 | if doc is not None: |
paulb@202 | 59 | self.doc = doc |
paulb@202 | 60 | else: |
paulb@202 | 61 | self.doc = libxml2dom.createDocument(ns, prefix+"table", None) |
paulb@202 | 62 | if root is not None: |
paulb@202 | 63 | self.root = root |
paulb@202 | 64 | else: |
paulb@202 | 65 | self.root = self.doc.xpath("*")[0] |
paulb@202 | 66 | |
paulb@214 | 67 | # Remember the last row element. |
paulb@214 | 68 | |
paulb@214 | 69 | self.last_row_element = None |
paulb@214 | 70 | |
paulb@202 | 71 | def add_rows(self, rows): |
paulb@202 | 72 | for row in rows: |
paulb@202 | 73 | self.add_row(row) |
paulb@202 | 74 | |
paulb@202 | 75 | def add_row(self, row, index=-1, row_element_name="row", use_key_as_element_name=0): |
paulb@214 | 76 | self.last_row_element = self.doc.createElementNS(self.ns, self.prefix+row_element_name) |
paulb@202 | 77 | if index == -1: |
paulb@214 | 78 | self.root.appendChild(self.last_row_element) |
paulb@202 | 79 | else: |
paulb@214 | 80 | self.root.insertBefore(self.last_row_element, self.root.xpath("*[%s]" % (index + 1))[0]) |
paulb@202 | 81 | |
paulb@202 | 82 | # Permit dictionaries. |
paulb@202 | 83 | |
paulb@202 | 84 | if hasattr(row, "items"): |
paulb@202 | 85 | for name, value in row.items(): |
paulb@202 | 86 | if use_key_as_element_name: |
paulb@202 | 87 | column_element = self.doc.createElementNS(self.ns, self.prefix+unicode(name)) |
paulb@202 | 88 | else: |
paulb@202 | 89 | column_element = self.doc.createElementNS(self.ns, self.prefix+"column") |
paulb@202 | 90 | column_element.setAttributeNS(self.ns, self.prefix+"name", unicode(name)) |
paulb@214 | 91 | self.last_row_element.appendChild(column_element) |
paulb@202 | 92 | text = self.doc.createTextNode(unicode(value)) |
paulb@202 | 93 | column_element.appendChild(text) |
paulb@202 | 94 | |
paulb@202 | 95 | # As well as sequences. |
paulb@202 | 96 | |
paulb@202 | 97 | else: |
paulb@202 | 98 | for column in row: |
paulb@202 | 99 | column_element = self.doc.createElementNS(self.ns, self.prefix+"column") |
paulb@214 | 100 | self.last_row_element.appendChild(column_element) |
paulb@202 | 101 | text = self.doc.createTextNode(unicode(column)) |
paulb@202 | 102 | column_element.appendChild(text) |
paulb@202 | 103 | |
paulb@214 | 104 | def last_row(self): |
paulb@214 | 105 | if self.last_row_element is not None: |
paulb@214 | 106 | return Converter(self.ns, self.prefix, self.doc, self.last_row_element) |
paulb@214 | 107 | else: |
paulb@214 | 108 | return None |
paulb@214 | 109 | |
paulb@202 | 110 | if __name__ == "__main__": |
paulb@202 | 111 | from rdflib.TripleStore import TripleStore |
paulb@202 | 112 | import sys |
paulb@202 | 113 | s = TripleStore() |
paulb@202 | 114 | s.load(sys.argv[1]) |
paulb@202 | 115 | converter = Converter() |
paulb@202 | 116 | converter.add_rows(s.triples((None, None, None))) |
paulb@202 | 117 | libxml2dom.toStream(converter.doc, sys.stdout) |
paulb@202 | 118 | |
paulb@202 | 119 | # vim: tabstop=4 expandtab shiftwidth=4 |