forked from nickrallison/obsidian-python-scripter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
161 lines (140 loc) · 5.01 KB
/
main.ts
File metadata and controls
161 lines (140 loc) · 5.01 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import { App, Editor, FileSystemAdapter, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian';
import * as path from 'path';
import * as fs from 'fs';
import { exec } from 'child_process';
interface PythonScripterSettings {
pythonPath: string;
pythonExe: string;
}
const DEFAULT_SETTINGS: PythonScripterSettings = {
pythonPath: "",
pythonExe: ""
}
export default class PythonScripterPlugin extends Plugin {
settings: PythonScripterSettings;
pythonDirectory: string;
pythonDirectoryRelative: string;
getBasePath(): string {
let basePath;
// base path
if (this.app.vault.adapter instanceof FileSystemAdapter) {
basePath = this.app.vault.adapter.getBasePath();
} else {
throw new Error('Cannot determine base path.');
}
return `${basePath}`;
}
async onload() {
await this.loadSettings();
var basePath = this.getBasePath();
var defaultRelativePath: string = path.join(".", this.app.vault.configDir, "scripts", "python");
this.pythonDirectory = path.join(basePath, defaultRelativePath);
this.pythonDirectoryRelative = defaultRelativePath
if (this.settings.pythonPath != "") {
this.pythonDirectory = path.join(basePath, this.settings.pythonPath);
this.pythonDirectoryRelative = this.settings.pythonPath
} else {
this.pythonDirectory = path.join(basePath, defaultRelativePath);
this.pythonDirectoryRelative = defaultRelativePath
}
console.log(this.pythonDirectoryRelative)
try {
await this.app.vault.createFolder(this.pythonDirectoryRelative);
//new Notice(this.pythonDirectory + " created");
} catch (error) {
//new Notice("Error creating " + this.pythonDirectory);
}
var files: string[] = fs.readdirSync(this.pythonDirectory);
for (var index = 0; index < files.length; index++) {
const filePath = path.join(this.pythonDirectory, files[index]);
const fileName = files[index];
const basePath = this.getBasePath();
const obsidianCommand = {
id: "run-"+files[index],
name: 'Run '+files[index],
callback: () => {
fs.stat(filePath, (err: any, stats: { isFile: () => any; isDirectory: () => any; }) => {
if (err) {
console.error(err);
return;
}
let python_exe = "python";
if (this.settings.pythonExe != "") {
python_exe = this.settings.pythonExe
}
if (stats.isFile()) {
var local_current_file_path = this.app.workspace.activeEditor?.file?.path;
if (local_current_file_path === undefined) {
local_current_file_path = "";
}
exec(`${python_exe} \"${filePath}\" \"${basePath}\" \"${local_current_file_path}\"`, {cwd: this.pythonDirectory}, (error: any, stdout: any, stderr: any) => {
if (error) {
new Notice(`Error executing script ${filePath}: ${error}`);
console.log(`Error executing script ${filePath}: ${error}`)
return;
}
new Notice(`Script ` + fileName + ` output:\n${stdout}`);
});
} else if (stats.isDirectory()) {
var dir = path.join(filePath);
var local_current_file_path = this.app.workspace.activeEditor?.file?.path;
if (local_current_file_path === undefined) {
local_current_file_path = "";
}
exec(`${python_exe} \"${path.join(filePath, "src", "main.py")}\" \"${basePath}\" \"${local_current_file_path}\"`, {cwd: dir}, (error: any, stdout: any, stderr: any) => {
if (error) {
new Notice(`Error executing folder program: ${error}`);
console.log(`Error executing folder program: ${error}`)
return;
}
new Notice(`Script ` + fileName + " " + basePath + ` output:\n${stdout}`);
});
}
});
}
}
this.addCommand(obsidianCommand);
}
// This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new PythonScripterSettingTab(this.app, this));
}
onunload() {
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class PythonScripterSettingTab extends PluginSettingTab {
plugin: PythonScripterPlugin;
constructor(app: App, plugin: PythonScripterPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const {containerEl} = this;
containerEl.empty();
new Setting(containerEl)
.setName('Python Script Path')
.setDesc('Defaults to .obsidian\\scripts\\python')
.addText(text => text
.setPlaceholder('Enter path')
.setValue(this.plugin.settings.pythonPath)
.onChange(async (value) => {
this.plugin.settings.pythonPath = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('Python Executable')
.setDesc('Defaults to python')
.addText(text => text
.setPlaceholder('Enter path or command')
.setValue(this.plugin.settings.pythonExe)
.onChange(async (value) => {
this.plugin.settings.pythonExe = value;
await this.plugin.saveSettings();
}));
}
}