5 |
5 |
|
6 |
6 |
import os.path
|
7 |
7 |
import sys
|
|
8 |
import traceback
|
8 |
9 |
import xml.dom.minidom
|
9 |
10 |
|
10 |
11 |
sys.path.append(os.path.dirname(__file__)+"/../lib")
|
... | ... | |
71 |
72 |
stream.close()
|
72 |
73 |
in_is_xml = in_is_xpaths and not in_is_db
|
73 |
74 |
|
74 |
|
# Input datasource to XML tree, mapping if needed
|
75 |
|
if in_is_xml:
|
76 |
|
doc0 = xml.dom.minidom.parse(sys.stdin)
|
77 |
|
if map_path != None:
|
|
75 |
if in_is_xml: doc0 = xml.dom.minidom.parse(sys.stdin)
|
|
76 |
|
|
77 |
def process_xml(use_row):
|
|
78 |
'''Maps datasource to XML tree'''
|
78 |
79 |
doc1 = xml_dom.create_doc(out_label)
|
79 |
80 |
root = doc1.documentElement
|
80 |
81 |
if in_is_db:
|
... | ... | |
102 |
103 |
value = sql.value_or_none(db_xml.get(in_db, in_,
|
103 |
104 |
in_pkeys))
|
104 |
105 |
if value != None:
|
105 |
|
xpath.put_obj(root, out, row_id, has_types, str(value))
|
|
106 |
try: xpath.put_obj(root, out, row_id, has_types,
|
|
107 |
str(value))
|
|
108 |
except Exception: traceback.print_exc()
|
106 |
109 |
in_db.close()
|
107 |
110 |
elif in_is_xml:
|
108 |
111 |
row = xpath.get(doc0.documentElement, in_root)
|
... | ... | |
115 |
118 |
node = xpath.get(row, in_)
|
116 |
119 |
if node != None: value = xml_dom.value(node)
|
117 |
120 |
if value != None:
|
118 |
|
xpath.put_obj(root, out, row_id, has_types, value)
|
|
121 |
try: xpath.put_obj(root, out, row_id, has_types, value)
|
|
122 |
except Exception: traceback.print_exc()
|
119 |
123 |
else: # input is CSV
|
120 |
124 |
map_ = dict(mappings)
|
121 |
125 |
reader = csv.reader(sys.stdin)
|
... | ... | |
136 |
140 |
value = row[in_]
|
137 |
141 |
if value == '': value = None
|
138 |
142 |
if value != None:
|
139 |
|
xpath.put_obj(root, out, row_id, has_types, value)
|
|
143 |
try: xpath.put_obj(root, out, row_id, has_types, value)
|
|
144 |
except Exception: traceback.print_exc()
|
140 |
145 |
xml_func.process(root)
|
141 |
|
else: doc1 = doc0
|
|
146 |
return doc1
|
142 |
147 |
|
|
148 |
def get_xml(use_row):
|
|
149 |
'''Inputs datasource to XML tree, mapping if needed'''
|
|
150 |
if map_path != None: return process_xml(use_row)
|
|
151 |
else: return doc0
|
|
152 |
|
143 |
153 |
# Output XML tree
|
144 |
154 |
if out_is_db:
|
145 |
155 |
from psycopg2.extensions import ISOLATION_LEVEL_SERIALIZABLE
|
... | ... | |
149 |
159 |
out_db.set_isolation_level(ISOLATION_LEVEL_SERIALIZABLE)
|
150 |
160 |
try:
|
151 |
161 |
row_ct_ref = [0]
|
152 |
|
db_xml.xml2db(out_db, doc1.documentElement, row_ct_ref)
|
|
162 |
def use_row(root): db_xml.xml2db(out_db, root, row_ct_ref)
|
|
163 |
db_xml.xml2db(out_db, get_xml(use_row).documentElement, row_ct_ref)
|
153 |
164 |
print 'Inserted '+str(row_ct_ref[0])+' rows'
|
154 |
165 |
if commit: out_db.commit()
|
155 |
166 |
finally:
|
156 |
167 |
out_db.rollback()
|
157 |
168 |
out_db.close()
|
158 |
|
else: xml_dom.writexml(sys.stdout, doc1) # output is XML
|
|
169 |
else:
|
|
170 |
def use_row(root): pass # TODO: implement this
|
|
171 |
xml_dom.writexml(sys.stdout, get_xml(use_row)) # output is XML
|
159 |
172 |
|
160 |
173 |
try: main()
|
161 |
|
except (Parser.SyntaxException, xml_func.SyntaxException), e:
|
162 |
|
raise SystemExit(str(e))
|
|
174 |
except Parser.SyntaxException, e: raise SystemExit(str(e))
|
Use traceback.print_exc() to print Exceptions for each row instead of exiting