From 6a9db0c1734a22e792f2204c029810ca1cea7f86 Mon Sep 17 00:00:00 2001 From: Arun Isaac Date: Wed, 26 Oct 2022 14:21:55 +0530 Subject: Add xapian database connection context manager. * gn3/settings.py (XAPIAN_DB_PATH): New variable. * gn3/db_utils.py: Import xapian and XAPIAN_DB_PATH from gn3.settings. (xapian_database): New function. --- gn3/db_utils.py | 14 +++++++++++++- gn3/settings.py | 3 +++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/gn3/db_utils.py b/gn3/db_utils.py index 69e88a5..ead0c5b 100644 --- a/gn3/db_utils.py +++ b/gn3/db_utils.py @@ -3,7 +3,8 @@ import contextlib from typing import Any, Iterator, Protocol, Tuple from urllib.parse import urlparse import MySQLdb as mdb -from gn3.settings import SQL_URI +import xapian +from gn3.settings import SQL_URI, XAPIAN_DB_PATH def parse_db_url() -> Tuple: @@ -42,3 +43,14 @@ def database_connection() -> Iterator[Connection]: yield connection finally: connection.close() + + +@contextlib.contextmanager +def xapian_database(): + """Open xapian database read-only.""" + # pylint: disable-next=invalid-name + db = xapian.Database(XAPIAN_DB_PATH) + try: + yield db + finally: + db.close() diff --git a/gn3/settings.py b/gn3/settings.py index 5bb5f17..86bdd95 100644 --- a/gn3/settings.py +++ b/gn3/settings.py @@ -37,6 +37,9 @@ CORRELATION_COMMAND = f"{os.environ.get('GN2_PROFILE')}/bin/correlation_rust" GENOTYPE_FILES = os.environ.get( "GENOTYPE_FILES", f"{os.environ.get('HOME')}/genotype_files/genotype") +# Xapian index +XAPIAN_DB_PATH = "xapian" + # CROSS-ORIGIN SETUP def parse_env_cors(default): """Parse comma-separated configuration into list of strings.""" -- cgit v1.2.3