2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
.DS_Store
|
||||
|
3
.nova/Configuration.json
Normal file
3
.nova/Configuration.json
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"python.interpreter" : "\/usr\/bin\/python3"
|
||||
}
|
11
.nova/Tasks/Python Debug.json
Normal file
11
.nova/Tasks/Python Debug.json
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"extension" : {
|
||||
"identifier" : "panic.Python",
|
||||
"name" : "Python"
|
||||
},
|
||||
"extensionTemplate" : "pythonDebug",
|
||||
"extensionValues" : {
|
||||
"python.debug.script" : "export2sqlite.py"
|
||||
},
|
||||
"openLogOnRun" : "fail"
|
||||
}
|
84
export2sqlite.py
Normal file
84
export2sqlite.py
Normal file
@@ -0,0 +1,84 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import json
|
||||
import sqlite3
|
||||
|
||||
EXPORT_FOLDER = "exported"
|
||||
|
||||
METADATA = EXPORT_FOLDER + "/metadata.json"
|
||||
|
||||
with open(METADATA, "rt") as f:
|
||||
meta = json.load(f)
|
||||
|
||||
for collection in meta["collections"]:
|
||||
with open(EXPORT_FOLDER + "/collections/" + collection + "/metadata.json", "rt") as f:
|
||||
coll_meta = json.load(f)
|
||||
|
||||
OUTPUT_FILE = collection + ".db3"
|
||||
TABLE_NAME = coll_meta["id"]
|
||||
|
||||
with open(EXPORT_FOLDER + "/collections/" + collection + "/documents.json", "rt") as f:
|
||||
coll_docs = json.load(f)
|
||||
|
||||
db = sqlite3.connect(OUTPUT_FILE)
|
||||
cursor = db.cursor()
|
||||
|
||||
# Create structure
|
||||
ftypes = {}
|
||||
sql = 'CREATE TABLE IF NOT EXISTS "' + TABLE_NAME + '" ('
|
||||
for field in coll_meta["fields"]:
|
||||
ftypes[field["id"]] = field["type"]
|
||||
sql += '"' + field["id"] + '" '
|
||||
|
||||
if field["type"] in ["number", "date", "bool"]:
|
||||
sql += 'INTEGER'
|
||||
elif field["type"] == "image_path":
|
||||
sql += 'BLOB'
|
||||
else:
|
||||
sql += 'TEXT'
|
||||
|
||||
# NOTE: The Collections JSON export doesn't properly separate
|
||||
# multi-select values. I.e. multiple selections will show up as
|
||||
# a string with all the values separated by commas in the JSON.
|
||||
|
||||
sql += ', '
|
||||
sql = sql[:-2] + ');'
|
||||
cursor.execute(sql)
|
||||
|
||||
# Import documents as records
|
||||
for doc in coll_docs:
|
||||
ifields = []
|
||||
iplaces = []
|
||||
ivalues = []
|
||||
for key, value in doc.items():
|
||||
if not key in ftypes:
|
||||
continue
|
||||
ifields.append('"' + key + '"')
|
||||
placeholder = "?"
|
||||
if ftypes[key] == "bool":
|
||||
if value:
|
||||
value = 1
|
||||
else:
|
||||
value = 0
|
||||
elif ftypes[key] == "date":
|
||||
placeholder = "unixepoch(?)"
|
||||
elif ftypes[key] == "image_path":
|
||||
if value:
|
||||
with open(EXPORT_FOLDER + "/collections/" + collection + "/media/" + value, "rb") as f:
|
||||
value = f.read()
|
||||
iplaces.append(placeholder)
|
||||
ivalues.append(value)
|
||||
|
||||
sql = 'INSERT INTO "' + collection + '" (' + ', '.join(ifields) + ') VALUES (' + ', '.join(iplaces) + ');'
|
||||
print(sql)
|
||||
cursor.execute(sql, ivalues)
|
||||
|
||||
db.commit()
|
||||
|
||||
|
||||
# TODO: subcollection handling - not needed for now
|
||||
|
||||
db.commit()
|
||||
db.close()
|
||||
|
||||
print(repr(meta))
|
3
exported/.gitignore
vendored
Normal file
3
exported/.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
*
|
||||
!/.gitignore
|
||||
|
Reference in New Issue
Block a user