added storage module

This commit is contained in:
d3m1g0d
2019-03-13 19:01:21 +01:00
parent e3bbab819b
commit 253ec3a28e

View File

@@ -2,24 +2,30 @@
# Copyright Kristjan Komloši 2019 # Copyright Kristjan Komloši 2019
# This code is licensed under the 3-clause BSD license # This code is licensed under the 3-clause BSD license
import file
import json
import pandas as pd
from pathlib import Path
class measurementStorage(): import sqlite3
def __init__(self, directory): class jsonStorage:
self.storagePath = Path(directory) def __init__(self, dbFile):
if not self.storagePath.exists(): '''Storage object constructor. Argument is filename'''
raise Exception('Storage directory does not exist') self.db = sqlite3.connect(dbFile)
def storeTemp(self, jsonObject): def listJSONs(self):
with self.storagepath / 'temp.thz' as tempfile: c = self.db.cursor()
json.dump(jsonObject, tempfile) c.execute('SELECT * FROM storage')
result = c.fetchall()
c.close()
return result
def loadTemp(self): def storeJSON(self, jsonString, comment):
with self.storagePath / 'temp.thz' as tempfile: c = self.db.cursor()
return json.load(tempfile) c.execute('INSERT INTO storage VALUES (datetime(\'now\', \'localtime\'), ?, ?)', (comment, jsonString))
c.close()
self.db.commit()
def tempToFile(self): def retrieveJSON(self, timestamp):
self '''Retrieves a JSON entry. Takes a timestamp string'''
c = self.db.cursor()
c.execute('SELECT * FROM storage WHERE timestamp = ?', (timestamp,))
result = c.fetchall()
c.close()
return result