janito 3.14.1__py3-none-any.whl → 3.15.0__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.
Files changed (38) hide show
  1. janito/platform_discovery.py +1 -8
  2. janito/plugins/tools/local/adapter.py +3 -2
  3. janito/plugins/tools/local/ask_user.py +111 -112
  4. janito/plugins/tools/local/copy_file.py +86 -87
  5. janito/plugins/tools/local/create_directory.py +111 -112
  6. janito/plugins/tools/local/create_file.py +0 -1
  7. janito/plugins/tools/local/delete_text_in_file.py +133 -134
  8. janito/plugins/tools/local/fetch_url.py +465 -466
  9. janito/plugins/tools/local/find_files.py +142 -143
  10. janito/plugins/tools/local/markdown_view.py +0 -1
  11. janito/plugins/tools/local/move_file.py +130 -131
  12. janito/plugins/tools/local/open_html_in_browser.py +50 -51
  13. janito/plugins/tools/local/open_url.py +36 -37
  14. janito/plugins/tools/local/python_code_run.py +171 -172
  15. janito/plugins/tools/local/python_command_run.py +170 -171
  16. janito/plugins/tools/local/python_file_run.py +171 -172
  17. janito/plugins/tools/local/read_chart.py +258 -259
  18. janito/plugins/tools/local/read_files.py +57 -58
  19. janito/plugins/tools/local/remove_directory.py +54 -55
  20. janito/plugins/tools/local/remove_file.py +57 -58
  21. janito/plugins/tools/local/replace_text_in_file.py +275 -276
  22. janito/plugins/tools/local/run_bash_command.py +182 -183
  23. janito/plugins/tools/local/run_powershell_command.py +217 -218
  24. janito/plugins/tools/local/show_image.py +0 -1
  25. janito/plugins/tools/local/show_image_grid.py +0 -1
  26. janito/plugins/tools/local/view_file.py +0 -1
  27. janito/providers/alibaba/provider.py +1 -1
  28. janito/providers/deepseek/model_info.py +16 -37
  29. janito/providers/deepseek/provider.py +4 -3
  30. janito/tools/base.py +19 -12
  31. janito/tools/tool_base.py +122 -121
  32. janito/tools/tools_schema.py +104 -104
  33. {janito-3.14.1.dist-info → janito-3.15.0.dist-info}/METADATA +9 -32
  34. {janito-3.14.1.dist-info → janito-3.15.0.dist-info}/RECORD +38 -38
  35. {janito-3.14.1.dist-info → janito-3.15.0.dist-info}/WHEEL +0 -0
  36. {janito-3.14.1.dist-info → janito-3.15.0.dist-info}/entry_points.txt +0 -0
  37. {janito-3.14.1.dist-info → janito-3.15.0.dist-info}/licenses/LICENSE +0 -0
  38. {janito-3.14.1.dist-info → janito-3.15.0.dist-info}/top_level.txt +0 -0
