Skip to content

Commit 26d77f2

Browse files
committed
roundtrip POC
1 parent d2997db commit 26d77f2

File tree

4 files changed

+39
-35
lines changed

4 files changed

+39
-35
lines changed

example/main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from pyscript_fsspec_client import io
2-
from pyscript import PyWorker, ffi
2+
from pyscript import PyWorker
33

44
config = {
55
"packages": ["fsspec", "fastparquet"],
@@ -10,4 +10,5 @@
1010
}
1111
}
1212
pw = PyWorker("./worker.py", type="pyodide", config=config)
13+
1314
pw.sync.session = io.request

example/worker.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
from pyscript import sync, document, window
1+
from pyscript import sync, ffi
22

33
import fsspec
44
import pyscript_fsspec_client.client
55

66
fs = fsspec.filesystem("pyscript")
77
print(fs.ls("local"))
8+
9+
out = fs.cat("local/mdurant/code/fsspec-proxy/pyproject.toml")
10+
print("binary:", type(out), out)
11+
12+
out = fs.cat("local/mdurant/code/fsspec-proxy/pyproject.toml", start=0, end=10)
13+
print("binary:", type(out), out)
14+
15+
fs.pipe_file("local/mdurant/code/fsspec-proxy/OUTPUT", b"hello world")

pyscript-fsspec-client/pyscript_fsspec_client/client.py

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import logging
33
import os
44

5-
from pyscript import sync
5+
from pyscript import sync, ffi
66
from fsspec.spec import AbstractFileSystem, AbstractBufferedFile
77
import fsspec.utils
88

@@ -17,7 +17,6 @@ class PyscriptFileSystem(AbstractFileSystem):
1717
def __init__(self, base_url=default_endpoint):
1818
super().__init__()
1919
self.base_url = base_url
20-
self.session = sync.session
2120

2221
def _split_path(self, path):
2322
key, *relpath = path.split("/", 1)
@@ -34,22 +33,19 @@ def _call(self, path, method="GET", range=None, binary=False, data=0, json=0):
3433
outmode = "text"
3534
if range:
3635
headers["Range"] = f"bytes={range[0]}-{range[1]}"
37-
try:
38-
print(method, f"{self.base_url}/{path}", headers, data, json, outmode)
39-
out = self.session(
40-
method, f"{self.base_url}/{path}",
41-
#hearder=headers, data=data, json=json, outmode=outmode
42-
)
43-
print(out)
44-
if isinstance(out, tuple) and out[0] == "error":
45-
num, txt = out[1:]
46-
raise OSError(num, txt)
47-
except OSError as e:
48-
if e.errno == 404:
49-
raise FileNotFoundError(path)
50-
if e.errno == 403:
51-
raise PermissionError
52-
raise
36+
if data:
37+
data = memoryview(data)
38+
outmode = None
39+
out = sync.session(
40+
method, f"{self.base_url}/{path}", ffi.to_js(data),
41+
ffi.to_js(headers), outmode
42+
)
43+
if isinstance(out, str) and out == "ISawAnError":
44+
raise OSError(0, out)
45+
if out is not None and not isinstance(out, str):
46+
# may need a different conversion
47+
out = bytes(out.to_py())
48+
return out
5349

5450
def ls(self, path, detail=True, **kwargs):
5551
path = self._strip_protocol(path)
Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
11
import json
22
import pyscript
3+
import js
4+
from pyodide import ffi, console
35

46

57
async def request(method, path, data=None, headers=None,
6-
outmode="json", **kwargs):
7-
print("main", method, path)
8-
if headers:
9-
print(headers)
10-
headers = json.loads(headers)
11-
resp = await pyscript.fetch(path, method=method, budy=data, headers=headers or {},
12-
**kwargs)
13-
print("fetched", resp, resp.status)
8+
outmode="text", **kwargs):
9+
if data:
10+
resp = await js.fetch(path, method=method, body=data.buffer, headers=headers or {},
11+
**kwargs)
12+
else:
13+
resp = await js.fetch(path, method=method, headers=headers or {},
14+
**kwargs)
15+
if not resp.ok:
16+
return "ISawAnError"
1417
if resp.status >= 400:
15-
return ("error", resp.status, await resp.text())
16-
if outmode == "json":
17-
d = (await resp.json()).copy()
18-
print(d)
19-
return d
18+
return "ISawAnError"
2019
if outmode == "text":
2120
return await resp.text()
2221
if outmode == "bytes":
23-
return await resp.bytearray()
22+
return await resp.arrayBuffer()
2423
if outmode is None:
2524
return
26-
raise ValueError
25+
return "ISawAnError"

0 commit comments

Comments
 (0)