-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathrepositories.py
339 lines (290 loc) · 12.7 KB
/
repositories.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# Copyright 2018-2021
# Institute of Neuroscience and Medicine (INM-1), Forschungszentrum Jülich GmbH
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from .requests import DECODERS, HttpRequest, EbrainsRequest, SiibraHttpRequestError
from .. import logger
from abc import ABC, abstractmethod
from urllib.parse import quote
from tqdm import tqdm
class RepositoryConnector(ABC):
"""
Base class for repository connectors.
"""
def __init__(self, base_url):
self.base_url = base_url
@abstractmethod
def search_files(folder: str, suffix: str, recursive: bool = False):
pass
@abstractmethod
def _build_url(self, folder: str, filename: str):
pass
def _decode_response(self, response, filename):
# see if we find a default encoder
suitable_decoders = [
dec for sfx, dec in DECODERS.items() if filename.endswith(sfx)
]
if len(suitable_decoders) > 0:
assert len(suitable_decoders) == 1
return suitable_decoders[0](response)
else:
return response
def get(self, filename, folder="", decode_func=None):
"""Get a file right away."""
return self.get_loader(filename, folder, decode_func).data
def get_loader(self, filename, folder="", decode_func=None):
"""Get a lazy loader for a file, for executing the query
only once loader.data is accessed."""
url = self._build_url(folder, filename)
if url is None:
raise RuntimeError(f"Cannot build url for ({folder}, {filename})")
if decode_func is None:
return HttpRequest(url, lambda b: self._decode_response(b, filename))
else:
return HttpRequest(url, decode_func)
def get_loaders(
self, folder="", suffix=None, progress=None, recursive=False, decode_func=None
):
"""
Returns an iterator with lazy loaders for the files in a given folder.
In each iteration, a tuple (filename,file content) is returned.
"""
fnames = self.search_files(folder, suffix, recursive)
result = [
(fname, self.get_loader(fname, decode_func=decode_func)) for fname in fnames
]
all_cached = all(_[1].cached for _ in result)
if progress is None or all_cached:
return result
else:
return tqdm(
result, total=len(fnames), desc=progress, disable=logger.level > 20
)
class GitlabConnector(RepositoryConnector):
def __init__(self, server: str, project: int, reftag: str, skip_branchtest=False):
# TODO: the query builder needs to check wether the reftag is a branch, and then not cache.
assert server.startswith("http")
RepositoryConnector.__init__(
self, base_url=f"{server}/api/v4/projects/{project}/repository"
)
self.reftag = reftag
self._per_page = 100
self._branchloader = HttpRequest(
f"{self.base_url}/branches", DECODERS[".json"], refresh=True
)
self._tag_checked = True if skip_branchtest else False
self._want_commit_cached = None
def __str__(self):
return f"{self.__class__.__name__} {self.base_url} {self.reftag}"
@property
def want_commit(self):
if not self._tag_checked:
try:
matched_branches = list(
filter(lambda b: b["name"] == self.reftag, self.branches)
)
if len(matched_branches) > 0:
self._want_commit_cached = matched_branches[0]["commit"]
logger.debug(
f"{self.reftag} is a branch of {self.base_url}! Want last commit "
f"{self._want_commit_cached['short_id']} from "
f"{self._want_commit_cached['created_at']}"
)
self._tag_checked = True
except Exception as e:
print(str(e))
print("Could not connect to gitlab server!")
return self._want_commit_cached
@property
def branches(self):
return self._branchloader.data
def _build_url(self, folder="", filename=None, recursive=False, page=1):
ref = self.reftag if self.want_commit is None else self.want_commit["short_id"]
if filename is None:
pathstr = "" if len(folder) == 0 else f"&path={quote(folder,safe='')}"
return f"{self.base_url}/tree?ref={ref}{pathstr}&per_page={self._per_page}&page={page}&recursive={recursive}"
else:
pathstr = filename if folder == "" else f"{folder}/{filename}"
filepath = quote(pathstr, safe="")
return f"{self.base_url}/files/{filepath}/raw?ref={ref}"
def search_files(self, folder="", suffix=None, recursive=False):
page = 1
results = []
while True:
loader = HttpRequest(
self._build_url(folder, recursive=recursive, page=page),
DECODERS[".json"],
)
results.extend(loader.data)
if len(loader.data) < self._per_page:
# no more pages
break
page += 1
end = "" if suffix is None else suffix
return [
e["path"]
for e in results
if e["type"] == "blob" and e["name"].endswith(end)
]
class OwncloudConnector(RepositoryConnector):
def __init__(self, server: str, share: int):
RepositoryConnector.__init__(self, base_url=f"{server}/s/{share}")
def search_files(self, folder="", suffix=None, recursive=False):
raise NotImplementedError(
f"File search in folders not implemented for {self.__class__.__name__}."
)
def _build_url(self, folder, filename):
fpath = "" if folder == "" else f"path={quote(folder,safe='')}&"
fpath += f"files={quote(filename)}"
url = f"{self.base_url}/download?{fpath}"
return url
class EbrainsHdgConnector(RepositoryConnector):
"""Download sensitive files from EBRAINS using
the Human Data Gateway (HDG) via the data proxy API.
"""
base_url = "https://data-proxy.ebrains.eu/api/datasets"
maxentries = 1000
def __init__(self, dataset_id):
"""Construct a dataset query for the Human Data Gateway.
Parameters
----------
dataset_id : str
EBRAINS dataset id for a dataset that is exposed
via the human data gateway.
"""
self._files = []
self.dataset_id = dataset_id
marker = None
while True:
# The endpoint implements basic pagination, using the filenames as markers.
if marker is None:
url = f"{self.base_url}/{dataset_id}?limit={self.maxentries}"
else:
url = f"{self.base_url}/{dataset_id}?limit={self.maxentries}&marker={marker}"
try:
result = EbrainsRequest(url, DECODERS[".json"]).get()
except SiibraHttpRequestError as e:
if e.response.status_code in [401, 422]:
# Request access to the dataset (401: expired, 422: not yet requested)
EbrainsRequest(f"{self.base_url}/{dataset_id}", post=True).get()
input(
"You should have received an email with a confirmation link - "
"please find that email and click on the link, then press enter "
"to continue"
)
continue
else:
raise RuntimeError(
f"Could not request private file links for dataset {dataset_id}. "
f"Status code was: {e.response.status_code}. "
f"Message was: {e.response.text}. "
)
newfiles = result["objects"]
self._files.extend(newfiles)
logger.debug(f"{len(newfiles)} of {self.maxentries} objects returned.")
if len(newfiles) == self.maxentries:
# there might be more files
marker = newfiles[-1]["name"]
else:
logger.info(
f"{len(self._files)} objects found for dataset {dataset_id} returned."
)
self.container = result["container"]
self.prefix = result["prefix"]
break
def search_files(self, folder="", suffix=None, recursive=False):
result = []
for f in self._files:
if f["name"].startswith(folder):
if suffix is None:
result.append(f["name"])
else:
if f["name"].endswith(suffix):
result.append(f["name"])
return result
def _build_url(self, folder, filename):
if len(folder) > 0:
fpath = quote(f"{folder}/{filename}", safe="")
else:
fpath = quote(f"{filename}", safe="")
url = f"{self.base_url}/{self.dataset_id}/{fpath}?redirect=true"
return url
def get_loader(self, filename, folder="", decode_func=None):
"""Get a lazy loader for a file, for executing the query
only once loader.data is accessed."""
return EbrainsRequest(self._build_url(folder, filename), decode_func)
class EbrainsPublicDatasetConnector(RepositoryConnector):
"""Access files from public EBRAINS datasets via the Knowledge Graph v3 API."""
QUERY_ID = "bebbe365-a0d6-41ea-9ff8-2554c15f70b7"
base_url = "https://core.kg.ebrains.eu/v3-beta/queries/"
maxentries = 1000
def __init__(self, dataset_id, in_progress=False):
"""Construct a dataset query with the dataset id.
Parameters
----------
dataset_id : str
EBRAINS dataset id of a public dataset in KG v3.
in_progress: bool (default:False)
If true, will request datasets that are still under curation.
Will only work when autenticated with an appropriately privileged
user account.
"""
self.dataset_id = dataset_id
stage = "IN_PROGRESS" if in_progress else "RELEASED"
url = f"{self.base_url}/{self.QUERY_ID}/instances?stage={stage}&dataset_id={dataset_id}"
result = EbrainsRequest(url, DECODERS[".json"]).get()
self.versions = {}
self.use_version = None
assert len(result["data"]) < 2
if len(result["data"]) == 1:
data = result["data"][0]
self.versions = {v["versionIdentifier"]: v for v in data["versions"]}
self.use_version = sorted(list(self.versions.keys()))[-1]
if len(self.versions) > 1:
logger.info(
f"Found {len(self.versions)} versions for dataset '{data['name']}' "
f"({', '.join(self.versions.keys())}). "
f"Will use {self.use_version} per default."
)
@property
def doi(self):
if self.use_version in self.versions:
return self.versions[self.use_version]["doi"]
else:
return None
@property
def _files(self):
if self.use_version in self.versions:
return {
f["name"]: f["url"] for f in self.versions[self.use_version]["files"]
}
else:
return {}
def search_files(self, folder="", suffix=None, recursive=False):
result = []
for fname in self._files:
if fname.startswith(folder):
if suffix is None:
result.append(fname)
else:
if fname.endswith(suffix):
result.append(fname)
return result
def _build_url(self, folder, filename):
fpath = f"{folder}/{filename}" if len(folder) > 0 else f"{filename}"
if fpath not in self._files:
raise RuntimeError(
f"The file {fpath} requested from EBRAINS dataset {self.dataset_id} cannot be."
)
return self._files[fpath]
def get_loader(self, filename, folder="", decode_func=None):
"""Get a lazy loader for a file, for executing the query
only once loader.data is accessed."""
return HttpRequest(self._build_url(folder, filename), decode_func)