Skip to content
This repository was archived by the owner on Jan 24, 2025. It is now read-only.

Commit ca135ff

Browse files
committed
Per script settings done
1 parent 14a9d1d commit ca135ff

File tree

2 files changed

+204
-34
lines changed

2 files changed

+204
-34
lines changed

main.ts

Lines changed: 203 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,20 @@ import { exec } from 'child_process';
77
interface PythonScripterSettings {
88
pythonPath: string;
99
pythonExe: string;
10-
pythonIndividualExes: { [index: string]: any };
10+
pythonIndividualExes: { [index: string]: string };
11+
passVaultPath: { [index: string]: boolean };
12+
passCurrentFile: { [index: string]: boolean };
13+
additionalArgs: { [index: string]: string[] };
1114
// useLastFile: boolean;
1215
}
1316

1417
const DEFAULT_SETTINGS: PythonScripterSettings = {
1518
pythonPath: "",
1619
pythonExe: "",
1720
pythonIndividualExes: {},
21+
passVaultPath: {},
22+
passCurrentFile: {},
23+
additionalArgs: {},
1824
// useLastFile: false
1925
}
2026

@@ -47,7 +53,6 @@ export default class PythonScripterPlugin extends Plugin {
4753
this.pythonDirectory = path.join(basePath, defaultRelativePath);
4854
this.pythonDirectoryRelative = defaultRelativePath
4955
}
50-
console.log(this.pythonDirectoryRelative)
5156
try {
5257
await this.app.vault.createFolder(this.pythonDirectoryRelative);
5358
//new Notice(this.pythonDirectory + " created");
@@ -69,6 +74,8 @@ export default class PythonScripterPlugin extends Plugin {
6974
console.error(err);
7075
return;
7176
}
77+
78+
// Getting Executable
7279
let python_exe = "";
7380
if (this.settings.pythonExe != "") {
7481
python_exe = this.settings.pythonExe
@@ -87,41 +94,52 @@ export default class PythonScripterPlugin extends Plugin {
8794

8895
}
8996
console.log(`Python Exe: ${python_exe}`)
97+
98+
// Getting Main File
99+
let main_file = "";
90100
if (stats.isFile()) {
91-
// var local_current_file_path = this.app.workspace.activeEditor?.file?.path;
101+
main_file = filePath;
102+
} else if (stats.isDirectory()) {
103+
main_file = path.join(filePath, "src", "main.py");
104+
}
105+
else {
106+
new Notice(`Error: ${filePath} is not a file or directory`)
107+
console.log(`Error: ${filePath} is not a file or directory`)
108+
return;
109+
}
110+
111+
// Getting Arguments
112+
var args = [];
113+
if (this.settings.passVaultPath[fileName]) {
114+
args.push(basePath);
115+
}
116+
if (this.settings.passCurrentFile[fileName]) {
92117
var local_current_file_path = this.app.workspace.getActiveFile()?.path?.toString();
93-
if (local_current_file_path === undefined) {
94-
// local_current_file_path = "";
95-
local_current_file_path = "";
118+
if (!(local_current_file_path === undefined)) {
119+
args.push(local_current_file_path);
120+
} else {
121+
args.push("");
96122
}
123+
}
124+
for (var i = 0; i < this.settings.additionalArgs[fileName].length; i++) {
125+
args.push(this.settings.additionalArgs[fileName][i]);
126+
}
97127

128+
// Running the script
129+
let command = `${python_exe} \"${main_file}\"`;
130+
for (var i = 0; i < args.length; i++) {
131+
command += ` \"${args[i]}\"`;
132+
}
98133

99-
100-
exec(`${python_exe} \"${filePath}\" \"${basePath}\" \"${local_current_file_path}\"`, { cwd: this.pythonDirectory }, (error: any, stdout: any, stderr: any) => {
101-
if (error) {
102-
new Notice(`Error executing script ${filePath}: ${error}`);
103-
console.log(`Error executing script ${filePath}: ${error}`)
104-
return;
105-
}
106-
new Notice(`Script ` + fileName + ` output:\n${stdout}`);
107-
console.log(`Script ` + fileName + ` output:\n${stdout}`)
108-
});
109-
} else if (stats.isDirectory()) {
110-
var dir = path.join(filePath);
111-
var local_current_file_path = this.app.workspace.activeEditor?.file?.path;
112-
if (local_current_file_path === undefined) {
113-
local_current_file_path = "";
134+
exec(command, { cwd: this.pythonDirectory }, (error: any, stdout: any, stderr: any) => {
135+
if (error) {
136+
new Notice(`Error executing script ${filePath}: ${error}`);
137+
console.log(`Error executing script ${filePath}: ${error}`)
138+
return;
114139
}
115-
exec(`${python_exe} \"${path.join(filePath, "src", "main.py")}\" \"${basePath}\" \"${local_current_file_path}\"`, { cwd: dir }, (error: any, stdout: any, stderr: any) => {
116-
if (error) {
117-
new Notice(`Error executing folder program: ${error}`);
118-
console.log(`Error executing folder program: ${error}`)
119-
return;
120-
}
121-
new Notice(`Script ` + fileName + " " + basePath + ` output:\n${stdout}`);
122-
console.log(`Script ` + fileName + " " + basePath + ` output:\n${stdout}`)
123-
});
124-
}
140+
new Notice(`Script ` + fileName + ` output:\n${stdout}`);
141+
console.log(`Script ` + fileName + ` output:\n${stdout}`)
142+
});
125143
});
126144

127145
}
@@ -147,6 +165,8 @@ export default class PythonScripterPlugin extends Plugin {
147165
}
148166
}
149167

