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