Add SQL bridge, support functions, cron jobs, error handling, and PyPI version pinning so Python triggers match PHP capabilities. Co-authored-by: Cursor <cursoragent@cursor.com>
33 lines
1.1 KiB
PHP
33 lines
1.1 KiB
PHP
<?php
|
|
/**
|
|
* Generates Python code for sandbox cron execution.
|
|
*
|
|
* @param string $functionName The name of the Python function to generate.
|
|
* @param string $pyCode The Python code to embed within the generated function.
|
|
*
|
|
* @return string The complete Python code ready for sandbox execution.
|
|
*/
|
|
function generatePythonCronCode($functionName, $pyCode) {
|
|
$constants = getPythonConstantsDefinition();
|
|
$supportFunctions = getPythonSupportFunctionsDefinition();
|
|
$imports = "import json\nimport sys as _sys\nimport os\n\n_sys.path.insert(0, os.path.join(os.getcwd(), 'sys'))\nfrom db_runtime import sql, sql_read, sql_write, sql_read_and_hydrate, get_tbl_primary_key_col_name, run_process\n" . getPythonImports();
|
|
|
|
$indentedCode = implode("\n", array_map(fn($line) => ' ' . $line, explode("\n", $pyCode)));
|
|
|
|
$pythonCode = <<<PY
|
|
$imports
|
|
$constants
|
|
$supportFunctions
|
|
def $functionName(error):
|
|
$indentedCode
|
|
|
|
error = [None]
|
|
_ret = $functionName(error)
|
|
if _ret is False or error[0]:
|
|
_sys.stderr.write("Error: " + str(error[0] or "") + "\\n")
|
|
_sys.exit(1)
|
|
PY;
|
|
|
|
return $pythonCode;
|
|
}
|