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

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 "asynclocal": { 

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

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

78 }, 

79 "asyncwrapper": { 

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

81 }, 

82 "az": { 

83 "class": "adlfs.AzureBlobFileSystem", 

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

85 }, 

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

87 "box": { 

88 "class": "boxfs.BoxFileSystem", 

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

90 }, 

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

92 "dask": { 

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

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

95 }, 

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

97 "dbfs": { 

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

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

100 }, 

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

102 "dropbox": { 

103 "class": "dropboxdrivefs.DropboxDriveFileSystem", 

104 "err": ( 

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

106 '"dropbox" to be installed' 

107 ), 

108 }, 

109 "dvc": { 

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

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

112 }, 

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

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

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

116 "gcs": { 

117 "class": "gcsfs.GCSFileSystem", 

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

119 }, 

120 "gdrive": { 

121 "class": "gdrive_fsspec.GoogleDriveFileSystem", 

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

123 }, 

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

125 "gist": { 

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

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

128 }, 

129 "git": { 

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

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

132 }, 

133 "github": { 

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

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

136 }, 

137 "gs": { 

138 "class": "gcsfs.GCSFileSystem", 

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

140 }, 

141 "hdfs": { 

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

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

144 }, 

145 "hf": { 

146 "class": "huggingface_hub.HfFileSystem", 

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

148 }, 

149 "http": { 

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

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

152 }, 

153 "https": { 

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

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

156 }, 

157 "jlab": { 

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

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

160 }, 

161 "jupyter": { 

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

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

164 }, 

165 "lakefs": { 

166 "class": "lakefs_spec.LakeFSFileSystem", 

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

168 }, 

169 "libarchive": { 

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

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

172 }, 

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

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

175 "oci": { 

176 "class": "ocifs.OCIFileSystem", 

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

178 }, 

179 "ocilake": { 

180 "class": "ocifs.OCIFileSystem", 

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

182 }, 

183 "oss": { 

184 "class": "ossfs.OSSFileSystem", 

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

186 }, 

187 "pyscript": { 

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

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

190 }, 

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

192 "root": { 

193 "class": "fsspec_xrootd.XRootDFileSystem", 

194 "err": ( 

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

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

197 "not referring to root directories" 

198 ), 

199 }, 

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

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

202 "sftp": { 

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

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

205 }, 

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

207 "smb": { 

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

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

210 }, 

211 "ssh": { 

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

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

214 }, 

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

216 "tos": { 

217 "class": "tosfs.TosFileSystem", 

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

219 }, 

220 "tosfs": { 

221 "class": "tosfs.TosFileSystem", 

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

223 }, 

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

225 "webdav": { 

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

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

228 }, 

229 "webhdfs": { 

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

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

232 }, 

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

234} 

235 

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

237 "Not in alphabetical order" 

238) 

239 

240 

241def get_filesystem_class(protocol): 

242 """Fetch named protocol implementation from the registry 

243 

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

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

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

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

248 

249 Some protocol implementations require additional dependencies, and so the 

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

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

252 """ 

253 if not protocol: 

254 protocol = default 

255 

256 if protocol not in registry: 

257 if protocol not in known_implementations: 

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

259 bit = known_implementations[protocol] 

260 try: 

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

262 except ImportError as e: 

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

264 cls = registry[protocol] 

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

266 cls.protocol = protocol 

267 

268 return cls 

269 

270 

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

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

273 

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

275update the current installation. 

276""" 

277 

278 

279def _import_class(fqp: str): 

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

281 

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

283 "package.module:subobject.klass". 

284 

285 Warnings 

286 -------- 

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

288 that may execute malicious code at import time. 

289 """ 

290 if ":" in fqp: 

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

292 else: 

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

294 

295 is_s3 = mod == "s3fs" 

296 mod = importlib.import_module(mod) 

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

298 warnings.warn(s3_msg) 

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

300 mod = getattr(mod, part) 

301 

302 if not isinstance(mod, type): 

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

304 

305 return mod 

306 

307 

308def filesystem(protocol, **storage_options): 

309 """Instantiate filesystems for given protocol and arguments 

310 

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

312 passed directly to the class. 

313 """ 

314 if protocol == "arrow_hdfs": 

315 warnings.warn( 

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

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

318 DeprecationWarning, 

319 ) 

320 

321 cls = get_filesystem_class(protocol) 

322 return cls(**storage_options) 

323 

324 

325def available_protocols(): 

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

327 

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

329 """ 

330 return list(known_implementations)