MCPcopy Create free account
hub / github.com/Datakitpage/Datakit / validatePythonSyntax

Function validatePythonSyntax

frontend/src/lib/python/executor.ts:306–333  ·  view source on GitHub ↗
(
  code: string
)

Source from the content-addressed store, hash-verified

304 * Validate Python code syntax
305 */
306export async function validatePythonSyntax(
307 code: string
308): Promise<{ isValid: boolean; error?: string }> {
309 const pyodide = getPyodide();
310 if (!pyodide) {
311 return { isValid: false, error: 'Pyodide not initialized' };
312 }
313
314 try {
315 await pyodide.runPython(`
316 import ast
317 try:
318 ast.parse('''${code.replace(/'/g, "\\'")}''')
319 syntax_valid = True
320 syntax_error = None
321 except SyntaxError as e:
322 syntax_valid = False
323 syntax_error = str(e)
324 `);
325
326 const isValid = pyodide.globals.get('syntax_valid');
327 const error = pyodide.globals.get('syntax_error');
328
329 return { isValid, error: error || undefined };
330 } catch (error) {
331 return { isValid: false, error: String(error) };
332 }
333}

Callers

nothing calls this directly

Calls 2

getPyodideFunction · 0.90
getMethod · 0.45

Tested by

no test coverage detected