Project

General

Profile

1
# XML-database conversion
2

    
3
import re
4
from xml.dom import Node
5

    
6
import sql
7
import strings
8
import xml_dom
9

    
10
def name_of(node): return re.sub(r'^.*\.', r'', xml_dom.name_of(node))
11

    
12
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
    return xml_dom.first_elem(node)
23

    
24
def find_by_name(node, name):
25
    for parent in xml_dom.NodeParentIter(node):
26
        if name_of(parent) == name: return parent
27
        else:
28
            for child in xml_dom.NodeElemIter(parent):
29
                child_name = name_of(child)
30
                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
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
def put(db, node, store_ids=False, row_ct_ref=None, pkeys=None, parent_id=None):
41
    # store_ids enables searching the tree for missing fields
42
    if pkeys == None: pkeys = {}
43
    def pkey(table): return sql.pkey(db, pkeys, table)
44
    
45
    table = name_of(node)
46
    pkey_ = pkey(table)
47
    row = {}
48
    children = []
49
    
50
    # 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
        if xml_dom.is_text(child):
54
            row[child_name] = strings.to_unicode(xml_dom.value(child))
55
        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
    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
    
67
    # Insert node
68
    for try_num in xrange(2):
69
        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
            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
    
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

    
87
def xml2db(db, node, row_ct_ref=None):
88
    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)
(2-2/10)