1- import os
1+ from json import dumps , loads
22import logging
3+ import os
34
4- import fsspec . utils
5+ from pyscript import sync , ffi
56from fsspec .spec import AbstractFileSystem , AbstractBufferedFile
6- from fsspec .implementations . http_sync import RequestsSessionShim
7+ import fsspec .utils
78
89logger = logging .getLogger ("pyscript_fsspec_client" )
910fsspec .utils .setup_logging (logger = logger )
@@ -16,41 +17,35 @@ class PyscriptFileSystem(AbstractFileSystem):
1617 def __init__ (self , base_url = default_endpoint ):
1718 super ().__init__ ()
1819 self .base_url = base_url
19- self ._session = None
2020
2121 def _split_path (self , path ):
2222 key , * relpath = path .split ("/" , 1 )
2323 return key , relpath [0 ] if relpath else ""
2424
25- @property
26- def session (self ):
27- if self ._session is None :
28- try :
29- import js # noqa: F401
30- self ._session = RequestsSessionShim ()
31- except (ImportError , ModuleNotFoundError ):
32- import requests
33- self ._session = requests .Session ()
34- return self ._session
35-
36- def _call (self , path , method = "GET" , range = None , binary = False , data = None , json = None , ** kw ):
37- logger .debug ("request: %s %s %s %s" , path , method , kw , range )
25+ def _call (self , path , method = "GET" , range = None , binary = False , data = 0 , json = 0 ):
26+ logger .debug ("request: %s %s %s" , path , method , range )
3827 headers = {}
28+ if binary :
29+ outmode = "bytes"
30+ elif json :
31+ outmode = "json"
32+ else :
33+ outmode = "text"
3934 if range :
4035 headers ["Range" ] = f"bytes={ range [0 ]} -{ range [1 ]} "
41- r = self .session .request (
42- method , f"{ self .base_url } /{ path } " , params = kw , headers = headers ,
43- data = data , json = json
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
4442 )
45- if r .status_code == 404 :
46- raise FileNotFoundError (path )
47- if r .status_code == 403 :
48- raise PermissionError
49- r .raise_for_status ()
50- if binary :
51- return r .content
52- j = r .json () if callable (r .json ) else r .json # inconsistency in shim - to fix!
53- return j ["contents" ]
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
5449
5550 def ls (self , path , detail = True , ** kwargs ):
5651 path = self ._strip_protocol (path )
@@ -104,4 +99,4 @@ def _upload_chunk(self, final=False):
10499 return True
105100 return False
106101
107- fsspec .register_implementation ("pyscript" , PyscriptFileSystem )
102+ fsspec .register_implementation ("pyscript" , PyscriptFileSystem )
0 commit comments