168+
169+
150170
class PythonScripterSettingTab extends PluginSettingTab {
151171
plugin: PythonScripterPlugin;
152172
files: string[];
@@ -161,7 +181,7 @@ class PythonScripterSettingTab extends PluginSettingTab {
161181
const { containerEl } = this;
162182

163183
containerEl.empty();
164-
184+
this.containerEl.createEl("h1", { text: `Default Behavior` });
165185
new Setting(containerEl)
166186
.setName('Python Script Path')
167187
.setDesc('Defaults to .obsidian\\scripts\\python')
@@ -182,9 +202,16 @@ class PythonScripterSettingTab extends PluginSettingTab {
182202
this.plugin.settings.pythonExe = value;
183203
await this.plugin.saveSettings();
184204
}));
185-
205+
this.containerEl.createEl("h1", { text: `Scripts` });
186206
for (var index = 0; index < this.files.length; index++) {
187207
let file = this.files[index];
208+
if (!(file in this.plugin.settings.passVaultPath)) {
209+
this.plugin.settings.passVaultPath[file] = true;
210+
}
211+
if (!(file in this.plugin.settings.passCurrentFile)) {
212+
this.plugin.settings.passCurrentFile[file] = true;
213+
}
214+
this.containerEl.createEl("h2", { text: `${file}` });
188215
new Setting(containerEl)
189216
.setName(`${file} Python Executable`)
190217
.setDesc(`Overides the default python executable for ${file}`)
@@ -196,6 +223,149 @@ class PythonScripterSettingTab extends PluginSettingTab {
196223
await this.plugin.saveSettings();
197224
});
198225
});
226+
new Setting(containerEl)
227+
.setName(`Pass Vault Path`)
228+
.setDesc(`Whether to pass the vault path to ${file}`)
229+
.addToggle((area) => {
230+
area
231+
.setValue(this.plugin.settings.passVaultPath[file])
232+
.onChange(async (value) => {
233+
this.plugin.settings.passVaultPath[file] = value;
234+
await this.plugin.saveSettings();
235+
this.display();
236+
});
237+
});
238+
new Setting(containerEl)
239+
.setName(`Pass Active File Path`)
240+
.setDesc(`Whether to pass the active file path to to ${file}`)
241+
.addToggle((area) => {
242+
area
243+
.setValue(this.plugin.settings.passCurrentFile[file])
244+
.onChange(async (value) => {
245+
this.plugin.settings.passCurrentFile[file] = value;
246+
await this.plugin.saveSettings();
247+
this.display();
248+
});
249+
});
250+
this.containerEl.createEl("h3", { text: `Arguments` });
251+
new Setting(containerEl)
252+
.setName(`Add Argument`)
253+
.setDesc(``)
254+
.addButton((area) => {
255+
area
256+
.onClick(async (value) => {
257+
this.plugin.settings.additionalArgs[file].push("");
258+
await this.plugin.saveSettings();
259+
this.display();
260+
}).setIcon("plus");
261+
});
262+
new Setting(containerEl)
263+
.setName(`Remove Argument`)
264+
.setDesc(``)
265+
.addButton((area) => {
266+
area
267+
.onClick(async (value) => {
268+
this.plugin.settings.additionalArgs[file].pop();
269+
await this.plugin.saveSettings();
270+
this.display();
271+
}).setIcon("minus");
272+
});
273+
if (!(file in this.plugin.settings.additionalArgs)) {
274+
this.plugin.settings.additionalArgs[file] = [];
275+
}
276+
if (this.plugin.settings.passVaultPath[file] && this.plugin.settings.passCurrentFile[file]) {
277+
new Setting(containerEl)
278+
.setName(`Arg 1`)
279+
.addText((area) => {
280+
area
281+
.setValue("[vault path]")
282+
.setPlaceholder("[vault path]")
283+
.setDisabled(true)
284+
});
285+
new Setting(containerEl)
286+
.setName(`Arg 2`)
287+
.addText((area) => {
288+
area
289+
.setValue("[active file]")
290+
.setPlaceholder("[active file]")
291+
.setDisabled(true)
292+
});
293+
for (var i = 0; i < this.plugin.settings.additionalArgs[file].length; i++) {
294+
new Setting(containerEl)
295+
.setName(`Arg ${i + 3}`)
296+
.addText((area) => {
297+
area
298+
.setPlaceholder('Enter argument')
299+
.setValue(this.plugin.settings.additionalArgs[file][i])
300+
.onChange(async (value) => {
301+
this.plugin.settings.additionalArgs[file][i] = value;
302+
await this.plugin.saveSettings();
303+
});
304+
});
305+
}
306+
}
307+
else if (this.plugin.settings.passVaultPath[file] && !this.plugin.settings.passCurrentFile[file]) {
308+
new Setting(containerEl)
309+
.setName(`Arg 1`)
310+
.addText((area) => {
311+
area
312+
.setValue("[vault path]")
313+
.setPlaceholder("[vault path]")
314+
.setDisabled(true)
315+
});
316+
for (var i = 0; i < this.plugin.settings.additionalArgs[file].length; i++) {
317+
new Setting(containerEl)
318+
.setName(`Arg ${i + 2}`)
319+
.addText((area) => {
320+
area
321+
.setPlaceholder('Enter argument')
322+
.setValue(this.plugin.settings.additionalArgs[file][i])
323+
.onChange(async (value) => {
324+
this.plugin.settings.additionalArgs[file][i] = value;
325+
await this.plugin.saveSettings();
326+
});
327+
});
328+
}
329+
}
330+
else if (!this.plugin.settings.passVaultPath[file] && this.plugin.settings.passCurrentFile[file]) {
331+
new Setting(containerEl)
332+
.setName(`Arg 1`)
333+
.addText((area) => {
334+
area
335+
.setValue("[active file]")
336+
.setPlaceholder("[active file]")
337+
.setDisabled(true)
338+
});
339+
for (var i = 0; i < this.plugin.settings.additionalArgs[file].length; i++) {
340+
new Setting(containerEl)
341+
.setName(`Arg ${i + 2}`)
342+
.addText((area) => {
343+
area
344+
.setPlaceholder('Enter argument')
345+
.setValue(this.plugin.settings.additionalArgs[file][i])
346+
.onChange(async (value) => {
347+
this.plugin.settings.additionalArgs[file][i] = value;
348+
await this.plugin.saveSettings();
349+
});
350+
});
351+
}
352+
} else {
353+
for (var i = 0; i < this.plugin.settings.additionalArgs[file].length; i++) {
354+
new Setting(containerEl)
355+
.setName(`Arg ${i + 1}`)
356+
.addText((area) => {
357+
area
358+
.setPlaceholder('Enter argument')
359+
.setValue(this.plugin.settings.additionalArgs[file][i])
360+
.onChange(async (value) => {
361+
this.plugin.settings.additionalArgs[file][i] = value;
362+
await this.plugin.saveSettings();
363+
});
364+
});
365+
}
366+
}
367+
368+
199369
}
200370
}
201371

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "python-scripter",
33
"name": "Python Scripter",
4-
"version": "1.0.8",
4+
"version": "1.0.9",
55
"minAppVersion": "0.15.0",
66
"description": "Run Python scripts directly as Obsidian commands.",
77
"author": "Nick Allison",

0 commit comments

Comments
 (0)