janito 2.33.0__py3-none-any.whl → 3.1.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/chat_mode/bindings.py +26 -0
- janito/cli/chat_mode/session.py +7 -1
- 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/cli/prompt_core.py +19 -9
- janito/drivers/openai/driver.py +1 -0
- janito/drivers/zai/driver.py +1 -0
- janito/llm/agent.py +30 -17
- janito/llm/auth_utils.py +14 -5
- janito/llm/cancellation_manager.py +62 -0
- janito/llm/driver.py +7 -0
- janito/llm/enter_cancellation.py +93 -0
- 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.1.0.dist-info}/METADATA +1 -2
- {janito-2.33.0.dist-info → janito-3.1.0.dist-info}/RECORD +146 -76
- 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.1.0.dist-info}/WHEEL +0 -0
- {janito-2.33.0.dist-info → janito-3.1.0.dist-info}/entry_points.txt +0 -0
- {janito-2.33.0.dist-info → janito-3.1.0.dist-info}/licenses/LICENSE +0 -0
- {janito-2.33.0.dist-info → janito-3.1.0.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,134 @@
|
|
1
|
+
from janito.tools.tool_base import ToolBase, ToolPermissions
|
2
|
+
from janito.report_events import ReportAction
|
3
|
+
from janito.plugins.tools.decorators import register_core_tool
|
4
|
+
from janito.i18n import tr
|
5
|
+
import shutil
|
6
|
+
from janito.plugins.tools.validate_file_syntax.core import validate_file_syntax
|
7
|
+
|
8
|
+
|
9
|
+
@register_core_tool
|
10
|
+
class DeleteTextInFile(ToolBase):
|
11
|
+
"""
|
12
|
+
Delete all occurrences of text between start_marker and end_marker (inclusive) in a file, using exact string markers.
|
13
|
+
|
14
|
+
Args:
|
15
|
+
path (str): Path to the file to modify.
|
16
|
+
start_marker (str): The starting delimiter string.
|
17
|
+
end_marker (str): The ending delimiter string.
|
18
|
+
|
19
|
+
Returns:
|
20
|
+
str: Status message indicating the result.
|
21
|
+
"""
|
22
|
+
|
23
|
+
permissions = ToolPermissions(read=True, write=True)
|
24
|
+
tool_name = "delete_text_in_file"
|
25
|
+
|
26
|
+
def run(
|
27
|
+
self,
|
28
|
+
path: str,
|
29
|
+
start_marker: str,
|
30
|
+
end_marker: str,
|
31
|
+
backup: bool = False,
|
32
|
+
) -> str:
|
33
|
+
from janito.tools.tool_utils import display_path
|
34
|
+
|
35
|
+
disp_path = display_path(path)
|
36
|
+
info_msg = tr(
|
37
|
+
"📝 Delete text in {disp_path} between markers: '{start_marker}' ... '{end_marker}'",
|
38
|
+
disp_path=disp_path,
|
39
|
+
start_marker=start_marker,
|
40
|
+
end_marker=end_marker,
|
41
|
+
)
|
42
|
+
self.report_action(info_msg, ReportAction.CREATE)
|
43
|
+
try:
|
44
|
+
content = self._read_file_content(path)
|
45
|
+
occurrences, match_lines = self._find_marker_blocks(
|
46
|
+
content, start_marker, end_marker
|
47
|
+
)
|
48
|
+
if occurrences == 0:
|
49
|
+
self.report_warning(
|
50
|
+
tr(" ℹ️ No blocks found between markers."), ReportAction.CREATE
|
51
|
+
)
|
52
|
+
return tr(
|
53
|
+
"No blocks found between markers in {path}.",
|
54
|
+
path=path,
|
55
|
+
)
|
56
|
+
|
57
|
+
new_content, deleted_blocks = self._delete_blocks(
|
58
|
+
content, start_marker, end_marker
|
59
|
+
)
|
60
|
+
self._write_file_content(path, new_content)
|
61
|
+
validation_result = validate_file_syntax(path)
|
62
|
+
self._report_success(match_lines)
|
63
|
+
return tr(
|
64
|
+
"Deleted {count} block(s) between markers in {path}. ",
|
65
|
+
count=deleted_blocks,
|
66
|
+
path=path,
|
67
|
+
) + (f"\n{validation_result}" if validation_result else "")
|
68
|
+
except Exception as e:
|
69
|
+
self.report_error(tr(" ❌ Error: {error}", error=e), ReportAction.REPLACE)
|
70
|
+
return tr("Error deleting text: {error}", error=e)
|
71
|
+
|
72
|
+
def _read_file_content(self, path):
|
73
|
+
with open(path, "r", encoding="utf-8", errors="replace") as f:
|
74
|
+
return f.read()
|
75
|
+
|
76
|
+
def _find_marker_blocks(self, content, start_marker, end_marker):
|
77
|
+
"""Find all blocks between start_marker and end_marker, return count and starting line numbers."""
|
78
|
+
lines = content.splitlines(keepends=True)
|
79
|
+
joined = "".join(lines)
|
80
|
+
match_lines = []
|
81
|
+
idx = 0
|
82
|
+
occurrences = 0
|
83
|
+
while True:
|
84
|
+
start_idx = joined.find(start_marker, idx)
|
85
|
+
if start_idx == -1:
|
86
|
+
break
|
87
|
+
end_idx = joined.find(end_marker, start_idx + len(start_marker))
|
88
|
+
if end_idx == -1:
|
89
|
+
break
|
90
|
+
upto = joined[:start_idx]
|
91
|
+
line_no = upto.count("\n") + 1
|
92
|
+
match_lines.append(line_no)
|
93
|
+
idx = end_idx + len(end_marker)
|
94
|
+
occurrences += 1
|
95
|
+
return occurrences, match_lines
|
96
|
+
|
97
|
+
def _delete_blocks(self, content, start_marker, end_marker):
|
98
|
+
"""Delete all blocks between start_marker and end_marker (inclusive)."""
|
99
|
+
count = 0
|
100
|
+
new_content = content
|
101
|
+
while True:
|
102
|
+
start_idx = new_content.find(start_marker)
|
103
|
+
if start_idx == -1:
|
104
|
+
break
|
105
|
+
end_idx = new_content.find(end_marker, start_idx + len(start_marker))
|
106
|
+
if end_idx == -1:
|
107
|
+
break
|
108
|
+
new_content = (
|
109
|
+
new_content[:start_idx] + new_content[end_idx + len(end_marker) :]
|
110
|
+
)
|
111
|
+
count += 1
|
112
|
+
return new_content, count
|
113
|
+
|
114
|
+
def _backup_file(self, path, backup_path):
|
115
|
+
shutil.copy2(path, backup_path)
|
116
|
+
|
117
|
+
def _write_file_content(self, path, content):
|
118
|
+
with open(path, "w", encoding="utf-8", errors="replace") as f:
|
119
|
+
f.write(content)
|
120
|
+
|
121
|
+
def _report_success(self, match_lines):
|
122
|
+
if match_lines:
|
123
|
+
lines_str = ", ".join(str(line_no) for line_no in match_lines)
|
124
|
+
self.report_success(
|
125
|
+
tr(
|
126
|
+
" ✅ deleted block(s) starting at line(s): {lines_str}",
|
127
|
+
lines_str=lines_str,
|
128
|
+
),
|
129
|
+
ReportAction.CREATE,
|
130
|
+
)
|
131
|
+
else:
|
132
|
+
self.report_success(
|
133
|
+
tr(" ✅ deleted block(s) (lines unknown)"), ReportAction.CREATE
|
134
|
+
)
|
@@ -5,7 +5,7 @@ import json
|
|
5
5
|
from pathlib import Path
|
6
6
|
from bs4 import BeautifulSoup
|
7
7
|
from typing import Dict, Any, Optional
|
8
|
-
from janito.tools.
|
8
|
+
from janito.plugins.tools.decorators import register_core_tool
|
9
9
|
from janito.tools.tool_base import ToolBase, ToolPermissions
|
10
10
|
from janito.report_events import ReportAction
|
11
11
|
from janito.i18n import tr
|
@@ -13,8 +13,8 @@ from janito.tools.tool_utils import pluralize
|
|
13
13
|
from janito.tools.loop_protection_decorator import protect_against_loops
|
14
14
|
|
15
15
|
|
16
|
-
@
|
17
|
-
class
|
16
|
+
@register_core_tool
|
17
|
+
class FetchUrl(ToolBase):
|
18
18
|
"""
|
19
19
|
Fetch the content of a web page and extract its text.
|
20
20
|
|
@@ -0,0 +1,143 @@
|
|
1
|
+
from janito.tools.tool_base import ToolBase, ToolPermissions
|
2
|
+
from janito.report_events import ReportAction
|
3
|
+
from janito.plugins.tools.decorators import register_core_tool
|
4
|
+
from janito.tools.tool_utils import pluralize, display_path
|
5
|
+
from janito.dir_walk_utils import walk_dir_with_gitignore
|
6
|
+
from janito.i18n import tr
|
7
|
+
import fnmatch
|
8
|
+
import os
|
9
|
+
from janito.tools.path_utils import expand_path
|
10
|
+
from janito.tools.loop_protection_decorator import protect_against_loops
|
11
|
+
|
12
|
+
|
13
|
+
@register_core_tool
|
14
|
+
class FindFiles(ToolBase):
|
15
|
+
"""
|
16
|
+
Find files or directories in one or more directories matching a pattern. Respects .gitignore.
|
17
|
+
|
18
|
+
If a path is an existing file, it is checked against the provided pattern(s) and included in the results if it matches. This allows find_files to be used to look for a specific set of filenames in a single call, as well as searching directories.
|
19
|
+
|
20
|
+
Args:
|
21
|
+
paths (str): String of one or more paths (space-separated) to search in. Each path can be a directory or a file.
|
22
|
+
pattern (str): File pattern(s) to match. Multiple patterns can be separated by spaces. Uses Unix shell-style wildcards (fnmatch), e.g. '*.py', 'data_??.csv', '[a-z]*.txt'.
|
23
|
+
- If the pattern ends with '/' or '\', only matching directory names (with trailing slash) are returned, not the files within those directories. For example, pattern '*/' will return only directories at the specified depth.
|
24
|
+
max_depth (int, optional): Maximum directory depth to search. If None, unlimited recursion. If 0, only the top-level directory. If 1, only the root directory (matches 'find . -maxdepth 1').
|
25
|
+
include_gitignored (bool, optional): If True, includes files/directories ignored by .gitignore. Defaults to False.
|
26
|
+
Returns:
|
27
|
+
str: Newline-separated list of matching file paths. Example:
|
28
|
+
"/path/to/file1.py\n/path/to/file2.py"
|
29
|
+
"Warning: Empty file pattern provided. Operation skipped."
|
30
|
+
"""
|
31
|
+
|
32
|
+
permissions = ToolPermissions(read=True)
|
33
|
+
tool_name = "find_files"
|
34
|
+
|
35
|
+
def _match_directories(self, root, dirs, pat):
|
36
|
+
dir_output = set()
|
37
|
+
dir_pat = pat.rstrip("/\\")
|
38
|
+
for d in dirs:
|
39
|
+
if fnmatch.fnmatch(d, dir_pat):
|
40
|
+
dir_output.add(os.path.join(root, d) + os.sep)
|
41
|
+
return dir_output
|
42
|
+
|
43
|
+
def _match_files(self, root, files, pat):
|
44
|
+
file_output = set()
|
45
|
+
for filename in fnmatch.filter(files, pat):
|
46
|
+
file_output.add(os.path.join(root, filename))
|
47
|
+
return file_output
|
48
|
+
|
49
|
+
def _match_dirs_without_slash(self, root, dirs, pat):
|
50
|
+
dir_output = set()
|
51
|
+
for d in fnmatch.filter(dirs, pat):
|
52
|
+
dir_output.add(os.path.join(root, d))
|
53
|
+
return dir_output
|
54
|
+
|
55
|
+
def _handle_path(self, directory, patterns):
|
56
|
+
dir_output = set()
|
57
|
+
filename = os.path.basename(directory)
|
58
|
+
for pat in patterns:
|
59
|
+
# Only match files, not directories, for file paths
|
60
|
+
if not (pat.endswith("/") or pat.endswith("\\")):
|
61
|
+
if fnmatch.fnmatch(filename, pat):
|
62
|
+
dir_output.add(directory)
|
63
|
+
break
|
64
|
+
return dir_output
|
65
|
+
|
66
|
+
def _handle_directory_path(
|
67
|
+
self, directory, patterns, max_depth, include_gitignored
|
68
|
+
):
|
69
|
+
dir_output = set()
|
70
|
+
for root, dirs, files in walk_dir_with_gitignore(
|
71
|
+
directory,
|
72
|
+
max_depth=max_depth,
|
73
|
+
include_gitignored=include_gitignored,
|
74
|
+
):
|
75
|
+
for pat in patterns:
|
76
|
+
if pat.endswith("/") or pat.endswith("\\"):
|
77
|
+
dir_output.update(self._match_directories(root, dirs, pat))
|
78
|
+
else:
|
79
|
+
dir_output.update(self._match_files(root, files, pat))
|
80
|
+
dir_output.update(self._match_dirs_without_slash(root, dirs, pat))
|
81
|
+
return dir_output
|
82
|
+
|
83
|
+
def _report_search(self, pattern, disp_path, depth_msg):
|
84
|
+
self.report_action(
|
85
|
+
tr(
|
86
|
+
"🔍 Search for files '{pattern}' in '{disp_path}'{depth_msg} ...",
|
87
|
+
pattern=pattern,
|
88
|
+
disp_path=disp_path,
|
89
|
+
depth_msg=depth_msg,
|
90
|
+
),
|
91
|
+
ReportAction.READ,
|
92
|
+
)
|
93
|
+
|
94
|
+
def _report_success(self, count):
|
95
|
+
self.report_success(
|
96
|
+
tr(
|
97
|
+
" ✅ {count} {file_word}",
|
98
|
+
count=count,
|
99
|
+
file_word=pluralize("file", count),
|
100
|
+
),
|
101
|
+
ReportAction.READ,
|
102
|
+
)
|
103
|
+
|
104
|
+
def _format_output(self, directory, dir_output):
|
105
|
+
if directory.strip() == ".":
|
106
|
+
dir_output = {
|
107
|
+
p[2:] if (p.startswith("./") or p.startswith(".\\")) else p
|
108
|
+
for p in dir_output
|
109
|
+
}
|
110
|
+
return sorted(dir_output)
|
111
|
+
|
112
|
+
@protect_against_loops(max_calls=5, time_window=10.0, key_field="paths")
|
113
|
+
def run(
|
114
|
+
self,
|
115
|
+
paths: str,
|
116
|
+
pattern: str,
|
117
|
+
max_depth: int = None,
|
118
|
+
include_gitignored: bool = False,
|
119
|
+
) -> str:
|
120
|
+
if not pattern:
|
121
|
+
self.report_warning(tr("ℹ️ Empty file pattern provided."), ReportAction.READ)
|
122
|
+
return tr("Warning: Empty file pattern provided. Operation skipped.")
|
123
|
+
patterns = pattern.split()
|
124
|
+
results = []
|
125
|
+
for directory in [expand_path(p) for p in paths.split()]:
|
126
|
+
disp_path = display_path(directory)
|
127
|
+
depth_msg = (
|
128
|
+
tr(" (max depth: {max_depth})", max_depth=max_depth)
|
129
|
+
if max_depth is not None and max_depth > 0
|
130
|
+
else ""
|
131
|
+
)
|
132
|
+
self._report_search(pattern, disp_path, depth_msg)
|
133
|
+
dir_output = set()
|
134
|
+
if os.path.isfile(directory):
|
135
|
+
dir_output = self._handle_path(directory, patterns)
|
136
|
+
elif os.path.isdir(directory):
|
137
|
+
dir_output = self._handle_directory_path(
|
138
|
+
directory, patterns, max_depth, include_gitignored
|
139
|
+
)
|
140
|
+
self._report_success(len(dir_output))
|
141
|
+
results.extend(self._format_output(directory, dir_output))
|
142
|
+
result = "\n".join(results)
|
143
|
+
return result
|
@@ -0,0 +1,122 @@
|
|
1
|
+
from janito.tools.adapters.local.adapter import register_local_tool
|
2
|
+
from .python_outline import parse_python_outline
|
3
|
+
from .markdown_outline import parse_markdown_outline
|
4
|
+
from janito.formatting import OutlineFormatter
|
5
|
+
from .java_outline import parse_java_outline
|
6
|
+
import os
|
7
|
+
from janito.tools.path_utils import expand_path
|
8
|
+
from janito.tools.tool_base import ToolBase, ToolPermissions
|
9
|
+
from janito.report_events import ReportAction
|
10
|
+
from janito.tools.tool_utils import display_path, pluralize
|
11
|
+
from janito.i18n import tr
|
12
|
+
|
13
|
+
from janito.plugins.tools.decorators import register_core_tool
|
14
|
+
from janito.tools.loop_protection_decorator import protect_against_loops
|
15
|
+
|
16
|
+
|
17
|
+
@register_core_tool
|
18
|
+
class GetFileOutline(ToolBase):
|
19
|
+
"""
|
20
|
+
Get an outline of a file's structure. Supports Python and Markdown files.
|
21
|
+
|
22
|
+
Args:
|
23
|
+
path (str): Path to the file to outline.
|
24
|
+
"""
|
25
|
+
|
26
|
+
permissions = ToolPermissions(read=True)
|
27
|
+
tool_name = "get_file_outline"
|
28
|
+
|
29
|
+
@protect_against_loops(max_calls=5, time_window=10.0, key_field="path")
|
30
|
+
def run(self, path: str) -> str:
|
31
|
+
try:
|
32
|
+
path = expand_path(path)
|
33
|
+
self.report_action(
|
34
|
+
tr(
|
35
|
+
"📄 Outline file '{disp_path}' ...",
|
36
|
+
disp_path=display_path(path),
|
37
|
+
),
|
38
|
+
ReportAction.READ,
|
39
|
+
)
|
40
|
+
ext = os.path.splitext(path)[1].lower()
|
41
|
+
with open(path, "r", encoding="utf-8", errors="replace") as f:
|
42
|
+
lines = f.readlines()
|
43
|
+
return self._outline_by_extension(ext, lines)
|
44
|
+
except Exception as e:
|
45
|
+
self.report_error(
|
46
|
+
tr("❌ Error reading file: {error}", error=e),
|
47
|
+
ReportAction.READ,
|
48
|
+
)
|
49
|
+
return tr("Error reading file: {error}", error=e)
|
50
|
+
|
51
|
+
def _outline_by_extension(self, ext, lines):
|
52
|
+
if ext == ".py":
|
53
|
+
outline_items = parse_python_outline(lines)
|
54
|
+
outline_type = "python"
|
55
|
+
table = OutlineFormatter.format_outline_table(outline_items)
|
56
|
+
self.report_success(
|
57
|
+
tr(
|
58
|
+
"✅ Outlined {count} {item_word}",
|
59
|
+
count=len(outline_items),
|
60
|
+
item_word=pluralize("item", len(outline_items)),
|
61
|
+
),
|
62
|
+
ReportAction.READ,
|
63
|
+
)
|
64
|
+
return (
|
65
|
+
tr(
|
66
|
+
"Outline: {count} items ({outline_type})\n",
|
67
|
+
count=len(outline_items),
|
68
|
+
outline_type=outline_type,
|
69
|
+
)
|
70
|
+
+ table
|
71
|
+
)
|
72
|
+
elif ext == ".md":
|
73
|
+
outline_items = parse_markdown_outline(lines)
|
74
|
+
outline_type = "markdown"
|
75
|
+
table = OutlineFormatter.format_markdown_outline_table(outline_items)
|
76
|
+
self.report_success(
|
77
|
+
tr(
|
78
|
+
"✅ Outlined {count} {item_word}",
|
79
|
+
count=len(outline_items),
|
80
|
+
item_word=pluralize("item", len(outline_items)),
|
81
|
+
),
|
82
|
+
ReportAction.READ,
|
83
|
+
)
|
84
|
+
return (
|
85
|
+
tr(
|
86
|
+
"Outline: {count} items ({outline_type})\n",
|
87
|
+
count=len(outline_items),
|
88
|
+
outline_type=outline_type,
|
89
|
+
)
|
90
|
+
+ table
|
91
|
+
)
|
92
|
+
elif ext == ".java":
|
93
|
+
outline_items = parse_java_outline(lines)
|
94
|
+
outline_type = "java"
|
95
|
+
table = OutlineFormatter.format_outline_table(outline_items)
|
96
|
+
self.report_success(
|
97
|
+
tr(
|
98
|
+
"✅ Outlined {count} {item_word}",
|
99
|
+
count=len(outline_items),
|
100
|
+
item_word=pluralize("item", len(outline_items)),
|
101
|
+
),
|
102
|
+
ReportAction.READ,
|
103
|
+
)
|
104
|
+
return (
|
105
|
+
tr(
|
106
|
+
"Outline: {count} items ({outline_type})\n",
|
107
|
+
count=len(outline_items),
|
108
|
+
outline_type=outline_type,
|
109
|
+
)
|
110
|
+
+ table
|
111
|
+
)
|
112
|
+
else:
|
113
|
+
outline_type = "default"
|
114
|
+
self.report_success(
|
115
|
+
tr("✅ Outlined {count} items", count=len(lines)),
|
116
|
+
ReportAction.READ,
|
117
|
+
)
|
118
|
+
return tr(
|
119
|
+
"Outline: {count} lines ({outline_type})\nFile has {count} lines.",
|
120
|
+
count=len(lines),
|
121
|
+
outline_type=outline_type,
|
122
|
+
)
|
@@ -0,0 +1,47 @@
|
|
1
|
+
import re
|
2
|
+
from typing import List, Dict
|
3
|
+
|
4
|
+
|
5
|
+
def parse_java_outline(lines: List[str]) -> List[Dict]:
|
6
|
+
"""
|
7
|
+
Parses Java source code lines and extracts classes and methods with their signatures.
|
8
|
+
Returns a list of outline items: {type, name, return_type, parameters, generics, line}
|
9
|
+
"""
|
10
|
+
outline = []
|
11
|
+
class_pattern = re.compile(r"\bclass\s+(\w+)(\s*<[^>]+>)?")
|
12
|
+
# Match methods with or without visibility modifiers (including package-private)
|
13
|
+
method_pattern = re.compile(
|
14
|
+
r"^(?:\s*(public|protected|private)\s+)?(?:static\s+)?([\w<>\[\]]+)\s+(\w+)\s*\(([^)]*)\)"
|
15
|
+
)
|
16
|
+
current_class = None
|
17
|
+
for idx, line in enumerate(lines, 1):
|
18
|
+
class_match = class_pattern.search(line)
|
19
|
+
if class_match:
|
20
|
+
class_name = class_match.group(1)
|
21
|
+
generics = class_match.group(2) or ""
|
22
|
+
outline.append(
|
23
|
+
{
|
24
|
+
"type": "class",
|
25
|
+
"name": class_name,
|
26
|
+
"generics": generics.strip("<>") if generics else None,
|
27
|
+
"line": idx,
|
28
|
+
}
|
29
|
+
)
|
30
|
+
current_class = class_name
|
31
|
+
else:
|
32
|
+
method_match = method_pattern.search(line)
|
33
|
+
if method_match:
|
34
|
+
return_type = method_match.group(2)
|
35
|
+
method_name = method_match.group(3)
|
36
|
+
params = method_match.group(4)
|
37
|
+
outline.append(
|
38
|
+
{
|
39
|
+
"type": "method",
|
40
|
+
"class": current_class,
|
41
|
+
"name": method_name,
|
42
|
+
"return_type": return_type,
|
43
|
+
"parameters": params.strip(),
|
44
|
+
"line": idx,
|
45
|
+
}
|
46
|
+
)
|
47
|
+
return outline
|
@@ -0,0 +1,14 @@
|
|
1
|
+
import re
|
2
|
+
from typing import List
|
3
|
+
|
4
|
+
|
5
|
+
def parse_markdown_outline(lines: List[str]):
|
6
|
+
header_pat = re.compile(r"^(#+)\s+(.*)")
|
7
|
+
outline = []
|
8
|
+
for idx, line in enumerate(lines):
|
9
|
+
match = header_pat.match(line)
|
10
|
+
if match:
|
11
|
+
level = len(match.group(1))
|
12
|
+
title = match.group(2).strip()
|
13
|
+
outline.append({"level": level, "title": title, "line": idx + 1})
|
14
|
+
return outline
|