Skip to content

Commit 269fbf0

Browse files
committed
🐛 Give a useful error message when input is empty
1 parent a812a89 commit 269fbf0

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

src/mods4pandas/alto4pandas.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import csv
44
import os
55
import sqlite3
6+
import sys
67
import warnings
78
from operator import attrgetter
89
from typing import List
@@ -19,6 +20,7 @@
1920
insert_into_db,
2021
ns,
2122
sorted_groupby,
23+
sqlite3_table_exists,
2224
)
2325

2426

@@ -239,6 +241,11 @@ def process(alto_files: List[str], output_file: str):
239241

240242
traceback.print_exc()
241243

244+
# Check if table exists
245+
if not sqlite3_table_exists(con, "alto_info"):
246+
logger.error("Table alto_info does not exist, empty input?")
247+
sys.exit(1)
248+
242249
# Convert the alto_info SQL to a pandas DataFrame
243250
logger.info("Writing DataFrame to {}".format(output_file))
244251
convert_db_to_parquet(con, "alto_info", "alto_file", output_file)

src/mods4pandas/lib.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,12 @@ def convert_db_to_parquet(con, table, index_col, output_file):
469469
df.to_parquet(output_file)
470470

471471

472+
def sqlite3_table_exists(con, table):
473+
"""Check if table exists."""
474+
cur = con.execute("SELECT 1 FROM pragma_table_info(?) LIMIT 1", (table,))
475+
return cur.fetchone() is not None
476+
477+
472478
def sqlite3_column_exists(con, table, col):
473479
"""Check if column col exists in table."""
474480
cur = con.execute(

src/mods4pandas/mods4pandas.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import csv
44
import os
55
import sqlite3
6+
import sys
67
import warnings
78
from operator import attrgetter
89
from typing import Dict, List
@@ -21,6 +22,7 @@
2122
ns,
2223
sorted_groupby,
2324
sqlite3_column_exists,
25+
sqlite3_table_exists,
2426
)
2527

2628

@@ -653,6 +655,11 @@ def process(
653655
except Exception:
654656
logger.exception("Exception in {}".format(mets_file))
655657

658+
# Check if table exists
659+
if not sqlite3_table_exists(con, "mods_info"):
660+
logger.error("Table mods_info does not exist, empty input?")
661+
sys.exit(1)
662+
656663
logger.info("Writing DataFrame to {}".format(output_file))
657664

658665
considered_indexes = ("recordInfo_recordIdentifier", "recordIdentifier-zdb")

0 commit comments

Comments
 (0)