Files

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

86 lines
3.2 KiB
Python
Raw Permalink Normal View History

#!/usr/bin/env python
"""
This script scans a directory for files with companion '.json' files, then loads
the data from the file, and attaches the .json contents using the 'extended_metadata'
system in the library
Sample call:
python load_data_with_metadata.py <api_key> <api_url> /data/folder "API Imports"
NOTE: The upload method used requires the data library filesystem upload allow_library_path_paste
"""
2016-11-23 18:04:33 +00:00
import argparse
import json
import os
import sys
from common import (
display,
submit,
2022-02-02 15:26:21 -05: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")
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)
def main(api_key, api_url, in_folder, data_library, uuid_field=None):
# 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:
lib_create_data = {"name": data_library}
library = submit(api_key, api_url + "libraries", lib_create_data, return_formatted=False)
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)
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.")
sys.exit(1)
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)
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)
load_file(fullpath, api_key, api_url, library_id, library_folder_id, uuid_field)
2016-11-14 21:12:14 +00:00
if __name__ == "__main__":
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)