sapiopycommons 2025.3.6a453__py3-none-any.whl → 2025.3.10a455__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.

Potentially problematic release.


This version of sapiopycommons might be problematic. Click here for more details.

Files changed (33) hide show
  1. sapiopycommons/callbacks/callback_util.py +366 -1220
  2. sapiopycommons/chem/Molecules.py +2 -0
  3. sapiopycommons/datatype/data_fields.py +1 -1
  4. sapiopycommons/eln/experiment_handler.py +1 -2
  5. sapiopycommons/eln/experiment_report_util.py +7 -7
  6. sapiopycommons/files/file_bridge.py +0 -76
  7. sapiopycommons/files/file_bridge_handler.py +110 -325
  8. sapiopycommons/files/file_data_handler.py +2 -2
  9. sapiopycommons/files/file_util.py +11 -36
  10. sapiopycommons/files/file_validator.py +5 -6
  11. sapiopycommons/files/file_writer.py +1 -1
  12. sapiopycommons/flowcyto/flow_cyto.py +1 -1
  13. sapiopycommons/general/accession_service.py +1 -1
  14. sapiopycommons/general/aliases.py +28 -48
  15. sapiopycommons/general/audit_log.py +2 -2
  16. sapiopycommons/general/custom_report_util.py +1 -24
  17. sapiopycommons/general/exceptions.py +2 -41
  18. sapiopycommons/general/popup_util.py +2 -2
  19. sapiopycommons/general/sapio_links.py +4 -12
  20. sapiopycommons/multimodal/multimodal.py +0 -1
  21. sapiopycommons/processtracking/custom_workflow_handler.py +3 -3
  22. sapiopycommons/recordmodel/record_handler.py +108 -156
  23. sapiopycommons/webhook/webhook_handlers.py +55 -445
  24. {sapiopycommons-2025.3.6a453.dist-info → sapiopycommons-2025.3.10a455.dist-info}/METADATA +1 -1
  25. {sapiopycommons-2025.3.6a453.dist-info → sapiopycommons-2025.3.10a455.dist-info}/RECORD +27 -33
  26. sapiopycommons/ai/__init__.py +0 -0
  27. sapiopycommons/ai/tool_of_tools.py +0 -917
  28. sapiopycommons/customreport/auto_pagers.py +0 -278
  29. sapiopycommons/general/directive_util.py +0 -86
  30. sapiopycommons/general/html_formatter.py +0 -456
  31. sapiopycommons/samples/aliquot.py +0 -48
  32. {sapiopycommons-2025.3.6a453.dist-info → sapiopycommons-2025.3.10a455.dist-info}/WHEEL +0 -0
  33. {sapiopycommons-2025.3.6a453.dist-info → sapiopycommons-2025.3.10a455.dist-info}/licenses/LICENSE +0 -0
