|
1 | 1 | import re |
2 | | -from typing import TYPE_CHECKING, Any, Optional |
| 2 | +from typing import TYPE_CHECKING, Any, Optional, Union |
| 3 | + |
| 4 | +if TYPE_CHECKING: |
| 5 | + from structlog._config import BoundLoggerLazyProxy |
3 | 6 |
|
4 | 7 | import openpyxl |
5 | 8 | from openpyxl.worksheet.worksheet import Worksheet |
@@ -95,6 +98,34 @@ def is_reduced_version(generated_code_value: str, code: str) -> bool: |
95 | 98 | return True |
96 | 99 |
|
97 | 100 |
|
| 101 | +def str_to_bool( |
| 102 | + value: Optional[Union[str, bool]], |
| 103 | + term: str, |
| 104 | + coordinate: str, |
| 105 | + sheet_title: str, |
| 106 | + logger: "BoundLoggerLazyProxy", |
| 107 | +) -> bool: |
| 108 | + """ |
| 109 | + Converts a string to a boolean value. |
| 110 | +
|
| 111 | + Args: |
| 112 | + value: The string to convert. |
| 113 | +
|
| 114 | + Returns: |
| 115 | + The boolean value. |
| 116 | + """ |
| 117 | + # No `value` provided |
| 118 | + if not value: |
| 119 | + return False |
| 120 | + |
| 121 | + val = str(value).strip().lower() |
| 122 | + if val not in ["true", "false"]: |
| 123 | + logger.error( |
| 124 | + f"Invalid {term.lower()} value found in the {term} column at position {coordinate} in {sheet_title}. Accepted values: TRUE or FALSE." |
| 125 | + ) |
| 126 | + return val == "true" |
| 127 | + |
| 128 | + |
98 | 129 | def properties_to_dict( |
99 | 130 | sheet: Worksheet, start_index_row: int, last_non_empty_row: int |
100 | 131 | ) -> dict[str, dict[str, Any]]: |
@@ -178,32 +209,26 @@ def properties_to_dict( |
178 | 209 | # Check the cell below "Mandatory" |
179 | 210 | elif term == "Mandatory": |
180 | 211 | for cell in sheet[term_letter][header_index:last_non_empty_row]: |
181 | | - mandatory = str(cell.value) |
182 | | - if mandatory is not None: |
183 | | - mandatory = mandatory.strip().lower() |
184 | | - if mandatory not in {"true", "false"}: |
185 | | - logger.error( |
186 | | - f"Invalid {term.lower()} value found in the {term} column at position {cell.coordinate} in {sheet.title}. Accepted values: TRUE or FALSE." |
187 | | - ) |
188 | | - mandatory = mandatory == "true" |
189 | | - mandatories.append(mandatory) |
190 | | - else: |
191 | | - mandatories.append(False) |
| 212 | + mandatory = str_to_bool( |
| 213 | + value=cell.value, |
| 214 | + term=term, |
| 215 | + coordinate=cell.coordinate, |
| 216 | + sheet_title=sheet.title, |
| 217 | + logger=logger, |
| 218 | + ) |
| 219 | + mandatories.append(mandatory) |
192 | 220 |
|
193 | 221 | # Check the cell below "Show in edit views" |
194 | 222 | elif term == "Show in edit views": |
195 | 223 | for cell in sheet[term_letter][header_index:last_non_empty_row]: |
196 | | - show = str(cell.value) |
197 | | - if show is not None: |
198 | | - show = show.strip().lower() |
199 | | - if show not in {"true", "false"}: |
200 | | - logger.error( |
201 | | - f"Invalid {term.lower()} value found in the {term} column at position {cell.coordinate} in {sheet.title}. Accepted values: TRUE or FALSE." |
202 | | - ) |
203 | | - show = show == "true" |
204 | | - shows.append(show) |
205 | | - else: |
206 | | - shows.append(False) |
| 224 | + show = str_to_bool( |
| 225 | + value=cell.value, |
| 226 | + term=term, |
| 227 | + coordinate=cell.coordinate, |
| 228 | + sheet_title=sheet.title, |
| 229 | + logger=logger, |
| 230 | + ) |
| 231 | + shows.append(show) |
207 | 232 |
|
208 | 233 | # Check the cell below "Section" |
209 | 234 | elif term == "Section": |
@@ -367,17 +392,14 @@ def terms_to_dict( |
367 | 392 | # Check the cell below "Officials" |
368 | 393 | elif term == "Official": |
369 | 394 | for cell in sheet[term_letter][header_index:last_non_empty_row]: |
370 | | - official = str(cell.value) |
371 | | - official = official.strip().lower() |
372 | | - if official is not None: |
373 | | - if official not in {"true", "false"}: |
374 | | - logger.error( |
375 | | - f"Invalid {term.lower()} value found in the {term} column at position {cell.coordinate} in {sheet.title}. Accepted values: TRUE or FALSE." |
376 | | - ) |
377 | | - official = official == "true" |
378 | | - officials.append(official) |
379 | | - else: |
380 | | - officials.append(False) |
| 395 | + official = str_to_bool( |
| 396 | + value=cell.value, |
| 397 | + term=term, |
| 398 | + coordinate=cell.coordinate, |
| 399 | + sheet_title=sheet.title, |
| 400 | + logger=logger, |
| 401 | + ) |
| 402 | + officials.append(official) |
381 | 403 |
|
382 | 404 | for i in range(0, len(codes)): |
383 | 405 | terms_dict[codes[i]] = { |
@@ -409,7 +431,7 @@ def block_to_entity_dict( |
409 | 431 | Returns: |
410 | 432 | A dictionary containing the entity attributes. |
411 | 433 | """ |
412 | | - attributes_dict = {} |
| 434 | + attributes_dict: dict = {} |
413 | 435 |
|
414 | 436 | # Get the entity type from the specified cell |
415 | 437 | entity_type_position = f"A{start_index_row}" |
@@ -505,17 +527,23 @@ def block_to_entity_dict( |
505 | 527 |
|
506 | 528 | # Check the cell below "Auto generate codes" |
507 | 529 | elif term == "Auto generate codes": |
508 | | - auto_generate_value = str( |
509 | | - sheet.cell( |
510 | | - row=start_index_row + 2, column=term_index + 1 |
511 | | - ).value |
| 530 | + cell = sheet.cell( |
| 531 | + row=start_index_row + 2, column=term_index + 1 |
512 | 532 | ) |
513 | | - auto_generate_value = auto_generate_value.strip().lower() |
514 | | - if auto_generate_value not in {"true", "false"}: |
515 | | - logger.error( |
516 | | - f"Invalid {term.lower()} value found in the {term} value for entity {code_value} at row {start_index_row + 2}" |
517 | | - ) |
518 | | - auto_generate_value = auto_generate_value == "true" |
| 533 | + auto_generate_value = str_to_bool( |
| 534 | + value=cell.value, |
| 535 | + term=term, |
| 536 | + coordinate=cell.coordinate, |
| 537 | + sheet_title=sheet.title, |
| 538 | + logger=logger, |
| 539 | + ) |
| 540 | + # auto_generate_value = "" |
| 541 | + # auto_generate_value = auto_generate_value.strip().lower() |
| 542 | + # if auto_generate_value not in {"true", "false"}: |
| 543 | + # logger.error( |
| 544 | + # f"Invalid {term.lower()} value found in the {term} value for entity {code_value} at row {start_index_row + 2}" |
| 545 | + # ) |
| 546 | + # auto_generate_value = auto_generate_value == "true" |
519 | 547 | attributes_dict["autoGeneratedCode"] = auto_generate_value |
520 | 548 |
|
521 | 549 | # Assign the properties dictionary as a field for the entity dictionary |
@@ -778,6 +806,7 @@ def block_to_entity_dict( |
778 | 806 | return dict( |
779 | 807 | sorted(complete_dict.items(), key=lambda item: item[0].count(".")) |
780 | 808 | ) |
| 809 | + return attributes_dict |
781 | 810 |
|
782 | 811 |
|
783 | 812 | def excel_to_entities( |
|
0 commit comments