Merge pull request #12 from makermelissa/main

bug fixes
This commit is contained in:
Melissa LeBlanc-Williams 2024-08-10 10:15:25 -07:00 committed by GitHub
commit 25c8306c0f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

36
repl.js
View file

@ -82,6 +82,7 @@ export class FileOps {
contents = btoa(byteString); // Convert binary string to base64 contents = btoa(byteString); // Convert binary string to base64
let code = ` let code = `
import os
import binascii import binascii
with open("${path}", "wb") as f: with open("${path}", "wb") as f:
f.seek(${offset}) f.seek(${offset})
@ -90,7 +91,8 @@ with open("${path}", "wb") as f:
`; `;
if (modificationTime) { if (modificationTime) {
code += `os.utime("${path}", (os.path.getatime("${path}"), ${modificationTime}))\n`; modificationTime = Math.floor(modificationTime / 1000);
code += `os.utime("${path}", (${modificationTime}, ${modificationTime}))\n`;
} }
await this._repl.runCode(code); await this._repl.runCode(code);
} }
@ -101,28 +103,26 @@ with open("${path}", "wb") as f:
contents = contents.replace(/"/g, '\\"'); contents = contents.replace(/"/g, '\\"');
let code = ` let code = `
import os
with open("${path}", "w") as f: with open("${path}", "w") as f:
f.seek(${offset}) f.seek(${offset})
f.write("""${contents}""") f.write("""${contents}""")
`; `;
if (modificationTime) { if (modificationTime) {
code += `os.utime("${path}", (os.path.getatime("${path}"), ${modificationTime}))\n`; modificationTime = Math.floor(modificationTime / 1000);
code += `os.utime("${path}", (${modificationTime}, ${modificationTime}))\n`;
} }
await this._repl.runCode(code); await this._repl.runCode(code);
} }
// Write a file to the device path with contents beginning at offset. Modification time can be set and if raw is true, contents is written as binary // Write a file to the device path with contents beginning at offset. Modification time can be set and if raw is true, contents is written as binary
async writeFile(path, contents, offset=0, modificationTime=null, raw=false) { async writeFile(path, contents, offset=0, modificationTime=null, raw=false) {
this._repl.terminalOutput = DEBUG;
if (raw) { if (raw) {
await this._writeRawFile(path, contents, offset, modificationTime); await this._writeRawFile(path, contents, offset, modificationTime);
} else { } else {
await this._writeTextFile(path, contents, offset, modificationTime); await this._writeTextFile(path, contents, offset, modificationTime);
} }
this._repl.terminalOutput = true;
} }
async _readRawFile(path) { async _readRawFile(path) {
@ -178,7 +178,6 @@ with open("${path}", "r") as f:
// Read a file from the device // Read a file from the device
async readFile(path, raw=false) { async readFile(path, raw=false) {
let result; let result;
this._repl.terminalOutput = DEBUG;
if (raw) { if (raw) {
result = await this._readRawFile(path); result = await this._readRawFile(path);
@ -186,14 +185,12 @@ with open("${path}", "r") as f:
result = await this._readTextFile(path); result = await this._readTextFile(path);
} }
this._repl.terminalOutput = true;
return result; return result;
} }
// List files using paste mode on the device returning the result as a javascript array // List files using paste mode on the device returning the result as a javascript array
// We need the file name, whether or not it is a directory, file size and file date // We need the file name, whether or not it is a directory, file size and file date
async listDir(path) { async listDir(path) {
this._repl.terminalOutput = DEBUG;
// Mask sure path has a trailing slash // Mask sure path has a trailing slash
if (path[path.length - 1] != "/") { if (path[path.length - 1] != "/") {
path += "/"; path += "/";
@ -222,12 +219,10 @@ for item in contents:
fileDate: parseInt(fileDate) * 1000, fileDate: parseInt(fileDate) * 1000,
}); });
} }
this._repl.terminalOutput = true;
return contents; return contents;
} }
async isReadOnly() { async isReadOnly() {
this._repl.terminalOutput = DEBUG;
// MicroPython doesn't have storage, but also doesn't have a CIRCUITPY drive // MicroPython doesn't have storage, but also doesn't have a CIRCUITPY drive
let code = ` let code = `
try: try:
@ -238,29 +233,28 @@ except:
`; `;
let result = await this._repl.runCode(code); let result = await this._repl.runCode(code);
let isReadOnly = result.match("True") != null; let isReadOnly = result.match("True") != null;
this._repl.terminalOutput = true;
return isReadOnly; return isReadOnly;
} }
async makeDir(path, modificationTime=null) { async makeDir(path, modificationTime=null) {
await this._checkReadOnly(); await this._checkReadOnly();
this._repl.terminalOutput = DEBUG; let code = `
let code = `os.mkdir("${path}")\n`; import os
os.mkdir("${path}")
`;
if (modificationTime) { if (modificationTime) {
code += `os.utime("${path}", (os.path.getatime("${path}"), ${modificationTime}))\n`; modificationTime = Math.floor(modificationTime / 1000);
code += `os.utime("${path}", (${modificationTime}, ${modificationTime}))\n`;
} }
await this._repl.runCode(code); await this._repl.runCode(code);
this._checkReplErrors(); this._checkReplErrors();
this._repl.terminalOutput = true;
} }
async delete(path) { async delete(path) {
await this._checkReadOnly(); await this._checkReadOnly();
this._repl.terminalOutput = DEBUG;
let code = ` let code = `
import os import os
stat = os.stat("${path}") stat = os.stat("${path}")
if stat[0] == ${TYPE_FILE}: if stat[0] == ${TYPE_FILE}:
os.remove("${path}") os.remove("${path}")
@ -269,7 +263,6 @@ else:
`; `;
await this._repl.runCode(code); await this._repl.runCode(code);
this._checkReplErrors(); this._checkReplErrors();
this._repl.terminalOutput = true;
} }
async move(oldPath, newPath) { async move(oldPath, newPath) {
@ -277,14 +270,13 @@ else:
// we need to check if the new path already exists // we need to check if the new path already exists
// Return true on success and false on failure // Return true on success and false on failure
this._repl.terminalOutput = DEBUG;
let code = ` let code = `
import os import os
os.rename("${oldPath}", "${newPath}") os.rename("${oldPath}", "${newPath}")
`; `;
await this._repl.runCode(code); await this._repl.runCode(code);
let error = this._checkReplErrors(); let error = this._checkReplErrors();
this._repl.terminalOutput = true;
return !error; return !error;
} }
} }
@ -786,7 +778,7 @@ export class REPL {
this.terminalOutput = DEBUG; this.terminalOutput = DEBUG;
await this.getToPrompt(); await this.getToPrompt();
let result = this.execRawMode(code + LINE_ENDING_LF, codeTimeoutMs); let result = await this.execRawMode(code + LINE_ENDING_LF, codeTimeoutMs);
this.terminalOutput = true; this.terminalOutput = true;
return result; return result;
} }