
#
# This code is added to the user code sent from the frontend and then executed by the sandbox
#

import warnings
warnings.filterwarnings('ignore')

import rasterio
import geopandas as gpd
import pandas as pd
import json
import mmap
import zlib
import os
import time
import hashlib
import ast

from utk_curio.sandbox.util.parsers import *

def userCode(arg):
{userCode}

file_path = "{filePath}"
dataType = "{dataType}"
boxType = "{boxType}"
input = ''

if(dataType == 'outputs'):
    input = []
    file_path = eval(file_path)
    for elem in file_path:
        input.append(load_memory_mapped_file(elem['path']))
elif(file_path):
    input = load_memory_mapped_file(file_path)

incomingInput = None

if input:
    checkIOType(input, boxType)
    if(dataType == 'outputs'):
        incomingInput = []
        for elem in input:
            incomingInput.append(parseInput(elem))
    else:
        incomingInput = parseInput(input)
else:
    incomingInput = ''

output = userCode(incomingInput)
parsedOutput = parseOutput(output)
checkIOType(parsedOutput, boxType, False)

# print(parsedOutput)
# print(type(parsedOutput['data'][0]))
# print(parsedOutput)
file_path = save_memory_mapped_file(parsedOutput)
# print(json.dumps(parsedOutput)) # stdout: 0,-2; node output: -2
print(json.dumps({'path': file_path, 'dataType': parsedOutput['dataType']})) # path: -1
# print(json.dumps(parsedOutput))