Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/fsspec/registry.py: 71%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

56 statements  

1from __future__ import annotations 

2 

3import importlib 

4import types 

5import warnings 

6 

7__all__ = ["registry", "get_filesystem_class", "default"] 

8 

9# internal, mutable 

10_registry: dict[str, type] = {} 

11 

12# external, immutable 

13registry = types.MappingProxyType(_registry) 

14default = "file" 

15 

16 

17def register_implementation(name, cls, clobber=False, errtxt=None): 

18 """Add implementation class to the registry 

19 

20 Parameters 

21 ---------- 

22 name: str 

23 Protocol name to associate with the class 

24 cls: class or str 

25 if a class: fsspec-compliant implementation class (normally inherits from 

26 ``fsspec.AbstractFileSystem``, gets added straight to the registry. If a 

27 str, the full path to an implementation class like package.module.class, 

28 which gets added to known_implementations, 

29 so the import is deferred until the filesystem is actually used. 

30 clobber: bool (optional) 

31 Whether to overwrite a protocol with the same name; if False, will raise 

32 instead. 

33 errtxt: str (optional) 

34 If given, then a failure to import the given class will result in this 

35 text being given. 

36 """ 

37 if isinstance(cls, str): 

38 if name in known_implementations and clobber is False: 

39 if cls != known_implementations[name]["class"]: 

40 raise ValueError( 

41 f"Name ({name}) already in the known_implementations and clobber " 

42 f"is False" 

43 ) 

44 else: 

45 known_implementations[name] = { 

46 "class": cls, 

47 "err": errtxt or f"{cls} import failed for protocol {name}", 

48 } 

49 

50 else: 

51 if name in registry and clobber is False: 

52 if _registry[name] is not cls: 

53 raise ValueError( 

54 f"Name ({name}) already in the registry and clobber is False" 

55 ) 

56 else: 

57 _registry[name] = cls 

58 

59 

60# protocols mapped to the class which implements them. This dict can be 

61# updated with register_implementation 

