sapiopycommons 2024.8.28a313__py3-none-any.whl → 2024.8.28a315__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.
- sapiopycommons/callbacks/callback_util.py +407 -69
- sapiopycommons/chem/IndigoMolecules.py +1 -0
- sapiopycommons/chem/Molecules.py +1 -0
- sapiopycommons/customreport/__init__.py +0 -0
- sapiopycommons/customreport/column_builder.py +60 -0
- sapiopycommons/customreport/custom_report_builder.py +125 -0
- sapiopycommons/customreport/term_builder.py +299 -0
- sapiopycommons/datatype/attachment_util.py +11 -10
- sapiopycommons/eln/experiment_handler.py +209 -48
- sapiopycommons/eln/experiment_report_util.py +118 -0
- sapiopycommons/files/complex_data_loader.py +5 -4
- sapiopycommons/files/file_bridge.py +31 -24
- sapiopycommons/files/file_bridge_handler.py +340 -0
- sapiopycommons/files/file_data_handler.py +2 -5
- sapiopycommons/files/file_util.py +50 -10
- sapiopycommons/files/file_validator.py +92 -6
- sapiopycommons/files/file_writer.py +44 -15
- sapiopycommons/general/accession_service.py +375 -0
- sapiopycommons/general/aliases.py +147 -3
- sapiopycommons/general/audit_log.py +196 -0
- sapiopycommons/general/custom_report_util.py +211 -37
- sapiopycommons/general/popup_util.py +17 -0
- sapiopycommons/general/sapio_links.py +50 -0
- sapiopycommons/general/time_util.py +40 -0
- sapiopycommons/multimodal/multimodal.py +146 -0
- sapiopycommons/multimodal/multimodal_data.py +486 -0
- sapiopycommons/processtracking/endpoints.py +22 -22
- sapiopycommons/recordmodel/record_handler.py +481 -97
- sapiopycommons/rules/eln_rule_handler.py +34 -25
- sapiopycommons/rules/on_save_rule_handler.py +34 -31
- sapiopycommons/webhook/webhook_handlers.py +147 -26
- sapiopycommons/webhook/webservice_handlers.py +67 -0
- {sapiopycommons-2024.8.28a313.dist-info → sapiopycommons-2024.8.28a315.dist-info}/METADATA +4 -2
- sapiopycommons-2024.8.28a315.dist-info/RECORD +50 -0
- sapiopycommons-2024.8.28a313.dist-info/RECORD +0 -38
- {sapiopycommons-2024.8.28a313.dist-info → sapiopycommons-2024.8.28a315.dist-info}/WHEEL +0 -0
- {sapiopycommons-2024.8.28a313.dist-info → sapiopycommons-2024.8.28a315.dist-info}/licenses/LICENSE +0 -0
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import io
|
|
2
|
+
import warnings
|
|
3
|
+
import zipfile
|
|
2
4
|
|
|
3
5
|
import pandas
|
|
4
6
|
from numpy import dtype
|
|
@@ -21,7 +23,8 @@ class FileUtil:
|
|
|
21
23
|
"""
|
|
22
24
|
@staticmethod
|
|
23
25
|
def tokenize_csv(file_bytes: bytes, required_headers: list[str] | None = None, header_row_index: int | None = 0,
|
|
24
|
-
seperator: str = ","
|
|
26
|
+
seperator: str = ",", *, encoding: str | None = None, exception_on_empty: bool = True) \
|
|
27
|
+
-> tuple[list[dict[str, str]], list[list[str]]]:
|
|
25
28
|
"""
|
|
26
29
|
Tokenize a CSV file. The provided file must be uniform. That is, if row 1 has 10 cells, all the rows in the file
|
|
27
30
|
must have 10 cells. Otherwise, the Pandas parser throws a tokenizer exception.
|
|
@@ -34,22 +37,30 @@ class FileUtil:
|
|
|
34
37
|
meaning that required headers are also ignored if any are provided. By default, the first row (0th index)
|
|
35
38
|
is assumed to be the header row.
|
|
36
39
|
:param seperator: The character that separates cells in the table.
|
|
40
|
+
:param encoding: The encoding used to read the given file bytes. If not provided, uses utf-8. If your file
|
|
41
|
+
contains a non-utf-8 character, then a UnicodeDecodeError will be thrown. If this happens, consider using
|
|
42
|
+
ISO-8859-1 as the encoding.
|
|
43
|
+
:param exception_on_empty: Throw a user error exception if the provided file bytes result in an empty list in
|
|
44
|
+
the first element of the returned tuple.
|
|
37
45
|
:return: The CSV parsed into a list of dicts where each dict is a row, mapping the headers to the cells for
|
|
38
46
|
that row. Also returns a list of each row above the headers (the metadata), parsed into a list of each cell.
|
|
39
47
|
If the header row index is 0 or None, this list will be empty.
|
|
40
48
|
"""
|
|
41
49
|
# Parse the file bytes into two DataFrames. The first is metadata of the file located above the header row,
|
|
42
50
|
# while the second is the body of the file below the header row.
|
|
43
|
-
file_body, file_metadata = FileUtil.csv_to_data_frames(file_bytes, header_row_index, seperator
|
|
51
|
+
file_body, file_metadata = FileUtil.csv_to_data_frames(file_bytes, header_row_index, seperator,
|
|
52
|
+
encoding=encoding)
|
|
44
53
|
# Parse the metadata from above the header row index into a list of lists.
|
|
45
54
|
metadata: list[list[str]] = FileUtil.data_frame_to_lists(file_metadata)
|
|
46
55
|
# Parse the data from the file body into a list of dicts.
|
|
47
56
|
rows: list[dict[str, str]] = FileUtil.data_frame_to_dicts(file_body, required_headers, header_row_index)
|
|
57
|
+
if exception_on_empty and not rows:
|
|
58
|
+
raise SapioUserErrorException("The provided file contains no rows of information below the headers.")
|
|
48
59
|
return rows, metadata
|
|
49
60
|
|
|
50
61
|
@staticmethod
|
|
51
|
-
def tokenize_xlsx(file_bytes: bytes, required_headers: list[str] | None = None, header_row_index: int | None = 0
|
|
52
|
-
|
|
62
|
+
def tokenize_xlsx(file_bytes: bytes, required_headers: list[str] | None = None, header_row_index: int | None = 0,
|
|
63
|
+
*, exception_on_empty: bool = True) -> tuple[list[dict[str, str]], list[list[str]]]:
|
|
53
64
|
"""
|
|
54
65
|
Tokenize an XLSX file row by row.
|
|
55
66
|
|
|
@@ -60,6 +71,8 @@ class FileUtil:
|
|
|
60
71
|
row is returned in the metadata list. If input is None, then no row is considered to be the header row,
|
|
61
72
|
meaning that required headers are also ignored if any are provided. By default, the first row (0th index)
|
|
62
73
|
is assumed to be the header row.
|
|
74
|
+
:param exception_on_empty: Throw a user error exception if the provided file bytes result in an empty list in
|
|
75
|
+
the first element of the returned tuple.
|
|
63
76
|
:return: The XLSX parsed into a list of dicts where each dict is a row, mapping the headers to the cells for
|
|
64
77
|
that row. Also returns a list of each row above the headers (the metadata), parsed into a list of each cell.
|
|
65
78
|
If the header row index is 0 or None, this list will be empty.
|
|
@@ -71,11 +84,13 @@ class FileUtil:
|
|
|
71
84
|
metadata: list[list[str]] = FileUtil.data_frame_to_lists(file_metadata)
|
|
72
85
|
# Parse the data from the file body into a list of dicts.
|
|
73
86
|
rows: list[dict[str, str]] = FileUtil.data_frame_to_dicts(file_body, required_headers, header_row_index)
|
|
87
|
+
if exception_on_empty and not rows:
|
|
88
|
+
raise SapioUserErrorException("The provided file contains no rows of information below the headers.")
|
|
74
89
|
return rows, metadata
|
|
75
90
|
|
|
76
91
|
@staticmethod
|
|
77
|
-
def csv_to_data_frames(file_bytes: bytes, header_row_index: int | None = 0, seperator: str = ","
|
|
78
|
-
|
|
92
|
+
def csv_to_data_frames(file_bytes: bytes, header_row_index: int | None = 0, seperator: str = ",",
|
|
93
|
+
*, encoding: str | None = None) -> tuple[DataFrame, DataFrame | None]:
|
|
79
94
|
"""
|
|
80
95
|
Parse the file bytes for a CSV into DataFrames. The provided file must be uniform. That is, if row 1 has 10
|
|
81
96
|
cells, all the rows in the file must have 10 cells. Otherwise, the Pandas parser throws a tokenizer exception.
|
|
@@ -86,6 +101,9 @@ class FileUtil:
|
|
|
86
101
|
meaning that required headers are also ignored if any are provided. By default, the first row (0th index)
|
|
87
102
|
is assumed to be the header row.
|
|
88
103
|
:param seperator: The character that separates cells in the table.
|
|
104
|
+
:param encoding: The encoding used to read the given file bytes. If not provided, uses utf-8. If your file
|
|
105
|
+
contains a non-utf-8 character, then a UnicodeDecodeError will be thrown. If this happens, consider using
|
|
106
|
+
ISO-8859-1 as the encoding.
|
|
89
107
|
:return: A tuple of two DataFrames. The first is the frame for the CSV table body, while the second is for the
|
|
90
108
|
metadata from above the header row, or None if there is no metadata.
|
|
91
109
|
"""
|
|
@@ -97,13 +115,13 @@ class FileUtil:
|
|
|
97
115
|
# can throw off the header row index.
|
|
98
116
|
file_metadata = pandas.read_csv(file_io, header=None, dtype=dtype(str),
|
|
99
117
|
skiprows=lambda x: x >= header_row_index,
|
|
100
|
-
skip_blank_lines=False, sep=seperator)
|
|
118
|
+
skip_blank_lines=False, sep=seperator, encoding=encoding)
|
|
101
119
|
with io.BytesIO(file_bytes) as file_io:
|
|
102
120
|
# The use of the dtype argument is to ensure that everything from the file gets read as a string. Added
|
|
103
121
|
# because some numerical values would get ".0" appended to them, even when casting the DataFrame cell to a
|
|
104
122
|
# string.
|
|
105
123
|
file_body: DataFrame = pandas.read_csv(file_io, header=header_row_index, dtype=dtype(str),
|
|
106
|
-
skip_blank_lines=False, sep=seperator)
|
|
124
|
+
skip_blank_lines=False, sep=seperator, encoding=encoding)
|
|
107
125
|
|
|
108
126
|
return file_body, file_metadata
|
|
109
127
|
|
|
@@ -222,7 +240,7 @@ class FileUtil:
|
|
|
222
240
|
:param file_data: The CSV file to be converted.
|
|
223
241
|
:return: The bytes of the CSV file converted to an XLSX file.
|
|
224
242
|
"""
|
|
225
|
-
with (io.BytesIO(file_data) if isinstance(file_data,
|
|
243
|
+
with (io.BytesIO(file_data.encode() if isinstance(file_data, str) else file_data)) as csv:
|
|
226
244
|
# Setting header to false makes pandas read the CSV as-is.
|
|
227
245
|
data_frame = pandas.read_csv(csv, sep=",", header=None)
|
|
228
246
|
|
|
@@ -266,6 +284,20 @@ class FileUtil:
|
|
|
266
284
|
file_bytes: bytes = buffer.getvalue()
|
|
267
285
|
return file_bytes
|
|
268
286
|
|
|
287
|
+
@staticmethod
|
|
288
|
+
def zip_files(files: dict[str, str | bytes]) -> bytes:
|
|
289
|
+
"""
|
|
290
|
+
Create a zip file for a collection of files.
|
|
291
|
+
|
|
292
|
+
:param files: A dictionary of file name to file data as a string or bytes.
|
|
293
|
+
:return: The bytes for a zip file containing the input files.
|
|
294
|
+
"""
|
|
295
|
+
zip_buffer: io.BytesIO = io.BytesIO()
|
|
296
|
+
with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zip_file:
|
|
297
|
+
for file_name, file_data in files.items():
|
|
298
|
+
zip_file.writestr(file_name, file_data)
|
|
299
|
+
return zip_buffer.getvalue()
|
|
300
|
+
|
|
269
301
|
# Deprecated functions:
|
|
270
302
|
|
|
271
303
|
# FR-46097 - Add write file request shorthand functions to FileUtil.
|
|
@@ -283,6 +315,8 @@ class FileUtil:
|
|
|
283
315
|
:param request_context: Context that will be returned to the webhook server in the client callback result.
|
|
284
316
|
:return: A SapioWebhookResult with the write request as its client callback request.
|
|
285
317
|
"""
|
|
318
|
+
warnings.warn("FileUtil.write_file is deprecated as of 24.5+. Use CallbackUtil.write_file instead.",
|
|
319
|
+
DeprecationWarning)
|
|
286
320
|
return SapioWebhookResult(True, client_callback_request=WriteFileRequest(file_bytes, file_name,
|
|
287
321
|
request_context))
|
|
288
322
|
|
|
@@ -299,6 +333,8 @@ class FileUtil:
|
|
|
299
333
|
:param request_context: Context that will be returned to the webhook server in the client callback result.
|
|
300
334
|
:return: A SapioWebhookResult with the write request as its client callback request.
|
|
301
335
|
"""
|
|
336
|
+
warnings.warn("FileUtil.write_files is deprecated as of 24.5+. Use CallbackUtil.write_file instead.",
|
|
337
|
+
DeprecationWarning)
|
|
302
338
|
return SapioWebhookResult(True, client_callback_request=MultiFileRequest(files, request_context))
|
|
303
339
|
|
|
304
340
|
@staticmethod
|
|
@@ -326,6 +362,8 @@ class FileUtil:
|
|
|
326
362
|
1 - The file name of the requested file if the user provided one.
|
|
327
363
|
2 - The file bytes of the requested file if the user provided one.
|
|
328
364
|
"""
|
|
365
|
+
warnings.warn("FileUtil.request_file is deprecated as of 24.5+. Use CallbackUtil.request_file instead.",
|
|
366
|
+
DeprecationWarning)
|
|
329
367
|
client_callback = context.client_callback_result
|
|
330
368
|
result_context: str | None = client_callback.callback_context_data if client_callback else None
|
|
331
369
|
# If the user cancels, terminate the interaction.
|
|
@@ -378,6 +416,8 @@ class FileUtil:
|
|
|
378
416
|
May also contain a result that will terminate the client interaction if the user canceled the prompt.
|
|
379
417
|
1 - A dictionary that maps the file names to the file bytes for each provided file.
|
|
380
418
|
"""
|
|
419
|
+
warnings.warn("FileUtil.request_files is deprecated as of 24.5+. Use CallbackUtil.request_files instead.",
|
|
420
|
+
DeprecationWarning)
|
|
381
421
|
client_callback = context.client_callback_result
|
|
382
422
|
result_context: str | None = client_callback.callback_context_data if client_callback else None
|
|
383
423
|
# If the user cancels, terminate the interaction.
|
|
@@ -420,7 +460,7 @@ class FileUtil:
|
|
|
420
460
|
if len(allowed_extensions) != 0:
|
|
421
461
|
matches: bool = False
|
|
422
462
|
for ext in allowed_extensions:
|
|
423
|
-
if file_path.endswith("." + ext):
|
|
463
|
+
if file_path.endswith("." + ext.lstrip(".")):
|
|
424
464
|
matches = True
|
|
425
465
|
break
|
|
426
466
|
if matches is False:
|
|
@@ -4,12 +4,15 @@ from abc import abstractmethod
|
|
|
4
4
|
from typing import Any
|
|
5
5
|
|
|
6
6
|
from sapiopylib.rest.User import SapioUser
|
|
7
|
+
from sapiopylib.rest.pojo.CustomReport import RawReportTerm, RawTermOperation
|
|
7
8
|
from sapiopylib.rest.pojo.datatype.FieldDefinition import VeloxIntegerFieldDefinition, VeloxStringFieldDefinition, \
|
|
8
9
|
AbstractVeloxFieldDefinition
|
|
9
|
-
from sapiopylib.rest.pojo.webhook.WebhookResult import SapioWebhookResult
|
|
10
10
|
|
|
11
11
|
from sapiopycommons.callbacks.callback_util import CallbackUtil
|
|
12
12
|
from sapiopycommons.files.file_data_handler import FileDataHandler, FilterList
|
|
13
|
+
from sapiopycommons.general.aliases import UserIdentifier, AliasUtil
|
|
14
|
+
from sapiopycommons.general.custom_report_util import CustomReportUtil
|
|
15
|
+
from sapiopycommons.general.exceptions import SapioUserCancelledException
|
|
13
16
|
from sapiopycommons.general.time_util import TimeUtil
|
|
14
17
|
|
|
15
18
|
|
|
@@ -77,10 +80,10 @@ class FileValidator:
|
|
|
77
80
|
|
|
78
81
|
return failed_rows
|
|
79
82
|
|
|
80
|
-
def build_violation_report(self, context:
|
|
83
|
+
def build_violation_report(self, context: UserIdentifier,
|
|
81
84
|
rule_violations: dict[int, list[ValidationRule]]) -> None:
|
|
82
85
|
"""
|
|
83
|
-
|
|
86
|
+
Display a simple report of any rule violations in the file to the user as a table dialog.
|
|
84
87
|
|
|
85
88
|
:param context: The current webhook context or a user object to send requests from.
|
|
86
89
|
:param rule_violations: A dict of rule violations generated by a call to validate_file.
|
|
@@ -118,9 +121,24 @@ class FileValidator:
|
|
|
118
121
|
"Reason": violation.reason[:2000]
|
|
119
122
|
})
|
|
120
123
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
+
callback = CallbackUtil(context)
|
|
125
|
+
callback.table_dialog("Errors", "The following rule violations were encountered in the provided file.",
|
|
126
|
+
columns, rows)
|
|
127
|
+
|
|
128
|
+
def validate_and_report_errors(self, context: UserIdentifier) -> None:
|
|
129
|
+
"""
|
|
130
|
+
Validate the file. If any rule violations are found, display a simple report of any rule violations in the file
|
|
131
|
+
to the user as a table dialog and throw a SapioUserCancelled exception after the user acknowledges the dialog
|
|
132
|
+
to end the webhook interaction.
|
|
133
|
+
|
|
134
|
+
Shorthand for calling validate_file() and then build_violation_report() if there are any errors.
|
|
135
|
+
|
|
136
|
+
:param context: The current webhook context or a user object to send requests from.
|
|
137
|
+
"""
|
|
138
|
+
violations = self.validate_file()
|
|
139
|
+
if violations:
|
|
140
|
+
self.build_violation_report(context, violations)
|
|
141
|
+
raise SapioUserCancelledException()
|
|
124
142
|
|
|
125
143
|
|
|
126
144
|
class ValidationRule:
|
|
@@ -480,3 +498,71 @@ class ContainsSubstringFromCellRule(RowRule):
|
|
|
480
498
|
|
|
481
499
|
def validate(self, row: dict[str, Any]) -> bool:
|
|
482
500
|
return row.get(self.second) in row.get(self.first)
|
|
501
|
+
|
|
502
|
+
|
|
503
|
+
class UniqueSystemValueRule(ColumnRule):
|
|
504
|
+
"""
|
|
505
|
+
Requires that every cell in the column has a value that is not already in use in the system for a given data type
|
|
506
|
+
and field name.
|
|
507
|
+
"""
|
|
508
|
+
user: SapioUser
|
|
509
|
+
data_type_name: str
|
|
510
|
+
data_field_name: str
|
|
511
|
+
|
|
512
|
+
def __init__(self, context: UserIdentifier, header: str, data_type_name: str,
|
|
513
|
+
data_field_name: str):
|
|
514
|
+
"""
|
|
515
|
+
:param context: The current webhook context or a user object to send requests from.
|
|
516
|
+
:param header: The header that this rule acts upon.
|
|
517
|
+
:param data_type_name: The data type name to search on.
|
|
518
|
+
:param data_field_name: The data field name to search on. This is expected to be a string field.
|
|
519
|
+
"""
|
|
520
|
+
self.user = AliasUtil.to_sapio_user(context)
|
|
521
|
+
self.data_type_name = data_type_name
|
|
522
|
+
self.data_field_name = data_field_name
|
|
523
|
+
super().__init__(header, f"This value already exists in the system.")
|
|
524
|
+
|
|
525
|
+
def validate(self, rows: list[dict[str, Any]]) -> list[int]:
|
|
526
|
+
file_handler = FileDataHandler(rows)
|
|
527
|
+
values: list[str] = file_handler.get_values_list(self.header)
|
|
528
|
+
|
|
529
|
+
# Run a quick report for all records of this type that match these field values.
|
|
530
|
+
term = RawReportTerm(self.data_type_name, self.data_field_name, RawTermOperation.EQUAL_TO_OPERATOR,
|
|
531
|
+
"{" + ",".join(values) + "}")
|
|
532
|
+
results: list[dict[str, Any]] = CustomReportUtil.run_quick_report(self.user, term)
|
|
533
|
+
existing_values: list[Any] = [x.get(self.data_field_name) for x in results]
|
|
534
|
+
return file_handler.get_in_list(self.header, existing_values)
|
|
535
|
+
|
|
536
|
+
|
|
537
|
+
class ExistingSystemValueRule(ColumnRule):
|
|
538
|
+
"""
|
|
539
|
+
Requires that every cell in the column has a value that is already in use in the system for a given data type
|
|
540
|
+
and field name.
|
|
541
|
+
"""
|
|
542
|
+
user: SapioUser
|
|
543
|
+
data_type_name: str
|
|
544
|
+
data_field_name: str
|
|
545
|
+
|
|
546
|
+
def __init__(self, context: UserIdentifier, header: str, data_type_name: str,
|
|
547
|
+
data_field_name: str):
|
|
548
|
+
"""
|
|
549
|
+
:param context: The current webhook context or a user object to send requests from.
|
|
550
|
+
:param header: The header that this rule acts upon.
|
|
551
|
+
:param data_type_name: The data type name to search on.
|
|
552
|
+
:param data_field_name: The data field name to search on. This is expected to be a string field.
|
|
553
|
+
"""
|
|
554
|
+
self.user = AliasUtil.to_sapio_user(context)
|
|
555
|
+
self.data_type_name = data_type_name
|
|
556
|
+
self.data_field_name = data_field_name
|
|
557
|
+
super().__init__(header, f"This value doesn't exist in the system.")
|
|
558
|
+
|
|
559
|
+
def validate(self, rows: list[dict[str, Any]]) -> list[int]:
|
|
560
|
+
file_handler = FileDataHandler(rows)
|
|
561
|
+
values: list[str] = file_handler.get_values_list(self.header)
|
|
562
|
+
|
|
563
|
+
# Run a quick report for all records of this type that match these field values.
|
|
564
|
+
term = RawReportTerm(self.data_type_name, self.data_field_name, RawTermOperation.EQUAL_TO_OPERATOR,
|
|
565
|
+
"{" + ",".join(values) + "}")
|
|
566
|
+
results: list[dict[str, Any]] = CustomReportUtil.run_quick_report(self.user, term)
|
|
567
|
+
existing_values: list[Any] = [x.get(self.data_field_name) for x in results]
|
|
568
|
+
return file_handler.get_not_in_list(self.header, existing_values)
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
|
+
import warnings
|
|
3
4
|
from abc import abstractmethod
|
|
4
5
|
from enum import Enum
|
|
5
6
|
from typing import Any
|
|
@@ -18,7 +19,7 @@ class FileWriter:
|
|
|
18
19
|
body: list[list[Any]]
|
|
19
20
|
delimiter: str
|
|
20
21
|
line_break: str
|
|
21
|
-
column_definitions:
|
|
22
|
+
column_definitions: dict[str, ColumnDef]
|
|
22
23
|
|
|
23
24
|
def __init__(self, headers: list[str], delimiter: str = ",", line_break: str = "\r\n"):
|
|
24
25
|
"""
|
|
@@ -30,7 +31,7 @@ class FileWriter:
|
|
|
30
31
|
self.delimiter = delimiter
|
|
31
32
|
self.line_break = line_break
|
|
32
33
|
self.body = []
|
|
33
|
-
self.column_definitions =
|
|
34
|
+
self.column_definitions = {}
|
|
34
35
|
|
|
35
36
|
def add_row_list(self, row: list[Any]) -> None:
|
|
36
37
|
"""
|
|
@@ -65,21 +66,49 @@ class FileWriter:
|
|
|
65
66
|
new_row.append(row.get(header, ""))
|
|
66
67
|
self.body.append(new_row)
|
|
67
68
|
|
|
68
|
-
def
|
|
69
|
+
def add_column_definition(self, header: str, column_def: ColumnDef) -> None:
|
|
69
70
|
"""
|
|
70
|
-
Add new column
|
|
71
|
-
meaning that they map to the header with the equivalent index. Before the file is built, the number of column
|
|
72
|
-
definitions must equal the number of headers if any column definition is provided.
|
|
71
|
+
Add a new column definition to this FileWriter for a specific header.
|
|
73
72
|
|
|
74
|
-
ColumnDefs are only used if the build_file function is provided with a list of RowBundles.
|
|
73
|
+
ColumnDefs are only used if the build_file function is provided with a list of RowBundles. Every header must
|
|
74
|
+
have a column definition if this is the case.
|
|
75
75
|
|
|
76
76
|
Custom column definitions can be created by defining a class that extends ColumnDef and implements the print
|
|
77
77
|
method.
|
|
78
78
|
|
|
79
|
-
:param
|
|
79
|
+
:param column_def: A column definitions to be used to construct the file when build_file is
|
|
80
80
|
called.
|
|
81
|
+
:param header: The header that this column definition is for. If a header is provided that isn't in the headers
|
|
82
|
+
list, the header is appended to the end of the list.
|
|
81
83
|
"""
|
|
82
|
-
self.
|
|
84
|
+
if header not in self.headers:
|
|
85
|
+
self.headers.append(header)
|
|
86
|
+
self.column_definitions[header] = column_def
|
|
87
|
+
|
|
88
|
+
def add_column_definitions(self, column_defs: dict[str, ColumnDef]) -> None:
|
|
89
|
+
"""
|
|
90
|
+
Add new column definitions to this FileWriter.
|
|
91
|
+
|
|
92
|
+
ColumnDefs are only used if the build_file function is provided with a list of RowBundles. Every header must
|
|
93
|
+
have a column definition if this is the case.
|
|
94
|
+
|
|
95
|
+
Custom column definitions can be created by defining a class that extends ColumnDef and implements the print
|
|
96
|
+
method.
|
|
97
|
+
|
|
98
|
+
:param column_defs: A dictionary of header names to column definitions to be used to construct the file when
|
|
99
|
+
build_file is called.
|
|
100
|
+
"""
|
|
101
|
+
# For backwards compatibility purposes, if column definitions are provided as a list,
|
|
102
|
+
# add them in order of appearance of the headers. This will only work if the headers are defined first, though.
|
|
103
|
+
if isinstance(column_defs, list):
|
|
104
|
+
warnings.warn("Adding column definitions is no longer expected as a list. Continuing to provide a list to "
|
|
105
|
+
"this function may result in undesirable behavior.", UserWarning)
|
|
106
|
+
if not self.headers:
|
|
107
|
+
raise SapioException("No headers provided to FileWriter before the column definitions were added.")
|
|
108
|
+
for header, column_def in zip(self.headers, column_defs):
|
|
109
|
+
self.column_definitions[header] = column_def
|
|
110
|
+
for header, column_def in column_defs.items():
|
|
111
|
+
self.add_column_definition(header, column_def)
|
|
83
112
|
|
|
84
113
|
def build_file(self, rows: list[RowBundle] | None = None, sorter=None, reverse: bool = False) -> str:
|
|
85
114
|
"""
|
|
@@ -100,11 +129,10 @@ class FileWriter:
|
|
|
100
129
|
"""
|
|
101
130
|
# If any column definitions have been provided, the number of column definitions and headers must be equal.
|
|
102
131
|
if self.column_definitions:
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
f"headers. The number of column definitions must equal the number of headers.")
|
|
132
|
+
for header in self.headers:
|
|
133
|
+
if header not in self.column_definitions:
|
|
134
|
+
raise SapioException(f"FileWriter has no column definition for the header {header}. If any column "
|
|
135
|
+
f"definitions are provided, then all headers must have a column definition.")
|
|
108
136
|
# If any RowBundles have been provided, there must be column definitions for mapping them to the file.
|
|
109
137
|
elif rows:
|
|
110
138
|
raise SapioException(f"FileWriter was given RowBundles but contains no column definitions for mapping "
|
|
@@ -130,7 +158,8 @@ class FileWriter:
|
|
|
130
158
|
rows.sort(key=lambda x: x.index)
|
|
131
159
|
for row in rows:
|
|
132
160
|
new_row: list[Any] = []
|
|
133
|
-
for
|
|
161
|
+
for header in self.headers:
|
|
162
|
+
column = self.column_definitions[header]
|
|
134
163
|
if column.may_skip and row.may_skip:
|
|
135
164
|
new_row.append("")
|
|
136
165
|
else:
|