janito 2.33.0__py3-none-any.whl → 3.0.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/cli/cli_commands/check_tools.py +212 -0
- janito/cli/cli_commands/list_plugins.py +52 -43
- janito/cli/core/getters.py +3 -0
- janito/cli/main_cli.py +9 -12
- janito/drivers/openai/driver.py +1 -0
- janito/drivers/zai/driver.py +1 -0
- janito/llm/auth_utils.py +14 -5
- janito/plugin_system/__init__.py +10 -0
- janito/{plugins → plugin_system}/base.py +5 -2
- janito/{plugins/core_loader_fixed.py → plugin_system/core_loader.py} +45 -26
- janito/plugin_system/core_loader_fixed.py +149 -0
- janito/plugins/__init__.py +31 -12
- janito/plugins/auto_loader_fixed.py +12 -11
- janito/plugins/builtin.py +15 -1
- janito/plugins/core/__init__.py +7 -0
- janito/plugins/core/codeanalyzer/__init__.py +43 -0
- janito/plugins/core/filemanager/__init__.py +124 -0
- janito/plugins/core/filemanager/tools/create_file.py +87 -0
- janito/plugins/core/filemanager/tools/replace_text_in_file.py +270 -0
- janito/plugins/core/imagedisplay/__init__.py +14 -0
- janito/plugins/core/imagedisplay/plugin.py +51 -0
- janito/plugins/core/imagedisplay/tools/__init__.py +1 -0
- janito/plugins/core/imagedisplay/tools/show_image.py +83 -0
- janito/{tools/adapters/local → plugins/core/imagedisplay/tools}/show_image_grid.py +13 -5
- janito/plugins/core/system/__init__.py +23 -0
- janito/plugins/core_adapter.py +11 -9
- janito/plugins/dev/__init__.py +7 -0
- janito/plugins/dev/pythondev/__init__.py +37 -0
- janito/plugins/dev/visualization/__init__.py +23 -0
- janito/plugins/discovery.py +5 -5
- janito/plugins/example_plugin.py +108 -0
- janito/plugins/manager.py +1 -1
- janito/plugins/tools/__init__.py +10 -0
- janito/{tools/adapters/local → plugins/tools}/ask_user.py +3 -3
- janito/plugins/tools/copy_file.py +87 -0
- janito/plugins/tools/core_tools_plugin.py +88 -0
- janito/plugins/tools/create_directory.py +70 -0
- janito/{tools/adapters/local → plugins/tools}/create_file.py +4 -4
- janito/plugins/tools/decorators.py +19 -0
- janito/plugins/tools/delete_text_in_file.py +134 -0
- janito/{tools/adapters/local → plugins/tools}/fetch_url.py +3 -3
- janito/plugins/tools/find_files.py +143 -0
- janito/plugins/tools/get_file_outline/__init__.py +7 -0
- janito/plugins/tools/get_file_outline/core.py +122 -0
- janito/plugins/tools/get_file_outline/java_outline.py +47 -0
- janito/plugins/tools/get_file_outline/markdown_outline.py +14 -0
- janito/plugins/tools/get_file_outline/python_outline.py +303 -0
- janito/plugins/tools/get_file_outline/search_outline.py +36 -0
- janito/plugins/tools/move_file.py +131 -0
- janito/plugins/tools/open_html_in_browser.py +51 -0
- janito/plugins/tools/open_url.py +37 -0
- janito/plugins/tools/python_code_run.py +172 -0
- janito/plugins/tools/python_command_run.py +171 -0
- janito/plugins/tools/python_file_run.py +172 -0
- janito/plugins/tools/read_chart.py +259 -0
- janito/plugins/tools/read_files.py +58 -0
- janito/plugins/tools/remove_directory.py +55 -0
- janito/plugins/tools/remove_file.py +58 -0
- janito/{tools/adapters/local → plugins/tools}/replace_text_in_file.py +4 -4
- janito/plugins/tools/run_bash_command.py +183 -0
- janito/plugins/tools/run_powershell_command.py +218 -0
- janito/plugins/tools/search_text/__init__.py +7 -0
- janito/plugins/tools/search_text/core.py +205 -0
- janito/plugins/tools/search_text/match_lines.py +67 -0
- janito/plugins/tools/search_text/pattern_utils.py +73 -0
- janito/plugins/tools/search_text/traverse_directory.py +145 -0
- janito/{tools/adapters/local → plugins/tools}/show_image.py +15 -6
- janito/plugins/tools/show_image_grid.py +85 -0
- janito/plugins/tools/validate_file_syntax/__init__.py +7 -0
- janito/plugins/tools/validate_file_syntax/core.py +114 -0
- janito/plugins/tools/validate_file_syntax/css_validator.py +35 -0
- janito/plugins/tools/validate_file_syntax/html_validator.py +100 -0
- janito/plugins/tools/validate_file_syntax/jinja2_validator.py +50 -0
- janito/plugins/tools/validate_file_syntax/js_validator.py +27 -0
- janito/plugins/tools/validate_file_syntax/json_validator.py +6 -0
- janito/plugins/tools/validate_file_syntax/markdown_validator.py +109 -0
- janito/plugins/tools/validate_file_syntax/ps1_validator.py +32 -0
- janito/plugins/tools/validate_file_syntax/python_validator.py +5 -0
- janito/plugins/tools/validate_file_syntax/xml_validator.py +11 -0
- janito/plugins/tools/validate_file_syntax/yaml_validator.py +6 -0
- janito/plugins/tools/view_file.py +172 -0
- janito/plugins/ui/__init__.py +7 -0
- janito/plugins/ui/userinterface/__init__.py +16 -0
- janito/plugins/ui/userinterface/tools/ask_user.py +110 -0
- janito/plugins/web/__init__.py +7 -0
- janito/plugins/web/webtools/__init__.py +33 -0
- janito/plugins/web/webtools/tools/fetch_url.py +458 -0
- janito/tools/__init__.py +31 -7
- janito/tools/adapters/__init__.py +6 -1
- janito/tools/adapters/local/__init__.py +7 -70
- janito/tools/cli_initializer.py +88 -0
- janito/tools/function_adapter.py +93 -16
- janito/tools/initialize.py +70 -0
- {janito-2.33.0.dist-info → janito-3.0.0.dist-info}/METADATA +1 -2
- {janito-2.33.0.dist-info → janito-3.0.0.dist-info}/RECORD +139 -71
- janito/plugins/core_loader.py +0 -120
- /janito/{tools/adapters/local → plugins/core/codeanalyzer/tools}/get_file_outline/__init__.py +0 -0
- /janito/{tools/adapters/local → plugins/core/codeanalyzer/tools}/get_file_outline/core.py +0 -0
- /janito/{tools/adapters/local → plugins/core/codeanalyzer/tools}/get_file_outline/java_outline.py +0 -0
- /janito/{tools/adapters/local → plugins/core/codeanalyzer/tools}/get_file_outline/markdown_outline.py +0 -0
- /janito/{tools/adapters/local → plugins/core/codeanalyzer/tools}/get_file_outline/python_outline.py +0 -0
- /janito/{tools/adapters/local → plugins/core/codeanalyzer/tools}/get_file_outline/search_outline.py +0 -0
- /janito/{tools/adapters/local → plugins/core/codeanalyzer/tools}/search_text/__init__.py +0 -0
- /janito/{tools/adapters/local → plugins/core/codeanalyzer/tools}/search_text/core.py +0 -0
- /janito/{tools/adapters/local → plugins/core/codeanalyzer/tools}/search_text/match_lines.py +0 -0
- /janito/{tools/adapters/local → plugins/core/codeanalyzer/tools}/search_text/pattern_utils.py +0 -0
- /janito/{tools/adapters/local → plugins/core/codeanalyzer/tools}/search_text/traverse_directory.py +0 -0
- /janito/{tools/adapters/local → plugins/core/filemanager/tools}/copy_file.py +0 -0
- /janito/{tools/adapters/local → plugins/core/filemanager/tools}/create_directory.py +0 -0
- /janito/{tools/adapters/local → plugins/core/filemanager/tools}/delete_text_in_file.py +0 -0
- /janito/{tools/adapters/local → plugins/core/filemanager/tools}/find_files.py +0 -0
- /janito/{tools/adapters/local → plugins/core/filemanager/tools}/move_file.py +0 -0
- /janito/{tools/adapters/local → plugins/core/filemanager/tools}/read_files.py +0 -0
- /janito/{tools/adapters/local → plugins/core/filemanager/tools}/remove_directory.py +0 -0
- /janito/{tools/adapters/local → plugins/core/filemanager/tools}/remove_file.py +0 -0
- /janito/{tools/adapters/local → plugins/core/filemanager/tools}/validate_file_syntax/__init__.py +0 -0
- /janito/{tools/adapters/local → plugins/core/filemanager/tools}/validate_file_syntax/core.py +0 -0
- /janito/{tools/adapters/local → plugins/core/filemanager/tools}/validate_file_syntax/css_validator.py +0 -0
- /janito/{tools/adapters/local → plugins/core/filemanager/tools}/validate_file_syntax/html_validator.py +0 -0
- /janito/{tools/adapters/local → plugins/core/filemanager/tools}/validate_file_syntax/jinja2_validator.py +0 -0
- /janito/{tools/adapters/local → plugins/core/filemanager/tools}/validate_file_syntax/js_validator.py +0 -0
- /janito/{tools/adapters/local → plugins/core/filemanager/tools}/validate_file_syntax/json_validator.py +0 -0
- /janito/{tools/adapters/local → plugins/core/filemanager/tools}/validate_file_syntax/markdown_validator.py +0 -0
- /janito/{tools/adapters/local → plugins/core/filemanager/tools}/validate_file_syntax/ps1_validator.py +0 -0
- /janito/{tools/adapters/local → plugins/core/filemanager/tools}/validate_file_syntax/python_validator.py +0 -0
- /janito/{tools/adapters/local → plugins/core/filemanager/tools}/validate_file_syntax/xml_validator.py +0 -0
- /janito/{tools/adapters/local → plugins/core/filemanager/tools}/validate_file_syntax/yaml_validator.py +0 -0
- /janito/{tools/adapters/local → plugins/core/filemanager/tools}/view_file.py +0 -0
- /janito/{tools/adapters/local → plugins/core/system/tools}/run_bash_command.py +0 -0
- /janito/{tools/adapters/local → plugins/core/system/tools}/run_powershell_command.py +0 -0
- /janito/{tools/adapters/local → plugins/dev/pythondev/tools}/python_code_run.py +0 -0
- /janito/{tools/adapters/local → plugins/dev/pythondev/tools}/python_command_run.py +0 -0
- /janito/{tools/adapters/local → plugins/dev/pythondev/tools}/python_file_run.py +0 -0
- /janito/{tools/adapters/local → plugins/dev/visualization/tools}/read_chart.py +0 -0
- /janito/{tools/adapters/local → plugins/web/webtools/tools}/open_html_in_browser.py +0 -0
- /janito/{tools/adapters/local → plugins/web/webtools/tools}/open_url.py +0 -0
- {janito-2.33.0.dist-info → janito-3.0.0.dist-info}/WHEEL +0 -0
- {janito-2.33.0.dist-info → janito-3.0.0.dist-info}/entry_points.txt +0 -0
- {janito-2.33.0.dist-info → janito-3.0.0.dist-info}/licenses/LICENSE +0 -0
- {janito-2.33.0.dist-info → janito-3.0.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.plugins.tools.decorators import register_core_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_core_tool
|
12
|
+
class MoveFile(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,51 @@
|
|
1
|
+
import os
|
2
|
+
import webbrowser
|
3
|
+
from janito.plugins.tools.decorators import register_core_tool
|
4
|
+
from janito.tools.tool_base import ToolBase, ToolPermissions
|
5
|
+
from janito.report_events import ReportAction
|
6
|
+
from janito.i18n import tr
|
7
|
+
from janito.tools.loop_protection_decorator import protect_against_loops
|
8
|
+
|
9
|
+
|
10
|
+
@register_core_tool
|
11
|
+
class OpenHtmlInBrowser(ToolBase):
|
12
|
+
"""
|
13
|
+
Open the supplied HTML file in the default web browser.
|
14
|
+
|
15
|
+
Args:
|
16
|
+
path (str): Path to the HTML file to open.
|
17
|
+
Returns:
|
18
|
+
str: Status message indicating the result.
|
19
|
+
"""
|
20
|
+
|
21
|
+
permissions = ToolPermissions(read=True)
|
22
|
+
tool_name = "open_html_in_browser"
|
23
|
+
|
24
|
+
@protect_against_loops(max_calls=5, time_window=10.0, key_field="path")
|
25
|
+
def run(self, path: str) -> str:
|
26
|
+
if not path.strip():
|
27
|
+
self.report_warning(tr("ℹ️ Empty file path provided."))
|
28
|
+
return tr("Warning: Empty file path provided. Operation skipped.")
|
29
|
+
if not os.path.isfile(path):
|
30
|
+
self.report_error(tr("❗ File does not exist: {path}", path=path))
|
31
|
+
return tr("Warning: File does not exist: {path}", path=path)
|
32
|
+
if not path.lower().endswith((".html", ".htm")):
|
33
|
+
self.report_warning(tr("⚠️ Not an HTML file: {path}", path=path))
|
34
|
+
return tr("Warning: Not an HTML file: {path}", path=path)
|
35
|
+
url = "file://" + os.path.abspath(path)
|
36
|
+
self.report_action(
|
37
|
+
tr("📖 Opening HTML file in browser: {path}", path=path), ReportAction.READ
|
38
|
+
)
|
39
|
+
try:
|
40
|
+
webbrowser.open(url)
|
41
|
+
except Exception as err:
|
42
|
+
self.report_error(
|
43
|
+
tr("❗ Error opening HTML file: {path}: {err}", path=path, err=str(err))
|
44
|
+
)
|
45
|
+
return tr(
|
46
|
+
"Warning: Error opening HTML file: {path}: {err}",
|
47
|
+
path=path,
|
48
|
+
err=str(err),
|
49
|
+
)
|
50
|
+
self.report_success(tr("✅ HTML file opened in browser: {path}", path=path))
|
51
|
+
return tr("HTML file opened in browser: {path}", path=path)
|
@@ -0,0 +1,37 @@
|
|
1
|
+
import webbrowser
|
2
|
+
from janito.plugins.tools.decorators import register_core_tool
|
3
|
+
from janito.tools.tool_base import ToolBase, ToolPermissions
|
4
|
+
from janito.report_events import ReportAction
|
5
|
+
from janito.i18n import tr
|
6
|
+
from janito.tools.loop_protection_decorator import protect_against_loops
|
7
|
+
|
8
|
+
|
9
|
+
@register_core_tool
|
10
|
+
class OpenUrl(ToolBase):
|
11
|
+
"""
|
12
|
+
Open the supplied URL or local file in the default web browser.
|
13
|
+
|
14
|
+
Args:
|
15
|
+
url (str): The URL or local file path (as a file:// URL) to open. Supports both web URLs (http, https) and local files (file://).
|
16
|
+
Returns:
|
17
|
+
str: Status message indicating the result.
|
18
|
+
"""
|
19
|
+
|
20
|
+
permissions = ToolPermissions(read=True)
|
21
|
+
tool_name = "open_url"
|
22
|
+
|
23
|
+
@protect_against_loops(max_calls=5, time_window=10.0, key_field="url")
|
24
|
+
def run(self, url: str) -> str:
|
25
|
+
if not url.strip():
|
26
|
+
self.report_warning(tr("ℹ️ Empty URL provided."))
|
27
|
+
return tr("Warning: Empty URL provided. Operation skipped.")
|
28
|
+
self.report_action(tr("🌐 Opening URL '{url}' ...", url=url), ReportAction.READ)
|
29
|
+
try:
|
30
|
+
webbrowser.open(url)
|
31
|
+
except Exception as err:
|
32
|
+
self.report_error(
|
33
|
+
tr("❗ Error opening URL: {url}: {err}", url=url, err=str(err))
|
34
|
+
)
|
35
|
+
return tr("Warning: Error opening URL: {url}: {err}", url=url, err=str(err))
|
36
|
+
self.report_success(tr("✅ URL opened in browser: {url}", url=url))
|
37
|
+
return tr("URL opened in browser: {url}", url=url)
|
@@ -0,0 +1,172 @@
|
|
1
|
+
import subprocess
|
2
|
+
import os
|
3
|
+
import sys
|
4
|
+
import tempfile
|
5
|
+
import threading
|
6
|
+
from janito.tools.tool_base import ToolBase, ToolPermissions
|
7
|
+
from janito.report_events import ReportAction
|
8
|
+
from janito.plugins.tools.decorators import register_core_tool
|
9
|
+
from janito.i18n import tr
|
10
|
+
|
11
|
+
|
12
|
+
@register_core_tool
|
13
|
+
class PythonCodeRun(ToolBase):
|
14
|
+
"""
|
15
|
+
Tool to execute Python code by passing it to the interpreter via standard input (stdin).
|
16
|
+
|
17
|
+
Args:
|
18
|
+
code (str): The Python code to execute as a string.
|
19
|
+
timeout (int): Timeout in seconds for the command. Defaults to 60.
|
20
|
+
silent (bool): If True, suppresses progress and status messages. Defaults to False.
|
21
|
+
|
22
|
+
Returns:
|
23
|
+
str: Output and status message, or file paths/line counts if output is large.
|
24
|
+
"""
|
25
|
+
|
26
|
+
permissions = ToolPermissions(execute=True)
|
27
|
+
tool_name = "python_code_run"
|
28
|
+
|
29
|
+
def run(self, code: str, timeout: int = 60, silent: bool = False) -> str:
|
30
|
+
if not code.strip():
|
31
|
+
self.report_warning(tr("ℹ️ Empty code provided."), ReportAction.EXECUTE)
|
32
|
+
return tr("Warning: Empty code provided. Operation skipped.")
|
33
|
+
if not silent:
|
34
|
+
self.report_action(
|
35
|
+
tr("⚡ Running: python (stdin mode) ...\n{code}\n", code=code),
|
36
|
+
ReportAction.EXECUTE,
|
37
|
+
)
|
38
|
+
self.report_stdout("\n")
|
39
|
+
else:
|
40
|
+
self.report_action(tr("⚡ Executing..."), ReportAction.EXECUTE)
|
41
|
+
try:
|
42
|
+
with (
|
43
|
+
tempfile.NamedTemporaryFile(
|
44
|
+
mode="w+",
|
45
|
+
prefix="python_stdin_stdout_",
|
46
|
+
delete=False,
|
47
|
+
encoding="utf-8",
|
48
|
+
) as stdout_file,
|
49
|
+
tempfile.NamedTemporaryFile(
|
50
|
+
mode="w+",
|
51
|
+
prefix="python_stdin_stderr_",
|
52
|
+
delete=False,
|
53
|
+
encoding="utf-8",
|
54
|
+
) as stderr_file,
|
55
|
+
):
|
56
|
+
process = subprocess.Popen(
|
57
|
+
[sys.executable],
|
58
|
+
stdin=subprocess.PIPE,
|
59
|
+
stdout=subprocess.PIPE,
|
60
|
+
stderr=subprocess.PIPE,
|
61
|
+
text=True,
|
62
|
+
bufsize=1,
|
63
|
+
universal_newlines=True,
|
64
|
+
encoding="utf-8",
|
65
|
+
env={**os.environ, "PYTHONIOENCODING": "utf-8"},
|
66
|
+
)
|
67
|
+
stdout_lines, stderr_lines = self._stream_process_output(
|
68
|
+
process, stdout_file, stderr_file, code
|
69
|
+
)
|
70
|
+
return_code = self._wait_for_process(process, timeout)
|
71
|
+
if return_code is None:
|
72
|
+
return tr(
|
73
|
+
"Code timed out after {timeout} seconds.", timeout=timeout
|
74
|
+
)
|
75
|
+
stdout_file.flush()
|
76
|
+
stderr_file.flush()
|
77
|
+
if not silent:
|
78
|
+
self.report_success(
|
79
|
+
tr("✅ Return code {return_code}", return_code=return_code),
|
80
|
+
ReportAction.EXECUTE,
|
81
|
+
)
|
82
|
+
return self._format_result(
|
83
|
+
stdout_file.name, stderr_file.name, return_code
|
84
|
+
)
|
85
|
+
except Exception as e:
|
86
|
+
self.report_error(tr("❌ Error: {error}", error=e), ReportAction.EXECUTE)
|
87
|
+
return tr("Error running code via stdin: {error}", error=e)
|
88
|
+
|
89
|
+
def _stream_process_output(self, process, stdout_file, stderr_file, code):
|
90
|
+
stdout_lines = 0
|
91
|
+
stderr_lines = 0
|
92
|
+
|
93
|
+
def stream_output(stream, file_obj, report_func, count_func):
|
94
|
+
nonlocal stdout_lines, stderr_lines
|
95
|
+
for line in stream:
|
96
|
+
file_obj.write(line)
|
97
|
+
file_obj.flush()
|
98
|
+
report_func(line.rstrip("\r\n"))
|
99
|
+
if count_func == "stdout":
|
100
|
+
stdout_lines += 1
|
101
|
+
else:
|
102
|
+
stderr_lines += 1
|
103
|
+
|
104
|
+
stdout_thread = threading.Thread(
|
105
|
+
target=stream_output,
|
106
|
+
args=(process.stdout, stdout_file, self.report_stdout, "stdout"),
|
107
|
+
)
|
108
|
+
stderr_thread = threading.Thread(
|
109
|
+
target=stream_output,
|
110
|
+
args=(process.stderr, stderr_file, self.report_stderr, "stderr"),
|
111
|
+
)
|
112
|
+
stdout_thread.start()
|
113
|
+
stderr_thread.start()
|
114
|
+
process.stdin.write(code)
|
115
|
+
process.stdin.close()
|
116
|
+
stdout_thread.join()
|
117
|
+
stderr_thread.join()
|
118
|
+
return stdout_lines, stderr_lines
|
119
|
+
|
120
|
+
def _wait_for_process(self, process, timeout):
|
121
|
+
try:
|
122
|
+
return process.wait(timeout=timeout)
|
123
|
+
except subprocess.TimeoutExpired:
|
124
|
+
process.kill()
|
125
|
+
self.report_error(
|
126
|
+
tr("❌ Timed out after {timeout} seconds.", timeout=timeout),
|
127
|
+
ReportAction.EXECUTE,
|
128
|
+
)
|
129
|
+
return None
|
130
|
+
|
131
|
+
def _format_result(self, stdout_file_name, stderr_file_name, return_code):
|
132
|
+
with open(stdout_file_name, "r", encoding="utf-8", errors="replace") as out_f:
|
133
|
+
stdout_content = out_f.read()
|
134
|
+
with open(stderr_file_name, "r", encoding="utf-8", errors="replace") as err_f:
|
135
|
+
stderr_content = err_f.read()
|
136
|
+
max_lines = 100
|
137
|
+
stdout_lines = stdout_content.count("\n")
|
138
|
+
stderr_lines = stderr_content.count("\n")
|
139
|
+
|
140
|
+
def head_tail(text, n=10):
|
141
|
+
lines = text.splitlines()
|
142
|
+
if len(lines) <= 2 * n:
|
143
|
+
return "\n".join(lines)
|
144
|
+
return "\n".join(
|
145
|
+
lines[:n]
|
146
|
+
+ ["... ({} lines omitted) ...".format(len(lines) - 2 * n)]
|
147
|
+
+ lines[-n:]
|
148
|
+
)
|
149
|
+
|
150
|
+
if stdout_lines <= max_lines and stderr_lines <= max_lines:
|
151
|
+
result = f"Return code: {return_code}\n--- python_code_run: STDOUT ---\n{stdout_content}"
|
152
|
+
if stderr_content.strip():
|
153
|
+
result += f"\n--- python_code_run: STDERR ---\n{stderr_content}"
|
154
|
+
return result
|
155
|
+
else:
|
156
|
+
result = f"stdout_file: {stdout_file_name} (lines: {stdout_lines})\n"
|
157
|
+
if stderr_lines > 0 and stderr_content.strip():
|
158
|
+
result += f"stderr_file: {stderr_file_name} (lines: {stderr_lines})\n"
|
159
|
+
result += f"returncode: {return_code}\n"
|
160
|
+
result += (
|
161
|
+
"--- python_code_run: STDOUT (head/tail) ---\n"
|
162
|
+
+ head_tail(stdout_content)
|
163
|
+
+ "\n"
|
164
|
+
)
|
165
|
+
if stderr_content.strip():
|
166
|
+
result += (
|
167
|
+
"--- python_code_run: STDERR (head/tail) ---\n"
|
168
|
+
+ head_tail(stderr_content)
|
169
|
+
+ "\n"
|
170
|
+
)
|
171
|
+
result += "Use the view_file tool to inspect the contents of these files when needed."
|
172
|
+
return result
|
@@ -0,0 +1,171 @@
|
|
1
|
+
import subprocess
|
2
|
+
import os
|
3
|
+
import sys
|
4
|
+
import tempfile
|
5
|
+
import threading
|
6
|
+
from janito.tools.tool_base import ToolBase, ToolPermissions
|
7
|
+
from janito.report_events import ReportAction
|
8
|
+
from janito.plugins.tools.decorators import register_core_tool
|
9
|
+
from janito.i18n import tr
|
10
|
+
|
11
|
+
|
12
|
+
@register_core_tool
|
13
|
+
class PythonCommandRun(ToolBase):
|
14
|
+
"""
|
15
|
+
Tool to execute Python code using the `python -c` command-line flag.
|
16
|
+
|
17
|
+
Args:
|
18
|
+
code (str): The Python code to execute as a string.
|
19
|
+
timeout (int): Timeout in seconds for the command. Defaults to 60.
|
20
|
+
silent (bool): If True, suppresses progress and status messages. Defaults to False.
|
21
|
+
|
22
|
+
Returns:
|
23
|
+
str: Output and status message, or file paths/line counts if output is large.
|
24
|
+
"""
|
25
|
+
|
26
|
+
permissions = ToolPermissions(execute=True)
|
27
|
+
tool_name = "python_command_run"
|
28
|
+
|
29
|
+
def run(self, code: str, timeout: int = 60, silent: bool = False) -> str:
|
30
|
+
if not code.strip():
|
31
|
+
self.report_warning(tr("ℹ️ Empty code provided."), ReportAction.EXECUTE)
|
32
|
+
return tr("Warning: Empty code provided. Operation skipped.")
|
33
|
+
if not silent:
|
34
|
+
self.report_action(
|
35
|
+
tr("🐍 Running: python -c ...\n{code}\n", code=code),
|
36
|
+
ReportAction.EXECUTE,
|
37
|
+
)
|
38
|
+
self.report_stdout("\n")
|
39
|
+
else:
|
40
|
+
self.report_action(tr("⚡ Executing..."), ReportAction.EXECUTE)
|
41
|
+
try:
|
42
|
+
with (
|
43
|
+
tempfile.NamedTemporaryFile(
|
44
|
+
mode="w+",
|
45
|
+
prefix="python_cmd_stdout_",
|
46
|
+
delete=False,
|
47
|
+
encoding="utf-8",
|
48
|
+
) as stdout_file,
|
49
|
+
tempfile.NamedTemporaryFile(
|
50
|
+
mode="w+",
|
51
|
+
prefix="python_cmd_stderr_",
|
52
|
+
delete=False,
|
53
|
+
encoding="utf-8",
|
54
|
+
) as stderr_file,
|
55
|
+
):
|
56
|
+
process = subprocess.Popen(
|
57
|
+
[sys.executable, "-c", code],
|
58
|
+
stdout=subprocess.PIPE,
|
59
|
+
stderr=subprocess.PIPE,
|
60
|
+
text=True,
|
61
|
+
bufsize=1,
|
62
|
+
universal_newlines=True,
|
63
|
+
encoding="utf-8",
|
64
|
+
env={**os.environ, "PYTHONIOENCODING": "utf-8"},
|
65
|
+
)
|
66
|
+
stdout_lines, stderr_lines = self._stream_process_output(
|
67
|
+
process, stdout_file, stderr_file
|
68
|
+
)
|
69
|
+
return_code = self._wait_for_process(process, timeout)
|
70
|
+
if return_code is None:
|
71
|
+
return tr(
|
72
|
+
"Code timed out after {timeout} seconds.", timeout=timeout
|
73
|
+
)
|
74
|
+
stdout_file.flush()
|
75
|
+
stderr_file.flush()
|
76
|
+
if not silent:
|
77
|
+
self.report_success(
|
78
|
+
tr("✅ Return code {return_code}", return_code=return_code),
|
79
|
+
ReportAction.EXECUTE,
|
80
|
+
)
|
81
|
+
return self._format_result(
|
82
|
+
stdout_file.name, stderr_file.name, return_code
|
83
|
+
)
|
84
|
+
except Exception as e:
|
85
|
+
self.report_error(tr("❌ Error: {error}", error=e), ReportAction.EXECUTE)
|
86
|
+
return tr("Error running code: {error}", error=e)
|
87
|
+
|
88
|
+
def _stream_process_output(self, process, stdout_file, stderr_file):
|
89
|
+
stdout_lines = 0
|
90
|
+
stderr_lines = 0
|
91
|
+
|
92
|
+
def stream_output(stream, file_obj, report_func, count_func):
|
93
|
+
nonlocal stdout_lines, stderr_lines
|
94
|
+
for line in stream:
|
95
|
+
file_obj.write(line)
|
96
|
+
file_obj.flush()
|
97
|
+
from janito.tools.tool_base import ReportAction
|
98
|
+
|
99
|
+
report_func(line.rstrip("\r\n"), ReportAction.EXECUTE)
|
100
|
+
if count_func == "stdout":
|
101
|
+
stdout_lines += 1
|
102
|
+
else:
|
103
|
+
stderr_lines += 1
|
104
|
+
|
105
|
+
stdout_thread = threading.Thread(
|
106
|
+
target=stream_output,
|
107
|
+
args=(process.stdout, stdout_file, self.report_stdout, "stdout"),
|
108
|
+
)
|
109
|
+
stderr_thread = threading.Thread(
|
110
|
+
target=stream_output,
|
111
|
+
args=(process.stderr, stderr_file, self.report_stderr, "stderr"),
|
112
|
+
)
|
113
|
+
stdout_thread.start()
|
114
|
+
stderr_thread.start()
|
115
|
+
stdout_thread.join()
|
116
|
+
stderr_thread.join()
|
117
|
+
return stdout_lines, stderr_lines
|
118
|
+
|
119
|
+
def _wait_for_process(self, process, timeout):
|
120
|
+
try:
|
121
|
+
return process.wait(timeout=timeout)
|
122
|
+
except subprocess.TimeoutExpired:
|
123
|
+
process.kill()
|
124
|
+
self.report_error(
|
125
|
+
tr("❌ Timed out after {timeout} seconds.", timeout=timeout),
|
126
|
+
ReportAction.EXECUTE,
|
127
|
+
)
|
128
|
+
return None
|
129
|
+
|
130
|
+
def _format_result(self, stdout_file_name, stderr_file_name, return_code):
|
131
|
+
with open(stdout_file_name, "r", encoding="utf-8", errors="replace") as out_f:
|
132
|
+
stdout_content = out_f.read()
|
133
|
+
with open(stderr_file_name, "r", encoding="utf-8", errors="replace") as err_f:
|
134
|
+
stderr_content = err_f.read()
|
135
|
+
max_lines = 100
|
136
|
+
stdout_lines = stdout_content.count("\n")
|
137
|
+
stderr_lines = stderr_content.count("\n")
|
138
|
+
|
139
|
+
def head_tail(text, n=10):
|
140
|
+
lines = text.splitlines()
|
141
|
+
if len(lines) <= 2 * n:
|
142
|
+
return "\n".join(lines)
|
143
|
+
return "\n".join(
|
144
|
+
lines[:n]
|
145
|
+
+ ["... ({} lines omitted) ...".format(len(lines) - 2 * n)]
|
146
|
+
+ lines[-n:]
|
147
|
+
)
|
148
|
+
|
149
|
+
if stdout_lines <= max_lines and stderr_lines <= max_lines:
|
150
|
+
result = f"Return code: {return_code}\n--- python_command_run: STDOUT ---\n{stdout_content}"
|
151
|
+
if stderr_content.strip():
|
152
|
+
result += f"\n--- python_command_run: STDERR ---\n{stderr_content}"
|
153
|
+
return result
|
154
|
+
else:
|
155
|
+
result = f"stdout_file: {stdout_file_name} (lines: {stdout_lines})\n"
|
156
|
+
if stderr_lines > 0 and stderr_content.strip():
|
157
|
+
result += f"stderr_file: {stderr_file_name} (lines: {stderr_lines})\n"
|
158
|
+
result += f"returncode: {return_code}\n"
|
159
|
+
result += (
|
160
|
+
"--- python_command_run: STDOUT (head/tail) ---\n"
|
161
|
+
+ head_tail(stdout_content)
|
162
|
+
+ "\n"
|
163
|
+
)
|
164
|
+
if stderr_content.strip():
|
165
|
+
result += (
|
166
|
+
"--- python_command_run: STDERR (head/tail) ---\n"
|
167
|
+
+ head_tail(stderr_content)
|
168
|
+
+ "\n"
|
169
|
+
)
|
170
|
+
result += "Use the view_file tool to inspect the contents of these files when needed."
|
171
|
+
return result
|