55import shutil
66import urllib .parse
77
8- from fsspec .implementations .cached import SimpleCacheFileSystem , hash_name
8+ from fsspec .implementations .cached import SimpleCacheFileSystem
99from fsspec import register_implementation
1010from pathlib import Path
1111
1212from .config import get_cache_dir
13- from .utils import inform
13+ from .utils import inform , hash_name
1414
1515_log = logging .getLogger (__name__ )
1616
@@ -57,12 +57,62 @@ def prefix_cache(fs, board_base_path):
5757 return f"{ proto_name } _{ base_hash } "
5858
5959
60+ class HashMapper :
61+ def __init__ (self , hash_prefix ):
62+ self .hash_prefix = hash_prefix
63+
64+ def __call__ (self , path : str ) -> str :
65+ if self .hash_prefix is not None :
66+ # optionally make the name relative to a parent path
67+ # using the hash of parent path as a prefix, to flatten a bit
68+ hash = Path (path ).relative_to (Path (self .hash_prefix ))
69+ return hash
70+
71+ else :
72+ raise NotImplementedError ()
73+
74+
75+ class PinsAccessTimeCacheMapper :
76+ def __init__ (self , hash_prefix ):
77+ self .hash_prefix = hash_prefix
78+
79+ def __call__ (self , path ):
80+ # hash full path, and put anything after the final / at the end, just
81+ # to make it easier to browse.
82+ # this has
83+ base_name = hash_name (path , False )
84+ suffix = Path (path ).name
85+ return f"{ base_name } _{ suffix } "
86+
87+
88+ class PinsRscCacheMapper :
89+ """Modifies the PinsCache to allow hash_prefix to be an RSC server url.
90+
91+ Note that this class also modifies the first / in a path to be a -, so that
92+ pin contents will not be put into subdirectories, for e.g. michael/mtcars/data.txt.
93+ """
94+
95+ def __init__ (self , hash_prefix ):
96+ self .hash_prefix = hash_prefix
97+
98+ def __call__ (self , path ):
99+ # the main change in this function is that, for same_name, it returns
100+ # the full path
101+ # change pin path of form <user>/<content> to <user>+<content>
102+ hash = path .replace ("/" , "+" , 1 )
103+ return hash
104+
105+
60106class PinsCache (SimpleCacheFileSystem ):
61107 protocol = "pinscache"
62108
63- def __init__ (self , * args , hash_prefix = None , ** kwargs ):
109+ def __init__ (self , * args , hash_prefix = None , mapper = HashMapper , ** kwargs ):
64110 super ().__init__ (* args , ** kwargs )
65111 self .hash_prefix = hash_prefix
112+ self ._mapper = mapper (hash_prefix )
113+
114+ def hash_name (self , path , * args , ** kwargs ):
115+ return self ._mapper (path )
66116
67117 def _open (self , path , * args , ** kwargs ):
68118 # For some reason, the open method of SimpleCacheFileSystem doesn't
@@ -83,43 +133,14 @@ def _make_local_details(self, path):
83133
84134 return fn
85135
86- def hash_name (self , path , same_name ):
87- # the main change in this function is that, for same_name, it returns
88- # the full path
89- if same_name :
90- if self .hash_prefix is not None :
91- # optionally make the name relative to a parent path
92- # using the hash of parent path as a prefix, to flatten a bit
93- hash = Path (path ).relative_to (Path (self .hash_prefix ))
94- return hash
95-
96- return path
97- else :
98- raise NotImplementedError ()
99-
100-
101- class PinsRscCache (PinsCache ):
102- """Modifies the PinsCache to allow hash_prefix to be an RSC server url.
103-
104- Note that this class also modifies the first / in a path to be a -, so that
105- pin contents will not be put into subdirectories, for e.g. michael/mtcars/data.txt.
106- """
107-
108- protocol = "pinsrsccache"
109-
110- def hash_name (self , path , same_name ):
111- # the main change in this function is that, for same_name, it returns
112- # the full path
113- if same_name :
114- if self .hash_prefix is None :
115- raise NotImplementedError ()
116-
117- # change pin path of form <user>/<content> to <user>+<content>
118- hash = path .replace ("/" , "+" , 1 )
119- return hash
120-
121- else :
122- raise NotImplementedError ()
136+ # same as upstream, brought in to preserve backwards compatibility
137+ def _check_file (self , path ):
138+ self ._check_cache ()
139+ sha = self ._mapper (path )
140+ for storage in self .storage :
141+ fn = os .path .join (storage , sha )
142+ if os .path .exists (fn ):
143+ return fn
123144
124145
125146class PinsUrlCache (PinsCache ):
@@ -154,15 +175,15 @@ def hash_name(self, path, same_name):
154175class PinsAccessTimeCache (SimpleCacheFileSystem ):
155176 name = "pinsaccesstimecache"
156177
157- def hash_name (self , path , same_name ):
158- if same_name :
159- raise NotImplementedError ("same_name not implemented." )
178+ def __init__ (
179+ self , * args , hash_prefix = None , mapper = PinsAccessTimeCacheMapper , ** kwargs
180+ ):
181+ super ().__init__ (* args , ** kwargs )
182+ self .hash_prefix = hash_prefix
183+ self ._mapper = mapper (hash_prefix )
160184
161- # hash full path, and put anything after the final / at the end, just
162- # to make it easier to browse.
163- base_name = super ().hash_name (path , same_name )
164- suffix = Path (path ).name
165- return f"{ base_name } _{ suffix } "
185+ def hash_name (self , path , * args , ** kwargs ):
186+ return self ._mapper (path )
166187
167188 def _open (self , path , mode = "rb" , ** kwargs ):
168189 f = super ()._open (path , mode = mode , ** kwargs )
@@ -177,6 +198,15 @@ def _open(self, path, mode="rb", **kwargs):
177198
178199 return f
179200
201+ # same as upstream, brought in to preserve backwards compatibility
202+ def _check_file (self , path ):
203+ self ._check_cache ()
204+ sha = self ._mapper (path )
205+ for storage in self .storage :
206+ fn = os .path .join (storage , sha )
207+ if os .path .exists (fn ):
208+ return fn
209+
180210
181211class CachePruner :
182212 """Prunes the cache directory, across multiple boards.
0 commit comments