From 1c3365fae3d2ee8ebd75a9698607d396bd64c1c8 Mon Sep 17 00:00:00 2001 From: Luuk Blom Date: Fri, 4 Oct 2024 09:41:31 +0200 Subject: [PATCH 1/2] convert str to Path to fix: AttributeError: 'str' object has no attribute 'stat' when checking for existance --- fiat_toolbox/infographics/risk_infographics.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/fiat_toolbox/infographics/risk_infographics.py b/fiat_toolbox/infographics/risk_infographics.py index 5a2961c..80831ec 100644 --- a/fiat_toolbox/infographics/risk_infographics.py +++ b/fiat_toolbox/infographics/risk_infographics.py @@ -97,10 +97,11 @@ def _encode_image_from_path(image_path: str) -> str: str The base64 encoded image string """ - if not Path.exists(image_path): - RiskInfographicsParser.logger.error(f"Image not found at {image_path}") + path = Path(image_path) + if not path.exists(): + RiskInfographicsParser.logger.error(f"Image not found at {path}") return - with open(image_path, "rb") as image_file: + with open(path, "rb") as image_file: encoded_string = base64.b64encode(image_file.read()).decode() return f'data:image/png;base64,{encoded_string}' From d1ad6cd2857e14637b96c56e8672409feba611e5 Mon Sep 17 00:00:00 2001 From: Luuk Blom Date: Fri, 4 Oct 2024 09:48:21 +0200 Subject: [PATCH 2/2] Fix AttributeError when checking for existence of image file --- fiat_toolbox/infographics/risk_infographics.py | 2 +- tests/infographics/test_risk_infographics.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/fiat_toolbox/infographics/risk_infographics.py b/fiat_toolbox/infographics/risk_infographics.py index 80831ec..ea29d05 100644 --- a/fiat_toolbox/infographics/risk_infographics.py +++ b/fiat_toolbox/infographics/risk_infographics.py @@ -98,7 +98,7 @@ def _encode_image_from_path(image_path: str) -> str: The base64 encoded image string """ path = Path(image_path) - if not path.exists(): + if not Path.exists(path): RiskInfographicsParser.logger.error(f"Image not found at {path}") return with open(path, "rb") as image_file: diff --git a/tests/infographics/test_risk_infographics.py b/tests/infographics/test_risk_infographics.py index 4280d1d..010f4f4 100644 --- a/tests/infographics/test_risk_infographics.py +++ b/tests/infographics/test_risk_infographics.py @@ -99,8 +99,8 @@ def test_encode_image_from_path(self, mock_open, mock_path_exists): # Assert expected_encoded_string = f'data:image/png;base64,{base64.b64encode(self.money_bin).decode()}' assert encoded_image == expected_encoded_string - mock_open.assert_called_once_with(self.money_path, "rb") - mock_path_exists.assert_called_once_with(self.money_path) + mock_open.assert_called_once_with(Path(self.money_path), "rb") + mock_path_exists.assert_called_once_with(Path(self.money_path)) @patch("fiat_toolbox.infographics.infographics.Path.exists")