ebi-eva-common-pyutils 0.6.7__tar.gz → 0.6.8.dev0__tar.gz

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.
Files changed (46) hide show
  1. {ebi_eva_common_pyutils-0.6.7 → ebi_eva_common_pyutils-0.6.8.dev0}/CHANGELOG.md +6 -0
  2. {ebi_eva_common_pyutils-0.6.7 → ebi_eva_common_pyutils-0.6.8.dev0}/PKG-INFO +10 -2
  3. ebi_eva_common_pyutils-0.6.8.dev0/ebi_eva_common_pyutils/biosamples_communicators.py +180 -0
  4. {ebi_eva_common_pyutils-0.6.7 → ebi_eva_common_pyutils-0.6.8.dev0}/ebi_eva_common_pyutils.egg-info/PKG-INFO +11 -3
  5. {ebi_eva_common_pyutils-0.6.7 → ebi_eva_common_pyutils-0.6.8.dev0}/ebi_eva_common_pyutils.egg-info/SOURCES.txt +4 -1
  6. {ebi_eva_common_pyutils-0.6.7 → ebi_eva_common_pyutils-0.6.8.dev0}/setup.py +1 -1
  7. ebi_eva_common_pyutils-0.6.8.dev0/tests/test_common.py +22 -0
  8. {ebi_eva_common_pyutils-0.6.7 → ebi_eva_common_pyutils-0.6.8.dev0}/LICENSE +0 -0
  9. {ebi_eva_common_pyutils-0.6.7 → ebi_eva_common_pyutils-0.6.8.dev0}/MANIFEST.in +0 -0
  10. {ebi_eva_common_pyutils-0.6.7 → ebi_eva_common_pyutils-0.6.8.dev0}/README.md +0 -0
  11. {ebi_eva_common_pyutils-0.6.7 → ebi_eva_common_pyutils-0.6.8.dev0}/ebi_eva_common_pyutils/__init__.py +0 -0
  12. {ebi_eva_common_pyutils-0.6.7 → ebi_eva_common_pyutils-0.6.8.dev0}/ebi_eva_common_pyutils/assembly/__init__.py +0 -0
  13. {ebi_eva_common_pyutils-0.6.7 → ebi_eva_common_pyutils-0.6.8.dev0}/ebi_eva_common_pyutils/assembly/assembly.py +0 -0
  14. {ebi_eva_common_pyutils-0.6.7 → ebi_eva_common_pyutils-0.6.8.dev0}/ebi_eva_common_pyutils/assembly_utils.py +0 -0
  15. {ebi_eva_common_pyutils-0.6.7 → ebi_eva_common_pyutils-0.6.8.dev0}/ebi_eva_common_pyutils/command_utils.py +0 -0
  16. {ebi_eva_common_pyutils-0.6.7 → ebi_eva_common_pyutils-0.6.8.dev0}/ebi_eva_common_pyutils/common_utils.py +0 -0
  17. {ebi_eva_common_pyutils-0.6.7 → ebi_eva_common_pyutils-0.6.8.dev0}/ebi_eva_common_pyutils/config.py +0 -0
  18. {ebi_eva_common_pyutils-0.6.7 → ebi_eva_common_pyutils-0.6.8.dev0}/ebi_eva_common_pyutils/contig_alias/__init__.py +0 -0
  19. {ebi_eva_common_pyutils-0.6.7 → ebi_eva_common_pyutils-0.6.8.dev0}/ebi_eva_common_pyutils/contig_alias/contig_alias.py +0 -0
  20. {ebi_eva_common_pyutils-0.6.7 → ebi_eva_common_pyutils-0.6.8.dev0}/ebi_eva_common_pyutils/ena_utils.py +0 -0
  21. {ebi_eva_common_pyutils-0.6.7 → ebi_eva_common_pyutils-0.6.8.dev0}/ebi_eva_common_pyutils/file_utils.py +0 -0
  22. {ebi_eva_common_pyutils-0.6.7 → ebi_eva_common_pyutils-0.6.8.dev0}/ebi_eva_common_pyutils/logger.py +0 -0
  23. {ebi_eva_common_pyutils-0.6.7 → ebi_eva_common_pyutils-0.6.8.dev0}/ebi_eva_common_pyutils/ncbi_utils.py +0 -0
  24. {ebi_eva_common_pyutils-0.6.7 → ebi_eva_common_pyutils-0.6.8.dev0}/ebi_eva_common_pyutils/network_utils.py +0 -0
  25. {ebi_eva_common_pyutils-0.6.7 → ebi_eva_common_pyutils-0.6.8.dev0}/ebi_eva_common_pyutils/reference/__init__.py +0 -0
  26. {ebi_eva_common_pyutils-0.6.7 → ebi_eva_common_pyutils-0.6.8.dev0}/ebi_eva_common_pyutils/reference/assembly.py +0 -0
  27. {ebi_eva_common_pyutils-0.6.7 → ebi_eva_common_pyutils-0.6.8.dev0}/ebi_eva_common_pyutils/reference/sequence.py +0 -0
  28. {ebi_eva_common_pyutils-0.6.7 → ebi_eva_common_pyutils-0.6.8.dev0}/ebi_eva_common_pyutils/taxonomy/__init__.py +0 -0
  29. {ebi_eva_common_pyutils-0.6.7 → ebi_eva_common_pyutils-0.6.8.dev0}/ebi_eva_common_pyutils/taxonomy/taxonomy.py +0 -0
  30. {ebi_eva_common_pyutils-0.6.7 → ebi_eva_common_pyutils-0.6.8.dev0}/ebi_eva_common_pyutils/variation/__init__.py +0 -0
  31. {ebi_eva_common_pyutils-0.6.7 → ebi_eva_common_pyutils-0.6.8.dev0}/ebi_eva_common_pyutils/variation/contig_utils.py +0 -0
  32. {ebi_eva_common_pyutils-0.6.7 → ebi_eva_common_pyutils-0.6.8.dev0}/ebi_eva_common_pyutils.egg-info/dependency_links.txt +0 -0
  33. {ebi_eva_common_pyutils-0.6.7 → ebi_eva_common_pyutils-0.6.8.dev0}/ebi_eva_common_pyutils.egg-info/requires.txt +0 -0
  34. {ebi_eva_common_pyutils-0.6.7 → ebi_eva_common_pyutils-0.6.8.dev0}/ebi_eva_common_pyutils.egg-info/top_level.txt +0 -0
  35. {ebi_eva_common_pyutils-0.6.7 → ebi_eva_common_pyutils-0.6.8.dev0}/ebi_eva_internal_pyutils/__init__.py +0 -0
  36. {ebi_eva_common_pyutils-0.6.7 → ebi_eva_common_pyutils-0.6.8.dev0}/ebi_eva_internal_pyutils/archive_directory.py +0 -0
  37. {ebi_eva_common_pyutils-0.6.7 → ebi_eva_common_pyutils-0.6.8.dev0}/ebi_eva_internal_pyutils/config_utils.py +0 -0
  38. {ebi_eva_common_pyutils-0.6.7 → ebi_eva_common_pyutils-0.6.8.dev0}/ebi_eva_internal_pyutils/metadata_utils.py +0 -0
  39. {ebi_eva_common_pyutils-0.6.7 → ebi_eva_common_pyutils-0.6.8.dev0}/ebi_eva_internal_pyutils/mongo_utils.py +0 -0
  40. {ebi_eva_common_pyutils-0.6.7 → ebi_eva_common_pyutils-0.6.8.dev0}/ebi_eva_internal_pyutils/mongodb/__init__.py +0 -0
  41. {ebi_eva_common_pyutils-0.6.7 → ebi_eva_common_pyutils-0.6.8.dev0}/ebi_eva_internal_pyutils/mongodb/mongo_database.py +0 -0
  42. {ebi_eva_common_pyutils-0.6.7 → ebi_eva_common_pyutils-0.6.8.dev0}/ebi_eva_internal_pyutils/nextflow/__init__.py +0 -0
  43. {ebi_eva_common_pyutils-0.6.7 → ebi_eva_common_pyutils-0.6.8.dev0}/ebi_eva_internal_pyutils/nextflow/nextflow_pipeline.py +0 -0
  44. {ebi_eva_common_pyutils-0.6.7 → ebi_eva_common_pyutils-0.6.8.dev0}/ebi_eva_internal_pyutils/pg_utils.py +0 -0
  45. {ebi_eva_common_pyutils-0.6.7 → ebi_eva_common_pyutils-0.6.8.dev0}/ebi_eva_internal_pyutils/spring_properties.py +0 -0
  46. {ebi_eva_common_pyutils-0.6.7 → ebi_eva_common_pyutils-0.6.8.dev0}/setup.cfg +0 -0
