Project

General

Profile

1
#!/usr/bin/env python
2
# Imports an XML file into a PostgreSQL database
3
# Format: see http://vegbank.org/vegdocs/xml/vegbank_example_ver1.0.2.xml
4

    
5
import os
6
import os.path
7
import psycopg2
8
from psycopg2.extensions import ISOLATION_LEVEL_SERIALIZABLE
9
import sys
10
import xml.dom.minidom
11

    
12
sys.path.append(os.path.dirname(__file__)+"/lib")
13
import db_xml
14

    
15
def env_flag(name): return name in os.environ and os.environ[name] != ''
16

    
17
def main():
18
    prog_name = sys.argv.pop(0)
19
    try:
20
        db_config = {}
21
        for name in ['host', 'user', 'password', 'database']:
22
            if os.environ[name] != '': db_config[name] = os.environ[name]
23
    except KeyError: raise Exception('Usage: env host=... user=... password=...'
24
        ' database=... [commit=1] '+prog_name+' <dataset')
25
    commit = env_flag('commit')
26
    
27
    # Process dataset
28
    db = psycopg2.connect(**db_config)
29
    db.set_isolation_level(ISOLATION_LEVEL_SERIALIZABLE)
30
    try:
31
        doc = xml.dom.minidom.parse(sys.stdin)
32
        row_ct_ref = [0]
33
        db_xml.xml2db(db, doc.documentElement, row_ct_ref)
34
        print 'Inserted '+str(row_ct_ref[0])+' rows'
35
        if commit: db.commit()
36
    finally:
37
        db.rollback()
38
        db.close()
39

    
40
main()
(4-4/5)