janito 3.2.0__py3-none-any.whl → 3.3.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 +0 -3
- janito/cli/chat_mode/bindings.py +0 -26
- janito/cli/chat_mode/session.py +1 -12
- janito/cli/chat_mode/shell/commands/security/allowed_sites.py +33 -47
- janito/cli/cli_commands/list_plugins.py +8 -13
- janito/cli/core/model_guesser.py +24 -40
- janito/cli/prompt_core.py +9 -20
- janito/i18n/it.py +46 -46
- janito/llm/agent.py +16 -32
- janito/llm/driver.py +0 -8
- janito/{plugin_system → plugin_system_backup_20250825_070018}/core_loader.py +3 -76
- janito/{plugin_system → plugin_system_backup_20250825_070018}/core_loader_fixed.py +3 -79
- janito/plugins/__init__.py +21 -29
- janito/plugins/__main__.py +85 -0
- janito/plugins/base.py +57 -0
- janito/plugins/builtin.py +1 -1
- janito/plugins/core/filemanager/tools/copy_file.py +25 -1
- janito/plugins/core/filemanager/tools/create_directory.py +28 -1
- janito/plugins/core/filemanager/tools/create_file.py +27 -3
- janito/plugins/core/filemanager/tools/delete_text_in_file.py +1 -0
- janito/plugins/core/imagedisplay/plugin.py +1 -1
- janito/plugins/core_loader.py +144 -0
- janito/plugins/discovery.py +3 -3
- janito/plugins/example_plugin.py +1 -1
- janito/plugins/manager.py +1 -1
- janito/plugins_backup_20250825_070018/__init__.py +36 -0
- janito/{plugins → plugins_backup_20250825_070018}/auto_loader.py +11 -12
- janito/plugins_backup_20250825_070018/builtin.py +102 -0
- janito/plugins_backup_20250825_070018/config.py +84 -0
- janito/plugins_backup_20250825_070018/core/__init__.py +7 -0
- janito/plugins_backup_20250825_070018/core/codeanalyzer/__init__.py +43 -0
- janito/plugins_backup_20250825_070018/core/codeanalyzer/tools/get_file_outline/__init__.py +1 -0
- janito/plugins_backup_20250825_070018/core/codeanalyzer/tools/get_file_outline/core.py +122 -0
- janito/plugins_backup_20250825_070018/core/codeanalyzer/tools/search_text/__init__.py +1 -0
- janito/plugins_backup_20250825_070018/core/codeanalyzer/tools/search_text/core.py +205 -0
- janito/plugins_backup_20250825_070018/core/filemanager/__init__.py +124 -0
- janito/plugins_backup_20250825_070018/core/filemanager/tools/copy_file.py +87 -0
- janito/plugins_backup_20250825_070018/core/filemanager/tools/create_directory.py +70 -0
- janito/plugins_backup_20250825_070018/core/filemanager/tools/create_file.py +87 -0
- janito/plugins_backup_20250825_070018/core/filemanager/tools/delete_text_in_file.py +135 -0
- janito/plugins_backup_20250825_070018/core/filemanager/tools/find_files.py +143 -0
- janito/plugins_backup_20250825_070018/core/filemanager/tools/move_file.py +131 -0
- janito/plugins_backup_20250825_070018/core/filemanager/tools/read_files.py +58 -0
- janito/plugins_backup_20250825_070018/core/filemanager/tools/remove_directory.py +55 -0
- janito/plugins_backup_20250825_070018/core/filemanager/tools/remove_file.py +58 -0
- janito/plugins_backup_20250825_070018/core/filemanager/tools/replace_text_in_file.py +270 -0
- janito/plugins_backup_20250825_070018/core/filemanager/tools/validate_file_syntax/__init__.py +1 -0
- janito/plugins_backup_20250825_070018/core/filemanager/tools/validate_file_syntax/core.py +114 -0
- janito/plugins_backup_20250825_070018/core/filemanager/tools/view_file.py +172 -0
- janito/plugins_backup_20250825_070018/core/imagedisplay/__init__.py +14 -0
- janito/plugins_backup_20250825_070018/core/imagedisplay/plugin.py +51 -0
- janito/plugins_backup_20250825_070018/core/imagedisplay/tools/__init__.py +1 -0
- janito/plugins_backup_20250825_070018/core/imagedisplay/tools/show_image.py +83 -0
- janito/plugins_backup_20250825_070018/core/imagedisplay/tools/show_image_grid.py +84 -0
- janito/plugins_backup_20250825_070018/core/system/__init__.py +23 -0
- janito/plugins_backup_20250825_070018/core/system/tools/run_bash_command.py +183 -0
- janito/plugins_backup_20250825_070018/core/system/tools/run_powershell_command.py +218 -0
- janito/plugins_backup_20250825_070018/core_adapter.py +55 -0
- janito/plugins_backup_20250825_070018/dev/__init__.py +7 -0
- janito/plugins_backup_20250825_070018/dev/pythondev/__init__.py +37 -0
- janito/plugins_backup_20250825_070018/dev/pythondev/tools/python_code_run.py +172 -0
- janito/plugins_backup_20250825_070018/dev/pythondev/tools/python_command_run.py +171 -0
- janito/plugins_backup_20250825_070018/dev/pythondev/tools/python_file_run.py +172 -0
- janito/plugins_backup_20250825_070018/dev/visualization/__init__.py +23 -0
- janito/plugins_backup_20250825_070018/dev/visualization/tools/read_chart.py +259 -0
- janito/plugins_backup_20250825_070018/discovery.py +289 -0
- janito/{plugins → plugins_backup_20250825_070018}/discovery_core.py +9 -14
- janito/plugins_backup_20250825_070018/example_plugin.py +108 -0
- janito/plugins_backup_20250825_070018/manager.py +243 -0
- janito/{plugins → plugins_backup_20250825_070018}/tools/core_tools_plugin.py +10 -9
- janito/{plugins → plugins_backup_20250825_070018}/tools/create_file.py +2 -2
- janito/{plugins → plugins_backup_20250825_070018}/tools/delete_text_in_file.py +1 -0
- janito/plugins_backup_20250825_070018/tools/get_file_outline/java_outline.py +47 -0
- janito/plugins_backup_20250825_070018/tools/get_file_outline/markdown_outline.py +14 -0
- janito/plugins_backup_20250825_070018/tools/get_file_outline/python_outline.py +303 -0
- janito/plugins_backup_20250825_070018/tools/get_file_outline/search_outline.py +36 -0
- janito/plugins_backup_20250825_070018/tools/search_text/match_lines.py +67 -0
- janito/plugins_backup_20250825_070018/tools/search_text/pattern_utils.py +73 -0
- janito/plugins_backup_20250825_070018/tools/search_text/traverse_directory.py +145 -0
- janito/plugins_backup_20250825_070018/tools/validate_file_syntax/css_validator.py +35 -0
- janito/plugins_backup_20250825_070018/tools/validate_file_syntax/html_validator.py +100 -0
- janito/plugins_backup_20250825_070018/tools/validate_file_syntax/jinja2_validator.py +50 -0
- janito/plugins_backup_20250825_070018/tools/validate_file_syntax/js_validator.py +27 -0
- janito/plugins_backup_20250825_070018/tools/validate_file_syntax/json_validator.py +6 -0
- janito/plugins_backup_20250825_070018/tools/validate_file_syntax/markdown_validator.py +109 -0
- janito/plugins_backup_20250825_070018/tools/validate_file_syntax/ps1_validator.py +32 -0
- janito/plugins_backup_20250825_070018/tools/validate_file_syntax/python_validator.py +5 -0
- janito/plugins_backup_20250825_070018/tools/validate_file_syntax/xml_validator.py +11 -0
- janito/plugins_backup_20250825_070018/tools/validate_file_syntax/yaml_validator.py +6 -0
- janito/plugins_backup_20250825_070018/ui/__init__.py +7 -0
- janito/plugins_backup_20250825_070018/ui/userinterface/__init__.py +16 -0
- janito/plugins_backup_20250825_070018/ui/userinterface/tools/ask_user.py +110 -0
- janito/plugins_backup_20250825_070018/web/__init__.py +7 -0
- janito/plugins_backup_20250825_070018/web/webtools/__init__.py +33 -0
- janito/plugins_backup_20250825_070018/web/webtools/tools/fetch_url.py +458 -0
- janito/plugins_backup_20250825_070018/web/webtools/tools/open_html_in_browser.py +51 -0
- janito/plugins_backup_20250825_070018/web/webtools/tools/open_url.py +37 -0
- janito/providers/__init__.py +0 -1
- janito/tools/base.py +31 -1
- janito/tools/cli_initializer.py +1 -1
- janito/tools/function_adapter.py +176 -0
- janito/tools/initialize.py +1 -1
- janito/tools/loop_protection_decorator.py +117 -114
- janito/tools/tool_base.py +142 -114
- janito/tools/tools_schema.py +12 -6
- {janito-3.2.0.dist-info → janito-3.3.0.dist-info}/METADATA +1 -1
- {janito-3.2.0.dist-info → janito-3.3.0.dist-info}/RECORD +160 -95
- janito/llm/cancellation_manager.py +0 -63
- janito/llm/enter_cancellation.py +0 -107
- janito/plugins/core_adapter.py +0 -131
- janito/providers/together/__init__.py +0 -1
- janito/providers/together/model_info.py +0 -69
- janito/providers/together/provider.py +0 -108
- /janito/{plugin_system → plugin_system_backup_20250825_070018}/__init__.py +0 -0
- /janito/{plugin_system → plugin_system_backup_20250825_070018}/base.py +0 -0
- /janito/{plugins → plugins_backup_20250825_070018}/auto_loader_fixed.py +0 -0
- /janito/{plugins → plugins_backup_20250825_070018/core/codeanalyzer}/tools/get_file_outline/java_outline.py +0 -0
- /janito/{plugins → plugins_backup_20250825_070018/core/codeanalyzer}/tools/get_file_outline/markdown_outline.py +0 -0
- /janito/{plugins → plugins_backup_20250825_070018/core/codeanalyzer}/tools/get_file_outline/python_outline.py +0 -0
- /janito/{plugins → plugins_backup_20250825_070018/core/codeanalyzer}/tools/get_file_outline/search_outline.py +0 -0
- /janito/{plugins → plugins_backup_20250825_070018/core/codeanalyzer}/tools/search_text/match_lines.py +0 -0
- /janito/{plugins → plugins_backup_20250825_070018/core/codeanalyzer}/tools/search_text/pattern_utils.py +0 -0
- /janito/{plugins → plugins_backup_20250825_070018/core/codeanalyzer}/tools/search_text/traverse_directory.py +0 -0
- /janito/{plugins → plugins_backup_20250825_070018/core/filemanager}/tools/validate_file_syntax/css_validator.py +0 -0
- /janito/{plugins → plugins_backup_20250825_070018/core/filemanager}/tools/validate_file_syntax/html_validator.py +0 -0
- /janito/{plugins → plugins_backup_20250825_070018/core/filemanager}/tools/validate_file_syntax/jinja2_validator.py +0 -0
- /janito/{plugins → plugins_backup_20250825_070018/core/filemanager}/tools/validate_file_syntax/js_validator.py +0 -0
- /janito/{plugins → plugins_backup_20250825_070018/core/filemanager}/tools/validate_file_syntax/json_validator.py +0 -0
- /janito/{plugins → plugins_backup_20250825_070018/core/filemanager}/tools/validate_file_syntax/markdown_validator.py +0 -0
- /janito/{plugins → plugins_backup_20250825_070018/core/filemanager}/tools/validate_file_syntax/ps1_validator.py +0 -0
- /janito/{plugins → plugins_backup_20250825_070018/core/filemanager}/tools/validate_file_syntax/python_validator.py +0 -0
- /janito/{plugins → plugins_backup_20250825_070018/core/filemanager}/tools/validate_file_syntax/xml_validator.py +0 -0
- /janito/{plugins → plugins_backup_20250825_070018/core/filemanager}/tools/validate_file_syntax/yaml_validator.py +0 -0
- /janito/{plugins → plugins_backup_20250825_070018}/tools/__init__.py +0 -0
- /janito/{plugins → plugins_backup_20250825_070018}/tools/ask_user.py +0 -0
- /janito/{plugins → plugins_backup_20250825_070018}/tools/copy_file.py +0 -0
- /janito/{plugins → plugins_backup_20250825_070018}/tools/create_directory.py +0 -0
- /janito/{plugins → plugins_backup_20250825_070018}/tools/decorators.py +0 -0
- /janito/{plugins → plugins_backup_20250825_070018}/tools/fetch_url.py +0 -0
- /janito/{plugins → plugins_backup_20250825_070018}/tools/find_files.py +0 -0
- /janito/{plugins → plugins_backup_20250825_070018}/tools/get_file_outline/__init__.py +0 -0
- /janito/{plugins → plugins_backup_20250825_070018}/tools/get_file_outline/core.py +0 -0
- /janito/{plugins → plugins_backup_20250825_070018}/tools/move_file.py +0 -0
- /janito/{plugins → plugins_backup_20250825_070018}/tools/open_html_in_browser.py +0 -0
- /janito/{plugins → plugins_backup_20250825_070018}/tools/open_url.py +0 -0
- /janito/{plugins → plugins_backup_20250825_070018}/tools/python_code_run.py +0 -0
- /janito/{plugins → plugins_backup_20250825_070018}/tools/python_command_run.py +0 -0
- /janito/{plugins → plugins_backup_20250825_070018}/tools/python_file_run.py +0 -0
- /janito/{plugins → plugins_backup_20250825_070018}/tools/read_chart.py +0 -0
- /janito/{plugins → plugins_backup_20250825_070018}/tools/read_files.py +0 -0
- /janito/{plugins → plugins_backup_20250825_070018}/tools/remove_directory.py +0 -0
- /janito/{plugins → plugins_backup_20250825_070018}/tools/remove_file.py +0 -0
- /janito/{plugins → plugins_backup_20250825_070018}/tools/replace_text_in_file.py +0 -0
- /janito/{plugins → plugins_backup_20250825_070018}/tools/run_bash_command.py +0 -0
- /janito/{plugins → plugins_backup_20250825_070018}/tools/run_powershell_command.py +0 -0
- /janito/{plugins → plugins_backup_20250825_070018}/tools/search_text/__init__.py +0 -0
- /janito/{plugins → plugins_backup_20250825_070018}/tools/search_text/core.py +0 -0
- /janito/{plugins → plugins_backup_20250825_070018}/tools/show_image.py +0 -0
- /janito/{plugins → plugins_backup_20250825_070018}/tools/show_image_grid.py +0 -0
- /janito/{plugins → plugins_backup_20250825_070018}/tools/validate_file_syntax/__init__.py +0 -0
- /janito/{plugins → plugins_backup_20250825_070018}/tools/validate_file_syntax/core.py +0 -0
- /janito/{plugins → plugins_backup_20250825_070018}/tools/view_file.py +0 -0
- {janito-3.2.0.dist-info → janito-3.3.0.dist-info}/WHEEL +0 -0
- {janito-3.2.0.dist-info → janito-3.3.0.dist-info}/entry_points.txt +0 -0
- {janito-3.2.0.dist-info → janito-3.3.0.dist-info}/licenses/LICENSE +0 -0
- {janito-3.2.0.dist-info → janito-3.3.0.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,131 @@
|
|
1
|
+
import os
|
2
|
+
from janito.tools.path_utils import expand_path
|
3
|
+
import shutil
|
4
|
+
from janito.tools.adapters.local.adapter import register_local_tool
|
5
|
+
from janito.tools.tool_utils import display_path
|
6
|
+
from janito.tools.tool_base import ToolBase, ToolPermissions
|
7
|
+
from janito.report_events import ReportAction
|
8
|
+
from janito.i18n import tr
|
9
|
+
|
10
|
+
|
11
|
+
@register_local_tool
|
12
|
+
class MoveFileTool(ToolBase):
|
13
|
+
"""
|
14
|
+
Move a file or directory from src_path to dest_path.
|
15
|
+
|
16
|
+
Args:
|
17
|
+
src_path (str): Source file or directory path.
|
18
|
+
dest_path (str): Destination file or directory path.
|
19
|
+
overwrite (bool, optional): Whether to overwrite if the destination exists. Defaults to False.
|
20
|
+
backup (bool, optional): Deprecated. No backups are created anymore. This flag is ignored. Defaults to False.
|
21
|
+
Returns:
|
22
|
+
str: Status message indicating the result.
|
23
|
+
"""
|
24
|
+
|
25
|
+
permissions = ToolPermissions(read=True, write=True)
|
26
|
+
tool_name = "move_file"
|
27
|
+
|
28
|
+
def run(
|
29
|
+
self,
|
30
|
+
src_path: str,
|
31
|
+
dest_path: str,
|
32
|
+
overwrite: bool = False,
|
33
|
+
backup: bool = False,
|
34
|
+
) -> str:
|
35
|
+
src = expand_path(src_path)
|
36
|
+
dest = expand_path(dest_path)
|
37
|
+
original_src = src_path
|
38
|
+
original_dest = dest_path
|
39
|
+
disp_src = display_path(original_src)
|
40
|
+
disp_dest = display_path(original_dest)
|
41
|
+
backup_path = None
|
42
|
+
|
43
|
+
valid, is_src_file, is_src_dir, err_msg = self._validate_source(src, disp_src)
|
44
|
+
if not valid:
|
45
|
+
return err_msg
|
46
|
+
|
47
|
+
dest_result = self._handle_destination(dest, disp_dest, overwrite, backup)
|
48
|
+
if dest_result is not None:
|
49
|
+
backup_path, err_msg = dest_result
|
50
|
+
if err_msg:
|
51
|
+
return err_msg
|
52
|
+
|
53
|
+
try:
|
54
|
+
self.report_action(
|
55
|
+
tr(
|
56
|
+
"📝 Moving from '{disp_src}' to '{disp_dest}' ...",
|
57
|
+
disp_src=disp_src,
|
58
|
+
disp_dest=disp_dest,
|
59
|
+
),
|
60
|
+
ReportAction.UPDATE,
|
61
|
+
)
|
62
|
+
shutil.move(src, dest)
|
63
|
+
self.report_success(tr("✅ Move complete."))
|
64
|
+
msg = tr("✅ Move complete.")
|
65
|
+
|
66
|
+
return msg
|
67
|
+
except Exception as e:
|
68
|
+
self.report_error(tr("❌ Error moving: {error}", error=e))
|
69
|
+
return tr("❌ Error moving: {error}", error=e)
|
70
|
+
|
71
|
+
def _validate_source(self, src, disp_src):
|
72
|
+
if not os.path.exists(src):
|
73
|
+
self.report_error(
|
74
|
+
tr("❌ Source '{disp_src}' does not exist.", disp_src=disp_src)
|
75
|
+
)
|
76
|
+
return (
|
77
|
+
False,
|
78
|
+
False,
|
79
|
+
False,
|
80
|
+
tr("❌ Source '{disp_src}' does not exist.", disp_src=disp_src),
|
81
|
+
)
|
82
|
+
is_src_file = os.path.isfile(src)
|
83
|
+
is_src_dir = os.path.isdir(src)
|
84
|
+
if not (is_src_file or is_src_dir):
|
85
|
+
self.report_error(
|
86
|
+
tr(
|
87
|
+
"❌ Source path '{disp_src}' is neither a file nor a directory.",
|
88
|
+
disp_src=disp_src,
|
89
|
+
)
|
90
|
+
)
|
91
|
+
return (
|
92
|
+
False,
|
93
|
+
False,
|
94
|
+
False,
|
95
|
+
tr(
|
96
|
+
"❌ Source path '{disp_src}' is neither a file nor a directory.",
|
97
|
+
disp_src=disp_src,
|
98
|
+
),
|
99
|
+
)
|
100
|
+
return True, is_src_file, is_src_dir, None
|
101
|
+
|
102
|
+
def _handle_destination(self, dest, disp_dest, overwrite, backup):
|
103
|
+
backup_path = None
|
104
|
+
if os.path.exists(dest):
|
105
|
+
if not overwrite:
|
106
|
+
self.report_error(
|
107
|
+
tr(
|
108
|
+
"❗ Destination '{disp_dest}' exists and overwrite is False.",
|
109
|
+
disp_dest=disp_dest,
|
110
|
+
),
|
111
|
+
ReportAction.UPDATE,
|
112
|
+
)
|
113
|
+
return None, tr(
|
114
|
+
"❗ Destination '{disp_dest}' already exists and overwrite is False.",
|
115
|
+
disp_dest=disp_dest,
|
116
|
+
)
|
117
|
+
|
118
|
+
try:
|
119
|
+
if os.path.isfile(dest):
|
120
|
+
os.remove(dest)
|
121
|
+
elif os.path.isdir(dest):
|
122
|
+
shutil.rmtree(dest)
|
123
|
+
except Exception as e:
|
124
|
+
self.report_error(
|
125
|
+
tr("❌ Error removing destination before move: {error}", error=e),
|
126
|
+
ReportAction.UPDATE,
|
127
|
+
)
|
128
|
+
return None, tr(
|
129
|
+
"❌ Error removing destination before move: {error}", error=e
|
130
|
+
)
|
131
|
+
return backup_path, None
|
@@ -0,0 +1,58 @@
|
|
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 ReadFilesTool(ToolBase):
|
11
|
+
"""
|
12
|
+
Read all text content from multiple files.
|
13
|
+
|
14
|
+
Args:
|
15
|
+
paths (list[str]): List of file paths to read.
|
16
|
+
|
17
|
+
Returns:
|
18
|
+
str: Concatenated content of all files, each prefixed by a header with the file name. If a file cannot be read, an error message is included for that file.
|
19
|
+
"""
|
20
|
+
|
21
|
+
permissions = ToolPermissions(read=True)
|
22
|
+
tool_name = "read_files"
|
23
|
+
|
24
|
+
@protect_against_loops(max_calls=5, time_window=10.0, key_field="paths")
|
25
|
+
def run(self, paths: list[str]) -> str:
|
26
|
+
from janito.tools.tool_utils import display_path
|
27
|
+
import os
|
28
|
+
from janito.tools.path_utils import expand_path
|
29
|
+
|
30
|
+
results = []
|
31
|
+
for path in [expand_path(p) for p in paths]:
|
32
|
+
disp_path = display_path(path)
|
33
|
+
self.report_action(
|
34
|
+
tr("📖 Read '{disp_path}'", disp_path=disp_path), ReportAction.READ
|
35
|
+
)
|
36
|
+
if not os.path.isfile(path):
|
37
|
+
self.report_warning(
|
38
|
+
tr("❗ not found: {disp_path}", disp_path=disp_path)
|
39
|
+
)
|
40
|
+
results.append(f"--- File: {disp_path} (not found) ---\n")
|
41
|
+
continue
|
42
|
+
try:
|
43
|
+
with open(path, "r", encoding="utf-8", errors="replace") as f:
|
44
|
+
content = f.read()
|
45
|
+
results.append(f"--- File: {disp_path} ---\n{content}\n")
|
46
|
+
self.report_success(tr("✅ Read {disp_path}", disp_path=disp_path))
|
47
|
+
except Exception as e:
|
48
|
+
self.report_error(
|
49
|
+
tr(
|
50
|
+
" ❌ Error reading {disp_path}: {error}",
|
51
|
+
disp_path=disp_path,
|
52
|
+
error=e,
|
53
|
+
)
|
54
|
+
)
|
55
|
+
results.append(
|
56
|
+
f"--- File: {disp_path} (error) ---\nError reading file: {e}\n"
|
57
|
+
)
|
58
|
+
return "\n".join(results)
|
@@ -0,0 +1,55 @@
|
|
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, display_path
|
5
|
+
from janito.i18n import tr
|
6
|
+
import shutil
|
7
|
+
import os
|
8
|
+
import zipfile
|
9
|
+
from janito.tools.path_utils import expand_path
|
10
|
+
|
11
|
+
|
12
|
+
@register_local_tool
|
13
|
+
class RemoveDirectoryTool(ToolBase):
|
14
|
+
"""
|
15
|
+
Remove a directory.
|
16
|
+
|
17
|
+
Args:
|
18
|
+
path (str): Path to the directory to remove.
|
19
|
+
recursive (bool, optional): If True, remove non-empty directories recursively (with backup). If False, only remove empty directories. Defaults to False.
|
20
|
+
Returns:
|
21
|
+
str: Status message indicating result. Example:
|
22
|
+
- "Directory removed: /path/to/dir"
|
23
|
+
- "Error removing directory: <error message>"
|
24
|
+
"""
|
25
|
+
|
26
|
+
permissions = ToolPermissions(write=True)
|
27
|
+
tool_name = "remove_directory"
|
28
|
+
|
29
|
+
def run(self, path: str, recursive: bool = False) -> str:
|
30
|
+
path = expand_path(path)
|
31
|
+
disp_path = display_path(path)
|
32
|
+
self.report_action(
|
33
|
+
tr("🗃️ Remove directory '{disp_path}' ...", disp_path=disp_path),
|
34
|
+
ReportAction.DELETE,
|
35
|
+
)
|
36
|
+
|
37
|
+
try:
|
38
|
+
if recursive:
|
39
|
+
|
40
|
+
shutil.rmtree(path)
|
41
|
+
else:
|
42
|
+
os.rmdir(path)
|
43
|
+
self.report_success(
|
44
|
+
tr("✅ 1 {dir_word}", dir_word=pluralize("directory", 1)),
|
45
|
+
ReportAction.DELETE,
|
46
|
+
)
|
47
|
+
msg = tr("Directory removed: {disp_path}", disp_path=disp_path)
|
48
|
+
|
49
|
+
return msg
|
50
|
+
except Exception as e:
|
51
|
+
self.report_error(
|
52
|
+
tr(" ❌ Error removing directory: {error}", error=e),
|
53
|
+
ReportAction.DELETE,
|
54
|
+
)
|
55
|
+
return tr("Error removing directory: {error}", error=e)
|
@@ -0,0 +1,58 @@
|
|
1
|
+
import os
|
2
|
+
from janito.tools.path_utils import expand_path
|
3
|
+
import shutil
|
4
|
+
from janito.tools.adapters.local.adapter import register_local_tool
|
5
|
+
|
6
|
+
from janito.tools.tool_utils import display_path
|
7
|
+
from janito.tools.tool_base import ToolBase, ToolPermissions
|
8
|
+
from janito.report_events import ReportAction
|
9
|
+
from janito.i18n import tr
|
10
|
+
|
11
|
+
|
12
|
+
@register_local_tool
|
13
|
+
class RemoveFileTool(ToolBase):
|
14
|
+
"""
|
15
|
+
Remove a file at the specified path.
|
16
|
+
|
17
|
+
Args:
|
18
|
+
path (str): Path to the file to remove.
|
19
|
+
backup (bool, optional): Deprecated. Backups are no longer created. Flag ignored.
|
20
|
+
Returns:
|
21
|
+
str: Status message indicating the result. Example:
|
22
|
+
- " Successfully removed the file at ..."
|
23
|
+
- " Cannot remove file: ..."
|
24
|
+
"""
|
25
|
+
|
26
|
+
permissions = ToolPermissions(write=True)
|
27
|
+
tool_name = "remove_file"
|
28
|
+
|
29
|
+
def run(self, path: str, backup: bool = False) -> str:
|
30
|
+
path = expand_path(path)
|
31
|
+
disp_path = display_path(path)
|
32
|
+
|
33
|
+
# Report initial info about what is going to be removed
|
34
|
+
self.report_action(
|
35
|
+
tr("🗑️ Remove file '{disp_path}' ...", disp_path=disp_path),
|
36
|
+
ReportAction.DELETE,
|
37
|
+
)
|
38
|
+
if not os.path.exists(path):
|
39
|
+
self.report_error(tr("❌ File does not exist."), ReportAction.DELETE)
|
40
|
+
return tr("❌ File does not exist.")
|
41
|
+
if not os.path.isfile(path):
|
42
|
+
self.report_error(tr("❌ Path is not a file."), ReportAction.DELETE)
|
43
|
+
return tr("❌ Path is not a file.")
|
44
|
+
try:
|
45
|
+
|
46
|
+
os.remove(path)
|
47
|
+
self.report_success(tr("✅ File removed"), ReportAction.DELETE)
|
48
|
+
msg = tr(
|
49
|
+
"✅ Successfully removed the file at '{disp_path}'.",
|
50
|
+
disp_path=disp_path,
|
51
|
+
)
|
52
|
+
|
53
|
+
return msg
|
54
|
+
except Exception as e:
|
55
|
+
self.report_error(
|
56
|
+
tr("❌ Error removing file: {error}", error=e), ReportAction.DELETE
|
57
|
+
)
|
58
|
+
return tr("❌ Error removing file: {error}", error=e)
|
@@ -0,0 +1,270 @@
|
|
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
|
+
)
|
@@ -0,0 +1 @@
|
|
1
|
+
# Validation syntax package
|
@@ -0,0 +1,114 @@
|
|
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
|