in modular_api_cli/modular_handler/usage_handler.py [0:0]
def _save_report_and_get_path(data, path) -> str:
if path:
if not os.path.exists(path):
_LOG.error(f'Provided path \'{path}\' does not exist')
raise ModularApiBadRequestException(
f'Seems like path "{path}" does not exist. Please check '
f'spelling')
else:
pre_configured_user_dir = os.environ.get("M3MODULAR_USER_HOME")
if pre_configured_user_dir:
path = os.path.join(
pre_configured_user_dir, LOG_FOLDER, 'reports')
else:
path = os.path.join(
str(Path.home()), LOG_FOLDER, 'reports')
if not os.path.exists(path):
os.makedirs(path)
tn = datetime.now(timezone.utc)
file_name = f'{tn.day}-{tn.month}-{tn.year}-{tn.microsecond}.json'
file_path = os.path.join(path, file_name)
sorted_list = sorted(data, key=lambda d: d[TIMESTAMP])
modified_list = list()
for key, item in enumerate(sorted_list, start=1):
item[KEY] = key
modified_list.append(item)
json_object = json.dumps(sorted_list, indent=4)
try:
with open(file_path, 'w') as file:
file.write(json_object)
except PermissionError:
raise ModularApiBadRequestException(
f'You do not have permissions to write files by path: '
f'\'{path}\'')
return file_path