2012-10-28 00:28:42 -07:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
"""
|
2016-02-17 20:27:09 +00:00
|
|
|
This script scans a directory for files with companion '.json' files, then loads
|
2012-10-28 00:28:42 -07:00
|
|
|
the data from the file, and attaches the .json contents using the 'extended_metadata'
|
|
|
|
|
system in the library
|
|
|
|
|
|
|
|
|
|
Sample call:
|
2012-11-30 10:58:39 -08:00
|
|
|
python load_data_with_metadata.py <api_key> <api_url> /data/folder "API Imports"
|
2012-10-28 00:28:42 -07:00
|
|
|
|
|
|
|
|
NOTE: The upload method used requires the data library filesystem upload allow_library_path_paste
|
|
|
|
|
"""
|
2016-11-23 18:04:33 +00:00
|
|
|
|
2013-01-29 15:58:58 -08:00
|
|
|
import argparse
|
2016-02-17 20:27:09 +00:00
|
|
|
import json
|
|
|
|
|
import os
|
|
|
|
|
import sys
|
2013-01-29 15:58:58 -08:00
|
|
|
|
2016-02-18 13:38:07 +00:00
|
|
|
from common import (
|
|
|
|
|
display,
|
|
|
|
|
submit,
|
2022-02-02 15:26:21 -05:00
|
|
|
)
|
2012-10-28 00:28:42 -07:00
|
|
|
|
2016-02-17 20:27:09 +00:00
|
|
|
|
2013-01-29 15:58:58 -08:00
|
|
|
def load_file(fullpath, api_key, api_url, library_id, library_folder_id, uuid_field=None):
|
|
|
|
|
data = {}
|
|
|
|
|
data["folder_id"] = library_folder_id
|
|
|
|
|
data["file_type"] = "auto"
|
|
|
|
|
data["dbkey"] = ""
|
|
|
|
|
data["upload_option"] = "upload_paths"
|
|
|
|
|
data["filesystem_paths"] = fullpath
|
|
|
|
|
data["create_type"] = "file"
|
|
|
|
|
data["link_data_only"] = "link_to_files"
|
|
|
|
|
|
2017-08-17 00:33:02 +01:00
|
|
|
handle = open(fullpath + ".json")
|
2013-01-29 15:58:58 -08:00
|
|
|
smeta = handle.read()
|
|
|
|
|
handle.close()
|
|
|
|
|
ext_meta = json.loads(smeta)
|
|
|
|
|
data["extended_metadata"] = ext_meta
|
|
|
|
|
if uuid_field is not None and uuid_field in ext_meta:
|
|
|
|
|
data["uuid"] = ext_meta[uuid_field]
|
|
|
|
|
|
2024-04-27 16:03:22 +01:00
|
|
|
libset = submit(api_key, api_url + f"libraries/{library_id}/contents", data, return_formatted=True)
|
2016-06-25 20:44:44 -04:00
|
|
|
print(libset)
|
2013-01-29 15:58:58 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def main(api_key, api_url, in_folder, data_library, uuid_field=None):
|
2012-10-28 00:28:42 -07:00
|
|
|
# Find/Create data library with the above name. Assume we're putting datasets in the root folder '/'
|
|
|
|
|
libs = display(api_key, api_url + "libraries", return_formatted=False)
|
|
|
|
|
library_id = None
|
|
|
|
|
for library in libs:
|
|
|
|
|
if library["name"] == data_library:
|
|
|
|
|
library_id = library["id"]
|
|
|
|
|
if not library_id:
|
2016-02-17 20:27:09 +00:00
|
|
|
lib_create_data = {"name": data_library}
|
2012-10-28 00:28:42 -07:00
|
|
|
library = submit(api_key, api_url + "libraries", lib_create_data, return_formatted=False)
|
2013-01-29 15:58:58 -08:00
|
|
|
library_id = library["id"]
|
2024-04-27 16:03:22 +01:00
|
|
|
folders = display(api_key, api_url + f"libraries/{library_id}/contents", return_formatted=False)
|
2012-10-28 00:28:42 -07:00
|
|
|
for f in folders:
|
|
|
|
|
if f["name"] == "/":
|
|
|
|
|
library_folder_id = f["id"]
|
|
|
|
|
if not library_id or not library_folder_id:
|
2016-06-25 20:44:44 -04:00
|
|
|
print("Failure to configure library destination.")
|
2012-10-28 00:28:42 -07:00
|
|
|
sys.exit(1)
|
2012-11-30 10:58:39 -08:00
|
|
|
|
2013-01-29 15:58:58 -08:00
|
|
|
if os.path.isfile(in_folder):
|
|
|
|
|
if os.path.exists(in_folder + ".json"):
|
|
|
|
|
fullpath = os.path.abspath(in_folder)
|
2016-06-25 20:44:44 -04:00
|
|
|
print("Loading", fullpath)
|
2013-01-29 15:58:58 -08:00
|
|
|
load_file(fullpath, api_key, api_url, library_id, library_folder_id, uuid_field)
|
|
|
|
|
else:
|
|
|
|
|
for fname in os.listdir(in_folder):
|
|
|
|
|
fullpath = os.path.join(in_folder, fname)
|
|
|
|
|
if os.path.isfile(fullpath) and os.path.exists(fullpath + ".json"):
|
2016-06-25 20:44:44 -04:00
|
|
|
print("Loading", fullpath)
|
2013-01-29 15:58:58 -08:00
|
|
|
load_file(fullpath, api_key, api_url, library_id, library_folder_id, uuid_field)
|
2012-10-28 00:28:42 -07:00
|
|
|
|
2016-11-14 21:12:14 +00:00
|
|
|
|
2012-10-28 00:28:42 -07:00
|
|
|
if __name__ == "__main__":
|
2013-01-29 15:58:58 -08:00
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
|
parser.add_argument("api_key", help="API KEY")
|
|
|
|
|
parser.add_argument("api_url", help="API URL")
|
|
|
|
|
parser.add_argument("in_folder", help="Input Folder")
|
|
|
|
|
parser.add_argument("data_library", help="Data Library")
|
|
|
|
|
parser.add_argument("--uuid_field", help="UUID Field", default=None)
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
main(args.api_key, args.api_url, args.in_folder, args.data_library, args.uuid_field)
|