Skip to content

Commit 767388a

Browse files
committed
spec: normalize the key for _ls_from_cache
1 parent 64868f1 commit 767388a

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

fsspec/spec.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ def _ls_from_cache(self, path):
340340
"""
341341
parent = self._parent(path)
342342
if path.rstrip("/") in self.dircache:
343-
return self.dircache[path]
343+
return self.dircache[path.rstrip("/")]
344344
try:
345345
files = [
346346
f

fsspec/tests/test_spec.py

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,21 @@ def __getitem__(self, name):
5757
return item
5858
raise IndexError("{name} not found!".format(name=name))
5959

60-
def ls(self, path, detail=True, **kwargs):
61-
path = self._strip_protocol(path)
62-
63-
files = {
64-
file["name"]: file
65-
for file in self._fs_contents
66-
if path == self._parent(file["name"])
67-
}
60+
def ls(self, path, detail=True, refresh=True, **kwargs):
61+
if kwargs.pop("strip_proto", True):
62+
path = self._strip_protocol(path)
63+
64+
files = not refresh and self._ls_from_cache(path)
65+
if not files:
66+
files = [
67+
file for file in self._fs_contents if path == self._parent(file["name"])
68+
]
69+
files.sort(key=lambda file: file["name"])
70+
self.dircache[path.rstrip("/")] = files
6871

6972
if detail:
70-
return [files[name] for name in sorted(files)]
71-
72-
return list(sorted(files))
73+
return files
74+
return [file["name"] for file in files]
7375

7476
@classmethod
7577
def get_test_paths(cls, start_with=""):
@@ -296,6 +298,23 @@ def test_json():
296298
assert DummyTestFS.from_json(outb) is b
297299

298300

301+
def test_ls_from_cache():
302+
fs = DummyTestFS()
303+
uncached_results = fs.ls("top_level/second_level/", refresh=True)
304+
305+
assert fs.ls("top_level/second_level/", refresh=False) == uncached_results
306+
307+
# _strip_protocol removes everything by default though
308+
# for the sake of testing the _ls_from_cache interface
309+
# directly, we need run one time more without that call
310+
# to actually verify that our stripping in the client
311+
# function works.
312+
assert (
313+
fs.ls("top_level/second_level/", refresh=False, strip_proto=True)
314+
== uncached_results
315+
)
316+
317+
299318
@pytest.mark.parametrize(
300319
"dt",
301320
[

0 commit comments

Comments
 (0)