-
Notifications
You must be signed in to change notification settings - Fork 376
/
Copy pathregistry.py
219 lines (184 loc) · 7.45 KB
/
registry.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import importlib
from distutils.version import LooseVersion
__all__ = ["registry", "get_filesystem_class", "default"]
# mapping protocol: implementation class object
_registry = {} # internal, mutable
class ReadOnlyError(TypeError):
pass
class ReadOnlyRegistry(dict):
"""Dict-like registry, but immutable
Maps backend name to implementation class
To add backend implementations, use ``register_implementation``
"""
def __init__(self, target):
self.target = target
def __getitem__(self, item):
return self.target[item]
def __delitem__(self, key):
raise ReadOnlyError
def __setitem__(self, key, value):
raise ReadOnlyError
def clear(self):
raise ReadOnlyError
def __contains__(self, item):
return item in self.target
def __iter__(self):
yield from self.target
def register_implementation(name, cls, clobber=True, errtxt=None):
"""Add implementation class to the registry
Parameters
----------
name: str
Protocol name to associate with the class
cls: class or str
if a class: fsspec-compliant implementation class (normally inherits from
``fsspec.AbstractFileSystem``, gets added straight to the registry. If a
str, the full path to an implementation class like package.module.class,
which gets added to known_implementations,
so the import is deferred until the filesystem is actually used.
clobber: bool (optional)
Whether to overwrite a protocol with the same name; if False, will raise
instead.
errtxt: str (optional)
If given, then a failure to import the given class will result in this
text being given.
"""
if isinstance(cls, str):
if name in known_implementations and clobber is False:
raise ValueError(
"Name (%s) already in the known_implementations and clobber "
"is False" % name
)
known_implementations[name] = {
"class": cls,
"err": errtxt or "%s import failed for protocol %s" % (cls, name),
}
else:
if name in registry and clobber is False:
raise ValueError(
"Name (%s) already in the registry and clobber is False" % name
)
_registry[name] = cls
registry = ReadOnlyRegistry(_registry)
default = "file"
# protocols mapped to the class which implements them. This dict can
# updated with register_implementation
known_implementations = {
"file": {"class": "fsspec.implementations.local.LocalFileSystem"},
"memory": {"class": "fsspec.implementations.memory.MemoryFileSystem"},
"dropbox": {
"class": "dropboxdrivefs.DropboxDriveFileSystem",
"err": (
'DropboxFileSystem requires "dropboxdrivefs",'
'"requests" and "dropbox" to be installed'
),
},
"http": {
"class": "fsspec.implementations.http.HTTPFileSystem",
"err": 'HTTPFileSystem requires "requests" to be installed',
},
"https": {
"class": "fsspec.implementations.http.HTTPFileSystem",
"err": 'HTTPFileSystem requires "requests" to be installed',
},
"zip": {"class": "fsspec.implementations.zip.ZipFileSystem"},
"gcs": {
"class": "gcsfs.GCSFileSystem",
"err": "Please install gcsfs to access Google Storage",
},
"gs": {
"class": "gcsfs.GCSFileSystem",
"err": "Please install gcsfs to access Google Storage",
},
"sftp": {
"class": "fsspec.implementations.sftp.SFTPFileSystem",
"err": 'SFTPFileSystem requires "paramiko" to be installed',
},
"ssh": {
"class": "fsspec.implementations.sftp.SFTPFileSystem",
"err": 'SFTPFileSystem requires "paramiko" to be installed',
},
"ftp": {"class": "fsspec.implementations.ftp.FTPFileSystem"},
"hdfs": {
"class": "fsspec.implementations.hdfs.PyArrowHDFS",
"err": "pyarrow and local java libraries required for HDFS",
},
"webhdfs": {
"class": "fsspec.implementations.webhdfs.WebHDFS",
"err": 'webHDFS access requires "requests" to be installed',
},
"s3": {"class": "s3fs.S3FileSystem", "err": "Install s3fs to access S3"},
"adl": {
"class": "adlfs.AzureDatalakeFileSystem",
"err": "Install adlfs to access Azure Datalake Gen1",
},
"abfs": {
"class": "adlfs.AzureBlobFileSystem",
"err": "Install adlfs to access Azure Datalake Gen2 and Azure Blob Storage",
},
"cached": {"class": "fsspec.implementations.cached.CachingFileSystem"},
"blockcache": {"class": "fsspec.implementations.cached.CachingFileSystem"},
"filecache": {"class": "fsspec.implementations.cached.WholeFileCacheFileSystem"},
"simplecache": {"class": "fsspec.implementations.cached.SimpleCacheFileSystem"},
"dask": {
"class": "fsspec.implementations.dask.DaskWorkerFileSystem",
"err": "Install dask distributed to access worker file system",
},
"github": {
"class": "fsspec.implementations.github.GithubFileSystem",
"err": "Install the requests package to use the github FS",
},
"git": {
"class": "fsspec.implementations.git.GitFileSystem",
"err": "Install pygit2 to browse local git repos",
},
"smb": {
"class": "fsspec.implementations.smb.SMBFileSystem",
"err": 'SMB requires "smbprotocol" or "smbprotocol[kerberos]" installed',
},
}
minversions = {"s3fs": LooseVersion("0.3.0"), "gcsfs": LooseVersion("0.3.0")}
def get_filesystem_class(protocol):
"""Fetch named protocol implementation from the registry
The dict ``known_implementations`` maps protocol names to the locations
of classes implementing the corresponding file-system. When used for the
first time, appropriate imports will happen and the class will be placed in
the registry. All subsequent calls will fetch directly from the registry.
Some protocol implementations require additional dependencies, and so the
import may fail. In this case, the string in the "err" field of the
``known_implementations`` will be given as the error message.
"""
if not protocol:
protocol = default
if protocol not in registry:
if protocol not in known_implementations:
raise ValueError("Protocol not known: %s" % protocol)
bit = known_implementations[protocol]
try:
register_implementation(protocol, _import_class(bit["class"]))
except ImportError as e:
raise ImportError(bit["err"]) from e
cls = registry[protocol]
if getattr(cls, "protocol", None) in ("abstract", None):
cls.protocol = protocol
return cls
def _import_class(cls, minv=None):
mod, name = cls.rsplit(".", 1)
minv = minv or minversions
minversion = minv.get(mod, None)
mod = importlib.import_module(mod)
if minversion:
version = getattr(mod, "__version__", None)
if version and LooseVersion(version) < minversion:
raise RuntimeError(
"'{}={}' is installed, but version '{}' or "
"higher is required".format(mod.__name__, version, minversion)
)
return getattr(mod, name)
def filesystem(protocol, **storage_options):
"""Instantiate filesystems for given protocol and arguments
``storage_options`` are specific to the protocol being chosen, and are
passed directly to the class.
"""
cls = get_filesystem_class(protocol)
return cls(**storage_options)