Source code for models.utils.script_execution

import os
from typing import Dict, Any


[docs]def script_execute(script_path: str) -> Dict[str, Any]: """This function executes a custom python script and returns the locals dict that was generated by the script. :param script_path: The path to the custom python script. :type script_path: str :raises ValueError: The script file doesn't exist. :return: The locals dict that was generated by the script. """ if not os.path.isfile(script_path): raise ValueError('utils.script_execute.file.doesnt.exist.%s' % script_path) # Open and read the given custom python script file = open(script_path, 'r') script = file.read() # Compile the script compiled_script = compile(script, '', 'exec') _locals = locals() # Execute the script, setting _locals dict exec(compiled_script, _locals) return _locals