8000 Handling file operations on inexistent paths · arduino/micropython.js@74b0af3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 74b0af3

Browse files
committed
Handling file operations on inexistent paths
1 parent 9da6c20 commit 74b0af3

File tree

3 files changed

+80
-16
lines changed

3 files changed

+80
-16
lines changed

examples/05_list_files.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,18 @@ board.open(process.env.PORT || '/dev/tty.usbmodem141101')
1717
try {
1818
let output = await board.fs_ls()
1919
let files = extractFileArray(output)
20-
console.log('files', files)
20+
console.log('files at "/"', files)
2121
console.log('disconnect')
2222
} catch(e) {
2323
console.log('error', e)
2424
}
25+
try {
26+
let output = await board.fs_ls('lib')
27+
let files = extractFileArray(output)
28+
console.log('files at "/lib"', files)
29+
} catch(e) {
30+
console.log('error', e)
31+
}
2532
board.close()
33+
console.log('disconnect')
2634
})

examples/08_file_exists.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const Board = require('../micropython.js')
2+
3+
console.log('connect')
4+
let board = new Board()
5+
board.open(process.env.PORT || '/dev/tty.usbmodem141101')
6+
.then(async () => {
7+
try {
8+
// Check for boot.py (probably exists)
9+
let output = await board.fs_exists('boot.py')
10+
if (output) {
11+
console.log('boot.py exists')
12+
} else {
13+
console.log('boot.py does not exists')
14+
}
15+
16+
// Check for xxx (probably won't exist)
17+
output = await board.fs_exists('xxx')
18+
if (output) {
19+
console.log('xxx exists')
20+
} else {
21+
console.log('xxx does not exists')
22+
}
23+
} catch(e) {
24+
console.log('error', e)
25+
}
26+
board.close()
27+
console.log('disconnect')
28+
})

micropython.js

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ class MicroPythonBoard {
2525
async open(device) {
2626
if (device) {
2727
this.device = device
28-
}
29-
if (!this.device) {
30-
throw new Error(`No device specified`)
28+
} else {
29+
return Promise.reject(
30+
new Error(`No device specified`)
31+
)
3132
}
3233
if (this.serial && this.serial.isOpen) {
3334
await this.serial.close()
@@ -66,8 +67,10 @@ class MicroPythonBoard {
6667
timeout = null,
6768
data_consumer = () => false
6869
} = options || {}
70+
const parser = this.serial.pipe(
71+
new ReadlineParser({ delimiter: ending })
72+
)
6973
return new Promise((resolve, reject) => {
70-
const parser = this.serial.pipe(new ReadlineParser({ delimiter: ending }))
7174
let waiting = 0
7275
if (timeout) {
7376
waiting = setTimeout(() => {
@@ -189,11 +192,31 @@ class MicroPythonBoard {
189192
return Promise.reject()
190193
}
191194

192-
async fs_ls() {
195+
async fs_exists(filePath) {
196+
filePath = filePath || ''
197+
let command = `try:\n`
198+
command += ` f = open("${filePath}", "r")\n`
199+
command += ` print(1)\n`
200+
command += `except OSError:\n`
201+
command += ` print(0)\n`
193202
await this.enter_raw_repl()
194-
const output = await this.exec_raw({
195-
command: `import uos\nprint(uos.listdir())`
196-
})
203+
let output = await this.exec_raw({ command: command })
204+
await this.exit_raw_repl()
205+
// Extract output
206+
output = output.split('OK')
207+
let result = output[2] || ''
208+
return Promise.resolve(result[0] === '1')
209+
}
210+
211+
async fs_ls(folderPath) {
212+
folderPath = folderPath || ''
213+
let command = `import uos\n`
214+
command += `try:\n`
215+
command += ` print(uos.listdir("${folderPath}"))\n`
216+
command += `except OSError:\n`
217+
command += ` print([])\n`
218+
await this.enter_raw_repl()
219+
const output = await this.exec_raw({ command: command })
197220
await this.exit_raw_repl()
198221
return Promise.resolve(output)
199222
}
@@ -261,29 +284,34 @@ class MicroPythonBoard {
261284
const output = await this.exec_raw({
262285
command: `import uos\nuos.mkdir('${filePath}')`
263286
})
264-
console.log(output)
265287
return this.exit_raw_repl()
266288
}
267289
return Promise.reject()
268290
}
269291

270292
async fs_rmdir() {
271293
if (filePath) {
294+
let command = `import uos\n`
295+
command += `try:\n`
296+
command += ` uos.rmdir("${filePath}")\n`
297+
command += `except OSError:\n`
298+
command += ` print(0)\n`
272299
await this.enter_raw_repl()
273-
const output = await this.exec_raw({
274-
command: `import uos\nuos.rmdir('${filePath}')`
275-
})
300+
const output = await this.exec_raw({ command: command })
276301
return this.exit_raw_repl()
277302
}
278303
return Promise.reject()
279304
}
280305

281306
async fs_rm(filePath) {
282307
if (filePath) {
308+
let command = `import uos\n`
309+
command += `try:\n`
310+
command += ` uos.remove("${filePath}")\n`
311+
command += `except OSError:\n`
312+
command += ` print(0)\n`
283313
await this.enter_raw_repl()
284-
const output = await this.exec_raw({
285-
command: `import uos\nuos.remove('${filePath}')`
286-
})
314+
const output = await this.exec_raw({ command: command })
287315
return this.exit_raw_repl()
288316
}
289317
return Promise.reject()

0 commit comments

Comments
 (0)
0