janito 3.3.0__py3-none-any.whl → 3.5.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/README.md +3 -0
- janito/cli/chat_mode/bindings.py +50 -0
- janito/cli/chat_mode/session.py +12 -1
- janito/cli/chat_mode/shell/commands/multi.py +5 -0
- janito/cli/chat_mode/shell/commands/security/allowed_sites.py +47 -33
- janito/cli/cli_commands/list_plugins.py +13 -8
- janito/cli/core/model_guesser.py +40 -24
- janito/cli/prompt_core.py +47 -9
- janito/cli/rich_terminal_reporter.py +2 -2
- janito/hello.txt +0 -0
- janito/i18n/it.py +46 -46
- janito/llm/agent.py +32 -16
- janito/llm/cancellation_manager.py +63 -0
- janito/llm/driver.py +8 -0
- janito/llm/enter_cancellation.py +107 -0
- janito/{plugin_system_backup_20250825_070018 → plugin_system}/core_loader.py +76 -3
- janito/{plugin_system_backup_20250825_070018 → plugin_system}/core_loader_fixed.py +79 -3
- janito/plugins/__init__.py +29 -21
- janito/{plugins_backup_20250825_070018 → plugins}/auto_loader.py +12 -11
- janito/plugins/builtin.py +1 -1
- janito/plugins/core/filemanager/tools/copy_file.py +1 -25
- janito/plugins/core/filemanager/tools/create_directory.py +1 -28
- janito/plugins/core/filemanager/tools/create_file.py +3 -27
- janito/plugins/core/filemanager/tools/delete_text_in_file.py +0 -1
- janito/plugins/core/imagedisplay/plugin.py +1 -1
- janito/plugins/core_adapter.py +131 -0
- janito/plugins/discovery.py +3 -3
- janito/{plugins_backup_20250825_070018 → plugins}/discovery_core.py +14 -9
- janito/plugins/example_plugin.py +1 -1
- janito/plugins/manager.py +1 -1
- janito/{plugins_backup_20250825_070018 → plugins}/tools/core_tools_plugin.py +9 -10
- janito/{plugins_backup_20250825_070018 → plugins}/tools/create_file.py +2 -2
- janito/{plugins_backup_20250825_070018 → plugins}/tools/delete_text_in_file.py +0 -1
- janito/providers/__init__.py +1 -0
- janito/providers/together/__init__.py +1 -0
- janito/providers/together/model_info.py +69 -0
- janito/providers/together/provider.py +108 -0
- janito/tools/base.py +1 -31
- janito/tools/cli_initializer.py +1 -1
- janito/tools/initialize.py +1 -1
- janito/tools/loop_protection_decorator.py +114 -117
- janito/tools/tool_base.py +114 -142
- janito/tools/tools_schema.py +6 -12
- janito-3.5.0.dist-info/METADATA +229 -0
- {janito-3.3.0.dist-info → janito-3.5.0.dist-info}/RECORD +98 -162
- janito/plugins/__main__.py +0 -85
- janito/plugins/base.py +0 -57
- janito/plugins/core_loader.py +0 -144
- janito/plugins_backup_20250825_070018/__init__.py +0 -36
- janito/plugins_backup_20250825_070018/builtin.py +0 -102
- janito/plugins_backup_20250825_070018/config.py +0 -84
- janito/plugins_backup_20250825_070018/core/__init__.py +0 -7
- janito/plugins_backup_20250825_070018/core/codeanalyzer/__init__.py +0 -43
- janito/plugins_backup_20250825_070018/core/codeanalyzer/tools/get_file_outline/__init__.py +0 -1
- janito/plugins_backup_20250825_070018/core/codeanalyzer/tools/get_file_outline/core.py +0 -122
- janito/plugins_backup_20250825_070018/core/codeanalyzer/tools/search_text/__init__.py +0 -1
- janito/plugins_backup_20250825_070018/core/codeanalyzer/tools/search_text/core.py +0 -205
- janito/plugins_backup_20250825_070018/core/filemanager/__init__.py +0 -124
- janito/plugins_backup_20250825_070018/core/filemanager/tools/copy_file.py +0 -87
- janito/plugins_backup_20250825_070018/core/filemanager/tools/create_directory.py +0 -70
- janito/plugins_backup_20250825_070018/core/filemanager/tools/create_file.py +0 -87
- janito/plugins_backup_20250825_070018/core/filemanager/tools/delete_text_in_file.py +0 -135
- janito/plugins_backup_20250825_070018/core/filemanager/tools/find_files.py +0 -143
- janito/plugins_backup_20250825_070018/core/filemanager/tools/move_file.py +0 -131
- janito/plugins_backup_20250825_070018/core/filemanager/tools/read_files.py +0 -58
- janito/plugins_backup_20250825_070018/core/filemanager/tools/remove_directory.py +0 -55
- janito/plugins_backup_20250825_070018/core/filemanager/tools/remove_file.py +0 -58
- janito/plugins_backup_20250825_070018/core/filemanager/tools/replace_text_in_file.py +0 -270
- janito/plugins_backup_20250825_070018/core/filemanager/tools/validate_file_syntax/__init__.py +0 -1
- janito/plugins_backup_20250825_070018/core/filemanager/tools/validate_file_syntax/core.py +0 -114
- janito/plugins_backup_20250825_070018/core/filemanager/tools/view_file.py +0 -172
- janito/plugins_backup_20250825_070018/core/imagedisplay/__init__.py +0 -14
- janito/plugins_backup_20250825_070018/core/imagedisplay/plugin.py +0 -51
- janito/plugins_backup_20250825_070018/core/imagedisplay/tools/__init__.py +0 -1
- janito/plugins_backup_20250825_070018/core/imagedisplay/tools/show_image.py +0 -83
- janito/plugins_backup_20250825_070018/core/imagedisplay/tools/show_image_grid.py +0 -84
- janito/plugins_backup_20250825_070018/core/system/__init__.py +0 -23
- janito/plugins_backup_20250825_070018/core/system/tools/run_bash_command.py +0 -183
- janito/plugins_backup_20250825_070018/core/system/tools/run_powershell_command.py +0 -218
- janito/plugins_backup_20250825_070018/core_adapter.py +0 -55
- janito/plugins_backup_20250825_070018/dev/__init__.py +0 -7
- janito/plugins_backup_20250825_070018/dev/pythondev/__init__.py +0 -37
- janito/plugins_backup_20250825_070018/dev/pythondev/tools/python_code_run.py +0 -172
- janito/plugins_backup_20250825_070018/dev/pythondev/tools/python_command_run.py +0 -171
- janito/plugins_backup_20250825_070018/dev/pythondev/tools/python_file_run.py +0 -172
- janito/plugins_backup_20250825_070018/dev/visualization/__init__.py +0 -23
- janito/plugins_backup_20250825_070018/dev/visualization/tools/read_chart.py +0 -259
- janito/plugins_backup_20250825_070018/discovery.py +0 -289
- janito/plugins_backup_20250825_070018/example_plugin.py +0 -108
- janito/plugins_backup_20250825_070018/manager.py +0 -243
- janito/plugins_backup_20250825_070018/tools/get_file_outline/java_outline.py +0 -47
- janito/plugins_backup_20250825_070018/tools/get_file_outline/markdown_outline.py +0 -14
- janito/plugins_backup_20250825_070018/tools/get_file_outline/python_outline.py +0 -303
- janito/plugins_backup_20250825_070018/tools/get_file_outline/search_outline.py +0 -36
- janito/plugins_backup_20250825_070018/tools/search_text/match_lines.py +0 -67
- janito/plugins_backup_20250825_070018/tools/search_text/pattern_utils.py +0 -73
- janito/plugins_backup_20250825_070018/tools/search_text/traverse_directory.py +0 -145
- janito/plugins_backup_20250825_070018/tools/validate_file_syntax/css_validator.py +0 -35
- janito/plugins_backup_20250825_070018/tools/validate_file_syntax/html_validator.py +0 -100
- janito/plugins_backup_20250825_070018/tools/validate_file_syntax/jinja2_validator.py +0 -50
- janito/plugins_backup_20250825_070018/tools/validate_file_syntax/js_validator.py +0 -27
- janito/plugins_backup_20250825_070018/tools/validate_file_syntax/json_validator.py +0 -6
- janito/plugins_backup_20250825_070018/tools/validate_file_syntax/markdown_validator.py +0 -109
- janito/plugins_backup_20250825_070018/tools/validate_file_syntax/ps1_validator.py +0 -32
- janito/plugins_backup_20250825_070018/tools/validate_file_syntax/python_validator.py +0 -5
- janito/plugins_backup_20250825_070018/tools/validate_file_syntax/xml_validator.py +0 -11
- janito/plugins_backup_20250825_070018/tools/validate_file_syntax/yaml_validator.py +0 -6
- janito/plugins_backup_20250825_070018/ui/__init__.py +0 -7
- janito/plugins_backup_20250825_070018/ui/userinterface/__init__.py +0 -16
- janito/plugins_backup_20250825_070018/ui/userinterface/tools/ask_user.py +0 -110
- janito/plugins_backup_20250825_070018/web/__init__.py +0 -7
- janito/plugins_backup_20250825_070018/web/webtools/__init__.py +0 -33
- janito/plugins_backup_20250825_070018/web/webtools/tools/fetch_url.py +0 -458
- janito/plugins_backup_20250825_070018/web/webtools/tools/open_html_in_browser.py +0 -51
- janito/plugins_backup_20250825_070018/web/webtools/tools/open_url.py +0 -37
- janito/tools/function_adapter.py +0 -176
- janito-3.3.0.dist-info/METADATA +0 -83
- /janito/{plugin_system_backup_20250825_070018 → plugin_system}/__init__.py +0 -0
- /janito/{plugin_system_backup_20250825_070018 → plugin_system}/base.py +0 -0
- /janito/{plugins_backup_20250825_070018 → plugins}/auto_loader_fixed.py +0 -0
- /janito/{plugins_backup_20250825_070018 → plugins}/tools/__init__.py +0 -0
- /janito/{plugins_backup_20250825_070018 → plugins}/tools/ask_user.py +0 -0
- /janito/{plugins_backup_20250825_070018 → plugins}/tools/copy_file.py +0 -0
- /janito/{plugins_backup_20250825_070018 → plugins}/tools/create_directory.py +0 -0
- /janito/{plugins_backup_20250825_070018 → plugins}/tools/decorators.py +0 -0
- /janito/{plugins_backup_20250825_070018 → plugins}/tools/fetch_url.py +0 -0
- /janito/{plugins_backup_20250825_070018 → plugins}/tools/find_files.py +0 -0
- /janito/{plugins_backup_20250825_070018 → plugins}/tools/get_file_outline/__init__.py +0 -0
- /janito/{plugins_backup_20250825_070018 → plugins}/tools/get_file_outline/core.py +0 -0
- /janito/{plugins_backup_20250825_070018/core/codeanalyzer → plugins}/tools/get_file_outline/java_outline.py +0 -0
- /janito/{plugins_backup_20250825_070018/core/codeanalyzer → plugins}/tools/get_file_outline/markdown_outline.py +0 -0
- /janito/{plugins_backup_20250825_070018/core/codeanalyzer → plugins}/tools/get_file_outline/python_outline.py +0 -0
- /janito/{plugins_backup_20250825_070018/core/codeanalyzer → plugins}/tools/get_file_outline/search_outline.py +0 -0
- /janito/{plugins_backup_20250825_070018 → plugins}/tools/move_file.py +0 -0
- /janito/{plugins_backup_20250825_070018 → plugins}/tools/open_html_in_browser.py +0 -0
- /janito/{plugins_backup_20250825_070018 → plugins}/tools/open_url.py +0 -0
- /janito/{plugins_backup_20250825_070018 → plugins}/tools/python_code_run.py +0 -0
- /janito/{plugins_backup_20250825_070018 → plugins}/tools/python_command_run.py +0 -0
- /janito/{plugins_backup_20250825_070018 → plugins}/tools/python_file_run.py +0 -0
- /janito/{plugins_backup_20250825_070018 → plugins}/tools/read_chart.py +0 -0
- /janito/{plugins_backup_20250825_070018 → plugins}/tools/read_files.py +0 -0
- /janito/{plugins_backup_20250825_070018 → plugins}/tools/remove_directory.py +0 -0
- /janito/{plugins_backup_20250825_070018 → plugins}/tools/remove_file.py +0 -0
- /janito/{plugins_backup_20250825_070018 → plugins}/tools/replace_text_in_file.py +0 -0
- /janito/{plugins_backup_20250825_070018 → plugins}/tools/run_bash_command.py +0 -0
- /janito/{plugins_backup_20250825_070018 → plugins}/tools/run_powershell_command.py +0 -0
- /janito/{plugins_backup_20250825_070018 → plugins}/tools/search_text/__init__.py +0 -0
- /janito/{plugins_backup_20250825_070018 → plugins}/tools/search_text/core.py +0 -0
- /janito/{plugins_backup_20250825_070018/core/codeanalyzer → plugins}/tools/search_text/match_lines.py +0 -0
- /janito/{plugins_backup_20250825_070018/core/codeanalyzer → plugins}/tools/search_text/pattern_utils.py +0 -0
- /janito/{plugins_backup_20250825_070018/core/codeanalyzer → plugins}/tools/search_text/traverse_directory.py +0 -0
- /janito/{plugins_backup_20250825_070018 → plugins}/tools/show_image.py +0 -0
- /janito/{plugins_backup_20250825_070018 → plugins}/tools/show_image_grid.py +0 -0
- /janito/{plugins_backup_20250825_070018 → plugins}/tools/validate_file_syntax/__init__.py +0 -0
- /janito/{plugins_backup_20250825_070018 → plugins}/tools/validate_file_syntax/core.py +0 -0
- /janito/{plugins_backup_20250825_070018/core/filemanager → plugins}/tools/validate_file_syntax/css_validator.py +0 -0
- /janito/{plugins_backup_20250825_070018/core/filemanager → plugins}/tools/validate_file_syntax/html_validator.py +0 -0
- /janito/{plugins_backup_20250825_070018/core/filemanager → plugins}/tools/validate_file_syntax/jinja2_validator.py +0 -0
- /janito/{plugins_backup_20250825_070018/core/filemanager → plugins}/tools/validate_file_syntax/js_validator.py +0 -0
- /janito/{plugins_backup_20250825_070018/core/filemanager → plugins}/tools/validate_file_syntax/json_validator.py +0 -0
- /janito/{plugins_backup_20250825_070018/core/filemanager → plugins}/tools/validate_file_syntax/markdown_validator.py +0 -0
- /janito/{plugins_backup_20250825_070018/core/filemanager → plugins}/tools/validate_file_syntax/ps1_validator.py +0 -0
- /janito/{plugins_backup_20250825_070018/core/filemanager → plugins}/tools/validate_file_syntax/python_validator.py +0 -0
- /janito/{plugins_backup_20250825_070018/core/filemanager → plugins}/tools/validate_file_syntax/xml_validator.py +0 -0
- /janito/{plugins_backup_20250825_070018/core/filemanager → plugins}/tools/validate_file_syntax/yaml_validator.py +0 -0
- /janito/{plugins_backup_20250825_070018 → plugins}/tools/view_file.py +0 -0
- {janito-3.3.0.dist-info → janito-3.5.0.dist-info}/WHEEL +0 -0
- {janito-3.3.0.dist-info → janito-3.5.0.dist-info}/entry_points.txt +0 -0
- {janito-3.3.0.dist-info → janito-3.5.0.dist-info}/licenses/LICENSE +0 -0
- {janito-3.3.0.dist-info → janito-3.5.0.dist-info}/top_level.txt +0 -0
@@ -1,270 +0,0 @@
|
|
1
|
-
from janito.tools.tool_base import ToolBase, ToolPermissions
|
2
|
-
from janito.report_events import ReportAction
|
3
|
-
from janito.tools.adapters.local.adapter import register_local_tool
|
4
|
-
from janito.i18n import tr
|
5
|
-
import shutil
|
6
|
-
import re
|
7
|
-
from janito.tools.adapters.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 = "(all)" 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):
|
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(" ✅ replaced at {lines_str}", lines_str=lines_str),
|
168
|
-
ReportAction.CREATE,
|
169
|
-
)
|
170
|
-
else:
|
171
|
-
self.report_success(tr(" ✅ replaced (lines unknown)"), ReportAction.CREATE)
|
172
|
-
|
173
|
-
def _get_line_delta_str(self, content, new_content):
|
174
|
-
"""Return a string describing the net line change after replacement."""
|
175
|
-
total_lines_before = content.count("\n") + 1
|
176
|
-
total_lines_after = new_content.count("\n") + 1
|
177
|
-
line_delta = total_lines_after - total_lines_before
|
178
|
-
if line_delta > 0:
|
179
|
-
return f" (+{line_delta} lines)"
|
180
|
-
elif line_delta < 0:
|
181
|
-
return f" ({line_delta} lines)"
|
182
|
-
else:
|
183
|
-
return " (no net line change)"
|
184
|
-
|
185
|
-
def _format_info_msg(
|
186
|
-
self,
|
187
|
-
disp_path,
|
188
|
-
search_lines,
|
189
|
-
replace_lines,
|
190
|
-
action,
|
191
|
-
search_text,
|
192
|
-
replacement_text,
|
193
|
-
path,
|
194
|
-
):
|
195
|
-
"""Format the info message for the operation."""
|
196
|
-
if replace_lines == 0:
|
197
|
-
return tr(
|
198
|
-
"📝 Replace in {disp_path} del {search_lines} lines {action}",
|
199
|
-
disp_path=disp_path,
|
200
|
-
search_lines=search_lines,
|
201
|
-
action=action,
|
202
|
-
)
|
203
|
-
else:
|
204
|
-
try:
|
205
|
-
with open(path, "r", encoding="utf-8", errors="replace") as f:
|
206
|
-
_content = f.read()
|
207
|
-
_new_content = _content.replace(
|
208
|
-
search_text, replacement_text, -1 if action else 1
|
209
|
-
)
|
210
|
-
_total_lines_before = _content.count("\n") + 1
|
211
|
-
_total_lines_after = _new_content.count("\n") + 1
|
212
|
-
_line_delta = _total_lines_after - _total_lines_before
|
213
|
-
except Exception:
|
214
|
-
_line_delta = replace_lines - search_lines
|
215
|
-
if _line_delta > 0:
|
216
|
-
delta_str = f"+{_line_delta} lines"
|
217
|
-
elif _line_delta < 0:
|
218
|
-
delta_str = f"{_line_delta} lines"
|
219
|
-
else:
|
220
|
-
delta_str = "+0"
|
221
|
-
return tr(
|
222
|
-
"📝 Replace in {disp_path} {delta_str} {action}",
|
223
|
-
disp_path=disp_path,
|
224
|
-
delta_str=delta_str,
|
225
|
-
action=action,
|
226
|
-
)
|
227
|
-
|
228
|
-
def _format_match_details(
|
229
|
-
self,
|
230
|
-
replaced_count,
|
231
|
-
match_lines,
|
232
|
-
search_lines,
|
233
|
-
replace_lines,
|
234
|
-
line_delta_str,
|
235
|
-
replace_all,
|
236
|
-
):
|
237
|
-
"""Format match info and details for the final message."""
|
238
|
-
if replaced_count > 0:
|
239
|
-
if replace_all:
|
240
|
-
match_info = tr(
|
241
|
-
"Matches found at lines: {lines}. ",
|
242
|
-
lines=", ".join(str(line) for line in match_lines),
|
243
|
-
)
|
244
|
-
else:
|
245
|
-
match_info = (
|
246
|
-
tr("Match found at line {line}. ", line=match_lines[0])
|
247
|
-
if match_lines
|
248
|
-
else ""
|
249
|
-
)
|
250
|
-
details = tr(
|
251
|
-
"Replaced {replaced_count} occurrence(s) at above line(s): {search_lines} lines replaced with {replace_lines} lines each.{line_delta_str}",
|
252
|
-
replaced_count=replaced_count,
|
253
|
-
search_lines=search_lines,
|
254
|
-
replace_lines=replace_lines,
|
255
|
-
line_delta_str=line_delta_str,
|
256
|
-
)
|
257
|
-
else:
|
258
|
-
match_info = ""
|
259
|
-
details = ""
|
260
|
-
return match_info, details
|
261
|
-
|
262
|
-
def _format_final_msg(self, path, warning, match_info, details):
|
263
|
-
"""Format the final status message."""
|
264
|
-
return tr(
|
265
|
-
"Text replaced in {path}{warning}. {match_info}{details}",
|
266
|
-
path=path,
|
267
|
-
warning=warning,
|
268
|
-
match_info=match_info,
|
269
|
-
details=details,
|
270
|
-
)
|
janito/plugins_backup_20250825_070018/core/filemanager/tools/validate_file_syntax/__init__.py
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
# Validation syntax package
|
@@ -1,114 +0,0 @@
|
|
1
|
-
import os
|
2
|
-
from janito.tools.path_utils import expand_path
|
3
|
-
from janito.i18n import tr
|
4
|
-
from janito.tools.tool_base import ToolBase, ToolPermissions
|
5
|
-
from janito.report_events import ReportAction
|
6
|
-
from janito.tools.adapters.local.adapter import register_local_tool
|
7
|
-
from janito.tools.tool_utils import display_path
|
8
|
-
from janito.tools.adapters.local.adapter import register_local_tool as register_tool
|
9
|
-
|
10
|
-
from .python_validator import validate_python
|
11
|
-
from .json_validator import validate_json
|
12
|
-
from .yaml_validator import validate_yaml
|
13
|
-
from .ps1_validator import validate_ps1
|
14
|
-
from .xml_validator import validate_xml
|
15
|
-
from .html_validator import validate_html
|
16
|
-
from .markdown_validator import validate_markdown
|
17
|
-
from .js_validator import validate_js
|
18
|
-
from .css_validator import validate_css
|
19
|
-
from .jinja2_validator import validate_jinja2
|
20
|
-
from janito.tools.loop_protection_decorator import protect_against_loops
|
21
|
-
|
22
|
-
|
23
|
-
def _get_validator(ext):
|
24
|
-
"""Return the appropriate validator function for the file extension."""
|
25
|
-
mapping = {
|
26
|
-
".py": validate_python,
|
27
|
-
".pyw": validate_python,
|
28
|
-
".json": validate_json,
|
29
|
-
".yml": validate_yaml,
|
30
|
-
".yaml": validate_yaml,
|
31
|
-
".ps1": validate_ps1,
|
32
|
-
".xml": validate_xml,
|
33
|
-
".html": validate_html,
|
34
|
-
".htm": validate_html,
|
35
|
-
".md": validate_markdown,
|
36
|
-
".js": validate_js,
|
37
|
-
".css": validate_css,
|
38
|
-
".j2": validate_jinja2,
|
39
|
-
".jinja2": validate_jinja2,
|
40
|
-
}
|
41
|
-
return mapping.get(ext)
|
42
|
-
|
43
|
-
|
44
|
-
def _handle_validation_error(e, report_warning):
|
45
|
-
msg = tr("⚠️ Warning: Syntax error: {error}", error=e)
|
46
|
-
if report_warning:
|
47
|
-
report_warning(msg)
|
48
|
-
return msg
|
49
|
-
|
50
|
-
|
51
|
-
def validate_file_syntax(
|
52
|
-
path: str, report_info=None, report_warning=None, report_success=None
|
53
|
-
) -> str:
|
54
|
-
ext = os.path.splitext(path)[1].lower()
|
55
|
-
validator = _get_validator(ext)
|
56
|
-
try:
|
57
|
-
if validator:
|
58
|
-
return validator(path)
|
59
|
-
else:
|
60
|
-
msg = tr("⚠️ Warning: Unsupported file extension: {ext}", ext=ext)
|
61
|
-
if report_warning:
|
62
|
-
report_warning(msg)
|
63
|
-
return msg
|
64
|
-
except Exception as e:
|
65
|
-
return _handle_validation_error(e, report_warning)
|
66
|
-
|
67
|
-
|
68
|
-
class ValidateFileSyntaxTool(ToolBase):
|
69
|
-
"""
|
70
|
-
Validate a file for syntax issues.
|
71
|
-
|
72
|
-
Supported types:
|
73
|
-
- Python (.py, .pyw)
|
74
|
-
- JSON (.json)
|
75
|
-
- YAML (.yml, .yaml)
|
76
|
-
- PowerShell (.ps1)
|
77
|
-
- XML (.xml)
|
78
|
-
- HTML (.html, .htm) [lxml]
|
79
|
-
- Markdown (.md)
|
80
|
-
- JavaScript (.js)
|
81
|
-
- Jinja2 templates (.j2, .jinja2)
|
82
|
-
|
83
|
-
Args:
|
84
|
-
path (str): Path to the file to validate.
|
85
|
-
Returns:
|
86
|
-
str: Validation status message. Example:
|
87
|
-
- "✅ Syntax OK"
|
88
|
-
- "⚠️ Warning: Syntax error: <error message>"
|
89
|
-
- "⚠️ Warning: Unsupported file extension: <ext>"
|
90
|
-
"""
|
91
|
-
|
92
|
-
permissions = ToolPermissions(read=True)
|
93
|
-
tool_name = "validate_file_syntax"
|
94
|
-
|
95
|
-
@protect_against_loops(max_calls=5, time_window=10.0, key_field="path")
|
96
|
-
def run(self, path: str) -> str:
|
97
|
-
path = expand_path(path)
|
98
|
-
disp_path = display_path(path)
|
99
|
-
self.report_action(
|
100
|
-
tr(
|
101
|
-
"🔎 Validate syntax for file '{disp_path}' ...",
|
102
|
-
disp_path=disp_path,
|
103
|
-
),
|
104
|
-
ReportAction.READ,
|
105
|
-
)
|
106
|
-
result = validate_file_syntax(
|
107
|
-
path,
|
108
|
-
report_warning=self.report_warning,
|
109
|
-
report_success=self.report_success,
|
110
|
-
)
|
111
|
-
if result.startswith("✅"):
|
112
|
-
self.report_success(result, ReportAction.READ)
|
113
|
-
|
114
|
-
return result
|
@@ -1,172 +0,0 @@
|
|
1
|
-
from janito.tools.tool_base import ToolBase, ToolPermissions
|
2
|
-
from janito.report_events import ReportAction
|
3
|
-
from janito.tools.adapters.local.adapter import register_local_tool
|
4
|
-
from janito.tools.tool_utils import pluralize
|
5
|
-
from janito.i18n import tr
|
6
|
-
from janito.tools.loop_protection_decorator import protect_against_loops
|
7
|
-
|
8
|
-
|
9
|
-
@register_local_tool
|
10
|
-
class ViewFileTool(ToolBase):
|
11
|
-
"""
|
12
|
-
Read lines from a file. You can specify a line range, or read the entire file by simply omitting the from_line and to_line parameters.
|
13
|
-
|
14
|
-
Args:
|
15
|
-
path (str): Path to the file to read lines from.
|
16
|
-
from_line (int, optional): Starting line number (1-based). Omit to start from the first line.
|
17
|
-
to_line (int, optional): Ending line number (1-based). Omit to read to the end of the file.
|
18
|
-
|
19
|
-
To read the full file, just provide path and leave from_line and to_line unset.
|
20
|
-
|
21
|
-
Returns:
|
22
|
-
str: File content with a header indicating the file name and line range. Example:
|
23
|
-
- "---\nFile: /path/to/file.py | Lines: 1-10 (of 100)\n---\n<lines...>"
|
24
|
-
- "---\nFile: /path/to/file.py | All lines (total: 100 (all))\n---\n<all lines...>"
|
25
|
-
- "Error reading file: <error message>"
|
26
|
-
- "❗ not found"
|
27
|
-
"""
|
28
|
-
|
29
|
-
permissions = ToolPermissions(read=True)
|
30
|
-
tool_name = "view_file"
|
31
|
-
|
32
|
-
@protect_against_loops(max_calls=5, time_window=10.0, key_field="path")
|
33
|
-
def run(self, path: str, from_line: int = None, to_line: int = None) -> str:
|
34
|
-
import os
|
35
|
-
from janito.tools.tool_utils import display_path
|
36
|
-
from janito.tools.path_utils import expand_path
|
37
|
-
|
38
|
-
path = expand_path(path)
|
39
|
-
disp_path = display_path(path)
|
40
|
-
self.report_action(
|
41
|
-
tr("📖 View '{disp_path}'", disp_path=disp_path),
|
42
|
-
ReportAction.READ,
|
43
|
-
)
|
44
|
-
try:
|
45
|
-
if os.path.isdir(path):
|
46
|
-
return self._list_directory(path, disp_path)
|
47
|
-
lines = self._read_file_lines(path)
|
48
|
-
selected, selected_len, total_lines = self._select_lines(
|
49
|
-
lines, from_line, to_line
|
50
|
-
)
|
51
|
-
self._report_success(selected_len, from_line, to_line, total_lines)
|
52
|
-
header = self._format_header(
|
53
|
-
disp_path, from_line, to_line, selected_len, total_lines
|
54
|
-
)
|
55
|
-
return header + "".join(selected)
|
56
|
-
except FileNotFoundError as e:
|
57
|
-
self.report_warning(tr("❗ not found"))
|
58
|
-
return f"Error reading file: {e}"
|
59
|
-
except Exception as e:
|
60
|
-
self.report_error(tr(" ❌ Error: {error}", error=e))
|
61
|
-
return tr("Error reading file: {error}", error=e)
|
62
|
-
|
63
|
-
def _list_directory(self, path, disp_path):
|
64
|
-
import os
|
65
|
-
|
66
|
-
try:
|
67
|
-
entries = os.listdir(path)
|
68
|
-
entries.sort()
|
69
|
-
# Suffix subdirectories with '/'
|
70
|
-
formatted_entries = []
|
71
|
-
for entry in entries:
|
72
|
-
full_path = os.path.join(path, entry)
|
73
|
-
if os.path.isdir(full_path):
|
74
|
-
formatted_entries.append(entry + "/")
|
75
|
-
else:
|
76
|
-
formatted_entries.append(entry)
|
77
|
-
header = (
|
78
|
-
f"--- view_file: {disp_path} [directory, {len(entries)} entries] ---\n"
|
79
|
-
)
|
80
|
-
listing = "\n".join(formatted_entries)
|
81
|
-
self.report_success(tr("📁 Directory ({count} items)", count=len(entries)))
|
82
|
-
return header + listing + "\n"
|
83
|
-
except Exception as e:
|
84
|
-
self.report_error(tr(" ❌ Error listing directory: {error}", error=e))
|
85
|
-
return tr("Error listing directory: {error}", error=e)
|
86
|
-
|
87
|
-
def _read_file_lines(self, path):
|
88
|
-
"""Read all lines from the file."""
|
89
|
-
with open(path, "r", encoding="utf-8", errors="replace") as f:
|
90
|
-
return f.readlines()
|
91
|
-
|
92
|
-
def _select_lines(self, lines, from_line, to_line):
|
93
|
-
"""Select the requested lines and return them with their count and total lines."""
|
94
|
-
selected = lines[
|
95
|
-
(from_line - 1 if from_line else 0) : (to_line if to_line else None)
|
96
|
-
]
|
97
|
-
selected_len = len(selected)
|
98
|
-
total_lines = len(lines)
|
99
|
-
return selected, selected_len, total_lines
|
100
|
-
|
101
|
-
def _report_success(self, selected_len, from_line, to_line, total_lines):
|
102
|
-
"""Report the success message after reading lines."""
|
103
|
-
if from_line and to_line:
|
104
|
-
requested = to_line - from_line + 1
|
105
|
-
at_end = to_line >= total_lines or selected_len < requested
|
106
|
-
if at_end:
|
107
|
-
self.report_success(
|
108
|
-
tr(
|
109
|
-
" ✅ {selected_len} {line_word} (end)",
|
110
|
-
selected_len=selected_len,
|
111
|
-
line_word=pluralize("line", selected_len),
|
112
|
-
)
|
113
|
-
)
|
114
|
-
elif to_line < total_lines:
|
115
|
-
self.report_success(
|
116
|
-
tr(
|
117
|
-
" ✅ {selected_len} {line_word} ({remaining} to end)",
|
118
|
-
selected_len=selected_len,
|
119
|
-
line_word=pluralize("line", selected_len),
|
120
|
-
remaining=total_lines - to_line,
|
121
|
-
)
|
122
|
-
)
|
123
|
-
else:
|
124
|
-
self.report_success(
|
125
|
-
tr(
|
126
|
-
" ✅ {selected_len} {line_word} (all)",
|
127
|
-
selected_len=selected_len,
|
128
|
-
line_word=pluralize("line", selected_len),
|
129
|
-
)
|
130
|
-
)
|
131
|
-
|
132
|
-
def _format_header(self, disp_path, from_line, to_line, selected_len, total_lines):
|
133
|
-
"""Format the header for the output."""
|
134
|
-
if from_line and to_line:
|
135
|
-
requested = to_line - from_line + 1
|
136
|
-
at_end = selected_len < requested or to_line >= total_lines
|
137
|
-
if at_end:
|
138
|
-
return tr(
|
139
|
-
"---\n{disp_path} {from_line}-{to_line} (end)\n---\n",
|
140
|
-
disp_path=disp_path,
|
141
|
-
from_line=from_line,
|
142
|
-
to_line=to_line,
|
143
|
-
)
|
144
|
-
else:
|
145
|
-
return tr(
|
146
|
-
"---\n{disp_path} {from_line}-{to_line} (of {total_lines})\n---\n",
|
147
|
-
disp_path=disp_path,
|
148
|
-
from_line=from_line,
|
149
|
-
to_line=to_line,
|
150
|
-
total_lines=total_lines,
|
151
|
-
)
|
152
|
-
elif from_line:
|
153
|
-
return tr(
|
154
|
-
"---\n{disp_path} {from_line}-END (of {total_lines})\n---\n",
|
155
|
-
disp_path=disp_path,
|
156
|
-
from_line=from_line,
|
157
|
-
total_lines=total_lines,
|
158
|
-
)
|
159
|
-
else:
|
160
|
-
return tr(
|
161
|
-
"---\n{disp_path} All lines (total: {total_lines} (all))\n---\n",
|
162
|
-
disp_path=disp_path,
|
163
|
-
total_lines=total_lines,
|
164
|
-
)
|
165
|
-
|
166
|
-
def _handle_read_error(self, e):
|
167
|
-
"""Handle file read errors and report appropriately."""
|
168
|
-
if isinstance(e, FileNotFoundError):
|
169
|
-
self.report_error(tr("❗ not found"), ReportAction.READ)
|
170
|
-
return tr("❗ not found")
|
171
|
-
self.report_error(tr(" ❌ Error: {error}", error=e), ReportAction.READ)
|
172
|
-
return tr("Error reading file: {error}", error=e)
|
@@ -1,14 +0,0 @@
|
|
1
|
-
"""
|
2
|
-
Image Display Plugin - Core plugin for displaying images inline in the terminal.
|
3
|
-
|
4
|
-
This plugin provides tools for displaying images directly in the terminal using
|
5
|
-
rich library's image rendering capabilities.
|
6
|
-
"""
|
7
|
-
|
8
|
-
from .plugin import ImageDisplayPlugin
|
9
|
-
|
10
|
-
# Plugin metadata for discovery/listing
|
11
|
-
__plugin_name__ = "core.imagedisplay"
|
12
|
-
__plugin_description__ = "Display images inline in the terminal using rich"
|
13
|
-
|
14
|
-
__all__ = ["ImageDisplayPlugin"]
|
@@ -1,51 +0,0 @@
|
|
1
|
-
"""
|
2
|
-
Image Display Plugin implementation.
|
3
|
-
"""
|
4
|
-
|
5
|
-
from typing import Dict, Any, List, Type
|
6
|
-
|
7
|
-
from janito.plugin_system.base import Plugin, PluginMetadata
|
8
|
-
from janito.tools.tool_base import ToolBase
|
9
|
-
from .tools.show_image import ShowImageTool
|
10
|
-
from .tools.show_image_grid import ShowImageGridTool
|
11
|
-
|
12
|
-
|
13
|
-
class ImageDisplayPlugin(Plugin):
|
14
|
-
"""Plugin for displaying images inline in the terminal using rich."""
|
15
|
-
|
16
|
-
def get_metadata(self) -> PluginMetadata:
|
17
|
-
return PluginMetadata(
|
18
|
-
name="core.imagedisplay",
|
19
|
-
version="1.0.0",
|
20
|
-
description="Display images inline in the terminal using rich",
|
21
|
-
author="Janito",
|
22
|
-
dependencies=["rich", "pillow"],
|
23
|
-
)
|
24
|
-
|
25
|
-
def get_tools(self) -> List[Type[ToolBase]]:
|
26
|
-
# Return tool classes so the PluginManager can register them
|
27
|
-
return [ShowImageTool, ShowImageGridTool]
|
28
|
-
|
29
|
-
def get_config_schema(self) -> Dict[str, Any]:
|
30
|
-
return {
|
31
|
-
"type": "object",
|
32
|
-
"properties": {
|
33
|
-
"default_width": {
|
34
|
-
"type": "integer",
|
35
|
-
"description": "Default width for image display",
|
36
|
-
"minimum": 1,
|
37
|
-
"maximum": 200,
|
38
|
-
},
|
39
|
-
"default_height": {
|
40
|
-
"type": "integer",
|
41
|
-
"description": "Default height for image display",
|
42
|
-
"minimum": 1,
|
43
|
-
"maximum": 100,
|
44
|
-
},
|
45
|
-
"preserve_aspect": {
|
46
|
-
"type": "boolean",
|
47
|
-
"description": "Preserve aspect ratio by default",
|
48
|
-
"default": True,
|
49
|
-
},
|
50
|
-
},
|
51
|
-
}
|
@@ -1 +0,0 @@
|
|
1
|
-
"""Tools for image display plugin."""
|