From a8830c9a057e06e1952bcf3b6b2cc248468a7914 Mon Sep 17 00:00:00 2001 From: "D. Berge" Date: Wed, 2 Sep 2020 15:04:04 +0200 Subject: [PATCH] Add system data import module --- bin/system_imports.py | 47 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100755 bin/system_imports.py diff --git a/bin/system_imports.py b/bin/system_imports.py new file mode 100755 index 0000000..58e3fba --- /dev/null +++ b/bin/system_imports.py @@ -0,0 +1,47 @@ +#!/usr/bin/python3 + +""" +Re-import Dougal-exported data created by +system_exports.py +""" + +import os +from glob import glob +import configuration +import preplots +from datastore import Datastore + +exportables = [ + "events_seq", + "events_seq_labels", + "events_timed", + "events_timed_labels" +] + +if __name__ == '__main__': + + print("Reading configuration") + surveys = configuration.surveys() + + print("Connecting to database") + db = Datastore() + + print("Reading surveys") + for survey in surveys: + print(f'Survey: {survey["id"]} ({survey["schema"]})') + db.set_survey(survey["schema"]) + with db.conn.cursor() as cursor: + + try: + pathPrefix = survey["exports"]["path"] + except ValueError: + print("Survey does not define an export path") + continue + + for table in exportables: + path = os.path.join(pathPrefix, table) + print(" ← ", path, " → ", table) + with open(path, "rb") as fd: + cursor.copy_from(fd, table); + + print("Done")