22
33import io
44import shutil
5+ from collections .abc import AsyncGenerator
56from pathlib import Path
67from typing import Union , Optional , List , Tuple
78
@@ -145,22 +146,19 @@ async def exists(self, key: str) -> bool:
145146 path = self .root / key
146147 return await to_thread (path .is_file )
147148
148- async def list (self ) -> List [str ]:
149+ async def list (self ) -> AsyncGenerator [str , None ]:
149150 """Retrieve all keys in the store.
150151
151152 Returns
152153 -------
153- list [str]
154+ AsyncGenerator [str, None ]
154155 """
156+ for p in self .root .rglob ("" ):
157+ if p .is_file ():
158+ yield str (p )
155159
156- # Q: do we want to return strings or Paths?
157- def _list (root : Path ) -> List [str ]:
158- files = [str (p ) for p in root .rglob ("" ) if p .is_file ()]
159- return files
160160
161- return await to_thread (_list , self .root )
162-
163- async def list_prefix (self , prefix : str ) -> List [str ]:
161+ async def list_prefix (self , prefix : str ) -> AsyncGenerator [str , None ]:
164162 """Retrieve all keys in the store with a given prefix.
165163
166164 Parameters
@@ -169,16 +167,14 @@ async def list_prefix(self, prefix: str) -> List[str]:
169167
170168 Returns
171169 -------
172- list [str]
170+ AsyncGenerator [str, None ]
173171 """
172+ for p in (self .root / prefix ).rglob ("*" ):
173+ if p .is_file ():
174+ yield str (p )
174175
175- def _list_prefix (root : Path , prefix : str ) -> List [str ]:
176- files = [str (p ) for p in (root / prefix ).rglob ("*" ) if p .is_file ()]
177- return files
178-
179- return await to_thread (_list_prefix , self .root , prefix )
180176
181- async def list_dir (self , prefix : str ) -> List [str ]:
177+ async def list_dir (self , prefix : str ) -> AsyncGenerator [str , None ]:
182178 """
183179 Retrieve all keys and prefixes with a given prefix and which do not contain the character
184180 “/” after the given prefix.
@@ -189,15 +185,16 @@ async def list_dir(self, prefix: str) -> List[str]:
189185
190186 Returns
191187 -------
192- list [str]
188+ AsyncGenerator [str, None ]
193189 """
190+ base = self .root / prefix
191+ to_strip = str (base ) + "/"
192+
193+ try :
194+ key_iter = base .iterdir ()
195+ except (FileNotFoundError , NotADirectoryError ):
196+ key_iter = []
194197
195- def _list_dir (root : Path , prefix : str ) -> List [str ]:
196- base = root / prefix
197- to_strip = str (base ) + "/"
198- try :
199- return [str (key ).replace (to_strip , "" ) for key in base .iterdir ()]
200- except (FileNotFoundError , NotADirectoryError ):
201- return []
198+ for key in key_iter :
199+ yield str (key ).replace (to_strip , "" )
202200
203- return await to_thread (_list_dir , self .root , prefix )
0 commit comments