#!/usr/bin/python3

"""
Export data that is entered directly into Dougal
as opposed to being read from external sources.

This data will be read back in when the database
is recreated for an existing survey.
"""

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"]["machine"]["path"]
			except KeyError:
				print("Survey does not define an export path for machine data")
				continue

			for table in exportables:
				path = os.path.join(pathPrefix, table)
				print(" → ", path, " ← ", table)
				with open(path, "wb") as fd:
					cursor.copy_to(fd, table);

	print("Done")