62known_implementations = { 

63 "abfs": { 

64 "class": "adlfs.AzureBlobFileSystem", 

65 "err": "Install adlfs to access Azure Datalake Gen2 and Azure Blob Storage", 

66 }, 

67 "adl": { 

68 "class": "adlfs.AzureDatalakeFileSystem", 

69 "err": ( 

70 "Azure Data Lake Storage Gen1 is retired and no longer supported. Please " 

71 "install adlfs and use the `az://` protocol to access Azure Blob Storage " 

72 "and Azure Data Lake Storage Gen2 instead." 

73 ), 

74 }, 

75 "arrow_hdfs": { 

76 "class": "fsspec.implementations.arrow.HadoopFileSystem", 

77 "err": "pyarrow and local java libraries required for HDFS", 

78 }, 

79 "async_wrapper": { 

80 "class": "fsspec.implementations.asyn_wrapper.AsyncFileSystemWrapper", 

81 }, 

82 "asynclocal": { 

83 "class": "morefs.asyn_local.AsyncLocalFileSystem", 

84 "err": "Install 'morefs[asynclocalfs]' to use AsyncLocalFileSystem", 

85 }, 

86 "asyncwrapper": { 

87 "class": "fsspec.implementations.asyn_wrapper.AsyncFileSystemWrapper", 

88 }, 

89 "az": { 

90 "class": "adlfs.AzureBlobFileSystem", 

91 "err": "Install adlfs to access Azure Datalake Gen2 and Azure Blob Storage", 

92 }, 

93 "blockcache": {"class": "fsspec.implementations.cached.CachingFileSystem"}, 

94 "box": { 

95 "class": "boxfs.BoxFileSystem", 

96 "err": "Please install boxfs to access BoxFileSystem", 

97 }, 

98 "cached": {"class": "fsspec.implementations.cached.CachingFileSystem"}, 

99 "dask": { 

100 "class": "fsspec.implementations.dask.DaskWorkerFileSystem", 

101 "err": "Install dask distributed to access worker file system", 

102 }, 

103 "data": {"class": "fsspec.implementations.data.DataFileSystem"}, 

104 "dbfs": { 

105 "class": "fsspec.implementations.dbfs.DatabricksFileSystem", 

106 "err": "Install the requests package to use the DatabricksFileSystem", 

107 }, 

108 "dir": {"class": "fsspec.implementations.dirfs.DirFileSystem"}, 

109 "dropbox": { 

110 "class": "dropboxdrivefs.DropboxDriveFileSystem", 

111 "err": ( 

112 'DropboxFileSystem requires "dropboxdrivefs","requests" and "' 

113 '"dropbox" to be installed' 

114 ), 

115 }, 

116 "dvc": { 

117 "class": "dvc.api.DVCFileSystem", 

118 "err": "Install dvc to access DVCFileSystem", 

119 }, 

120 "file": {"class": "fsspec.implementations.local.LocalFileSystem"}, 

121 "filecache": {"class": "fsspec.implementations.cached.WholeFileCacheFileSystem"}, 

122 "ftp": {"class": "fsspec.implementations.ftp.FTPFileSystem"}, 

123 "gcs": { 

124 "class": "gcsfs.GCSFileSystem", 

125 "err": "Please install gcsfs to access Google Storage", 

126 }, 

127 "gdrive": { 

128 "class": "gdrive_fsspec.GoogleDriveFileSystem", 

129 "err": "Please install gdrive_fs for access to Google Drive", 

130 }, 

131 "generic": {"class": "fsspec.generic.GenericFileSystem"}, 

132 "gist": { 

133 "class": "fsspec.implementations.gist.GistFileSystem", 

134 "err": "Install the requests package to use the gist FS", 

135 }, 

136 "git": { 

137 "class": "fsspec.implementations.git.GitFileSystem", 

138 "err": "Install pygit2 to browse local git repos", 

139 }, 

140 "github": { 

141 "class": "fsspec.implementations.github.GithubFileSystem", 

142 "err": "Install the requests package to use the github FS", 

143 }, 

144 "gs": { 

145 "class": "gcsfs.GCSFileSystem", 

146 "err": "Please install gcsfs to access Google Storage", 

147 }, 

148 "hdfs": { 

149 "class": "fsspec.implementations.arrow.HadoopFileSystem", 

150 "err": "pyarrow and local java libraries required for HDFS", 

151 }, 

152 "hf": { 

153 "class": "huggingface_hub.HfFileSystem", 

154 "err": "Install huggingface_hub to access HfFileSystem", 

155 }, 

156 "http": { 

157 "class": "fsspec.implementations.http.HTTPFileSystem", 

158 "err": 'HTTPFileSystem requires "requests" and "aiohttp" to be installed', 

159 }, 

160 "https": { 

161 "class": "fsspec.implementations.http.HTTPFileSystem", 

162 "err": 'HTTPFileSystem requires "requests" and "aiohttp" to be installed', 

163 }, 

164 "jlab": { 

165 "class": "fsspec.implementations.jupyter.JupyterFileSystem", 

166 "err": "Jupyter FS requires requests to be installed", 

167 }, 

168 "jupyter": { 

169 "class": "fsspec.implementations.jupyter.JupyterFileSystem", 

170 "err": "Jupyter FS requires requests to be installed", 

171 }, 

172 "lakefs": { 

173 "class": "lakefs_spec.LakeFSFileSystem", 

174 "err": "Please install lakefs-spec to access LakeFSFileSystem", 

175 }, 

176 "libarchive": { 

177 "class": "fsspec.implementations.libarchive.LibArchiveFileSystem", 

178 "err": "LibArchive requires to be installed", 

179 }, 

180 "local": {"class": "fsspec.implementations.local.LocalFileSystem"}, 

181 "memory": {"class": "fsspec.implementations.memory.MemoryFileSystem"}, 

182 "oci": { 

183 "class": "ocifs.OCIFileSystem", 

184 "err": "Install ocifs to access OCI Object Storage", 

185 }, 

186 "ocilake": { 

187 "class": "ocifs.OCIFileSystem", 

188 "err": "Install ocifs to access OCI Data Lake", 

189 }, 

190 "oss": { 

191 "class": "ossfs.OSSFileSystem", 

192 "err": "Install ossfs to access Alibaba Object Storage System", 

193 }, 

194 "pyscript": { 

195 "class": "pyscript_fsspec_client.client.PyscriptFileSystem", 

196 "err": "This only runs in a pyscript context", 

197 }, 

198 "reference": {"class": "fsspec.implementations.reference.ReferenceFileSystem"}, 

199 "root": { 

200 "class": "fsspec_xrootd.XRootDFileSystem", 

201 "err": ( 

202 "Install fsspec-xrootd to access xrootd storage system. " 

203 "Note: 'root' is the protocol name for xrootd storage systems, " 

204 "not referring to root directories" 

205 ), 

206 }, 

207 "s3": {"class": "s3fs.S3FileSystem", "err": "Install s3fs to access S3"}, 

208 "s3a": {"class": "s3fs.S3FileSystem", "err": "Install s3fs to access S3"}, 

209 "sftp": { 

210 "class": "fsspec.implementations.sftp.SFTPFileSystem", 

211 "err": 'SFTPFileSystem requires "paramiko" to be installed', 

212 }, 

213 "simplecache": {"class": "fsspec.implementations.cached.SimpleCacheFileSystem"}, 

214 "smb": { 

215 "class": "fsspec.implementations.smb.SMBFileSystem", 

216 "err": 'SMB requires "smbprotocol" or "smbprotocol[kerberos]" installed', 

217 }, 

218 "ssh": { 

219 "class": "fsspec.implementations.sftp.SFTPFileSystem", 

220 "err": 'SFTPFileSystem requires "paramiko" to be installed', 

221 }, 

222 "tar": {"class": "fsspec.implementations.tar.TarFileSystem"}, 

223 "tos": { 

224 "class": "tosfs.TosFileSystem", 

225 "err": "Install tosfs to access ByteDance volcano engine Tinder Object Storage", 

226 }, 

227 "tosfs": { 

228 "class": "tosfs.TosFileSystem", 

229 "err": "Install tosfs to access ByteDance volcano engine Tinder Object Storage", 

230 }, 

231 "wandb": {"class": "wandbfs.WandbFS", "err": "Install wandbfs to access wandb"}, 

232 "webdav": { 

233 "class": "webdav4.fsspec.WebdavFileSystem", 

234 "err": "Install webdav4 to access WebDAV", 

235 }, 

236 "webhdfs": { 

237 "class": "fsspec.implementations.webhdfs.WebHDFS", 

238 "err": 'webHDFS access requires "requests" to be installed', 

239 }, 

240 "zip": {"class": "fsspec.implementations.zip.ZipFileSystem"}, 

241} 

