@@ -7,14 +7,20 @@ import { exec } from 'child_process';
7
7
interface PythonScripterSettings {
8
8
pythonPath : string ;
9
9
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 [ ] } ;
11
14
// useLastFile: boolean;
12
15
}
13
16
14
17
const DEFAULT_SETTINGS : PythonScripterSettings = {
15
18
pythonPath : "" ,
16
19
pythonExe : "" ,
17
20
pythonIndividualExes : { } ,
21
+ passVaultPath : { } ,
22
+ passCurrentFile : { } ,
23
+ additionalArgs : { } ,
18
24
// useLastFile: false
19
25
}
20
26
@@ -47,7 +53,6 @@ export default class PythonScripterPlugin extends Plugin {
47
53
this . pythonDirectory = path . join ( basePath , defaultRelativePath ) ;
48
54
this . pythonDirectoryRelative = defaultRelativePath
49
55
}
50
- console . log ( this . pythonDirectoryRelative )
51
56
try {
52
57
await this . app . vault . createFolder ( this . pythonDirectoryRelative ) ;
53
58
//new Notice(this.pythonDirectory + " created");
@@ -69,6 +74,8 @@ export default class PythonScripterPlugin extends Plugin {
69
74
console . error ( err ) ;
70
75
return ;
71
76
}
77
+
78
+ // Getting Executable
72
79
let python_exe = "" ;
73
80
if ( this . settings . pythonExe != "" ) {
74
81
python_exe = this . settings . pythonExe
@@ -87,41 +94,52 @@ export default class PythonScripterPlugin extends Plugin {
87
94
88
95
}
89
96
console . log ( `Python Exe: ${ python_exe } ` )
97
+
98
+ // Getting Main File
99
+ let main_file = "" ;
90
100
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 ] ) {
92
117
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 ( "" ) ;
96
122
}
123
+ }
124
+ for ( var i = 0 ; i < this . settings . additionalArgs [ fileName ] . length ; i ++ ) {
125
+ args . push ( this . settings . additionalArgs [ fileName ] [ i ] ) ;
126
+ }
97
127
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
+ }
98
133
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 ;
114
139
}
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
+ } ) ;
125
143
} ) ;
126
144
127
145
}
@@ -147,6 +165,8 @@ export default class PythonScripterPlugin extends Plugin {
147
165
}
148
166
}
149
167
168
+
169
+
150
170
class PythonScripterSettingTab extends PluginSettingTab {
151
171
plugin : PythonScripterPlugin ;
152
172
files : string [ ] ;
@@ -161,7 +181,7 @@ class PythonScripterSettingTab extends PluginSettingTab {
161
181
const { containerEl } = this ;
162
182
163
183
containerEl . empty ( ) ;
164
-
184
+ this . containerEl . createEl ( "h1" , { text : `Default Behavior` } ) ;
165
185
new Setting ( containerEl )
166
186
. setName ( 'Python Script Path' )
167
187
. setDesc ( 'Defaults to .obsidian\\scripts\\python' )
@@ -182,9 +202,16 @@ class PythonScripterSettingTab extends PluginSettingTab {
182
202
this . plugin . settings . pythonExe = value ;
183
203
await this . plugin . saveSettings ( ) ;
184
204
} ) ) ;
185
-
205
+ this . containerEl . createEl ( "h1" , { text : `Scripts` } ) ;
186
206
for ( var index = 0 ; index < this . files . length ; index ++ ) {
187
207
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 } ` } ) ;
188
215
new Setting ( containerEl )
189
216
. setName ( `${ file } Python Executable` )
190
217
. setDesc ( `Overides the default python executable for ${ file } ` )
@@ -196,6 +223,149 @@ class PythonScripterSettingTab extends PluginSettingTab {
196
223
await this . plugin . saveSettings ( ) ;
197
224
} ) ;
198
225
} ) ;
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
+
199
369
}
200
370
}
201
371
0 commit comments