Started to produce top-down code for JMdict SQLite3 database.
[jblite.git] / jblite / helpers.py
blobe462b0d79fd28c9e2c48c40b1199e1aaf3fba362
1 import sqlite3, gzip
4 def with_db(db_fname, fn, *args, **kwargs):
5 """Wrapper for a full self-contained SQLite database transaction."""
6 conn = sqlite3.connect(db_fname)
7 cur = conn.cursor()
8 try:
9 fn(cur, *args, **kwargs)
10 conn.commit()
11 finally:
12 cur.close()
13 conn.close()
16 def gzread(fname):
17 try:
18 infile = gzip.open(fname)
19 data = infile.read()
20 infile.close()
21 except IOError, e:
22 if e.args[0] == "Not a gzipped file":
23 with open(fname) as infile:
24 data = infile.read()
25 else:
26 raise e
27 return data