Project

General

Profile

1 13 aaronmk
# XML-database conversion
2
3 16 aaronmk
import re
4 13 aaronmk
from xml.dom import Node
5
6 46 aaronmk
import sql
7 84 aaronmk
import strings
8 46 aaronmk
import xml_dom
9 13 aaronmk
10 46 aaronmk
def name_of(node): return re.sub(r'^.*\.', r'', xml_dom.name_of(node))
11 16 aaronmk
12 13 aaronmk
ptr_suffix = '_id'
13
14
def is_ptr(node_name): return node_name.endswith(ptr_suffix)
15
16
def ptr_type(node_name):
17
    assert is_ptr(node_name)
18
    return node_name[:-len(ptr_suffix)]
19
20
def ptr_target(node):
21
    assert is_ptr(name_of(node))
22 46 aaronmk
    return xml_dom.first_elem(node)
23 13 aaronmk
24
def find_by_name(node, name):
25 46 aaronmk
    for parent in xml_dom.NodeParentIter(node):
26 13 aaronmk
        if name_of(parent) == name: return parent
27
        else:
28 46 aaronmk
            for child in xml_dom.NodeElemIter(parent):
29 16 aaronmk
                child_name = name_of(child)
30 13 aaronmk
                if is_ptr(child_name):
31
                    if ptr_type(child_name) == name: return ptr_target(child)
32
                elif child_name == name: return child
33
    return None
34
35 126 aaronmk
def get(db, node, pkeys=None, parent_id=None):
36
    if pkeys == None: pkeys = {}
37
    def pkey(table): return sql.pkey(db, pkeys, table)
38
39
40 48 aaronmk
def put(db, node, store_ids=False, row_ct_ref=None, pkeys=None, parent_id=None):
41 40 aaronmk
    # store_ids enables searching the tree for missing fields
42
    if pkeys == None: pkeys = {}
43 126 aaronmk
    def pkey(table): return sql.pkey(db, pkeys, table)
44 15 aaronmk
45 48 aaronmk
    table = name_of(node)
46
    pkey_ = pkey(table)
47
    row = {}
48
    children = []
49 13 aaronmk
50 48 aaronmk
    # Divide children into fields and children with fkeys to parent
51
    for child in xml_dom.NodeElemIter(node):
52
        child_name = name_of(child)
53 84 aaronmk
        if xml_dom.is_text(child):
54
            row[child_name] = strings.to_unicode(xml_dom.value(child))
55 48 aaronmk
        elif is_ptr(child_name): row[child_name] = put(db, ptr_target(child),
56
            store_ids, row_ct_ref, pkeys)
57
        else: children.append(child)
58
    try: del row[pkey_]
59
    except KeyError: pass
60
61
    # Add fkey to parent
62 59 aaronmk
    if parent_id != None:
63
        parent_ptr = node.getAttribute('fkey')
64
        if parent_ptr == '': parent_ptr = pkey(name_of(node.parentNode))
65
        row[parent_ptr] = parent_id
66 48 aaronmk
67
    # Insert node
68 70 aaronmk
    for try_num in xrange(2):
69 48 aaronmk
        try:
70
            id_ = sql.get(db, table, row, pkey_, True, row_ct_ref)
71
            if store_ids: xml_dom.set_id(node, id_)
72
            break
73
        except sql.NullValueException, ex:
74
            if try_num > 0: raise # exception still raised after retry
75 73 aaronmk
            if is_ptr(ex.col):
76
                # Search for required column in ancestors and their children
77
                target = find_by_name(node, ptr_type(ex.col))
78
                if target == None: raise
79
                row[ex.col] = xml_dom.get_id(target)
80
            else: raise
81 48 aaronmk
82
    # Insert children with fkeys to parent
83
    for child in children: put(db, child, store_ids, row_ct_ref, pkeys, id_)
84
85
    return id_
86 40 aaronmk
87
def xml2db(db, node, row_ct_ref=None):
88 48 aaronmk
    iter_ = xml_dom.NodeElemIter(node)
89
    while xml_dom.is_text(iter_.curr()): iter_.next() # skip metadata
90
    for child in iter_: put(db, child, True, row_ct_ref)