-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathget_plot_methods.js
executable file
·39 lines (30 loc) · 1.18 KB
/
get_plot_methods.js
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
#!/usr/bin/env node
import * as Plot from "@observablehq/plot";
import { promises as fs } from "fs";
import path from "path";
import { fileURLToPath } from "url";
// Get current file path
const __filename = fileURLToPath(import.meta.url);
const parentDir = path.dirname(path.dirname(__filename));
async function replaceLines(filePath, newContent) {
let content = await fs.readFile(filePath, "utf8");
const regex =
/( # STATIC PLOT METHODS START\n)[\s\S]*?(\n # STATIC PLOT METHODS END\n)/;
content = content.replace(regex, `$1${newContent}\n$2`);
await fs.writeFile(filePath, content, "utf8");
}
function python_method(name) {
return `
@staticmethod
def ${name}(*args, **kwargs) -> PlotSpec: # noqa: ARG001, ARG004, RUF100
return method_to_spec("${name}", *args, **kwargs)`;
}
process.stdin.write("Importing methods...\n");
let methods = Object.keys(Plot)
.filter((d) => d[0] !== d[0].toUpperCase())
.filter((d) => d != "plot")
.map(python_method)
.join("\n");
process.stdin.write("Inserting methods in plot.py...\n");
replaceLines(parentDir + "/src/pyobsplot/plot.py", methods).catch(console.error);
process.stdin.write("Done.");