@@ -1,276 +1,275 @@
1
- from janito.tools.tool_base import ToolBase, ToolPermissions
2
- from janito.report_events import ReportAction
3
- from janito.plugins.tools.local.adapter import register_local_tool
4
- from janito.i18n import tr
5
- import shutil
6
- import re
7
- from janito.plugins.tools.local.validate_file_syntax.core import validate_file_syntax
8
-
9
-
10
- @register_local_tool
11
- class ReplaceTextInFileTool(ToolBase):
12
- """
13
- Replace exact occurrences of a given text in a file.
14
-
15
- Note:
16
- To avoid syntax errors, ensure your replacement text is pre-indented as needed, matching the indentation of the
17
- search text in its original location.
18
-
19
- Args:
20
- path (str): Path to the file to modify.
21
- search_text (str): The exact text to search for (including indentation).
22
- replacement_text (str): The text to replace with (including indentation).
23
- replace_all (bool): If True, replace all occurrences; otherwise, only the first occurrence.
24
- backup (bool, optional): Deprecated. No backups are created anymore and this flag is ignored. Defaults to False.
25
- Returns:
26
- str: Status message. Example:
27
- - "Text replaced in /path/to/file"
28
- - "No changes made. [Warning: Search text not found in file] Please review the original file."
29
- - "Error replacing text: <error message>"
30
- """
31
-
32
- permissions = ToolPermissions(read=True, write=True)
33
- tool_name = "replace_text_in_file"
34
-
35
- def run(
36
- self,
37
- path: str,
38
- search_text: str,
39
- replacement_text: str,
40
- replace_all: bool = False,
41
- backup: bool = False,
42
- ) -> str:
43
- from janito.tools.tool_utils import display_path
44
-
45
- disp_path = display_path(path)
46
- action = "∞" if replace_all else ""
47
- search_lines = len(search_text.splitlines())
48
- replace_lines = len(replacement_text.splitlines())
49
- info_msg = self._format_info_msg(
50
- disp_path,
51
- search_lines,
52
- replace_lines,
53
- action,
54
- search_text,
55
- replacement_text,
56
- path,
57
- )
58
- self.report_action(info_msg, ReportAction.CREATE)
59
- try:
60
- content = self._read_file_content(path)
61
- match_lines = self._find_match_lines(content, search_text)
62
- occurrences = content.count(search_text)
63
- replaced_count, new_content = self._replace_content(
64
- content, search_text, replacement_text, replace_all, occurrences
65
- )
66
- file_changed = new_content != content
67
- backup_path = None
68
- validation_result = ""
69
- if file_changed:
70
- self._write_file_content(path, new_content)
71
- # Perform syntax validation and append result
72
- validation_result = validate_file_syntax(path)
73
- warning, concise_warning = self._handle_warnings(
74
- replaced_count, file_changed, occurrences
75
- )
76
-
77
- if concise_warning:
78
- return concise_warning
79
- self._report_success(match_lines)
80
- line_delta_str = self._get_line_delta_str(content, new_content)
81
- match_info, details = self._format_match_details(
82
- replaced_count,
83
- match_lines,
84
- search_lines,
85
- replace_lines,
86
- line_delta_str,
87
- replace_all,
88
- )
89
- return self._format_final_msg(path, warning, match_info, details) + (
90
- f"\n{validation_result}" if validation_result else ""
91
- )
92
- except Exception as e:
93
- self.report_error(tr(" Error"), ReportAction.UPDATE)
94
- return tr("Error replacing text: {error}", error=e)
95
-
96
- def _read_file_content(self, path):
97
- """Read the entire content of the file."""
98
- with open(path, "r", encoding="utf-8", errors="replace") as f:
99
- return f.read()
100
-
101
- def _find_match_lines(self, content, search_text):
102
- """Find all line numbers where search_text occurs in content."""
103
- lines = content.splitlines(keepends=True)
104
- joined = "".join(lines)
105
- match_lines = []
106
- idx = 0
107
- while True:
108
- idx = joined.find(search_text, idx)
109
- if idx == -1:
110
- break
111
- upto = joined[:idx]
112
- line_no = upto.count("\n") + 1
113
- match_lines.append(line_no)
114
- idx += 1 if not search_text else len(search_text)
115
- return match_lines
116
-
117
- def _replace_content(
118
- self, content, search_text, replacement_text, replace_all, occurrences
119
- ):
120
- """Replace occurrences of search_text with replacement_text in content."""
121
- if replace_all:
122
- replaced_count = content.count(search_text)
123
- new_content = content.replace(search_text, replacement_text)
124
- else:
125
- if occurrences > 1:
126
- return 0, content # No changes made, not unique
127
- replaced_count = 1 if occurrences == 1 else 0
128
- new_content = content.replace(search_text, replacement_text, 1)
129
- return replaced_count, new_content
130
-
131
- def _backup_file(self, path, backup_path):
132
- """Create a backup of the file."""
133
- shutil.copy2(path, backup_path)
134
-
135
- def _write_file_content(self, path, content):
136
- """Write content to the file."""
137
- with open(path, "w", encoding="utf-8", errors="replace") as f:
138
- f.write(content)
139
-
140
- def _handle_warnings(self, replaced_count, file_changed, occurrences):
141
- """Handle and return warnings and concise warnings if needed."""
142
- warning = ""
143
- concise_warning = None
144
- if replaced_count == 0:
145
- warning = tr(" [Warning: Search text not found in file]")
146
- if not file_changed:
147
- self.report_warning(
148
- tr(" ℹ️ No changes made. (not found)"), ReportAction.CREATE
149
- )
150
- concise_warning = tr(
151
- "No changes made. The search text was not found. Expand your search context with surrounding lines if needed."
152
- )
153
- if occurrences > 1 and replaced_count == 0:
154
- self.report_warning(
155
- tr(" ℹ️ No changes made. (not unique)"), ReportAction.CREATE
156
- )
157
- concise_warning = tr(
158
- "No changes made. The search text is not unique. Expand your search context with surrounding lines to ensure uniqueness."
159
- )
160
- return warning, concise_warning
161
-
162
- def _report_success(self, match_lines, line_delta_str=""):
163
- """Report success with line numbers where replacements occurred."""
164
- if match_lines:
165
- lines_str = ", ".join(str(line_no) for line_no in match_lines)
166
- self.report_success(
167
- tr(
168
- " ✅ replaced at {lines_str}{delta}",
169
- lines_str=lines_str,
170
- delta=line_delta_str,
171
- ),
172
- ReportAction.CREATE,
173
- )
174
- else:
175
- self.report_success(
176
- tr(" ✅ replaced{delta}", delta=line_delta_str), ReportAction.CREATE
177
- )
178
-
179
- def _get_line_delta_str(self, content, new_content):
180
- """Return a string describing the net line change after replacement."""
181
- total_lines_before = content.count("\n") + 1
182
- total_lines_after = new_content.count("\n") + 1
183
- line_delta = total_lines_after - total_lines_before
184
- if line_delta > 0:
185
- return f" (+{line_delta} lines)"
186
- elif line_delta < 0:
187
- return f" ({line_delta} lines)"
188
- else:
189
- return " (no net line change)"
190
-
191
- def _format_info_msg(
192
- self,
193
- disp_path,
194
- search_lines,
195
- replace_lines,
196
- action,
197
- search_text,
198
- replacement_text,
199
- path,
200
- ):
201
- """Format the info message for the operation."""
202
- if replace_lines == 0:
203
- return tr(
204
- "📝 Replace in {disp_path} del {search_lines} lines {action}",
205
- disp_path=disp_path,
206
- search_lines=search_lines,
207
- action=action,
208
- )
209
- else:
210
- try:
211
- with open(path, "r", encoding="utf-8", errors="replace") as f:
212
- _content = f.read()
213
- _new_content = _content.replace(
214
- search_text, replacement_text, -1 if action else 1
215
- )
216
- _total_lines_before = _content.count("\n") + 1
217
- _total_lines_after = _new_content.count("\n") + 1
218
- _line_delta = _total_lines_after - _total_lines_before
219
- except Exception:
220
- _line_delta = replace_lines - search_lines
221
- if _line_delta > 0:
222
- delta_str = f"+{_line_delta} lines"
223
- elif _line_delta < 0:
224
- delta_str = f"{_line_delta} lines"
225
- else:
226
- delta_str = "+0"
227
- return tr(
228
- "📝 Replace in {disp_path} {delta_str} {action}",
229
- disp_path=disp_path,
230
- delta_str=delta_str,
231
- action=action,
232
- )
233
-
234
- def _format_match_details(
235
- self,
236
- replaced_count,
237
- match_lines,
238
- search_lines,
239
- replace_lines,
240
- line_delta_str,
241
- replace_all,
242
- ):
243
- """Format match info and details for the final message."""
244
- if replaced_count > 0:
245
- if replace_all:
246
- match_info = tr(
247
- "Matches found at lines: {lines}. ",
248
- lines=", ".join(str(line) for line in match_lines),
249
- )
250
- else:
251
- match_info = (
252
- tr("Match found at line {line}. ", line=match_lines[0])
253
- if match_lines
254
- else ""
255
- )
256
- details = tr(
257
- "Replaced {replaced_count} occurrence(s) at above line(s): {search_lines} lines replaced with {replace_lines} lines each.{line_delta_str}",
258
- replaced_count=replaced_count,
259
- search_lines=search_lines,
260
- replace_lines=replace_lines,
261
- line_delta_str=line_delta_str,
262
- )
263
- else:
264
- match_info = ""
265
- details = ""
266
- return match_info, details
267
-
268
- def _format_final_msg(self, path, warning, match_info, details):
269
- """Format the final status message."""
270
- return tr(
271
- "Text replaced in {path}{warning}. {match_info}{details}",
272
- path=path,
273
- warning=warning,
274
- match_info=match_info,
275
- details=details,
276
- )
1
+ from janito.tools.tool_base import ToolBase, ToolPermissions
2
+ from janito.report_events import ReportAction
3
+ from janito.plugins.tools.local.adapter import register_local_tool
4
+ from janito.i18n import tr
5
+ import shutil
6
+ import re
7
+ from janito.plugins.tools.local.validate_file_syntax.core import validate_file_syntax
8
+
9
+
10
+ @register_local_tool
11
+ class ReplaceTextInFileTool(ToolBase):
12
+ """
13
+ Replace exact occurrences of a given text in a file.
14
+
15
+ Note:
16
+ To avoid syntax errors, ensure your replacement text is pre-indented as needed, matching the indentation of the
17
+ search text in its original location.
18
+
19
+ Args:
20
+ path (str): Path to the file to modify.
21
+ search_text (str): The exact text to search for (including indentation).
22
+ replacement_text (str): The text to replace with (including indentation).
23
+ replace_all (bool): If True, replace all occurrences; otherwise, only the first occurrence.
24
+ backup (bool, optional): Deprecated. No backups are created anymore and this flag is ignored. Defaults to False.
25
+ Returns:
26
+ str: Status message. Example:
27
+ - "Text replaced in /path/to/file"
28
+ - "No changes made. [Warning: Search text not found in file] Please review the original file."
29
+ - "Error replacing text: <error message>"
30
+ """
31
+
32
+ permissions = ToolPermissions(read=True, write=True)
33
+
34
+ def run(
35
+ self,
36
+ path: str,
37
+ search_text: str,
38
+ replacement_text: str,
39
+ replace_all: bool = False,
40
+ backup: bool = False,
41
+ ) -> str:
42
+ from janito.tools.tool_utils import display_path
43
+
44
+ disp_path = display_path(path)
45
+ action = "∞" if replace_all else ""
46
+ search_lines = len(search_text.splitlines())
47
+ replace_lines = len(replacement_text.splitlines())
48
+ info_msg = self._format_info_msg(
49
+ disp_path,
50
+ search_lines,
51
+ replace_lines,
52
+ action,
53
+ search_text,
54
+ replacement_text,
55
+ path,
56
+ )
57
+ self.report_action(info_msg, ReportAction.CREATE)
58
+ try:
59
+ content = self._read_file_content(path)
60
+ match_lines = self._find_match_lines(content, search_text)
61
+ occurrences = content.count(search_text)
62
+ replaced_count, new_content = self._replace_content(
63
+ content, search_text, replacement_text, replace_all, occurrences
64
+ )
65
+ file_changed = new_content != content
66
+ backup_path = None
67
+ validation_result = ""
68
+ if file_changed:
69
+ self._write_file_content(path, new_content)
70
+ # Perform syntax validation and append result
71
+ validation_result = validate_file_syntax(path)
72
+ warning, concise_warning = self._handle_warnings(
73
+ replaced_count, file_changed, occurrences
74
+ )
75
+
76
+ if concise_warning:
77
+ return concise_warning
78
+ self._report_success(match_lines)
79
+ line_delta_str = self._get_line_delta_str(content, new_content)
80
+ match_info, details = self._format_match_details(
81
+ replaced_count,
82
+ match_lines,
83
+ search_lines,
84
+ replace_lines,
85
+ line_delta_str,
86
+ replace_all,
87
+ )
88
+ return self._format_final_msg(path, warning, match_info, details) + (
89
+ f"\n{validation_result}" if validation_result else ""
90
+ )
91
+ except Exception as e:
92
+ self.report_error(tr(" Error"), ReportAction.UPDATE)
93
+ return tr("Error replacing text: {error}", error=e)
94
+
95
+ def _read_file_content(self, path):
96
+ """Read the entire content of the file."""
97
+ with open(path, "r", encoding="utf-8", errors="replace") as f:
98
+ return f.read()
99
+
100
+ def _find_match_lines(self, content, search_text):
101
+ """Find all line numbers where search_text occurs in content."""
102
+ lines = content.splitlines(keepends=True)
103
+ joined = "".join(lines)
104
+ match_lines = []
105
+ idx = 0
106
+ while True:
107
+ idx = joined.find(search_text, idx)
108
+ if idx == -1:
109
+ break
110
+ upto = joined[:idx]
111
+ line_no = upto.count("\n") + 1
112
+ match_lines.append(line_no)
113
+ idx += 1 if not search_text else len(search_text)
114
+ return match_lines
115
+
116
+ def _replace_content(
117
+ self, content, search_text, replacement_text, replace_all, occurrences
118
+ ):
119
+ """Replace occurrences of search_text with replacement_text in content."""
120
+ if replace_all:
121
+ replaced_count = content.count(search_text)
122
+ new_content = content.replace(search_text, replacement_text)
123
+ else:
124
+ if occurrences > 1:
125
+ return 0, content # No changes made, not unique
126
+ replaced_count = 1 if occurrences == 1 else 0
127
+ new_content = content.replace(search_text, replacement_text, 1)
128
+ return replaced_count, new_content
129
+
130
+ def _backup_file(self, path, backup_path):
131
+ """Create a backup of the file."""
132
+ shutil.copy2(path, backup_path)
133
+
134
+ def _write_file_content(self, path, content):
135
+ """Write content to the file."""
136
+ with open(path, "w", encoding="utf-8", errors="replace") as f:
137
+ f.write(content)
138
+
139
+ def _handle_warnings(self, replaced_count, file_changed, occurrences):
140
+ """Handle and return warnings and concise warnings if needed."""
141
+ warning = ""
142
+ concise_warning = None
143
+ if replaced_count == 0:
144
+ warning = tr(" [Warning: Search text not found in file]")
145
+ if not file_changed:
146
+ self.report_warning(
147
+ tr(" ℹ️ No changes made. (not found)"), ReportAction.CREATE
148
+ )
149
+ concise_warning = tr(
150
+ "No changes made. The search text was not found. Expand your search context with surrounding lines if needed."
151
+ )
152
+ if occurrences > 1 and replaced_count == 0:
153
+ self.report_warning(
154
+ tr(" ℹ️ No changes made. (not unique)"), ReportAction.CREATE
155
+ )
156
+ concise_warning = tr(
157
+ "No changes made. The search text is not unique. Expand your search context with surrounding lines to ensure uniqueness."
158
+ )
159
+ return warning, concise_warning
160
+
161
+ def _report_success(self, match_lines, line_delta_str=""):
162
+ """Report success with line numbers where replacements occurred."""
163
+ if match_lines:
164
+ lines_str = ", ".join(str(line_no) for line_no in match_lines)
165
+ self.report_success(
166
+ tr(
167
+ " ✅ replaced at {lines_str}{delta}",
168
+ lines_str=lines_str,
169
+ delta=line_delta_str,
170
+ ),
171
+ ReportAction.CREATE,
172
+ )
173
+ else:
174
+ self.report_success(
175
+ tr(" ✅ replaced{delta}", delta=line_delta_str), ReportAction.CREATE
176
+ )
177
+
178
+ def _get_line_delta_str(self, content, new_content):
179
+ """Return a string describing the net line change after replacement."""
180
+ total_lines_before = content.count("\n") + 1
181
+ total_lines_after = new_content.count("\n") + 1
182
+ line_delta = total_lines_after - total_lines_before
183
+ if line_delta > 0:
184
+ return f" (+{line_delta} lines)"
185
+ elif line_delta < 0:
186
+ return f" ({line_delta} lines)"
187
+ else:
188
+ return " (no net line change)"
189
+
190
+ def _format_info_msg(
191
+ self,
192
+ disp_path,
193
+ search_lines,
194
+ replace_lines,
195
+ action,
196
+ search_text,
197
+ replacement_text,
198
+ path,
199
+ ):
200
+ """Format the info message for the operation."""
201
+ if replace_lines == 0:
202
+ return tr(
203
+ "📝 Replace in {disp_path} del {search_lines} lines {action}",
204
+ disp_path=disp_path,
205
+ search_lines=search_lines,
206
+ action=action,
207
+ )
208
+ else:
209
+ try:
210
+ with open(path, "r", encoding="utf-8", errors="replace") as f:
211
+ _content = f.read()
212
+ _new_content = _content.replace(
213
+ search_text, replacement_text, -1 if action else 1
214
+ )
215
+ _total_lines_before = _content.count("\n") + 1
216
+ _total_lines_after = _new_content.count("\n") + 1
217
+ _line_delta = _total_lines_after - _total_lines_before
218
+ except Exception:
219
+ _line_delta = replace_lines - search_lines
220
+ if _line_delta > 0:
221
+ delta_str = f"+{_line_delta} lines"
222
+ elif _line_delta < 0:
223
+ delta_str = f"{_line_delta} lines"
224
+ else:
225
+ delta_str = "+0"
226
+ return tr(
227
+ "📝 Replace in {disp_path} {delta_str} {action}",
228
+ disp_path=disp_path,
229
+ delta_str=delta_str,
230
+ action=action,
231
+ )
232
+
233
+ def _format_match_details(
234
+ self,
235
+ replaced_count,
236
+ match_lines,
237
+ search_lines,
238
+ replace_lines,
239
+ line_delta_str,
240
+ replace_all,
241
+ ):
242
+ """Format match info and details for the final message."""
243
+ if replaced_count > 0:
244
+ if replace_all:
245
+ match_info = tr(
246
+ "Matches found at lines: {lines}. ",
247
+ lines=", ".join(str(line) for line in match_lines),
248
+ )
249
+ else:
250
+ match_info = (
251
+ tr("Match found at line {line}. ", line=match_lines[0])
252
+ if match_lines
253
+ else ""
254
+ )
255
+ details = tr(
256
+ "Replaced {replaced_count} occurrence(s) at above line(s): {search_lines} lines replaced with {replace_lines} lines each.{line_delta_str}",
257
+ replaced_count=replaced_count,
258
+ search_lines=search_lines,
259
+ replace_lines=replace_lines,
260
+ line_delta_str=line_delta_str,
261
+ )
262
+ else:
263
+ match_info = ""
264
+ details = ""
265
+ return match_info, details
266
+
267
+ def _format_final_msg(self, path, warning, match_info, details):
268
+ """Format the final status message."""
269
+ return tr(
270
+ "Text replaced in {path}{warning}. {match_info}{details}",
271
+ path=path,
272
+ warning=warning,
273
+ match_info=match_info,
274
+ details=details,
275
+ )