Skip to content

Commit 75c4e1f

Browse files
authored
Fixing mypy and other extras (#61)
Added str_to_bool function Fix mypy in excel_to_entities Added tmp/ to gitignore
1 parent 830362b commit 75c4e1f

File tree

2 files changed

+77
-46
lines changed

2 files changed

+77
-46
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,4 +174,6 @@ pytest.xml
174174
pytest-coverage.txt
175175

176176
# artifacts
177-
artifacts
177+
artifacts
178+
# and tmp files
179+
tmp/

bam_masterdata/cli/excel_to_entities.py

Lines changed: 74 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
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
36

47
import openpyxl
58
from openpyxl.worksheet.worksheet import Worksheet
@@ -95,6 +98,34 @@ def is_reduced_version(generated_code_value: str, code: str) -> bool:
9598
return True
9699

97100

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+
98129
def properties_to_dict(
99130
sheet: Worksheet, start_index_row: int, last_non_empty_row: int
100131
) -> dict[str, dict[str, Any]]:
@@ -178,32 +209,26 @@ def properties_to_dict(
178209
# Check the cell below "Mandatory"
179210
elif term == "Mandatory":
180211
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)
192220

193221
# Check the cell below "Show in edit views"
194222
elif term == "Show in edit views":
195223
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)
207232

208233
# Check the cell below "Section"
209234
elif term == "Section":
@@ -367,17 +392,14 @@ def terms_to_dict(
367392
# Check the cell below "Officials"
368393
elif term == "Official":
369394
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)
381403

382404
for i in range(0, len(codes)):
383405
terms_dict[codes[i]] = {
@@ -409,7 +431,7 @@ def block_to_entity_dict(
409431
Returns:
410432
A dictionary containing the entity attributes.
411433
"""
412-
attributes_dict = {}
434+
attributes_dict: dict = {}
413435

414436
# Get the entity type from the specified cell
415437
entity_type_position = f"A{start_index_row}"
@@ -505,17 +527,23 @@ def block_to_entity_dict(
505527

506528
# Check the cell below "Auto generate codes"
507529
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
512532
)
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"
519547
attributes_dict["autoGeneratedCode"] = auto_generate_value
520548

521549
# Assign the properties dictionary as a field for the entity dictionary
@@ -778,6 +806,7 @@ def block_to_entity_dict(
778806
return dict(
779807
sorted(complete_dict.items(), key=lambda item: item[0].count("."))
780808
)
809+
return attributes_dict
781810

782811

783812
def excel_to_entities(

0 commit comments

Comments
 (0)