aboutsummaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2024-10-29 14:12:01 -0500
committerFrederick Muriuki Muriithi2024-10-29 14:12:01 -0500
commitbfbefb6594c0b063ce169aa5f14dc9bc341c97e9 (patch)
tree48b89adcf458f0534fdb292a834e7a178023d1ce /scripts
parent37282ca8cdd10c82a4d84b3fb9a70777d0166914 (diff)
downloadgenenetwork2-bfbefb6594c0b063ce169aa5f14dc9bc341c97e9.tar.gz
Read connection detail from file. Use contextmanager.
Read connection details from file to make it easier to change host(s) in use. Use contextmanager to manage the connections.
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/maintenance/QTL_Reaper_v6.py29
1 files changed, 28 insertions, 1 deletions
diff --git a/scripts/maintenance/QTL_Reaper_v6.py b/scripts/maintenance/QTL_Reaper_v6.py
index 3f202285..477af973 100755
--- a/scripts/maintenance/QTL_Reaper_v6.py
+++ b/scripts/maintenance/QTL_Reaper_v6.py
@@ -3,6 +3,7 @@
#!/usr/bin/python
import reaper
import MySQLdb
+import contextlib
from argparse import ArgumentParser
@@ -100,7 +101,31 @@ def run_qtl_reaper(cursor, genotypeDir, ProbeSetFreezeIds):
print(ProbeSetFreezeIds)
- cursor.close()
+
+@contextlib.contextmanager
+def dbconnection(db, user, passwd, host, port):
+ """Connect to the database in a contextmanager."""
+ conn = MySQLdb.Connect(db=db, user=user, passwd=passwd, host=host, port=port)
+ try:
+ yield conn
+ except MySQLdb.Error as _err:
+ conn.rollback()
+ finally:
+ conn.commit()
+ conn.close()
+
+
+def read_db_config(path):
+ with open(path, "r") as infile:
+ lines = tuple(line.strip() for line in infile.readlines())
+ return {
+ "user": lines[0],
+ "passwd": lines[1],
+ "host": lines[2],
+ "port": int(lines[3] or "3306"),
+ "db": lines[4]
+ }
+
def main():
parser = ArgumentParser(prog="QTLReaper", description="Python-2 QTLReaper.")
@@ -116,6 +141,8 @@ def main():
nargs="*",
help="The ProbeSetFreezeIds to act on.")
args = parser.parse_args()
+ with dbconnection(**read_db_config(args.db_config_file)) as conn:
+ run_qtl_reaper(conn.cursor(), args.genotype_dir, args.ProbeSetFreezeIds)
if __name__ == "__main__":
main()