Open
Description
Ideally we'd also have some test cases for the filesystem code.
For reference, here is mine
const backingStore = new Map<string, string | null>();
const fs: NotebookFilesystem = {
async get({ path }) {
const file = backingStore.get(path);
if (file === undefined) {
return { ok: false, status: 1, error: new Error("File not found " + path) };
} else {
return { ok: true, data: file };
}
},
async put({ path, value }) {
backingStore.set(path, value);
return { ok: true, data: undefined };
},
async delete({ path }) {
backingStore.delete(path);
return { ok: true, data: undefined };
},
async move({ path, newPath }) {
// TODO: Implement directory moving
const getFileResult = await fs.get({ path });
if (!getFileResult.ok) return getFileResult;
const deleteFileResult = await fs.delete({ path });
if (!deleteFileResult.ok) return deleteFileResult;
const putFileResult = await fs.put({ path: newPath, value: getFileResult.data });
if (!putFileResult.ok) return putFileResult;
return { ok: true, data: undefined };
},
async listDirectory({ path }) {
if (!path.endsWith("/")) path += "/";
const filePaths = [...backingStore.keys()]
.filter((k) => k.startsWith(path) && k !== path)
.map((v) => v.slice(path.length).split("/")[0]);
return { ok: true, data: [...new Set(filePaths)] };
},
};
rt.internal.fs = fs;
(rt.internal.fs as any).backingStore = backingStore;
# %% [python]
import os
print(os.path.dirname(os.path.realpath('__file__')))
f = open("demofile.txt", "a")
f.write("File content 🍕!")
f.close()
#open and read the file after the appending:
f = open("demofile.txt", "r")
print(f.read()) # Should print out "File content 🍕!"
os.mkdir("cat")
f = open("./cat/demofile2.txt", "a")
f.write("Also a file")
f.close()
f = open("cat/demofile2.txt", "r")
print(f.read()) # Should print out "Also a file"
# %% [python]
f = open("cat/demofile4.txt", "r") # Should cause an error
print(f.read())
# %% [python]
import os
# Should print all files and directories
for currentpath, folders, files in os.walk('/mnt'):
for folder in folders:
print(os.path.join(currentpath, folder))
for file in files:
print(os.path.join(currentpath, file))
# %% [python]
import glob
# Should print all files and directories in /mnt/shared
for filename in glob.iglob('**/**', recursive=True):
print(filename)
# %% [python]
import os
print(os.listdir(path='.')) # Should show ['demofile.txt', 'cat']
os.rename('demofile.txt', 'catapprovedfile.txt')
print(os.listdir(path='.')) # Should show ['cat', 'catapprovedfile.txt']
os.rename('catapprovedfile.txt', './cat/myfile.txt')
print(os.listdir(path='cat')) # Should show ['demofile2.txt', 'myfile.txt']
Metadata
Assignees
Labels
No labels
Activity