ebi-eva-common-pyutils 0.6.7__py3-none-any.whl → 0.6.8__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ebi_eva_common_pyutils/biosamples_communicators.py +180 -0
- {ebi_eva_common_pyutils-0.6.7.dist-info → ebi_eva_common_pyutils-0.6.8.dist-info}/METADATA +3 -3
- {ebi_eva_common_pyutils-0.6.7.dist-info → ebi_eva_common_pyutils-0.6.8.dist-info}/RECORD +7 -6
- {ebi_eva_common_pyutils-0.6.7.dist-info → ebi_eva_common_pyutils-0.6.8.dist-info}/WHEEL +1 -1
- {ebi_eva_common_pyutils-0.6.7.data → ebi_eva_common_pyutils-0.6.8.data}/scripts/archive_directory.py +0 -0
- {ebi_eva_common_pyutils-0.6.7.dist-info → ebi_eva_common_pyutils-0.6.8.dist-info}/LICENSE +0 -0
- {ebi_eva_common_pyutils-0.6.7.dist-info → ebi_eva_common_pyutils-0.6.8.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
# Copyright 2020 EMBL - European Bioinformatics Institute
|
|
3
|
+
#
|
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
# you may not use this file except in compliance with the License.
|
|
6
|
+
# You may obtain a copy of the License at
|
|
7
|
+
#
|
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
#
|
|
10
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
# See the License for the specific language governing permissions and
|
|
14
|
+
# limitations under the License.
|
|
15
|
+
|
|
16
|
+
import re
|
|
17
|
+
|
|
18
|
+
import requests
|
|
19
|
+
from functools import cached_property
|
|
20
|
+
from ebi_eva_common_pyutils.logger import AppLogger
|
|
21
|
+
from retry import retry
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class HALNotReadyError(Exception):
|
|
25
|
+
pass
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class HALCommunicator(AppLogger):
|
|
29
|
+
"""
|
|
30
|
+
This class helps navigate through REST API that uses the HAL standard.
|
|
31
|
+
"""
|
|
32
|
+
acceptable_code = [200, 201]
|
|
33
|
+
|
|
34
|
+
def __init__(self, auth_url, bsd_url, username, password):
|
|
35
|
+
self.auth_url = auth_url
|
|
36
|
+
self.bsd_url = bsd_url
|
|
37
|
+
self.username = username
|
|
38
|
+
self.password = password
|
|
39
|
+
|
|
40
|
+
def _validate_response(self, response):
|
|
41
|
+
"""Check that the response has an acceptable code and raise if it does not"""
|
|
42
|
+
if response.status_code not in self.acceptable_code:
|
|
43
|
+
self.error(response.request.method + ': ' + response.request.url + " with " + str(response.request.body))
|
|
44
|
+
self.error("headers: {}".format(response.request.headers))
|
|
45
|
+
self.error("<{}>: {}".format(response.status_code, response.text))
|
|
46
|
+
raise ValueError('The HTTP status code ({}) is not one of the acceptable codes ({})'.format(
|
|
47
|
+
str(response.status_code), str(self.acceptable_code))
|
|
48
|
+
)
|
|
49
|
+
return response
|
|
50
|
+
|
|
51
|
+
@cached_property
|
|
52
|
+
def token(self):
|
|
53
|
+
"""Retrieve the token from the AAP REST API then cache it for further quering"""
|
|
54
|
+
response = requests.get(self.auth_url, auth=(self.username, self.password))
|
|
55
|
+
self._validate_response(response)
|
|
56
|
+
return response.text
|
|
57
|
+
|
|
58
|
+
@retry(exceptions=(ValueError, requests.RequestException), tries=3, delay=2, backoff=1.2, jitter=(1, 3))
|
|
59
|
+
def _req(self, method, url, **kwargs):
|
|
60
|
+
"""Private method that sends a request using the specified method. It adds the headers required by bsd"""
|
|
61
|
+
headers = kwargs.pop('headers', {})
|
|
62
|
+
headers.update({'Accept': 'application/hal+json'})
|
|
63
|
+
if self.token is not None:
|
|
64
|
+
headers.update({'Authorization': 'Bearer ' + self.token})
|
|
65
|
+
if 'json' in kwargs:
|
|
66
|
+
headers['Content-Type'] = 'application/json'
|
|
67
|
+
response = requests.request(
|
|
68
|
+
method=method,
|
|
69
|
+
url=url,
|
|
70
|
+
headers=headers,
|
|
71
|
+
**kwargs
|
|
72
|
+
)
|
|
73
|
+
self._validate_response(response)
|
|
74
|
+
return response
|
|
75
|
+
|
|
76
|
+
def follows(self, query, json_obj=None, method='GET', url_template_values=None, join_url=None, **kwargs):
|
|
77
|
+
"""
|
|
78
|
+
Finds a link within the json_obj using a query string or list, modify the link using the
|
|
79
|
+
url_template_values dictionary then query the link using the method and any additional keyword argument.
|
|
80
|
+
If the json_obj is not specified then it will use the root query defined by the base url.
|
|
81
|
+
"""
|
|
82
|
+
all_pages = kwargs.pop('all_pages', False)
|
|
83
|
+
|
|
84
|
+
if json_obj is None:
|
|
85
|
+
json_obj = self.root
|
|
86
|
+
# Drill down into a dict using dot notation
|
|
87
|
+
_json_obj = json_obj
|
|
88
|
+
if isinstance(query, str):
|
|
89
|
+
query_list = query.split('.')
|
|
90
|
+
else:
|
|
91
|
+
query_list = query
|
|
92
|
+
for query_element in query_list:
|
|
93
|
+
if query_element in _json_obj:
|
|
94
|
+
_json_obj = _json_obj[query_element]
|
|
95
|
+
else:
|
|
96
|
+
raise KeyError('{} does not exist in json object'.format(query_element, _json_obj))
|
|
97
|
+
if not isinstance(_json_obj, str):
|
|
98
|
+
raise ValueError('The result of the query_string must be a string to use as a url')
|
|
99
|
+
url = _json_obj
|
|
100
|
+
# replace the template in the url with the value provided
|
|
101
|
+
if url_template_values:
|
|
102
|
+
for k, v in url_template_values.items():
|
|
103
|
+
url = re.sub('{(' + k + ')(:.*)?}', v, url)
|
|
104
|
+
if join_url:
|
|
105
|
+
url += '/' + join_url
|
|
106
|
+
# Now query the url
|
|
107
|
+
json_response = self._req(method, url, **kwargs).json()
|
|
108
|
+
|
|
109
|
+
# Depaginate the call if requested
|
|
110
|
+
if all_pages is True:
|
|
111
|
+
# This depagination code will iterate over all the pages available until the pages comes back without a
|
|
112
|
+
# next page. It stores the embedded elements in the initial query's json response
|
|
113
|
+
content = json_response
|
|
114
|
+
while 'next' in content.get('_links'):
|
|
115
|
+
content = self._req(method, content.get('_links').get('next').get('href'), **kwargs).json()
|
|
116
|
+
for key in content.get('_embedded'):
|
|
117
|
+
json_response['_embedded'][key].extend(content.get('_embedded').get(key))
|
|
118
|
+
# Remove the pagination information as it is not relevant to the depaginated response
|
|
119
|
+
if 'page' in json_response: json_response.pop('page')
|
|
120
|
+
if 'first' in json_response['_links']: json_response['_links'].pop('first')
|
|
121
|
+
if 'last' in json_response['_links']: json_response['_links'].pop('last')
|
|
122
|
+
if 'next' in json_response['_links']: json_response['_links'].pop('next')
|
|
123
|
+
return json_response
|
|
124
|
+
|
|
125
|
+
def follows_link(self, key, json_obj=None, method='GET', url_template_values=None, join_url=None, **kwargs):
|
|
126
|
+
"""
|
|
127
|
+
Same function as follows but construct the query_string from a single keyword surrounded by '_links' and 'href'.
|
|
128
|
+
"""
|
|
129
|
+
return self.follows(('_links', key, 'href'),
|
|
130
|
+
json_obj=json_obj, method=method, url_template_values=url_template_values,
|
|
131
|
+
join_url=join_url, **kwargs)
|
|
132
|
+
|
|
133
|
+
@cached_property
|
|
134
|
+
def root(self):
|
|
135
|
+
return self._req('GET', self.bsd_url).json()
|
|
136
|
+
|
|
137
|
+
@property
|
|
138
|
+
def communicator_attributes(self):
|
|
139
|
+
raise NotImplementedError
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
class AAPHALCommunicator(HALCommunicator):
|
|
143
|
+
"""Class to navigate BioSamples API using AAP authentication."""
|
|
144
|
+
|
|
145
|
+
def __init__(self, auth_url, bsd_url, username, password, domain=None):
|
|
146
|
+
super(AAPHALCommunicator, self).__init__(auth_url, bsd_url, username, password)
|
|
147
|
+
self.domain = domain
|
|
148
|
+
|
|
149
|
+
@property
|
|
150
|
+
def communicator_attributes(self):
|
|
151
|
+
return {'domain': self.domain}
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
class WebinHALCommunicator(HALCommunicator):
|
|
155
|
+
"""Class to navigate BioSamples API using Webin authentication."""
|
|
156
|
+
|
|
157
|
+
@cached_property
|
|
158
|
+
def token(self):
|
|
159
|
+
"""Retrieve the token from the ENA Webin REST API then cache it for further querying"""
|
|
160
|
+
response = requests.post(self.auth_url,
|
|
161
|
+
json={"authRealms": ["ENA"], "password": self.password,
|
|
162
|
+
"username": self.username})
|
|
163
|
+
self._validate_response(response)
|
|
164
|
+
return response.text
|
|
165
|
+
|
|
166
|
+
@property
|
|
167
|
+
def communicator_attributes(self):
|
|
168
|
+
return {'webinSubmissionAccountId': self.username}
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
class NoAuthHALCommunicator(HALCommunicator):
|
|
172
|
+
"""Class to navigate BioSamples API without authentication."""
|
|
173
|
+
|
|
174
|
+
def __init__(self, bsd_url):
|
|
175
|
+
super(NoAuthHALCommunicator, self).__init__(None, bsd_url, None, None)
|
|
176
|
+
|
|
177
|
+
@cached_property
|
|
178
|
+
def token(self):
|
|
179
|
+
"""No auth token, so errors will be raised if auth is required for requests"""
|
|
180
|
+
return None
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
|
-
Name:
|
|
3
|
-
Version: 0.6.
|
|
2
|
+
Name: ebi_eva_common_pyutils
|
|
3
|
+
Version: 0.6.8
|
|
4
4
|
Summary: EBI EVA - Common Python Utilities
|
|
5
5
|
Home-page: https://github.com/EBIVariation/eva-common-pyutils
|
|
6
6
|
License: Apache
|
|
@@ -19,5 +19,5 @@ Requires-Dist: retry
|
|
|
19
19
|
Provides-Extra: eva-internal
|
|
20
20
|
Requires-Dist: psycopg2-binary ; extra == 'eva-internal'
|
|
21
21
|
Requires-Dist: pymongo ; extra == 'eva-internal'
|
|
22
|
-
Requires-Dist: networkx
|
|
22
|
+
Requires-Dist: networkx <=2.5 ; extra == 'eva-internal'
|
|
23
23
|
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
ebi_eva_common_pyutils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
2
|
ebi_eva_common_pyutils/assembly_utils.py,sha256=CklyCGlCjlFp0e9pugg6kSsh5L0xfCe2qPvA2eLVtn0,4187
|
|
3
|
+
ebi_eva_common_pyutils/biosamples_communicators.py,sha256=ngWuSvEYkkttkMnJ9Gjxc5D7ZSi7D3_JX1c8BnEO3os,7596
|
|
3
4
|
ebi_eva_common_pyutils/command_utils.py,sha256=PtelWWqcC0eOwIVesjwBw3F9KaXRzEE_uAUJhQFZ4l8,2340
|
|
4
5
|
ebi_eva_common_pyutils/common_utils.py,sha256=ty_glvfRa3VGhnpAht4qtVkNNmv-IYfVtO958mY-BaA,1192
|
|
5
6
|
ebi_eva_common_pyutils/config.py,sha256=PtD2SgHf96kk21OA9tVIjEgsDXEFuAU-INy_kfQdoPw,4828
|
|
@@ -19,7 +20,7 @@ ebi_eva_common_pyutils/taxonomy/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm
|
|
|
19
20
|
ebi_eva_common_pyutils/taxonomy/taxonomy.py,sha256=p3XV4g3y0hEjyeZ4PwgN7Q3Et9G515ctQkSIo1kdDbU,2259
|
|
20
21
|
ebi_eva_common_pyutils/variation/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
21
22
|
ebi_eva_common_pyutils/variation/contig_utils.py,sha256=kMNEW_P2yPnd8Xx1tep19hy5ee7ojxz6ZOO1grTQsRQ,5230
|
|
22
|
-
ebi_eva_common_pyutils-0.6.
|
|
23
|
+
ebi_eva_common_pyutils-0.6.8.data/scripts/archive_directory.py,sha256=0lWJ0ju_AB2ni7lMnJXPFx6U2OdTGbe-WoQs-4BfKOM,4976
|
|
23
24
|
ebi_eva_internal_pyutils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
24
25
|
ebi_eva_internal_pyutils/archive_directory.py,sha256=IxVEfh_gaCiT652k0Q_-58fonRusy1yzXu7BCO8yVLo,4989
|
|
25
26
|
ebi_eva_internal_pyutils/config_utils.py,sha256=EGRC5rsmU_ug7OY9-t1UW1XZXRsauSyZB9xPcBux8ts,7909
|
|
@@ -31,8 +32,8 @@ ebi_eva_internal_pyutils/mongodb/__init__.py,sha256=0oyTlkYZCV7udlPl09Zl-sDyE3c9
|
|
|
31
32
|
ebi_eva_internal_pyutils/mongodb/mongo_database.py,sha256=kesaJaaxYFeF_uYZBgL8tbufGKUXll7bXb4WlOS9vKM,9596
|
|
32
33
|
ebi_eva_internal_pyutils/nextflow/__init__.py,sha256=OOiJS8jZOz98q0t77NNog7aI_fFrVxi4kGmiSskuAqM,122
|
|
33
34
|
ebi_eva_internal_pyutils/nextflow/nextflow_pipeline.py,sha256=ew623hhK8jmFLQjJwLZbgBmW9RTiJBEULVqHfIUv_dc,10114
|
|
34
|
-
ebi_eva_common_pyutils-0.6.
|
|
35
|
-
ebi_eva_common_pyutils-0.6.
|
|
36
|
-
ebi_eva_common_pyutils-0.6.
|
|
37
|
-
ebi_eva_common_pyutils-0.6.
|
|
38
|
-
ebi_eva_common_pyutils-0.6.
|
|
35
|
+
ebi_eva_common_pyutils-0.6.8.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
|
|
36
|
+
ebi_eva_common_pyutils-0.6.8.dist-info/METADATA,sha256=xL-ZPU8JZ9arUWs_6aAraTEq2Xmi4DPBKCz9mWgL6YU,822
|
|
37
|
+
ebi_eva_common_pyutils-0.6.8.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
|
|
38
|
+
ebi_eva_common_pyutils-0.6.8.dist-info/top_level.txt,sha256=sXoiqiGU8vlMQpFWDlKrekxhlusk06AhkOH3kSvDT6c,48
|
|
39
|
+
ebi_eva_common_pyutils-0.6.8.dist-info/RECORD,,
|
{ebi_eva_common_pyutils-0.6.7.data → ebi_eva_common_pyutils-0.6.8.data}/scripts/archive_directory.py
RENAMED
|
File without changes
|
|
File without changes
|
{ebi_eva_common_pyutils-0.6.7.dist-info → ebi_eva_common_pyutils-0.6.8.dist-info}/top_level.txt
RENAMED
|
File without changes
|