Skip to content

Commit ba9cbdc

Browse files
committed
Fix Python 2/3 compatibility issues using six.
1 parent 1ebfa46 commit ba9cbdc

File tree

3 files changed

+28
-18
lines changed

3 files changed

+28
-18
lines changed

load_into_pg.py

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import argparse
55
import psycopg2 as pg
66
import row_processor as Processor
7+
import six
78

89
# Special rules needed for certain tables (esp. for old database dumps)
910
specialRules = {
@@ -45,7 +46,7 @@ def handleTable(table, keys, dbname, mbDbFile, mbHost, mbPort, mbUsername, mbPas
4546
pre = open('./sql/' + table + '_pre.sql').read()
4647
post = open('./sql/' + table + '_post.sql').read()
4748
except IOError as e:
48-
print >> sys.stderr, "Could not load pre/post sql. Are you running from the correct path?"
49+
six.print_("Could not load pre/post sql. Are you running from the correct path?", file=sys.stderr)
4950
sys.exit(-1)
5051

5152
dbConnectionParam = "dbname={}".format(dbname)
@@ -68,20 +69,20 @@ def handleTable(table, keys, dbname, mbDbFile, mbHost, mbPort, mbUsername, mbPas
6869
with pg.connect(dbConnectionParam) as conn:
6970
with conn.cursor() as cur:
7071
try:
71-
with open(dbFile) as xml:
72+
with open(dbFile, 'rb') as xml:
7273
# Pre-processing (dropping/creation of tables)
73-
print ('Pre-processing ...')
74+
six.print_('Pre-processing ...')
7475
if pre != '':
7576
cur.execute(pre)
7677
conn.commit()
77-
print ('Pre-processing took {:.1f} seconds'.format(time.time() - start_time))
78+
six.print_('Pre-processing took {:.1f} seconds'.format(time.time() - start_time))
7879

7980
# Handle content of the table
8081
start_time = time.time()
81-
print ('Processing data ...')
82+
six.print_('Processing data ...')
8283
for rows in Processor.batch(Processor.parse(xml), 500):
8384
valuesStr = ',\n'.join(
84-
[ _createCmdTuple(cur, keys, tmpl, row_attribs)
85+
[ _createCmdTuple(cur, keys, tmpl, row_attribs).decode('utf-8')
8586
for row_attribs in rows
8687
]
8788
)
@@ -91,26 +92,26 @@ def handleTable(table, keys, dbname, mbDbFile, mbHost, mbPort, mbUsername, mbPas
9192
' VALUES\n' + valuesStr + ';'
9293
cur.execute(cmd)
9394
conn.commit()
94-
print ('Table processing took {:.1f} seconds'.format(time.time() - start_time))
95+
six.print_('Table processing took {:.1f} seconds'.format(time.time() - start_time))
9596

9697
# Post-processing (creation of indexes)
9798
start_time = time.time()
98-
print ('Post processing ...')
99+
six.print_('Post processing ...')
99100
if post != '':
100101
cur.execute(post)
101102
conn.commit()
102-
print ('Post processing took {} seconds'.format(time.time() - start_time))
103+
six.print_('Post processing took {} seconds'.format(time.time() - start_time))
103104

104105
except IOError as e:
105-
print ("Could not read from file {}.".format(dbFile), file=sys.stderr)
106-
print ("IOError: {0}".format(e.strerror), file=sys.stderr)
106+
six.print_("Could not read from file {}.".format(dbFile), file=sys.stderr)
107+
six.print_("IOError: {0}".format(e.strerror), file=sys.stderr)
107108
except pg.Error as e:
108-
print ("Error in dealing with the database.", file=sys.stderr)
109-
print ("pg.Error ({0}): {1}".format(e.pgcode, e.pgerror), file=sys.stderr)
110-
print (str(e), file=sys.stderr)
109+
six.print_("Error in dealing with the database.", file=sys.stderr)
110+
six.print_("pg.Error ({0}): {1}".format(e.pgcode, e.pgerror), file=sys.stderr)
111+
six.print_(str(e), file=sys.stderr)
111112
except pg.Warning as w:
112-
print ("Warning from the database.", file=sys.stderr)
113-
print ("pg.Warning: {0}".format(str(w)), file=sys.stderr)
113+
six.print_("Warning from the database.", file=sys.stderr)
114+
six.print_("pg.Warning: {0}".format(str(w)), file=sys.stderr)
114115

115116

116117

@@ -268,10 +269,17 @@ def handleTable(table, keys, dbname, mbDbFile, mbHost, mbPort, mbUsername, mbPas
268269
'CreationDate',
269270
'UserId',
270271
]
272+
273+
try:
274+
# Python 2/3 compatibility
275+
input = raw_input
276+
except NameError:
277+
pass
278+
271279
choice = input('This will drop the {} table. Are you sure [y/n]?'.format(table))
272280

273281
if len(choice) > 0 and choice[0].lower() == 'y':
274282
handleTable(table, keys, args.dbname, args.file, args.host, args.port, args.username, args.password)
275283
else:
276-
print ("Cancelled.")
284+
six.print_("Cancelled.")
277285

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ distribute==0.6.24
33
lxml==3.4.1
44
psycopg2==2.5.4
55
wsgiref==0.1.2
6+
six==1.10.0

row_processor.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from lxml import etree
22
from itertools import islice, chain
3+
import six
34

45
# Efficient parsing of large XML files from
56
# http://stackoverflow.com/a/9814580/987185
@@ -31,5 +32,5 @@ def batch(iterable, size):
3132
sourceiter = iter(iterable)
3233
while True:
3334
batchiter = islice(sourceiter, size)
34-
yield chain([batchiter.next()], batchiter)
35+
yield chain([six.next(batchiter)], batchiter)
3536

0 commit comments

Comments
 (0)