mirror of
https://github.com/echemdata/galvani.git
synced 2025-12-14 09:15:34 +00:00
Remove compability code for Python 2 subprocess module
This commit is contained in:
@@ -371,27 +371,28 @@ def mdb_get_data_text(s3db, filename, table):
|
|||||||
r'INSERT INTO "\w+" \([^)]+?\) VALUES \(("[^"]*"|[^")])+?\);\n',
|
r'INSERT INTO "\w+" \([^)]+?\) VALUES \(("[^"]*"|[^")])+?\);\n',
|
||||||
re.IGNORECASE
|
re.IGNORECASE
|
||||||
)
|
)
|
||||||
# TODO after dropping Python 2 support - use Popen as contextmanager
|
|
||||||
try:
|
try:
|
||||||
mdb_sql = sp.Popen(['mdb-export', '-I', 'postgres', filename, table],
|
# Initialize values to avoid NameError in except clause
|
||||||
bufsize=-1, stdin=None, stdout=sp.PIPE,
|
mdb_output = ''
|
||||||
universal_newlines=True)
|
insert_match = None
|
||||||
|
with sp.Popen(['mdb-export', '-I', 'postgres', filename, table],
|
||||||
|
bufsize=-1, stdin=sp.DEVNULL, stdout=sp.PIPE,
|
||||||
|
universal_newlines=True) as mdb_sql:
|
||||||
|
|
||||||
|
mdb_output = mdb_sql.stdout.read()
|
||||||
|
while len(mdb_output) > 0:
|
||||||
|
insert_match = insert_pattern.match(mdb_output)
|
||||||
|
s3db.execute(insert_match.group())
|
||||||
|
mdb_output = mdb_output[insert_match.end():]
|
||||||
|
mdb_output += mdb_sql.stdout.read()
|
||||||
|
s3db.commit()
|
||||||
|
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
if e.errno == 2:
|
if e.errno == 2:
|
||||||
raise RuntimeError('Could not locate the `mdb-export` executable. '
|
raise RuntimeError('Could not locate the `mdb-export` executable. '
|
||||||
'Check that mdbtools is properly installed.')
|
'Check that mdbtools is properly installed.')
|
||||||
else:
|
else:
|
||||||
raise
|
raise
|
||||||
try:
|
|
||||||
# Initialize values to avoid NameError in except clause
|
|
||||||
mdb_output = ''
|
|
||||||
insert_match = None
|
|
||||||
mdb_output = mdb_sql.stdout.read()
|
|
||||||
while len(mdb_output) > 0:
|
|
||||||
insert_match = insert_pattern.match(mdb_output)
|
|
||||||
s3db.execute(insert_match.group())
|
|
||||||
mdb_output = mdb_output[insert_match.end():]
|
|
||||||
s3db.commit()
|
|
||||||
except BaseException:
|
except BaseException:
|
||||||
print("Error while importing %s" % table)
|
print("Error while importing %s" % table)
|
||||||
if mdb_output:
|
if mdb_output:
|
||||||
@@ -399,38 +400,32 @@ def mdb_get_data_text(s3db, filename, table):
|
|||||||
if insert_match:
|
if insert_match:
|
||||||
print("insert_re match:", insert_match)
|
print("insert_re match:", insert_match)
|
||||||
raise
|
raise
|
||||||
finally:
|
|
||||||
mdb_sql.terminate()
|
|
||||||
|
|
||||||
|
|
||||||
def mdb_get_data_numeric(s3db, filename, table):
|
def mdb_get_data_numeric(s3db, filename, table):
|
||||||
print("Reading %s..." % table)
|
print("Reading %s..." % table)
|
||||||
# TODO after dropping Python 2 support - use Popen as contextmanager
|
|
||||||
try:
|
try:
|
||||||
mdb_sql = sp.Popen(['mdb-export', filename, table],
|
with sp.Popen(['mdb-export', filename, table],
|
||||||
bufsize=-1, stdin=None, stdout=sp.PIPE,
|
bufsize=-1, stdin=sp.DEVNULL, stdout=sp.PIPE,
|
||||||
universal_newlines=True)
|
universal_newlines=True) as mdb_sql:
|
||||||
|
mdb_csv = csv.reader(mdb_sql.stdout)
|
||||||
|
mdb_headers = next(mdb_csv)
|
||||||
|
quoted_headers = ['"%s"' % h for h in mdb_headers]
|
||||||
|
joined_headers = ', '.join(quoted_headers)
|
||||||
|
joined_placemarks = ', '.join(['?' for h in mdb_headers])
|
||||||
|
insert_stmt = 'INSERT INTO "{0}" ({1}) VALUES ({2});'.format(
|
||||||
|
table,
|
||||||
|
joined_headers,
|
||||||
|
joined_placemarks,
|
||||||
|
)
|
||||||
|
s3db.executemany(insert_stmt, mdb_csv)
|
||||||
|
s3db.commit()
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
if e.errno == 2:
|
if e.errno == 2:
|
||||||
raise RuntimeError('Could not locate the `mdb-export` executable. '
|
raise RuntimeError('Could not locate the `mdb-export` executable. '
|
||||||
'Check that mdbtools is properly installed.')
|
'Check that mdbtools is properly installed.')
|
||||||
else:
|
else:
|
||||||
raise
|
raise
|
||||||
try:
|
|
||||||
mdb_csv = csv.reader(mdb_sql.stdout)
|
|
||||||
mdb_headers = next(mdb_csv)
|
|
||||||
quoted_headers = ['"%s"' % h for h in mdb_headers]
|
|
||||||
joined_headers = ', '.join(quoted_headers)
|
|
||||||
joined_placemarks = ', '.join(['?' for h in mdb_headers])
|
|
||||||
insert_stmt = 'INSERT INTO "{0}" ({1}) VALUES ({2});'.format(
|
|
||||||
table,
|
|
||||||
joined_headers,
|
|
||||||
joined_placemarks,
|
|
||||||
)
|
|
||||||
s3db.executemany(insert_stmt, mdb_csv)
|
|
||||||
s3db.commit()
|
|
||||||
finally:
|
|
||||||
mdb_sql.terminate()
|
|
||||||
|
|
||||||
|
|
||||||
def mdb_get_data(s3db, filename, table):
|
def mdb_get_data(s3db, filename, table):
|
||||||
|
|||||||
@@ -9,8 +9,8 @@ import pytest
|
|||||||
from galvani import res2sqlite
|
from galvani import res2sqlite
|
||||||
|
|
||||||
|
|
||||||
# TODO - change to subprocess.DEVNULL when python 2 support is removed
|
have_mdbtools = (subprocess.call(['which', 'mdb-export'],
|
||||||
have_mdbtools = (subprocess.call(['which', 'mdb-export'], stdout=None) == 0)
|
stdout=subprocess.DEVNULL) == 0)
|
||||||
|
|
||||||
|
|
||||||
def test_res2sqlite_help():
|
def test_res2sqlite_help():
|
||||||
|
|||||||
Reference in New Issue
Block a user