@@ -2,6 +2,12 @@
2
2
  Changelog for ebi_eva_common_pyutils
3
3
  ===========================
4
4
 
5
+ ## 0.6.8 (unreleased)
6
+ ---------------------
7
+
8
+ - Nothing changed yet.
9
+
10
+
5
11
  ## 0.6.7 (2024-05-15)
6
12
  ---------------------
7
13
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: ebi_eva_common_pyutils
3
- Version: 0.6.7
3
+ Version: 0.6.8.dev0
4
4
  Summary: EBI EVA - Common Python Utilities
5
5
  Home-page: https://github.com/EBIVariation/eva-common-pyutils
6
6
  License: Apache
@@ -10,5 +10,13 @@ Classifier: Intended Audience :: Developers
10
10
  Classifier: Topic :: Software Development :: Build Tools
11
11
  Classifier: License :: OSI Approved :: Apache Software License
12
12
  Classifier: Programming Language :: Python :: 3
13
- Provides-Extra: eva-internal
14
13
  License-File: LICENSE
14
+ Requires-Dist: requests
15
+ Requires-Dist: lxml
16
+ Requires-Dist: pyyaml
17
+ Requires-Dist: cached-property
18
+ Requires-Dist: retry
19
+ Provides-Extra: eva-internal
20
+ Requires-Dist: psycopg2-binary; extra == "eva-internal"
21
+ Requires-Dist: pymongo; extra == "eva-internal"
22
+ Requires-Dist: networkx<=2.5; extra == "eva-internal"
@@ -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: ebi-eva-common-pyutils
3
- Version: 0.6.7
2
+ Name: ebi_eva_common_pyutils
3
+ Version: 0.6.8.dev0
4
4
  Summary: EBI EVA - Common Python Utilities
