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

51 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-07 06:56 +0000

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 "Name (%s) already in the known_implementations and clobber " 

42 "is False" % name 

43 ) 

44 else: 

45 known_implementations[name] = { 

46 "class": cls, 

47 "err": errtxt or "%s import failed for protocol %s" % (cls, 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 "Name (%s) already in the registry and clobber is False" % name 

55 ) 

56 else: 

57 _registry[name] = cls 

58 

59 

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

61# updated with register_implementation 

62known_implementations = { 

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

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

65 "dropbox": { 

66 "class": "dropboxdrivefs.DropboxDriveFileSystem", 

67 "err": ( 

68 'DropboxFileSystem requires "dropboxdrivefs",' 

69 '"requests" and "dropbox" to be installed' 

70 ), 

71 }, 

72 "http": { 

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

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

75 }, 

76 "https": { 

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

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

79 }, 

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

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

82 "gcs": { 

83 "class": "gcsfs.GCSFileSystem", 

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

85 }, 

86 "gs": { 

87 "class": "gcsfs.GCSFileSystem", 

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

89 }, 

90 "gdrive": { 

91 "class": "gdrivefs.GoogleDriveFileSystem", 

92 "err": "Please install gdrivefs for access to Google Drive", 

93 }, 

94 "sftp": { 

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

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

97 }, 

98 "ssh": { 

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

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

101 }, 

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

103 "hdfs": { 

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

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

106 }, 

107 "arrow_hdfs": { 

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

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

110 }, 

111 "webhdfs": { 

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

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

114 }, 

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

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

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

118 "oci": { 

119 "class": "ocifs.OCIFileSystem", 

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

121 }, 

122 "asynclocal": { 

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

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

125 }, 

126 "adl": { 

127 "class": "adlfs.AzureDatalakeFileSystem", 

128 "err": "Install adlfs to access Azure Datalake Gen1", 

129 }, 

130 "abfs": { 

131 "class": "adlfs.AzureBlobFileSystem", 

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

133 }, 

134 "az": { 

135 "class": "adlfs.AzureBlobFileSystem", 

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

137 }, 

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

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

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

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

142 "dask": { 

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

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

145 }, 

146 "dbfs": { 

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

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

149 }, 

150 "github": { 

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

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

153 }, 

154 "git": { 

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

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

157 }, 

158 "smb": { 

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

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

161 }, 

162 "jupyter": { 

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

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

165 }, 

166 "jlab": { 

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

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

169 }, 

170 "libarchive": { 

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

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

173 }, 

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

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

176 "oss": { 

177 "class": "ossfs.OSSFileSystem", 

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

179 }, 

180 "webdav": { 

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

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

183 }, 

184 "dvc": { 

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

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

187 }, 

188 "hf": { 

189 "class": "huggingface_hub.HfFileSystem", 

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

191 }, 

192 "root": { 

193 "class": "fsspec_xrootd.XRootDFileSystem", 

194 "err": "Install fsspec-xrootd to access xrootd storage system." 

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

196 + " not referring to root directories", 

197 }, 

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

199 "box": { 

200 "class": "boxfs.BoxFileSystem", 

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

202 }, 

203} 

204 

205 

206def get_filesystem_class(protocol): 

207 """Fetch named protocol implementation from the registry 

208 

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

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

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

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

213 

214 Some protocol implementations require additional dependencies, and so the 

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

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

217 """ 

218 if not protocol: 

219 protocol = default 

220 

221 if protocol not in registry: 

222 if protocol not in known_implementations: 

223 raise ValueError("Protocol not known: %s" % protocol) 

224 bit = known_implementations[protocol] 

225 try: 

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

227 except ImportError as e: 

228 raise ImportError(bit["err"]) from e 

229 cls = registry[protocol] 

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

231 cls.protocol = protocol 

232 

233 return cls 

234 

235 

236def _import_class(cls, minv=None): 

237 """Take a string FQP and return the imported class or identifier 

238 

239 clas is of the form "package.module.klass" or "package.module:subobject.klass" 

240 """ 

241 if ":" in cls: 

242 mod, name = cls.rsplit(":", 1) 

243 mod = importlib.import_module(mod) 

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

245 mod = getattr(mod, part) 

246 return mod 

247 else: 

248 mod, name = cls.rsplit(".", 1) 

249 mod = importlib.import_module(mod) 

250 return getattr(mod, name) 

251 

252 

253def filesystem(protocol, **storage_options): 

254 """Instantiate filesystems for given protocol and arguments 

255 

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

257 passed directly to the class. 

258 """ 

259 if protocol == "arrow_hdfs": 

260 warnings.warn( 

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

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

263 DeprecationWarning, 

264 ) 

265 

266 cls = get_filesystem_class(protocol) 

267 return cls(**storage_options) 

268 

269 

270def available_protocols(): 

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

272 

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

274 """ 

275 return list(known_implementations)