MCPcopy
hub / github.com/NVIDIA-NeMo/Speech / open_datastore_object_with_binary

Function open_datastore_object_with_binary

nemo/utils/data_utils.py:183–234  ·  view source on GitHub ↗

Open a datastore object and return a file-like object. Args: path: path to an object num_retries: number of retries if the get command fails with ais binary, as AIS Python SDK has its own retry mechanism Returns: File-like object that supports read()

(path: str, num_retries: int = 5)

Source from the content-addressed store, hash-verified

181
182
183def open_datastore_object_with_binary(path: str, num_retries: int = 5):
184 """Open a datastore object and return a file-like object.
185
186 Args:
187 path: path to an object
188 num_retries: number of retries if the get command fails with ais binary,
189 as AIS Python SDK has its own retry mechanism
190
191 Returns:
192 File-like object that supports read()
193 """
194
195 if is_datastore_path(path):
196 endpoint = ais_endpoint()
197 if endpoint is None:
198 raise RuntimeError(f'AIS endpoint not set, cannot resolve {path}')
199
200 binary = ais_binary()
201
202 if not binary:
203 raise RuntimeError(
204 f"AIS binary is not found, cannot resolve {path}. "
205 "Please either install it or install Lhotse with `pip install lhotse`.\n"
206 "Lhotse's native open_best supports AIS Python SDK, "
207 "which is the recommended way to operate with the data from AIStore.\n"
208 "See AIS binary installation instructions at "
209 "https://github.com/NVIDIA/aistore?tab=readme-ov-file#install-from-release-binaries.\n"
210 )
211
212 cmd = [binary, 'get', path, '-']
213
214 done = False
215
216 for _ in range(num_retries):
217 with subprocess.Popen(
218 cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=False # bytes mode
219 ) as proc:
220 stream = proc.stdout
221 if stream.peek(1):
222 done = True
223 return stream
224
225 if not done:
226 with subprocess.Popen(
227 cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=False
228 ) as proc:
229 error = proc.stderr.read().decode("utf-8", errors="ignore").strip()
230 raise ValueError(
231 f"{path} couldn't be opened with AIS binary "
232 f"after {num_retries} attempts because of the following exception: {error}"
233 )
234 return None
235
236
237def open_best(path: str, mode: str = "rb"):

Callers 1

open_bestFunction · 0.85

Calls 4

is_datastore_pathFunction · 0.85
ais_endpointFunction · 0.85
ais_binaryFunction · 0.85
decodeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…