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.
- janito/platform_discovery.py +1 -8
- janito/plugins/tools/local/adapter.py +3 -2
- janito/plugins/tools/local/ask_user.py +111 -112
- janito/plugins/tools/local/copy_file.py +86 -87
- janito/plugins/tools/local/create_directory.py +111 -112
- janito/plugins/tools/local/create_file.py +0 -1
- janito/plugins/tools/local/delete_text_in_file.py +133 -134
- janito/plugins/tools/local/fetch_url.py +465 -466
- janito/plugins/tools/local/find_files.py +142 -143
- janito/plugins/tools/local/markdown_view.py +0 -1
- janito/plugins/tools/local/move_file.py +130 -131
- janito/plugins/tools/local/open_html_in_browser.py +50 -51
- janito/plugins/tools/local/open_url.py +36 -37
- janito/plugins/tools/local/python_code_run.py +171 -172
- janito/plugins/tools/local/python_command_run.py +170 -171
- janito/plugins/tools/local/python_file_run.py +171 -172
- janito/plugins/tools/local/read_chart.py +258 -259
- janito/plugins/tools/local/read_files.py +57 -58
- janito/plugins/tools/local/remove_directory.py +54 -55
- janito/plugins/tools/local/remove_file.py +57 -58
- janito/plugins/tools/local/replace_text_in_file.py +275 -276
- janito/plugins/tools/local/run_bash_command.py +182 -183
- janito/plugins/tools/local/run_powershell_command.py +217 -218
- janito/plugins/tools/local/show_image.py +0 -1
- janito/plugins/tools/local/show_image_grid.py +0 -1
- janito/plugins/tools/local/view_file.py +0 -1
- janito/providers/alibaba/provider.py +1 -1
- janito/providers/deepseek/model_info.py +16 -37
- janito/providers/deepseek/provider.py +4 -3
- janito/tools/base.py +19 -12
- janito/tools/tool_base.py +122 -121
- janito/tools/tools_schema.py +104 -104
- {janito-3.14.1.dist-info → janito-3.15.0.dist-info}/METADATA +9 -32
- {janito-3.14.1.dist-info → janito-3.15.0.dist-info}/RECORD +38 -38
- {janito-3.14.1.dist-info → janito-3.15.0.dist-info}/WHEEL +0 -0
- {janito-3.14.1.dist-info → janito-3.15.0.dist-info}/entry_points.txt +0 -0
- {janito-3.14.1.dist-info → janito-3.15.0.dist-info}/licenses/LICENSE +0 -0
- {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
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
)
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
self.
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
)
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
"""
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
idx
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
"""
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
line_delta
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
)
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
"
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
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
|
+
)
|