"""
json.py
==========
Contains helper functions for reading json using concepts from the registrar
"""
from os import remove
from os.path import join, basename
import json
from tempfile import gettempdir
from typing import Union, Dict, List
from .source import Source
[docs]def read_json(source: Source, path: str) -> Union[Dict, List]:
"""Helper to easily read a file on a source as JSON and decode it.
Args:
source (Source): From which source to read the file
path (str): Path to file
Returns:
Union[Dict, List]: json converted to dict
"""
out_filename = join(gettempdir(), basename(path))
try:
source.get_file(path, out_filename)
with open(out_filename) as f:
data = json.load(f)
finally:
remove(out_filename)
return data