@@ -1,278 +0,0 @@
1
- from abc import ABC
2
- from copy import copy
3
- from queue import Queue
4
-
5
- from sapiopylib.rest.CustomReportService import CustomReportManager
6
- from sapiopylib.rest.DataMgmtService import DataMgmtServer
7
- from sapiopylib.rest.pojo.CustomReport import CustomReportCriteria, CustomReport, RawReportTerm, ReportColumn
8
- from sapiopylib.rest.pojo.datatype.FieldDefinition import FieldType
9
- from sapiopylib.rest.utils.autopaging import SapioPyAutoPager, PagerResultCriteriaType, _default_report_page_size, \
10
- _default_record_page_size
11
- from sapiopylib.rest.utils.recordmodel.PyRecordModel import PyRecordModel
12
- from sapiopylib.rest.utils.recordmodel.RecordModelWrapper import WrappedType
13
-
14
- from sapiopycommons.general.aliases import FieldValue, UserIdentifier, AliasUtil, RecordModel
15
- from sapiopycommons.general.custom_report_util import CustomReportUtil
16
- from sapiopycommons.general.exceptions import SapioException
17
- from sapiopycommons.recordmodel.record_handler import RecordHandler
18
-
19
-
20
- # FR-47389: Create auto pagers for running custom/system/quick reports that return dictionaries or records for each row.
21
- class _DictReportPagerBase(SapioPyAutoPager[CustomReportCriteria, dict[str, FieldValue]], ABC):
22
- """
23
- A base class for automatically paging through a report and returning the results as a list of dictionaries.
24
- """
25
- _columns: list[ReportColumn]
26
- _report_man: CustomReportManager
27
-
28
- def __init__(self, user: UserIdentifier, first_page_criteria: CustomReportCriteria):
29
- self._columns = first_page_criteria.column_list
30
- super().__init__(AliasUtil.to_sapio_user(user), first_page_criteria)
31
- self._report_man = DataMgmtServer.get_custom_report_manager(self.user)
32
-
33
- def get_all_at_once(self) -> list[dict[str, FieldValue]]:
34
- """
35
- Get the results of all pages. Be cautious of client memory usage.
36
- """
37
- if self.has_iterated:
38
- raise BrokenPipeError("Cannot use this method if the iterator has already been used.")
39
- return [x for x in self]
40
-
41
- def default_first_page_criteria(self) -> PagerResultCriteriaType:
42
- raise ValueError("Cannot generate a default first page criteria for custom reports.")
43
-
44
- def get_next_page_result(self) -> tuple[CustomReportCriteria | None, Queue[dict[str, FieldValue]]]:
45
- report: CustomReport = self._report_man.run_custom_report(self.next_page_criteria)
46
- queue: Queue[dict[str, FieldValue]] = Queue()
47
- for row in _process_results(report.result_table, self._columns):
48
- queue.put(row)
49
- if report.has_next_page:
50
- next_page_criteria = copy(self.next_page_criteria)
51
- next_page_criteria.page_number += 1
52
- return next_page_criteria, queue
53
- else:
54
- return None, queue
55
-
56
-
57
- class CustomReportDictAutoPager(_DictReportPagerBase):
58
- """
59
- A class that automatically pages through a custom report and returns the results as a list of dictionaries.
60
- """
61
- def __init__(self, user: UserIdentifier, report_criteria: CustomReportCriteria,
62
- page_number: int = 0, page_size: int = _default_report_page_size):
63
- """
64
- :param user: The current webhook context or a user object to send requests from.
65
- :param report_criteria: The custom report criteria to run.
66
- :param page_number: The page number to start on. The first page is page 0.
67
- :param page_size: The number of results to return per page.
68
- """
69
- first_page_criteria: CustomReportCriteria = copy(report_criteria)
70
- first_page_criteria.page_number = page_number
71
- first_page_criteria.page_size = page_size
72
- super().__init__(user, first_page_criteria)
73
-
74
-
75
- class SystemReportDictAutoPager(_DictReportPagerBase):
76
- """
77
- A class that automatically pages through a system report and returns the results as a list of dictionaries.
78
-
79
- System reports are also known as predefined searches in the system and must be defined in the data designer for
80
- a specific data type. That is, saved searches created by users cannot be run using this function.
81
- """
82
- def __init__(self, user: UserIdentifier, report_name: str,
83
- page_number: int = 0, page_size: int = _default_report_page_size):
84
- """
85
- :param user: The current webhook context or a user object to send requests from.
86
- :param report_name: The name of the system report to run.
87
- :param page_number: The page number to start on. The first page is page 0.
88
- :param page_size: The number of results to return per page.
89
- """
90
- first_page_criteria: CustomReportCriteria = CustomReportUtil.get_system_report_criteria(user, report_name)
91
- first_page_criteria.page_number = page_number
92
- first_page_criteria.page_size = page_size
93
- super().__init__(user, first_page_criteria)
94
-
95
-
96
- class QuickReportDictAutoPager(_DictReportPagerBase):
97
- """
98
- A class that automatically pages through a quick report and returns the results as a list of dictionaries.
99
- """
100
- def __init__(self, user: UserIdentifier, report_term: RawReportTerm,
101
- page_number: int = 0, page_size: int = _default_report_page_size):
102
- """
103
- :param user: The current webhook context or a user object to send requests from.
104
- :param report_term: The raw report term to use for the quick report.
105
- :param page_number: The page number to start on. The first page is page 0.
106
- :param page_size: The number of results to return per page.
107
- """
108
- first_page_criteria: CustomReportCriteria = CustomReportUtil.get_quick_report_criteria(user, report_term)
109
- first_page_criteria.page_number = page_number
110
- first_page_criteria.page_size = page_size
111
- super().__init__(user, first_page_criteria)
112
-
113
-
114
- class _RecordReportPagerBase(SapioPyAutoPager[CustomReportCriteria, RecordModel], ABC):
115
- """
116
- A base class for automatically paging through a report and returning the results as a list of records.
117
- """
118
- _columns: list[ReportColumn]
119
- _query_type: type[WrappedType] | str
120
- _data_type: str
121
- _rec_handler: RecordHandler
122
- _report_man: CustomReportManager
123
-
124
- def __init__(self, user: UserIdentifier, first_page_criteria: CustomReportCriteria, wrapper_type: type[WrappedType] | str):
125
- self._columns = first_page_criteria.column_list
126
- self._query_type = wrapper_type
127
- self._data_type = AliasUtil.to_data_type_name(wrapper_type)
128
- self._rec_handler = RecordHandler(user)
129
- super().__init__(AliasUtil.to_sapio_user(user), first_page_criteria)
130
- self._report_man = DataMgmtServer.get_custom_report_manager(self.user)
131
-
132
- def get_all_at_once(self) -> list[RecordModel]:
133
- """
134
- Get the results of all pages. Be cautious of client memory usage.
135
- """
136
- if self.has_iterated:
137
- raise BrokenPipeError("Cannot use this method if the iterator has already been used.")
138
- return [x for x in self]
139
-
140
- def default_first_page_criteria(self) -> PagerResultCriteriaType:
141
- raise ValueError("Cannot generate a default first page criteria for custom reports.")
142
-
143
- def get_next_page_result(self) -> tuple[CustomReportCriteria | None, Queue[WrappedType] | Queue[PyRecordModel]]:
144
- report: CustomReport = self._report_man.run_custom_report(self.next_page_criteria)
145
- queue = Queue()
146
- id_index: int = -1
147
- for i, column in enumerate(self._columns):
148
- if column.data_type_name == self._data_type and column.data_field_name == "RecordId":
149
- id_index = i
150
- break
151
- if id_index == -1:
152
- raise SapioException(f"This report does not contain a Record ID column for the given record model type "
153
- f"{self._data_type}.")
154
- ids: list[int] = [row[id_index] for row in report.result_table]
155
- for row in self._rec_handler.query_models_by_id(self._query_type, ids, page_size=report.page_size):
156
- queue.put(row)
157
- if report.has_next_page:
158
- next_page_criteria = copy(self.next_page_criteria)
159
- next_page_criteria.page_number += 1
160
- return next_page_criteria, queue
161
- else:
162
- return None, queue
163
-
164
-
165
- class CustomReportRecordAutoPager(_RecordReportPagerBase):
166
- """
167
- A class that automatically pages through a custom report and returns the results as a list of records.
168
- """
169
- def __init__(self, user: UserIdentifier, report_criteria: CustomReportCriteria,
170
- wrapper_type: type[WrappedType] | str, page_number: int = 0,
171
- page_size: int = _default_record_page_size):
172
- """
173
- :param user: The current webhook context or a user object to send requests from.
174
- :param report_criteria: The custom report criteria to run.
175
- :param wrapper_type: The record model wrapper type or data type name of the records being searched for.
176
- If a data type name was used instead of a model wrapper, then the returned records will be PyRecordModels
177
- instead of WrappedRecordModels.
178
- :param page_number: The page number to start on. The first page is page 0.
179
- :param page_size: The number of results to return per page.
180
- """
181
- first_page_criteria: CustomReportCriteria = copy(report_criteria)
182
- _add_record_id_column(first_page_criteria, wrapper_type)
183
- first_page_criteria.page_number = page_number
184
- first_page_criteria.page_size = page_size
185
- super().__init__(user, first_page_criteria, wrapper_type)
186
-
187
-
188
- class SystemReportRecordAutoPager(_RecordReportPagerBase):
189
- """
190
- A class that automatically pages through a system report and returns the results as a list of records.
191
-
192
- System reports are also known as predefined searches in the system and must be defined in the data designer for
193
- a specific data type. That is, saved searches created by users cannot be run using this function.
194
- """
195
- def __init__(self, user: UserIdentifier, report_name: str, wrapper_type: type[WrappedType] | str,
196
- page_number: int = 0, page_size: int = _default_record_page_size):
197
- """
198
- :param user: The current webhook context or a user object to send requests from.
199
- :param report_name: The name of the system report to run.
200
- :param wrapper_type: The record model wrapper type or data type name of the records being searched for.
201
- If a data type name was used instead of a model wrapper, then the returned records will be PyRecordModels
202
- instead of WrappedRecordModels.
203
- :param page_number: The page number to start on. The first page is page 0.
204
- :param page_size: The number of results to return per page.
205
- """
206
- first_page_criteria: CustomReportCriteria = CustomReportUtil.get_system_report_criteria(user, report_name)
207
- _add_record_id_column(first_page_criteria, wrapper_type)
208
- first_page_criteria.page_number = page_number
209
- first_page_criteria.page_size = page_size
210
- super().__init__(user, first_page_criteria, wrapper_type)
211
-
212
-
213
- class QuickReportRecordAutoPager(_RecordReportPagerBase):
214
- """
215
- A class that automatically pages through a quick report and returns the results as a list of records.
216
- """
217
- def __init__(self, user: UserIdentifier, report_term: RawReportTerm, wrapper_type: type[WrappedType] | str,
218
- page_number: int = 0, page_size: int = _default_record_page_size):
219
- """
220
- :param user: The current webhook context or a user object to send requests from.
221
- :param report_term: The raw report term to use for the quick report.
222
- :param wrapper_type: The record model wrapper type or data type name of the records being searched for.
223
- If a data type name was used instead of a model wrapper, then the returned records will be PyRecordModels
224
- instead of WrappedRecordModels.
225
- :param page_number: The page number to start on. The first page is page 0.
226
- :param page_size: The number of results to return per page.
227
- """
228
- if report_term.data_type_name != wrapper_type.get_wrapper_data_type_name():
229
- raise SapioException("The data type name of the report term must match the data type name of the wrapper type.")
230
- first_page_criteria: CustomReportCriteria = CustomReportUtil.get_quick_report_criteria(user, report_term)
231
- first_page_criteria.page_number = page_number
232
- first_page_criteria.page_size = page_size
233
- super().__init__(user, first_page_criteria, wrapper_type)
234
-
235
-
236
- def _add_record_id_column(report: CustomReportCriteria, wrapper_type: type[WrappedType] | str) -> None:
237
- """
238
- Given a custom report criteria, ensure that the report contains a Record ID column for the given record model's
239
- data type. Add one if it is missing.
240
- """
241
- dt: str = AliasUtil.to_data_type_name(wrapper_type)
242
- # Ensure that the root data type is the one we're looking for.
243
- report.root_data_type = dt
244
- # Enforce that the given custom report has a record ID column.
245
- if not any([x.data_type_name == dt and x.data_field_name == "RecordId" for x in report.column_list]):
246
- report.column_list.append(ReportColumn(dt, "RecordId", FieldType.LONG))
247
-
248
-
249
- def _process_results(rows: list[list[FieldValue]], columns: list[ReportColumn]) -> list[dict[str, FieldValue]]:
250
- """
251
- Given the results of a report as a list of row values and the report's columns, combine these lists to
252
- result in a singular list of dictionaries for each row in the results.
253
- """
254
- # It may be the case that two columns have the same data field name but differing data type names.
255
- # If this occurs, then we need to be able to differentiate these columns in the resulting dictionary.
256
- prepend_dt: set[str] = set()
257
- encountered_names: list[str] = []
258
- for column in columns:
259
- field_name: str = column.data_field_name
260
- if field_name in encountered_names:
261
- prepend_dt.add(field_name)
262
- else:
263
- encountered_names.append(field_name)
264
-
265
- ret: list[dict[str, FieldValue]] = []
266
- for row in rows:
267
- row_data: dict[str, FieldValue] = {}
268
- filter_row: bool = False
269
- for value, column in zip(row, columns):
270
- header: str = column.data_field_name
271
- # If two columns share the same data field name, prepend the data type name of the column to the
272
- # data field name.
273
- if header in prepend_dt:
274
- header = column.data_type_name + "." + header
275
- row_data.update({header: value})
276
- if filter_row is False:
277
- ret.append(row_data)
278
- return ret
@@ -1,86 +0,0 @@
1
- from typing import Iterable, cast
2
-
3
- from sapiopylib.rest.User import SapioUser
4
- from sapiopylib.rest.pojo.CustomReport import CustomReportCriteria, CustomReport
5
- from sapiopylib.rest.pojo.webhook.WebhookDirective import HomePageDirective, FormDirective, TableDirective, \
6
- CustomReportDirective, ElnExperimentDirective, ExperimentEntryDirective
7
-
8
- from sapiopycommons.general.aliases import SapioRecord, AliasUtil, ExperimentIdentifier, ExperimentEntryIdentifier, \
9
- UserIdentifier
10
- from sapiopycommons.general.custom_report_util import CustomReportUtil
11
-
12
-
13
- # FR-47392: Create a DirectiveUtil class to simplify the creation of directives.
14
- class DirectiveUtil:
15
- """
16
- DirectiveUtil is a class for creating webhook directives. The utility functions reduce the provided variables
17
- down to the exact type that the directives require, removing the need for the caller to handle the conversion.
18
- """
19
- user: SapioUser
20
-
21
- def __init__(self, context: UserIdentifier):
22
- """
23
- :param context: The current webhook context or a user object to send requests from.
24
- """
25
- self.user = AliasUtil.to_sapio_user(context)
26
-
27
- @staticmethod
28
- def homepage() -> HomePageDirective:
29
- """
30
- :return: A directive that sends the user back to their home page.
31
- """
32
- return HomePageDirective()
33
-
34
- @staticmethod
35
- def record_form(record: SapioRecord) -> FormDirective:
36
- """
37
- :param record: A record in the system.
38
- :return: A directive that sends the user to a specific data record form.
39
- """
40
- return FormDirective(AliasUtil.to_data_record(record))
41
-
42
- @staticmethod
43
- def record_table(records: Iterable[SapioRecord]) -> TableDirective:
44
- """
45
- :param records: A list of records in the system.
46
- :return: A directive that sends the user to a table of data records.
47
- """
48
- return TableDirective(AliasUtil.to_data_records(records))
49
-
50
- @staticmethod
51
- def record_adaptive(records: Iterable[SapioRecord]) -> TableDirective | FormDirective:
52
- """
53
- :param records: A list of records in the system.
54
- :return: A directive that sends the user to a table of data records if there are multiple records,
55
- or a directive that sends the user to a specific data record form if there is only one record.
56
- """
57
- records: list[SapioRecord] = list(records)
58
- if len(records) == 1:
59
- return DirectiveUtil.record_form(records[0])
60
- return DirectiveUtil.record_table(records)
61
-
62
- def custom_report(self, report: CustomReport | CustomReportCriteria | str) -> CustomReportDirective:
63
- """
64
- :param report: A custom report, the criteria for a custom report, or the name of a system report.
65
- :return: A directive that sends the user to the results of the provided custom report.
66
- """
67
- if isinstance(report, str):
68
- report: CustomReport = CustomReportUtil.get_system_report_criteria(self.user, report)
69
- return CustomReportDirective(cast(CustomReport, report))
70
-
71
- @staticmethod
72
- def eln_experiment(experiment: ExperimentIdentifier) -> ElnExperimentDirective:
73
- """
74
- :param experiment: An identifier for an experiment.
75
- :return: A directive that sends the user to the ELN experiment.
76
- """
77
- return ElnExperimentDirective(AliasUtil.to_notebook_id(experiment))
78
-
79
- @staticmethod
80
- def eln_entry(experiment: ExperimentIdentifier, entry: ExperimentEntryIdentifier) -> ExperimentEntryDirective:
81
- """
82
- :param experiment: An identifier for an experiment.
83
- :param entry: An identifier for an entry in the experiment.
84
- :return: A directive that sends the user to the provided experiment entry within its ELN experiment.
85
- """
86
- return ExperimentEntryDirective(AliasUtil.to_notebook_id(experiment), AliasUtil.to_entry_id(entry))