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

217 "class": "tosfs.TosFileSystem", 

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

219 }, 

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

221 "webdav": { 

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

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

224 }, 

225 "webhdfs": { 

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

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

228 }, 

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

230} 

231 

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

233 "Not in alphabetical order" 

234) 

235 

236 

237def get_filesystem_class(protocol): 

238 """Fetch named protocol implementation from the registry 

239 

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

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

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

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

244 

245 Some protocol implementations require additional dependencies, and so the 

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

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

248 """ 

249 if not protocol: 

250 protocol = default 

251 

252 if protocol not in registry: 

253 if protocol not in known_implementations: 

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

255 bit = known_implementations[protocol] 

256 try: 

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

258 except ImportError as e: 

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

260 cls = registry[protocol] 

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

262 cls.protocol = protocol 

263 

264 return cls 

265 

266 

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

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

269 

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

271update the current installation. 

272""" 

273 

274 

275def _import_class(fqp: str): 

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

277 

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

279 "package.module:subobject.klass". 

280 

281 Warnings 

282 -------- 

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

284 that may execute malicious code at import time. 

285 """ 

286 if ":" in fqp: 

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

288 else: 

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

290 

291 is_s3 = mod == "s3fs" 

292 mod = importlib.import_module(mod) 

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

294 warnings.warn(s3_msg) 

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

296 mod = getattr(mod, part) 

297 

298 if not isinstance(mod, type): 

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

300 

301 return mod 

302 

303 

304def filesystem(protocol, **storage_options): 

305 """Instantiate filesystems for given protocol and arguments 

306 

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

308 passed directly to the class. 

309 """ 

310 if protocol == "arrow_hdfs": 

311 warnings.warn( 

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

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

314 DeprecationWarning, 

315 ) 

316 

317 cls = get_filesystem_class(protocol) 

318 return cls(**storage_options) 

319 

320 

321def available_protocols(): 

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

323 

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

325 """ 

326 return list(known_implementations)