From 61347769a2d2f2296e605518b053dea3189ea24e Mon Sep 17 00:00:00 2001 From: Hugh Greenberg Date: Thu, 1 Aug 2024 10:06:27 -0600 Subject: [PATCH] Added function and a test for importing a json file --- dsi/backends/sqlite.py | 51 ++++++++++++++++++++++++++++++- dsi/backends/tests/test_sqlite.py | 13 ++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/dsi/backends/sqlite.py b/dsi/backends/sqlite.py index 7e5d86a9..0cae3444 100644 --- a/dsi/backends/sqlite.py +++ b/dsi/backends/sqlite.py @@ -1,5 +1,7 @@ import csv import sqlite3 +import json +import re from dsi.backends.filesystem import Filesystem @@ -9,6 +11,7 @@ STRING = "VARCHAR" FLOAT = "FLOAT" INT = "INT" +JSON = "TEXT" # Holds table name and data properties @@ -83,7 +86,6 @@ def put_artifact_type(self, types, isVerbose=False): if isVerbose: print(str_query) - print(str_query) self.cur.execute(str_query) self.con.commit() @@ -219,6 +221,53 @@ def put_artifacts_lgcy(self,artifacts, isVerbose=False): self.cur.execute(str_query) self.con.commit() + def put_artifacts_json(self, fname, tname, isVerbose=False): + """ + Function for insertion of Artifact metadata into a defined schema by using a JSON file + `fname`: filepath to the .json file to be read and inserted into the database + + `tname`: String name of the table to be inserted + + `return`: none + """ + + json_str = None + try: + j = open(fname) + data = json.load(j) + json_str = json.dumps(data) + json_str = "'" + json_str + "'" + j.close() + except IOError as i: + print(i) + return + except ValueError as v: + print(v) + return + + types = DataType() + types.properties = {} + types.name = tname + + # Check if this has been defined from helper function + if self.types != None: + types.name = self.types.name + + col_name = re.sub(r'.json', '', fname) + col_name = re.sub(r'.*/', '', col_name) + col_name = "'" + col_name + "'" + types.properties[col_name] = JSON + + self.put_artifact_type(types) + col_names = ', '.join(types.properties.keys()) + str_query = "INSERT INTO {} ({}) VALUES ({});".format(str(types.name), col_names, json_str) + if isVerbose: + print(str_query) + + self.types = types + self.cur.execute(str_query) + self.con.commit() + # Adds columns and rows automaticallly based on a csv file #[NOTE 3] This method should be deprecated in favor of put_artifacts. def put_artifacts_csv(self, fname, tname, isVerbose=False): diff --git a/dsi/backends/tests/test_sqlite.py b/dsi/backends/tests/test_sqlite.py index dbe02553..54ecc0dd 100644 --- a/dsi/backends/tests/test_sqlite.py +++ b/dsi/backends/tests/test_sqlite.py @@ -46,6 +46,16 @@ def test_wildfiredata_artifact_put_t(): # No error implies success assert True +#Data from: https://microsoftedge.github.io/Demos/json-dummy-data/64KB.json +def test_jsondata_artifact_put(): + jsonpath = '/'.join([get_git_root('.'), 'dsi/data/64KB.json']) + dbpath = "jsondata.db" + store = Sqlite(dbpath) + store.put_artifacts_json(jsonpath, tname="JSONData") + store.close() + # No error implies success + assert True + def test_yosemite_data_csv_artifact(): csvpath = '/'.join([get_git_root('.'), 'dsi/data/yosemite5.csv']) dbpath = "yosemite.db" @@ -68,3 +78,6 @@ def test_artifact_query(): store.close() # No error implies success assert True + + +test_jsondata_artifact_put()