1 |
13
|
aaronmk
|
# XML-database conversion
|
2 |
|
|
|
3 |
16
|
aaronmk
|
import re
|
4 |
13
|
aaronmk
|
from xml.dom import Node
|
5 |
|
|
|
6 |
|
|
import db_util
|
7 |
|
|
import xml_util
|
8 |
|
|
|
9 |
16
|
aaronmk
|
def name_of(node): return re.sub(r'^.*\.', r'', xml_util.name_of(node))
|
10 |
|
|
|
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 |
|
|
return xml_util.first_elem(node)
|
22 |
|
|
|
23 |
|
|
def find_by_name(node, name):
|
24 |
|
|
for parent in xml_util.NodeParentIter(node):
|
25 |
|
|
if name_of(parent) == name: return parent
|
26 |
|
|
else:
|
27 |
|
|
for child in xml_util.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 |
40
|
aaronmk
|
def get(db, node, create=False, store_ids=False, row_ct_ref=None, pkeys=None):
|
35 |
|
|
# store_ids enables searching the tree for missing fields
|
36 |
|
|
if pkeys == None: pkeys = {}
|
37 |
15
|
aaronmk
|
def pkey(table):
|
38 |
|
|
if table not in pkeys: pkeys[table] = db_util.pkey(db, table)
|
39 |
|
|
return pkeys[table]
|
40 |
|
|
|
41 |
39
|
aaronmk
|
def obj(node, parent_id=None):
|
42 |
13
|
aaronmk
|
table = name_of(node)
|
43 |
15
|
aaronmk
|
pkey_ = pkey(table)
|
44 |
13
|
aaronmk
|
row = {}
|
45 |
|
|
children = []
|
46 |
|
|
|
47 |
|
|
# Divide children into fields and children with fkeys to parent
|
48 |
16
|
aaronmk
|
for child in xml_util.NodeElemIter(node):
|
49 |
|
|
child_name = name_of(child)
|
50 |
|
|
if xml_util.is_text(child): row[child_name] = xml_util.value(child)
|
51 |
39
|
aaronmk
|
elif is_ptr(child_name): row[child_name] = obj(ptr_target(child))
|
52 |
13
|
aaronmk
|
else: children.append(child)
|
53 |
16
|
aaronmk
|
try: del row[pkey_]
|
54 |
|
|
except KeyError: pass
|
55 |
13
|
aaronmk
|
|
56 |
|
|
# Add fkey to parent
|
57 |
39
|
aaronmk
|
if parent_id != None: row[pkey(name_of(node.parentNode))] = parent_id
|
58 |
13
|
aaronmk
|
|
59 |
|
|
# Insert node
|
60 |
|
|
for try_num in range(2):
|
61 |
|
|
try:
|
62 |
40
|
aaronmk
|
id_ = db_util.get(db, table, row, pkey_, create, row_ct_ref)
|
63 |
|
|
if store_ids: xml_util.set_id(node, id_)
|
64 |
13
|
aaronmk
|
break
|
65 |
|
|
except db_util.NullValueException, ex:
|
66 |
|
|
if try_num > 0: raise # exception still raised after retry
|
67 |
14
|
aaronmk
|
# Search for required column in ancestors and their children
|
68 |
13
|
aaronmk
|
target = find_by_name(node, ptr_type(ex.col))
|
69 |
|
|
if target == None: raise
|
70 |
|
|
row[ex.col] = xml_util.get_id(target)
|
71 |
|
|
|
72 |
|
|
# Insert children with fkeys to parent
|
73 |
39
|
aaronmk
|
for child in children: obj(child, id_)
|
74 |
|
|
|
75 |
|
|
return id_
|
76 |
13
|
aaronmk
|
|
77 |
41
|
aaronmk
|
return obj(node)
|
78 |
40
|
aaronmk
|
|
79 |
|
|
def xml2db(db, node, row_ct_ref=None):
|
80 |
41
|
aaronmk
|
for child in xml_util.NodeElemIter(node):
|
81 |
|
|
if not xml_util.is_text(child): # not XML metadata
|
82 |
|
|
get(db, child, True, True, row_ct_ref)
|