Project

General

Profile

« Previous | Next » 

Revision 461

db_xml.py: put(): Added on_error handler. Reordered arguments according to how often they are likely to be used.

View differences:

lib/db_xml.py
57 57
    
58 58
    return sql.select(db, table, fields, conds, limit)
59 59

  
60
def put(db, node, store_ids=False, row_ct_ref=None, pkeys=None, parent_id=None):
60
def put(db, node, pkeys=None, row_ct_ref=None, on_error=exc.raise_,
61
    store_ids=False, parent_id=None):
61 62
    '''store_ids enables searching the tree for missing fields'''
62 63
    if pkeys == None: pkeys = {}
63 64
    def pkey(table): return sql.pkey(db, pkeys, table)
64 65
    
65
    def on_error(e):
66
    def put_(node, parent_id=None):
67
        return put(db, node, pkeys, row_ct_ref, on_error, store_ids, parent_id)
68
    
69
    def on_error_(e):
66 70
        exc.add_msg(e, 'node:\n'+str(node))
67
        raise
71
        on_error(e)
68 72
    
69 73
    table = name_of(node)
70 74
    try: pkey_ = pkey(table)
71
    except sql.DatabaseErrors, e: on_error(e)
75
    except sql.DatabaseErrors, e: on_error_(e); return None
72 76
    row = {}
73 77
    children = []
74 78
    
......
78 82
        if xml_dom.is_empty(child): pass # ignore empty field
79 83
        elif xml_dom.is_text(child):
80 84
            row[child_name] = strings.to_unicode(xml_dom.value(child))
81
        elif is_ptr(child_name):
82
            row[child_name] = put(db, ptr_target(child), store_ids,
83
                row_ct_ref, pkeys)
85
        elif is_ptr(child_name): row[child_name] = put_(ptr_target(child))
84 86
        else: children.append(child)
85 87
    try: del row[pkey_]
86 88
    except KeyError: pass
......
106 108
                    if target == None: raise
107 109
                    row[e.col] = xml_dom.get_id(target)
108 110
                else: raise
109
    except sql.DatabaseErrors, e: on_error(e)
111
    except sql.DatabaseErrors, e: on_error_(e); return None
110 112
    
111 113
    # Insert children with fkeys to parent
112
    for child in children: put(db, child, store_ids, row_ct_ref, pkeys, id_)
114
    for child in children: put_(child, id_)
113 115
    
114 116
    return id_
bin/map
176 176
                    assert xml_dom.has_one_child(root)
177 177
                    try:
178 178
                        sql.with_savepoint(out_db, lambda: db_xml.put(out_db,
179
                            root.firstChild, False, row_ct_ref, out_pkeys))
179
                            root.firstChild, out_pkeys, row_ct_ref))
180 180
                        if commit: out_db.commit()
181 181
                    except sql.DatabaseErrors, e: on_error(e)
182 182
                root.clear()

Also available in: Unified diff