MCPcopy Index your code
hub / github.com/pydata/xarray / guess_engine

Function guess_engine

xarray/backends/plugins.py:139–217  ·  view source on GitHub ↗
(
    store_spec: str
    | os.PathLike[Any]
    | ReadBuffer
    | bytes
    | memoryview
    | AbstractDataStore,
    must_support_groups: bool = False,
)

Source from the content-addressed store, hash-verified

137
138
139def guess_engine(
140 store_spec: str
141 | os.PathLike[Any]
142 | ReadBuffer
143 | bytes
144 | memoryview
145 | AbstractDataStore,
146 must_support_groups: bool = False,
147) -> str | type[BackendEntrypoint]:
148 engines = list_engines()
149
150 for engine, backend in engines.items():
151 if must_support_groups and not backend.supports_groups:
152 continue
153 try:
154 if backend.guess_can_open(store_spec):
155 return engine
156 except PermissionError:
157 raise
158 except Exception:
159 warnings.warn(
160 f"{engine!r} fails while guessing", RuntimeWarning, stacklevel=2
161 )
162
163 compatible_engines = []
164 for engine, (_, backend_cls) in BACKEND_ENTRYPOINTS.items():
165 try:
166 backend = backend_cls()
167 if must_support_groups and not backend.supports_groups:
168 continue
169 if backend.guess_can_open(store_spec):
170 compatible_engines.append(engine)
171 except Exception:
172 warnings.warn(
173 f"{engine!r} fails while guessing", RuntimeWarning, stacklevel=2
174 )
175
176 installed_engines = [k for k in engines if k != "store"]
177 if not compatible_engines:
178 if installed_engines:
179 error_msg = (
180 "did not find a match in any of xarray's currently installed IO "
181 f"backends {installed_engines}. Consider explicitly selecting one of the "
182 "installed engines via the ``engine`` parameter, or installing "
183 "additional IO dependencies, see:\n"
184 "https://docs.xarray.dev/en/stable/getting-started-guide/installing.html\n"
185 "https://docs.xarray.dev/en/stable/user-guide/io.html"
186 )
187 elif must_support_groups:
188 error_msg = (
189 "xarray is unable to open this file because it has no currently "
190 "installed IO backends that support reading groups (e.g., h5netcdf "
191 "or netCDF4-python). Xarray's read/write support requires "
192 "installing optional IO dependencies, see:\n"
193 "https://docs.xarray.dev/en/stable/getting-started-guide/installing.html\n"
194 "https://docs.xarray.dev/en/stable/user-guide/io"
195 )
196 else:

Calls 4

is_remote_uriFunction · 0.90
list_enginesFunction · 0.85
itemsMethod · 0.80
guess_can_openMethod · 0.45

Tested by 1

Used in the wild real call sites across dependent graphs

searching dependent graphs…