Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optionally read Arbin into in-memory sqlite without temporary file #105

Merged
merged 2 commits into from
Mar 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions galvani/res2sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,13 +571,25 @@ def mdb_get_version(filename):
return version_tuple


def convert_arbin_to_sqlite(input_file, output_file):
def convert_arbin_to_sqlite(input_file, output_file=None):
"""Read data from an Arbin .res data file and write to a sqlite file.

Any data currently in the sqlite file will be erased!
Any data currently in an sqlite file at `output_file` will be erased!

Parameters:
input_file (str): The path to the Arbin .res file to read from.
output_file (str or None): The path to the sqlite file to write to; if None,
return a `sqlite3.Connection` into an in-memory database.

Returns:
None or sqlite3.Connection

"""
arbin_version = mdb_get_version(input_file)

if output_file is None:
output_file = ":memory:"

s3db = sqlite3.connect(output_file)

tables_to_convert = copy(mdb_tables)
Expand All @@ -602,6 +614,11 @@ def convert_arbin_to_sqlite(input_file, output_file):
print("Vacuuming database...")
s3db.executescript("VACUUM; ANALYZE;")

if output_file == ":memory:":
return s3db

s3db.close()


def main(argv=None):
parser = argparse.ArgumentParser(
Expand Down
10 changes: 10 additions & 0 deletions tests/test_Arbin.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ def test_convert_Arbin_to_sqlite_function(testdata_dir, tmpdir, basename):
csr.fetchone()


@pytest.mark.parametrize("basename", ["arbin1", "UM34_Test005E"])
def test_convert_Arbin_to_sqlite_function_in_memory(testdata_dir, tmpdir, basename):
"""Convert an Arbin file to an in-memory SQLite database."""
res_file = os.path.join(testdata_dir, basename + ".res")
conn = None
with res2sqlite.convert_arbin_to_sqlite(res_file) as conn:
csr = conn.execute("SELECT * FROM Channel_Normal_Table;")
csr.fetchone()


@pytest.mark.skipif(
not have_mdbtools, reason="Reading the Arbin file requires MDBTools"
)
Expand Down
Loading