5
5
  Home-page: https://github.com/EBIVariation/eva-common-pyutils
6
6
  License: Apache
@@ -10,5 +10,13 @@ Classifier: Intended Audience :: Developers
10
10
  Classifier: Topic :: Software Development :: Build Tools
11
11
  Classifier: License :: OSI Approved :: Apache Software License
12
12
  Classifier: Programming Language :: Python :: 3
13
- Provides-Extra: eva-internal
14
13
  License-File: LICENSE
14
+ Requires-Dist: requests
15
+ Requires-Dist: lxml
16
+ Requires-Dist: pyyaml
17
+ Requires-Dist: cached-property
18
+ Requires-Dist: retry
19
+ Provides-Extra: eva-internal
20
+ Requires-Dist: psycopg2-binary; extra == "eva-internal"
21
+ Requires-Dist: pymongo; extra == "eva-internal"
22
+ Requires-Dist: networkx<=2.5; extra == "eva-internal"
@@ -4,8 +4,10 @@ MANIFEST.in
4
4
  README.md
5
5
  setup.cfg
6
6
  setup.py
7
+ /home/april/projects/eva-common-pyutils/ebi_eva_internal_pyutils/archive_directory.py
7
8
  ebi_eva_common_pyutils/__init__.py
8
9
  ebi_eva_common_pyutils/assembly_utils.py
10
+ ebi_eva_common_pyutils/biosamples_communicators.py
9
11
  ebi_eva_common_pyutils/command_utils.py
10
12
  ebi_eva_common_pyutils/common_utils.py
11
13
  ebi_eva_common_pyutils/config.py
@@ -40,4 +42,5 @@ ebi_eva_internal_pyutils/spring_properties.py
40
42
  ebi_eva_internal_pyutils/mongodb/__init__.py
41
43
  ebi_eva_internal_pyutils/mongodb/mongo_database.py
42
44
  ebi_eva_internal_pyutils/nextflow/__init__.py
43
- ebi_eva_internal_pyutils/nextflow/nextflow_pipeline.py
45
+ ebi_eva_internal_pyutils/nextflow/nextflow_pipeline.py
46
+ tests/test_common.py
@@ -7,7 +7,7 @@ setup(
7
7
  name='ebi_eva_common_pyutils',
8
8
  scripts=[os.path.join(os.path.dirname(__file__), 'ebi_eva_internal_pyutils', 'archive_directory.py')],
9
9
  packages=find_packages(),
10
- version='0.6.7',
10
+ version='0.6.8.dev0',
11
11
  license='Apache',
12
12
  description='EBI EVA - Common Python Utilities',
13
13
  url='https://github.com/EBIVariation/eva-common-pyutils',
@@ -0,0 +1,22 @@
1
+ import os
2
+ from unittest import TestCase
3
+
4
+ from ebi_eva_common_pyutils.common_utils import merge_two_dicts, pretty_print
5
+
6
+
7
+ class TestCommon(TestCase):
8
+
9
+ resources_folder = os.path.join(os.path.dirname(__file__), 'resources')
10
+
11
+
12
+ class TestCommonUtils(TestCase):
13
+
14
+ def test_merge_two_dicts(self):
15
+ d1 = {'a': 1, 'b': 2, 'c': 3}
16
+ d2 = {'d': 4, 'a': 5, 'e': 6}
17
+ assert merge_two_dicts(d1, d2) == {'a': 5, 'b': 2, 'c': 3, 'd': 4, 'e': 6}
18
+ assert merge_two_dicts(d2, d1) == {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 6}
19
+
20
+ def test_pretty_print(self):
21
+ pretty_print(['Header 1', 'Long Header 2'],
22
+ [['row1 cell 1', 'row1 cell 2'], ['row2 cell 1', 'Super long row2 cell 2']])