-
Notifications
You must be signed in to change notification settings - Fork 1
/
python.ts
88 lines (77 loc) · 3.48 KB
/
python.ts
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
import { ChildProcessWithoutNullStreams, spawn } from "child_process";
import { mkdirSync, writeFileSync } from "fs";
import { getTempPath } from "../config";
import { Cell, CommentDecorator } from "../types";
import vscode from "vscode"
import path from "path"
let tempDir = getTempPath();
export let processCellsPython = (cells: Cell[], command: string): { stream: ChildProcessWithoutNullStreams, clearOutput: boolean } => {
let innerScope = "";
let cellCount = 0;
let clearOutput = false;
const activeFilePath = path.dirname(vscode.window.activeTextEditor?.document.uri.fsPath as string);
for (const cell of cells) {
innerScope += `\nprint("!!output-start-cell", flush=True)\n`;
cell.contents = cell.contents.trim();
const regex = /(\s*print\s*\()(.*?)(\)\s*$)/gm;
cell.contents = cell.contents.replace(regex, (_, before, content, after) => {
// Check if 'flush=True' is already present
if (!content.includes('flush=True')) {
if (content.trim()) { // If there's content inside the print statement, add ', flush=True'
content += ", flush=True";
} else { // If the print statement is empty, just add 'flush=True'
content = "flush=True";
}
}
return `${before}${content}${after}`;
});
cellCount++;
if (cell.contents.startsWith("#mdl:skip") || cell.contents.startsWith("# mdl:skip")) {
continue;
}
if (cell.contents.startsWith("#mdl:skip")) {
continue
}
let lines = cell.contents.split("\n");
const len = lines.length;
let i = 0
for (let line of lines) {
i++
if (i == 1 && line.replace(/\s/g, "").substring(0, 6) == "#file:") {
let file = line.split(":")[1].trim()
if (file != "main.py") {
let cleaned = ""
for (let line2 of lines) {
if (line2.trim() != 'print("!!output-start-cell", flush=True)') {
cleaned += line2 + "\n"
}
}
if (path.isAbsolute(file)) {
writeFileSync(file, cleaned);
} else {
writeFileSync(path.join(tempDir, file), cleaned);
}
}
continue
}
if (i == 1 && cellCount == cells.length && line.startsWith("# " + CommentDecorator.clear)) {
clearOutput = true
}
if (line[0] !== " " && i == len && !line.includes("#") && line.trim().split(" ").length == 1 && !line.endsWith(")")) {
// if first char is `!` pretty print
if (line[0] === "!") {
innerScope += "from pprint import pprint\n";
line = "pprint(" + line.substring(1) + ", flush=True)";
} else {
line = "print(" + line + ", flush=True)";
}
}
innerScope += line + "\n";
}
};
let mainFile = path.join(tempDir, "mdl.py");
let header = `import sys\nsys.path.append("${activeFilePath}")\nsys.path.append("${tempDir}")\nfrom builtins import *\n`
mkdirSync(tempDir, { recursive: true });
writeFileSync(mainFile, header + innerScope);
return { stream: spawn(command, [mainFile], { cwd: activeFilePath }), clearOutput };
};