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

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": "Install adlfs to access Azure Datalake Gen1", 

70 }, 

71 "arrow_hdfs": { 

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

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

74 }, 

75 "async_wrapper": { 

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

77 }, 

78 "asynclocal": { 

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

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

81 }, 

82 "asyncwrapper": { 

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

84 }, 

85 "az": { 

86 "class": "adlfs.AzureBlobFileSystem", 

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

88 }, 

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

90 "box": { 

91 "class": "boxfs.BoxFileSystem", 

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

93 }, 

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

95 "dask": { 

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

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

98 }, 

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

100 "dbfs": { 

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

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

103 }, 

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

105 "dropbox": { 

106 "class": "dropboxdrivefs.DropboxDriveFileSystem", 

107 "err": ( 

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

109 '"dropbox" to be installed' 

110 ), 

111 }, 

112 "dvc": { 

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

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

115 }, 

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

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

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

119 "gcs": { 

120 "class": "gcsfs.GCSFileSystem", 

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

122 }, 

123 "gdrive": { 

124 "class": "gdrive_fsspec.GoogleDriveFileSystem", 

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

126 }, 

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

128 "gist": { 

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

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

131 }, 

132 "git": { 

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

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

135 }, 

136 "github": { 

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

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

139 }, 

140 "gs": { 

141 "class": "gcsfs.GCSFileSystem", 

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

143 }, 

144 "hdfs": { 

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

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

147 }, 

148 "hf": { 

149 "class": "huggingface_hub.HfFileSystem", 

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

151 }, 

152 "http": { 

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

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

155 }, 

156 "https": { 

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

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

159 }, 

160 "jlab": { 

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

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

163 }, 

164 "jupyter": { 

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

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

167 }, 

168 "lakefs": { 

169 "class": "lakefs_spec.LakeFSFileSystem", 

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

171 }, 

172 "libarchive": { 

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

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

175 }, 

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

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

178 "oci": { 

179 "class": "ocifs.OCIFileSystem", 

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

181 }, 

182 "ocilake": { 

183 "class": "ocifs.OCIFileSystem", 

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

185 }, 

186 "oss": { 

187 "class": "ossfs.OSSFileSystem", 

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

189 }, 

190 "pyscript": { 

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

192 "err": "Install requests (cpython) or run in pyscript", 

193 }, 

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

195 "root": { 

196 "class": "fsspec_xrootd.XRootDFileSystem", 

197 "err": ( 

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

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

200 "not referring to root directories" 

201 ), 

202 }, 

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

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

205 "sftp": { 

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

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

208 }, 

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

210 "smb": { 

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

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

213 }, 

214 "ssh": { 

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

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

217 }, 

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

219 "tos": { 

220 "class": "tosfs.TosFileSystem", 

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

222 }, 

223 "tosfs": { 

224 "class": "tosfs.TosFileSystem", 

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

226 }, 

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

228 "webdav": { 

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

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

231 }, 

232 "webhdfs": { 

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

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

235 }, 

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

237} 

238 

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

240 "Not in alphabetical order" 

241) 

242 

243 

244def get_filesystem_class(protocol): 

245 """Fetch named protocol implementation from the registry 

246 

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

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

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

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

251 

252 Some protocol implementations require additional dependencies, and so the 

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

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

255 """ 

256 if not protocol: 

257 protocol = default 

258 

259 if protocol not in registry: 

260 if protocol not in known_implementations: 

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

262 bit = known_implementations[protocol] 

263 try: 

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

265 except ImportError as e: 

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

267 cls = registry[protocol] 

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

269 cls.protocol = protocol 

270 

271 return cls 

272 

273 

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

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

276 

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

278update the current installation. 

279""" 

280 

281 

282def _import_class(fqp: str): 

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

284 

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

286 "package.module:subobject.klass". 

287 

288 Warnings 

289 -------- 

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

291 that may execute malicious code at import time. 

292 """ 

293 if ":" in fqp: 

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

295 else: 

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

297 

298 is_s3 = mod == "s3fs" 

299 mod = importlib.import_module(mod) 

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

301 warnings.warn(s3_msg) 

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

303 mod = getattr(mod, part) 

304 

305 if not isinstance(mod, type): 

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

307 

308 return mod 

309 

310 

311def filesystem(protocol, **storage_options): 

312 """Instantiate filesystems for given protocol and arguments 

313 

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

315 passed directly to the class. 

316 """ 

317 if protocol == "arrow_hdfs": 

318 warnings.warn( 

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

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

321 DeprecationWarning, 

322 ) 

323 

324 cls = get_filesystem_class(protocol) 

325 return cls(**storage_options) 

326 

327 

328def available_protocols(): 

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

330 

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

332 """ 

333 return list(known_implementations)