242 

243assert list(known_implementations) == sorted(known_implementations), ( 

244 "Not in alphabetical order" 

245) 

246 

247 

248def get_filesystem_class(protocol): 

249 """Fetch named protocol implementation from the registry 

250 

251 The dict ``known_implementations`` maps protocol names to the locations 

252 of classes implementing the corresponding file-system. When used for the 

253 first time, appropriate imports will happen and the class will be placed in 

254 the registry. All subsequent calls will fetch directly from the registry. 

255 

256 Some protocol implementations require additional dependencies, and so the 

257 import may fail. In this case, the string in the "err" field of the 

258 ``known_implementations`` will be given as the error message. 

259 """ 

260 if not protocol: 

261 protocol = default 

262 

263 if protocol not in registry: 

264 if protocol not in known_implementations: 

265 raise ValueError(f"Protocol not known: {protocol}") 

266 bit = known_implementations[protocol] 

267 try: 

268 register_implementation(protocol, _import_class(bit["class"])) 

269 except ImportError as e: 

270 raise ImportError(bit.get("err")) from e 

271 cls = registry[protocol] 

272 if getattr(cls, "protocol", None) in ("abstract", None): 

273 cls.protocol = protocol 

274 

275 return cls 

276 

277 

278s3_msg = """Your installed version of s3fs is very old and known to cause 

279severe performance issues, see also https://github.com/dask/dask/issues/10276 

280 

281To fix, you should specify a lower version bound on s3fs, or 

282update the current installation. 

283""" 

284 

285 

286def _import_class(fqp: str): 

287 """Take a fully-qualified path and return the imported class or identifier. 

288 

289 ``fqp`` is of the form "package.module.klass" or 

290 "package.module:subobject.klass". 

291 

292 Warnings 

293 -------- 

294 This can import arbitrary modules. Make sure you haven't installed any modules 

295 that may execute malicious code at import time. 

296 """ 

297 if ":" in fqp: 

298 mod, name = fqp.rsplit(":", 1) 

299 else: 

300 mod, name = fqp.rsplit(".", 1) 

301 

302 is_s3 = mod == "s3fs" 

303 mod = importlib.import_module(mod) 

304 if is_s3 and mod.__version__.split(".") < ["0", "5"]: 

305 warnings.warn(s3_msg) 

306 for part in name.split("."): 

307 mod = getattr(mod, part) 

308 

309 if not isinstance(mod, type): 

310 raise TypeError(f"{fqp} is not a class") 

311 

312 return mod 

313 

314 

315def filesystem(protocol, **storage_options): 

316 """Instantiate filesystems for given protocol and arguments 

317 

318 ``storage_options`` are specific to the protocol being chosen, and are 

319 passed directly to the class. 

320 """ 

321 if protocol == "arrow_hdfs": 

322 warnings.warn( 

323 "The 'arrow_hdfs' protocol has been deprecated and will be " 

324 "removed in the future. Specify it as 'hdfs'.", 

325 DeprecationWarning, 

326 ) 

327 

328 cls = get_filesystem_class(protocol) 

329 return cls(**storage_options) 

330 

331 

332def available_protocols(): 

333 """Return a list of the implemented protocols. 

334 

335 Note that any given protocol may require extra packages to be importable. 

336 """ 

337 return list(known_implementations)