-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathpyodide.js
More file actions
111 lines (100 loc) · 3.34 KB
/
Copy pathpyodide.js
File metadata and controls
111 lines (100 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import AnsiUp from './ansi_up'
const runRobotPyPath = '/pyworker/runRobot.py'
const pyodideWebWorkerPath = '/pyworker/py_worker.js'
const ansiUp = new AnsiUp()
var pythonProgram = ''
var pyodideWorker = null
const writeOutputEvent = new Event('writeOutput')
const clearOutputEvent = new Event('clearOutput')
const writeLogEvent = new Event('writeLog')
const writeReportEvent = new Event('writeReport')
const writeFinishEvent = new Event('finished')
const addLibraryEvent = new Event('addLibdoc')
function loadFileToPythonProgram() {
fetch(runRobotPyPath)
.then(response => response.text())
.then(result => { pythonProgram = result })
}
loadFileToPythonProgram()
function updateLogHtml(html) {
const iframeContent = encodeURIComponent(html
.replace(/<a href="#"><\/a>/is, '')
.replace(/\{\{if source\}\}.*?<\/tr>.*?\{\{\/if\}\}/is, ''))
writeLogEvent.src = 'data:text/html;charset=utf-8,' + iframeContent
window.dispatchEvent(writeLogEvent)
}
function updateReportHtml(html) {
const iframeContent = encodeURIComponent(html
.replace(/<a href="#"><\/a>/is, '')
.replace(/\{\{if source\}\}.*?<\/tr>.*?\{\{\/if\}\}/is, ''))
writeReportEvent.src = 'data:text/html;charset=utf-8,' + iframeContent
window.dispatchEvent(writeReportEvent)
}
function writeToOutput(consoleOutput) {
if (!consoleOutput) return
const html = ansiUp.ansi_to_html(consoleOutput)
writeOutputEvent.text = html
window.dispatchEvent(writeOutputEvent)
}
function clearOutput() {
window.dispatchEvent(clearOutputEvent)
}
function run(script, context, onSuccess, onError, initialize) {
if (!pyodideWorker || initialize) {
pyodideWorker = new Worker(pyodideWebWorkerPath)
}
pyodideWorker.onerror = onError
pyodideWorker.onmessage = (e) => onSuccess(e.data)
pyodideWorker.postMessage({
...context,
python: script
})
}
function asyncRun(script, context, onMessage, initialize) {
return new Promise((resolve) => {
run(script, context, (data) => {
if (Object.prototype.hasOwnProperty.call(data, 'results')) {
console.log('FINISHED')
resolve(data)
writeFinishEvent.returnCode = data.returnCode
window.dispatchEvent(writeFinishEvent)
} else {
onMessage(data)
}
}, onMessage, initialize)
})
}
export async function runRobot(files, initialize, testCaseName = '', version = '', robotArgs = {}, requirements = []) {
console.log(`init: ${initialize}`)
clearOutput()
writeToOutput('Initializing...\n')
await asyncRun(
pythonProgram, {
file_catalog: JSON.stringify(files),
test_case_name: testCaseName,
version: version,
robot_args: JSON.stringify(robotArgs),
requirements: JSON.stringify(requirements)
}, (data) => {
try {
data = JSON.parse(data)
} catch (error) {
console.log('Error parsing JSON')
console.error(error)
console.log(data)
return
}
writeToOutput(data.std_output)
if (Object.prototype.hasOwnProperty.call(data, 'log_html')) {
updateLogHtml(data.log_html)
}
if (Object.prototype.hasOwnProperty.call(data, 'report_html')) {
updateReportHtml(data.report_html)
}
if (Object.prototype.hasOwnProperty.call(data, 'libdocJson')) {
addLibraryEvent.libdoc = data.libdocJson
window.dispatchEvent(addLibraryEvent)
}
}, initialize
)
}