tol-sdk 1.8.3__py3-none-any.whl → 1.8.4__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.
tol/api_client/client.py CHANGED
@@ -206,7 +206,7 @@ class JsonApiClient(HttpClient):
206
206
  )
207
207
 
208
208
  headers = self._merge_headers()
209
- session = self._get_session()
209
+ session = self.get_session()
210
210
  r = session.post(
211
211
  url,
212
212
  headers=headers,
@@ -228,7 +228,7 @@ class JsonApiClient(HttpClient):
228
228
 
229
229
  url = self.__insert_url(object_type)
230
230
  headers = self._merge_headers()
231
- session = self._get_session()
231
+ session = self.get_session()
232
232
  r = session.post(url, headers=headers, json=transfer)
233
233
  self.__assert_no_error(r)
234
234
  return r.json()
tol/core/http_client.py CHANGED
@@ -55,7 +55,7 @@ class HttpClient:
55
55
  **__empty_if_none(self.__token)
56
56
  }
57
57
 
58
- def _get_session(self) -> requests.Session:
58
+ def get_session(self) -> requests.Session:
59
59
 
60
60
  cert_path = os.path.join(
61
61
  os.path.dirname(__file__),
@@ -74,7 +74,7 @@ class HttpClient:
74
74
  """
75
75
  Attempts a call to the endpoint 5 times, with a delay of 1 second
76
76
  """
77
- session = self._get_session()
77
+ session = self.get_session()
78
78
 
79
79
  retry_strategy = Retry(
80
80
  total=self.__retries,
tol/sql/auth/blueprint.py CHANGED
@@ -22,6 +22,7 @@ from ...api_base.auth import (
22
22
  )
23
23
  from ...api_base.auth.abc import AuthorisationManager
24
24
  from ...api_base.misc import AuthContext
25
+ from ...core import HttpClient
25
26
 
26
27
 
27
28
  class DbAuthManager(AuthManager):
@@ -194,7 +195,12 @@ class DbAuthManager(AuthManager):
194
195
  Raises:
195
196
  requests.HTTPError: If the revocation request fails
196
197
  """
197
- r = requests.post(
198
+
199
+ client = HttpClient()
200
+
201
+ session = client.get_session()
202
+
203
+ r = session.post(
198
204
  self.__config.revoke_url,
199
205
  data={
200
206
  'token': token,
@@ -21,4 +21,6 @@ from .unique_whole_organisms import UniqueWholeOrganismsValidator # noqa
21
21
  from .interfaces import Condition # noqa
22
22
  from .min_one_valid_value import MinOneValidValueValidator # noqa
23
23
  from .value_check import ValueCheckValidator # noqa
24
+ from .branching import BranchingValidator # noqa
24
25
  from .unique_value_check import UniqueValueCheckValidator # noqa
26
+ from .date_sorting import DateSortingValidator # noqa
@@ -0,0 +1,160 @@
1
+ # SPDX-FileCopyrightText: 2026 Genome Research Ltd.
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+
5
+ import importlib
6
+ from dataclasses import dataclass
7
+ from typing import Dict, List, cast
8
+
9
+ from tol.core import DataObject
10
+ from tol.core.validate import ValidationResult, Validator
11
+
12
+ from .interfaces import Condition, ConditionDict, ConditionEvaluator
13
+
14
+
15
+ Subvalidation = Dict[str, ConditionDict | str | Dict]
16
+
17
+
18
+ class BranchingValidator(Validator, ConditionEvaluator):
19
+ """
20
+ This validator is configured with a list of conditions.
21
+ If a condition passes, the corresponding sub-validator will be run.
22
+ """
23
+ @dataclass(slots=True, frozen=True, kw_only=True)
24
+ class Config:
25
+ """
26
+ ```
27
+ validations=[
28
+ {
29
+ 'condition': {
30
+ 'field': 'column_name',
31
+ 'operator': '==',
32
+ 'value': 'expected_value',
33
+ },
34
+ 'module': '<path.to.module>',
35
+ 'class_name': '<path.to.ValidatorClass>',
36
+ 'config_details': { ... },
37
+ },
38
+ {
39
+ ...
40
+ }
41
+ ]
42
+ ```
43
+ """
44
+ validations: List[Subvalidation]
45
+
46
+ __slots__ = ['__config', '_cached_validators']
47
+ __config: Config
48
+ _cached_validators: Dict[int, Validator]
49
+ """
50
+ Stores all sub-validators that have already been seen so that they can be used again.
51
+ Their keys are their indexes in the `validations` list in the validator config
52
+ """
53
+
54
+ def __init__(
55
+ self,
56
+ config: Config,
57
+ **kwargs
58
+ ) -> None:
59
+ super().__init__()
60
+
61
+ del kwargs
62
+ self.__config = config
63
+ self._cached_validators = {}
64
+
65
+ def _validate_data_object(
66
+ self,
67
+ obj: DataObject
68
+ ) -> None:
69
+ for subvalidator_index, subvalidation in enumerate(self.__config.validations):
70
+ # Do not run this sub-validation if its condition does not pass
71
+ condition_dict = cast(ConditionDict, subvalidation['condition'])
72
+ if not self._does_condition_pass(Condition.from_dict(condition_dict), obj):
73
+ continue
74
+
75
+ # Obtain validator, either from cache or anew
76
+ validator: Validator
77
+ if subvalidator_index in self._cached_validators:
78
+ # Use existing validator to perform subvalidation
79
+ validator = self._cached_validators[subvalidator_index]
80
+ else:
81
+ # Create a new validator and use that for the subvalidation
82
+ validator = self.__instantiate_validator(subvalidation)
83
+
84
+ # Add the new validator to the store of cached validators
85
+ self._cached_validators[subvalidator_index] = validator
86
+
87
+ # Validate data object
88
+ validator._validate_data_object(obj)
89
+
90
+ # At this point, I initially added `break` so that once a condition passed, the rest
91
+ # did not need to be checked. However, I decided against it because it is more flexible
92
+ # to have the opportunity to allow multiple conditions to pass, and thus execute
93
+ # multiple subvalidations, on the same `DataObject` in the same iteration
94
+
95
+ def __instantiate_validator(self, subvalidation: Subvalidation) -> Validator:
96
+ # Before attempting to extract items from the subvalidation, ensure all of the required
97
+ # keys are there and that they contain values of the correct types.
98
+ # This allows the dictionary to be safely queried thereafter
99
+ try:
100
+ # Accessing with square brackets will also check whether the key exists or not
101
+ if not isinstance(subvalidation['module'], str):
102
+ raise TypeError('module')
103
+ elif not isinstance(subvalidation['class_name'], str):
104
+ raise TypeError('class_name')
105
+ except (KeyError, TypeError) as e:
106
+ # Make errors more specific to validators
107
+ if isinstance(e, KeyError):
108
+ raise KeyError(
109
+ f'Invalid config in BranchingValidator: `{e.args[0]}` not found'
110
+ )
111
+ else:
112
+ raise TypeError(
113
+ f'Invalid config in BranchingValidator: '
114
+ f'`{e.args[0]}` contains an erroneous value type'
115
+ )
116
+
117
+ # Dynamically retrieve the validator class
118
+ validator_module = importlib.import_module(subvalidation['module'])
119
+ validator_class = getattr(validator_module, subvalidation['class_name'])
120
+
121
+ # Construct and return the new validator
122
+ validator_config = validator_class.Config(
123
+ subvalidation['config_details']
124
+ )
125
+ return validator_class(
126
+ config=validator_config,
127
+ )
128
+
129
+ @property
130
+ def results(self) -> List[ValidationResult]:
131
+ """
132
+ Fetches results from all sub-validators, collated into a single list
133
+ """
134
+ return [
135
+ result
136
+ for validator in self._cached_validators.values()
137
+ for result in validator.results
138
+ ]
139
+
140
+ @property
141
+ def warnings(self) -> List[ValidationResult]:
142
+ """
143
+ Fetches warnings from all sub-validators, collated into a single list
144
+ """
145
+ return [
146
+ warning
147
+ for validator in self._cached_validators.values()
148
+ for warning in validator.warnings
149
+ ]
150
+
151
+ @property
152
+ def errors(self) -> List[ValidationResult]:
153
+ """
154
+ Fetches errors from all sub-validators, collated into a single list
155
+ """
156
+ return [
157
+ error
158
+ for validator in self._cached_validators.values()
159
+ for error in validator.errors
160
+ ]
@@ -0,0 +1,58 @@
1
+ # SPDX-FileCopyrightText: 2026 Genome Research Ltd.
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+
5
+ from dataclasses import dataclass
6
+ from datetime import date, datetime
7
+
8
+ from tol.core import Validator
9
+ from tol.core.data_object import DataObject
10
+
11
+
12
+ class DateSortingValidator(Validator):
13
+ """
14
+ Validates an incoming stream of `DataObject` instances.
15
+ For each data object (sample) check the collection date
16
+ is not preceding the plating date
17
+ """
18
+ @dataclass(slots=True, frozen=True, kw_only=True)
19
+ class Config:
20
+ dates: list[str]
21
+
22
+ __slots__ = ['__config']
23
+ __config: Config
24
+
25
+ def __init__(self, config: Config, **kwargs) -> None:
26
+ super().__init__()
27
+ self.__config = config
28
+
29
+ def _validate_data_object(self, obj: DataObject) -> None:
30
+ # This function is used to check if the dates obtained
31
+ # are in the standard format and the date of collection
32
+ # is not preceding the date of plating
33
+
34
+ previous_date = None
35
+
36
+ for date_field in self.__config.dates:
37
+ date_value = obj.get_field_by_name(date_field)
38
+
39
+ # Validate that the value is a date or datetime object
40
+ if not isinstance(date_value, (date, datetime)):
41
+ self.add_error(
42
+ object_id=obj.id,
43
+ detail=f'{date_field} of {date_value} is not in the right date format',
44
+ field=self.__config.dates,
45
+ )
46
+ return
47
+
48
+ # Check if dates are in ascending order
49
+ if previous_date is not None and date_value < previous_date:
50
+ self.add_error(
51
+ object_id=obj.id,
52
+ detail=f'Date {date_field} ({date_value})'
53
+ f'is before previous date ({previous_date})',
54
+ field=self.__config.dates,
55
+ )
56
+ return
57
+
58
+ previous_date = date_value
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: tol-sdk
3
- Version: 1.8.3
3
+ Version: 1.8.4
4
4
  Summary: SDK for interaction with ToL, Sanger and external services
5
5
  Author-email: ToL Platforms Team <tol-platforms@sanger.ac.uk>
6
6
  License: MIT
@@ -32,7 +32,7 @@ tol/api_base/misc/relation_url.py,sha256=qfo-okp8Gv9-PEDghMfGZ2pHdYbHRhohvA9v3Go
32
32
  tol/api_base/misc/stats_parameters.py,sha256=IVpHqUeGQyjuih59jwqT-fIQMCBeESi2T9b4r9i4J28,1721
33
33
  tol/api_client/__init__.py,sha256=58SAywuMrIUCBAY9us_d_RLTMnaUTYWWts0LRQC5wLo,187
34
34
  tol/api_client/api_datasource.py,sha256=9CPNsujgnp__EwkZGwu9ZTmOxSwOldfLraOEQ7GBLng,14490
35
- tol/api_client/client.py,sha256=gcnX4iCZtjnCC6qylizXxLe3l6xLhME6LEJH0UeW7V4,13979
35
+ tol/api_client/client.py,sha256=hNqoyGPSiKSiT2DeHi13agrDBlXIWm7wtNjrMRCUuQg,13977
36
36
  tol/api_client/converter.py,sha256=g32fjqga4mC923n95HmQImPuawMfeb9rQcl3ZxUWP2s,4463
37
37
  tol/api_client/exception.py,sha256=MkvJaIyRVCzQ2rKOYnCOcT747mpOeQwGJJl3Kkb1BsQ,3999
38
38
  tol/api_client/factory.py,sha256=WGHA5wio4XS8OHqG07DLSVjehOeAsVoVCc5phAIq4H8,3737
@@ -98,7 +98,7 @@ tol/core/datasource_error.py,sha256=TqfqaPANG0gishadhA7myCmTO1Fg9u7hVZOvsY6BdAo,
98
98
  tol/core/datasource_filter.py,sha256=RY2S9kTx0XwdrFRSE2n2GohB9__fKGzFVsZrkN5hzQk,726
99
99
  tol/core/datasource_utils.py,sha256=18mwvFmtJL73_mxtFb56rKXZCGCtZFoEb8sWFKn3Yf0,6232
100
100
  tol/core/factory.py,sha256=pLLu8l-yK8QaLTt52izMhKZ2VlFHqRQlUHwMaLL6DI4,9156
101
- tol/core/http_client.py,sha256=05Wa_ySmpm_lC8OkxqXkq6jer0t693GexpaKNQBHufI,2244
101
+ tol/core/http_client.py,sha256=QyZarplEHVYIrqEfrySeHbawfbnBU4nN62TLt41x4tY,2242
102
102
  tol/core/relationship.py,sha256=etdyCjLbfi2tgkaqzE6cntpNtTzgT_jOPGeNKmPu5yc,4624
103
103
  tol/core/requested_fields.py,sha256=QFNky8QmT1Cmfn42EYPciwbCueJ0DVQNKkP0Vz-_7Jk,6715
104
104
  tol/core/session.py,sha256=6AohamIEfB8oV3Z414ishKqmlTgVfUaGYWzvxLgZgM4,3803
@@ -306,7 +306,7 @@ tol/sql/sql_datasource.py,sha256=jLPQSolGtKRKezjVnMrlj8YO6bLepyiFARMbDYEXuN8,162
306
306
  tol/sql/action/__init__.py,sha256=T1zAsCza_lvsNtXF1ecSLt9OFGup8tGnIs68YylBmXI,142
307
307
  tol/sql/action/factory.py,sha256=HkareJp_57ud0_Bdd9Kwz3_Rnq2l211sGJgftohFAHg,3589
308
308
  tol/sql/auth/__init__.py,sha256=e3JuwugXmXobklqZ1Mt1w03qPgb1WdUaJVM7oblzHyk,202
309
- tol/sql/auth/blueprint.py,sha256=u0vT_TC9IMKrg08QFa9W29_83mT0y0jzLj3DvXy1BBw,25906
309
+ tol/sql/auth/blueprint.py,sha256=3M55fYi_H1PgFaFxYSW8pgOwgFwdl1lN6VRbEt1r8N8,26008
310
310
  tol/sql/auth/models.py,sha256=U4CsKMMyzGMg6hj4tp_iRenr3_Q--64WJmHWvxQ2--Q,12297
311
311
  tol/sql/pipeline_step/__init__.py,sha256=O7u4RrLfuoB0mwLcPxFoUrdTBZGB_4bE1vWCn5ho-qw,147
312
312
  tol/sql/pipeline_step/factory.py,sha256=FaO61WLST4GQdAWuCIGqAvpVBvzkfBOgZWgHEZy2OXo,5483
@@ -323,12 +323,14 @@ tol/treeval/treeval_datasource.py,sha256=GzY6JwH67b5QdV-UVdCFJfgGAIuZ96J2nl53YxZ
323
323
  tol/utils/__init__.py,sha256=764-Na1OaNGUDWpMIu51ZtXG7n_nB5MccUFK6LmkWRI,138
324
324
  tol/utils/csv.py,sha256=mihww25fSn72c4h-RFeqD_pFIG6KHZP4v1_C0rx81ws,421
325
325
  tol/utils/s3.py,sha256=aoYCwJ-qcMqFrpxmViFqPa0O1jgp0phtztO3-0CSNjw,491
326
- tol/validators/__init__.py,sha256=vRuNMVhUlRPinTZQ-TBtmfIAW40vcE4dez6RI89sgoI,1295
326
+ tol/validators/__init__.py,sha256=_ETv6oGQ2bTH_6-foYFy9T5wP5OG3cl96zEjvrIS7zk,1399
327
327
  tol/validators/allowed_keys.py,sha256=RJcHBiguL84B8hjSRaXLNES21yZqaKFwJNp2Tz9zvh0,1506
328
328
  tol/validators/allowed_values.py,sha256=-Yy3Sqo1WYacGKlot_dn3M2o7Oj5MXOioJrJmrWCCxs,1536
329
329
  tol/validators/allowed_values_from_datasource.py,sha256=ICFO6FcYXDN7M2Cv1OwpyN38CdhmY7oU-njzIatA3-w,3185
330
330
  tol/validators/assert_on_condition.py,sha256=eBGgSVfIQ6e45SheM-ZDg7daXJjyZxRVS5L8AWvbXag,2027
331
+ tol/validators/branching.py,sha256=7YFjHNjrrTmy4hZ3E7JKDT6MEsBMhrc3P3p3ykv4wKI,5720
331
332
  tol/validators/converter_and_validate.py,sha256=O1uYdrU4YDZ8eZjb7Koots4-8fMVOkJFXESg-LVw2o8,2992
333
+ tol/validators/date_sorting.py,sha256=NzYsBfhgeG4NYlYjVUYgcGGwEHns5hESqeaPvXUxjUI,1918
332
334
  tol/validators/ena_checklist.py,sha256=M10VAFGpaxnm7rWO4jmFhTWkYRlCmU0Ox2IUEDFGKbo,2812
333
335
  tol/validators/ena_submittable.py,sha256=CujF9t4mA4N3Wm_5rA5MRp401aW19kbioOZpfWVXg6I,1965
334
336
  tol/validators/min_one_valid_value.py,sha256=gZUHtfRA-Lvpw0d1FJoAA31cRJpLbbxAJCC9DCt5lCY,1442
@@ -345,9 +347,9 @@ tol/validators/unique_whole_organisms.py,sha256=RdqA1GzIf3LTdrmNGGdxv0aW2udDY2P9
345
347
  tol/validators/value_check.py,sha256=lxfhfL8BCIs_B838CQ5znJ6KFD7ms_fSVCS9QuVearE,1052
346
348
  tol/validators/interfaces/__init__.py,sha256=jtOxnwnwqV_29xjmmMcS_kvlt-pQiWwQYJn2YRP07_w,172
347
349
  tol/validators/interfaces/condition_evaluator.py,sha256=nj8Cb8hi47OBy6OVNfeLhF-Pjwtr8MiOSymYL6hfVes,3766
348
- tol_sdk-1.8.3.dist-info/licenses/LICENSE,sha256=RF9Jacy-9BpUAQQ20INhTgtaNBkmdTolYCHtrrkM2-8,1077
349
- tol_sdk-1.8.3.dist-info/METADATA,sha256=xD9AOIVwrZlC5Vgb3rPRM9eT5HdJ0FmCsJDRmxfdvw8,3142
350
- tol_sdk-1.8.3.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
351
- tol_sdk-1.8.3.dist-info/entry_points.txt,sha256=jH3HfTwxjzog7E3lq8CKpUWGIRY9FSXbyL6CpUmv6D0,36
352
- tol_sdk-1.8.3.dist-info/top_level.txt,sha256=PwKMQLphyZNvagBoriVbl8uwHXQl8IC1niawVG0iXMM,10
353
- tol_sdk-1.8.3.dist-info/RECORD,,
350
+ tol_sdk-1.8.4.dist-info/licenses/LICENSE,sha256=RF9Jacy-9BpUAQQ20INhTgtaNBkmdTolYCHtrrkM2-8,1077
351
+ tol_sdk-1.8.4.dist-info/METADATA,sha256=g4uV0VKLkExU3Zc1waAU3ukQbtb6wRhzkaZgX-BoT5Y,3142
352
+ tol_sdk-1.8.4.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
353
+ tol_sdk-1.8.4.dist-info/entry_points.txt,sha256=jH3HfTwxjzog7E3lq8CKpUWGIRY9FSXbyL6CpUmv6D0,36
354
+ tol_sdk-1.8.4.dist-info/top_level.txt,sha256=PwKMQLphyZNvagBoriVbl8uwHXQl8IC1niawVG0iXMM,10
355
+ tol_sdk-1.8.4.dist-info/RECORD,,