Skip to content

Commit 0f61de5

Browse files
cleaning the eventual creation of dirs
1 parent a342d17 commit 0f61de5

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

pandasai/core/code_generation/code_cleaning.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ def clean_code(self, code: str) -> str:
138138
code = self._replace_output_filenames_with_temp_chart(code)
139139
code = self._replace_output_filenames_with_temp_json_chart(code)
140140

141+
code = self._remove_make_dirs(code)
142+
141143
# If plt.show or fig.show is in the code, remove that line
142144
code = re.sub(r"[a-z].show\(\)", "", code)
143145

@@ -180,3 +182,16 @@ def _replace_output_filenames_with_temp_json_chart(self, code: str) -> str:
180182
lambda m: f"{m.group(1)}{chart_path}{m.group(1)}",
181183
code,
182184
)
185+
186+
def _remove_make_dirs(self, code: str) -> str:
187+
"""
188+
Remove any directory creation commands from the code.
189+
"""
190+
# Remove lines that create directories, except for the default chart directory DEFAULT_CHART_DIRECTORY
191+
code_lines = code.splitlines()
192+
cleaned_lines = []
193+
for line in code_lines:
194+
if DEFAULT_CHART_DIRECTORY not in line and ("os.makedirs(" in line or "os.mkdir(" in line):
195+
continue
196+
cleaned_lines.append(line)
197+
return "\n".join(cleaned_lines)

tests/unit_tests/core/code_generation/test_code_cleaning.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from unittest.mock import MagicMock
66

77
from pandasai.agent.state import AgentState
8+
from pandasai.constants import DEFAULT_CHART_DIRECTORY
89
from pandasai.core.code_generation.code_cleaning import CodeCleaner
910
from pandasai.dataframe.base import DataFrame
1011
from pandasai.exceptions import MaliciousQueryError
@@ -201,14 +202,45 @@ def test_replace_output_filenames_with_temp_json_chart_no_json(self):
201202
handler = self.cleaner
202203

203204
code = "some text without json"
204-
expected_code = "some text without json" # No change should occur
205205

206206
result = handler._replace_output_filenames_with_temp_json_chart(code)
207207

208+
self.assertEqual(
209+
result, code, f"Expected '{code}', but got '{result}'"
210+
)
211+
212+
def test_remove_make_dirs(self):
213+
handler = self.cleaner
214+
215+
code = "os.makedirs('/some/path')\nplt.show()\nfig.show()"
216+
expected_code = "plt.show()\nfig.show()" # Should remove the os.makedirs line
217+
result = handler._remove_make_dirs(code)
218+
self.assertEqual(
219+
result, expected_code, f"Expected '{expected_code}', but got '{result}'"
220+
)
221+
222+
code = "os.mkdir('/some/path')\nplt.show()\nfig.show()"
223+
expected_code = "plt.show()\nfig.show()" # Should remove the os.mkdir line
224+
result = handler._remove_make_dirs(code)
208225
self.assertEqual(
209226
result, expected_code, f"Expected '{expected_code}', but got '{result}'"
210227
)
211228

229+
def test_do_not_remove_make_default_chart_dir(self):
230+
handler = self.cleaner
231+
232+
code = f"os.makedirs({DEFAULT_CHART_DIRECTORY}')\nplt.show()\nfig.show()"
233+
result = handler._remove_make_dirs(code)
234+
self.assertEqual(
235+
result, code, f"Expected '{code}', but got '{result}'"
236+
)
237+
238+
code = f"os.mkdir({DEFAULT_CHART_DIRECTORY}')\nplt.show()\nfig.show()"
239+
result = handler._remove_make_dirs(code)
240+
self.assertEqual(
241+
result, code, f"Expected '{code}', but got '{result}'"
242+
)
243+
212244

213245
if __name__ == "__main__":
214246
unittest.main()

0 commit comments

Comments
 (0)