Skip to content

Commit

Permalink
Added function and a test for importing a json file
Browse files Browse the repository at this point in the history
  • Loading branch information
hugegreenbug committed Aug 1, 2024
1 parent 16aa7cd commit 6134776
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
51 changes: 50 additions & 1 deletion dsi/backends/sqlite.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import csv
import sqlite3
import json
import re

from dsi.backends.filesystem import Filesystem

Expand All @@ -9,6 +11,7 @@
STRING = "VARCHAR"
FLOAT = "FLOAT"
INT = "INT"
JSON = "TEXT"

# Holds table name and data properties

Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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):
Expand Down
13 changes: 13 additions & 0 deletions dsi/backends/tests/test_sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -68,3 +78,6 @@ def test_artifact_query():
store.close()
# No error implies success
assert True


test_jsondata_artifact_put()

0 comments on commit 6134776

Please sign in to comment.