janito 3.6.1__py3-none-any.whl → 3.7.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.
Files changed (170) hide show
  1. janito/README.md +4 -7
  2. janito/cli/chat_mode/bindings.py +0 -50
  3. janito/cli/chat_mode/session.py +1 -12
  4. janito/cli/chat_mode/shell/commands/multi.py +0 -5
  5. janito/cli/chat_mode/shell/commands/security/allowed_sites.py +33 -47
  6. janito/cli/cli_commands/list_plugins.py +43 -52
  7. janito/cli/cli_commands/list_tools.py +1 -6
  8. janito/cli/core/getters.py +0 -3
  9. janito/cli/core/model_guesser.py +24 -40
  10. janito/cli/main_cli.py +13 -10
  11. janito/cli/prompt_core.py +9 -47
  12. janito/cli/rich_terminal_reporter.py +4 -4
  13. janito/docs/GETTING_STARTED.md +9 -8
  14. janito/drivers/openai/driver.py +0 -1
  15. janito/drivers/zai/driver.py +0 -1
  16. janito/i18n/it.py +46 -46
  17. janito/llm/agent.py +16 -32
  18. janito/llm/auth_utils.py +5 -14
  19. janito/llm/driver.py +0 -8
  20. janito/plugins/__init__.py +12 -31
  21. janito/plugins/auto_loader.py +11 -12
  22. janito/plugins/auto_loader_fixed.py +11 -12
  23. janito/{plugin_system → plugins}/base.py +2 -5
  24. janito/plugins/builtin.py +1 -15
  25. janito/plugins/core_adapter.py +11 -89
  26. janito/plugins/core_loader.py +120 -0
  27. janito/plugins/core_loader_fixed.py +125 -0
  28. janito/plugins/discovery.py +5 -5
  29. janito/plugins/discovery_core.py +9 -14
  30. janito/plugins/manager.py +1 -1
  31. janito/providers/__init__.py +0 -1
  32. janito/providers/moonshot/model_info.py +7 -7
  33. janito/providers/moonshot/provider.py +1 -1
  34. janito/tools/__init__.py +7 -41
  35. janito/tools/adapters/__init__.py +1 -6
  36. janito/tools/adapters/local/__init__.py +70 -7
  37. janito/{plugins/tools/core → tools/adapters/local}/ask_user.py +3 -3
  38. janito/tools/adapters/local/copy_file.py +87 -0
  39. janito/tools/adapters/local/create_file.py +138 -0
  40. janito/{plugins/tools/core → tools/adapters/local}/fetch_url.py +23 -20
  41. janito/tools/adapters/local/move_file.py +131 -0
  42. janito/{plugins/tools/core → tools/adapters/local}/python_code_run.py +7 -23
  43. janito/{plugins/tools/core → tools/adapters/local}/python_command_run.py +5 -21
  44. janito/{plugins/tools/core → tools/adapters/local}/python_file_run.py +5 -21
  45. janito/tools/adapters/local/remove_file.py +58 -0
  46. janito/{plugins/tools/core → tools/adapters/local}/replace_text_in_file.py +4 -4
  47. janito/{plugins/tools/core → tools/adapters/local}/run_bash_command.py +3 -3
  48. janito/{plugins/tools/core → tools/adapters/local}/run_powershell_command.py +3 -3
  49. janito/{plugins/tools/core → tools/adapters/local}/show_image.py +6 -15
  50. janito/{plugins/core/imagedisplay/tools → tools/adapters/local}/show_image_grid.py +5 -13
  51. janito/tools/adapters/local/view_file.py +172 -0
  52. janito/tools/function_adapter.py +65 -0
  53. janito/tools/loop_protection_decorator.py +117 -114
  54. janito-3.7.0.dist-info/METADATA +84 -0
  55. janito-3.7.0.dist-info/RECORD +264 -0
  56. janito/cli/cli_commands/check_tools.py +0 -212
  57. janito/data/blocked.txt +0 -96
  58. janito/llm/cancellation_manager.py +0 -63
  59. janito/llm/enter_cancellation.py +0 -107
  60. janito/plugin_system/__init__.py +0 -10
  61. janito/plugin_system/core_loader.py +0 -217
  62. janito/plugin_system/core_loader_fixed.py +0 -225
  63. janito/plugins/core/__init__.py +0 -7
  64. janito/plugins/core/codeanalyzer/__init__.py +0 -43
  65. janito/plugins/core/filemanager/__init__.py +0 -124
  66. janito/plugins/core/filemanager/tools/copy_file.py +0 -87
  67. janito/plugins/core/filemanager/tools/create_file.py +0 -87
  68. janito/plugins/core/filemanager/tools/move_file.py +0 -131
  69. janito/plugins/core/filemanager/tools/remove_file.py +0 -58
  70. janito/plugins/core/filemanager/tools/replace_text_in_file.py +0 -270
  71. janito/plugins/core/filemanager/tools/view_file.py +0 -172
  72. janito/plugins/core/imagedisplay/__init__.py +0 -14
  73. janito/plugins/core/imagedisplay/plugin.py +0 -51
  74. janito/plugins/core/imagedisplay/tools/__init__.py +0 -1
  75. janito/plugins/core/imagedisplay/tools/show_image.py +0 -83
  76. janito/plugins/core/system/__init__.py +0 -23
  77. janito/plugins/core/system/tools/run_bash_command.py +0 -204
  78. janito/plugins/core/system/tools/run_powershell_command.py +0 -234
  79. janito/plugins/dev/__init__.py +0 -7
  80. janito/plugins/dev/pythondev/__init__.py +0 -37
  81. janito/plugins/dev/visualization/__init__.py +0 -23
  82. janito/plugins/example_plugin.py +0 -108
  83. janito/plugins/tools/__init__.py +0 -39
  84. janito/plugins/tools/core/__init__.py +0 -65
  85. janito/plugins/tools/core/copy_file.py +0 -87
  86. janito/plugins/tools/core/create_directory.py +0 -70
  87. janito/plugins/tools/core/create_file.py +0 -138
  88. janito/plugins/tools/core/decorators.py +0 -19
  89. janito/plugins/tools/core/delete_text_in_file.py +0 -134
  90. janito/plugins/tools/core/find_files.py +0 -143
  91. janito/plugins/tools/core/get_file_outline/__init__.py +0 -7
  92. janito/plugins/tools/core/get_file_outline/core.py +0 -122
  93. janito/plugins/tools/core/get_file_outline/java_outline.py +0 -47
  94. janito/plugins/tools/core/get_file_outline/markdown_outline.py +0 -14
  95. janito/plugins/tools/core/get_file_outline/python_outline.py +0 -303
  96. janito/plugins/tools/core/get_file_outline/search_outline.py +0 -36
  97. janito/plugins/tools/core/move_file.py +0 -131
  98. janito/plugins/tools/core/open_html_in_browser.py +0 -51
  99. janito/plugins/tools/core/open_url.py +0 -37
  100. janito/plugins/tools/core/read_chart.py +0 -259
  101. janito/plugins/tools/core/read_files.py +0 -58
  102. janito/plugins/tools/core/remove_directory.py +0 -55
  103. janito/plugins/tools/core/remove_file.py +0 -58
  104. janito/plugins/tools/core/search_text/__init__.py +0 -7
  105. janito/plugins/tools/core/search_text/core.py +0 -205
  106. janito/plugins/tools/core/search_text/match_lines.py +0 -67
  107. janito/plugins/tools/core/search_text/pattern_utils.py +0 -73
  108. janito/plugins/tools/core/search_text/traverse_directory.py +0 -145
  109. janito/plugins/tools/core/show_image_grid.py +0 -85
  110. janito/plugins/tools/core/validate_file_syntax/__init__.py +0 -7
  111. janito/plugins/tools/core/validate_file_syntax/core.py +0 -114
  112. janito/plugins/tools/core/validate_file_syntax/css_validator.py +0 -35
  113. janito/plugins/tools/core/validate_file_syntax/html_validator.py +0 -100
  114. janito/plugins/tools/core/validate_file_syntax/jinja2_validator.py +0 -50
  115. janito/plugins/tools/core/validate_file_syntax/js_validator.py +0 -27
  116. janito/plugins/tools/core/validate_file_syntax/json_validator.py +0 -6
  117. janito/plugins/tools/core/validate_file_syntax/markdown_validator.py +0 -109
  118. janito/plugins/tools/core/validate_file_syntax/ps1_validator.py +0 -32
  119. janito/plugins/tools/core/validate_file_syntax/python_validator.py +0 -5
  120. janito/plugins/tools/core/validate_file_syntax/xml_validator.py +0 -11
  121. janito/plugins/tools/core/validate_file_syntax/yaml_validator.py +0 -6
  122. janito/plugins/tools/core/view_file.py +0 -172
  123. janito/plugins/ui/__init__.py +0 -7
  124. janito/plugins/ui/userinterface/__init__.py +0 -16
  125. janito/plugins/ui/userinterface/tools/ask_user.py +0 -110
  126. janito/plugins/web/__init__.py +0 -7
  127. janito/plugins/web/webtools/__init__.py +0 -23
  128. janito/providers/together/__init__.py +0 -1
  129. janito/providers/together/model_info.py +0 -69
  130. janito/providers/together/provider.py +0 -108
  131. janito/tools/blocked_sites.py +0 -74
  132. janito/tools/cli_initializer.py +0 -88
  133. janito/tools/initialize.py +0 -70
  134. janito-3.6.1.dist-info/METADATA +0 -228
  135. janito-3.6.1.dist-info/RECORD +0 -339
  136. /janito/{plugins/core/filemanager/tools → tools/adapters/local}/create_directory.py +0 -0
  137. /janito/{plugins/core/filemanager/tools → tools/adapters/local}/delete_text_in_file.py +0 -0
  138. /janito/{plugins/core/filemanager/tools → tools/adapters/local}/find_files.py +0 -0
  139. /janito/{plugins/core/codeanalyzer/tools → tools/adapters/local}/get_file_outline/__init__.py +0 -0
  140. /janito/{plugins/core/codeanalyzer/tools → tools/adapters/local}/get_file_outline/core.py +0 -0
  141. /janito/{plugins/core/codeanalyzer/tools → tools/adapters/local}/get_file_outline/java_outline.py +0 -0
  142. /janito/{plugins/core/codeanalyzer/tools → tools/adapters/local}/get_file_outline/markdown_outline.py +0 -0
  143. /janito/{plugins/core/codeanalyzer/tools → tools/adapters/local}/get_file_outline/python_outline.py +0 -0
  144. /janito/{plugins/core/codeanalyzer/tools → tools/adapters/local}/get_file_outline/search_outline.py +0 -0
  145. /janito/{plugins/web/webtools/tools → tools/adapters/local}/open_html_in_browser.py +0 -0
  146. /janito/{plugins/web/webtools/tools → tools/adapters/local}/open_url.py +0 -0
  147. /janito/{plugins/dev/visualization/tools → tools/adapters/local}/read_chart.py +0 -0
  148. /janito/{plugins/core/filemanager/tools → tools/adapters/local}/read_files.py +0 -0
  149. /janito/{plugins/core/filemanager/tools → tools/adapters/local}/remove_directory.py +0 -0
  150. /janito/{plugins/core/codeanalyzer/tools → tools/adapters/local}/search_text/__init__.py +0 -0
  151. /janito/{plugins/core/codeanalyzer/tools → tools/adapters/local}/search_text/core.py +0 -0
  152. /janito/{plugins/core/codeanalyzer/tools → tools/adapters/local}/search_text/match_lines.py +0 -0
  153. /janito/{plugins/core/codeanalyzer/tools → tools/adapters/local}/search_text/pattern_utils.py +0 -0
  154. /janito/{plugins/core/codeanalyzer/tools → tools/adapters/local}/search_text/traverse_directory.py +0 -0
  155. /janito/{plugins/core/filemanager/tools → tools/adapters/local}/validate_file_syntax/__init__.py +0 -0
  156. /janito/{plugins/core/filemanager/tools → tools/adapters/local}/validate_file_syntax/core.py +0 -0
  157. /janito/{plugins/core/filemanager/tools → tools/adapters/local}/validate_file_syntax/css_validator.py +0 -0
  158. /janito/{plugins/core/filemanager/tools → tools/adapters/local}/validate_file_syntax/html_validator.py +0 -0
  159. /janito/{plugins/core/filemanager/tools → tools/adapters/local}/validate_file_syntax/jinja2_validator.py +0 -0
  160. /janito/{plugins/core/filemanager/tools → tools/adapters/local}/validate_file_syntax/js_validator.py +0 -0
  161. /janito/{plugins/core/filemanager/tools → tools/adapters/local}/validate_file_syntax/json_validator.py +0 -0
  162. /janito/{plugins/core/filemanager/tools → tools/adapters/local}/validate_file_syntax/markdown_validator.py +0 -0
  163. /janito/{plugins/core/filemanager/tools → tools/adapters/local}/validate_file_syntax/ps1_validator.py +0 -0
  164. /janito/{plugins/core/filemanager/tools → tools/adapters/local}/validate_file_syntax/python_validator.py +0 -0
  165. /janito/{plugins/core/filemanager/tools → tools/adapters/local}/validate_file_syntax/xml_validator.py +0 -0
  166. /janito/{plugins/core/filemanager/tools → tools/adapters/local}/validate_file_syntax/yaml_validator.py +0 -0
  167. {janito-3.6.1.dist-info → janito-3.7.0.dist-info}/WHEEL +0 -0
  168. {janito-3.6.1.dist-info → janito-3.7.0.dist-info}/entry_points.txt +0 -0
  169. {janito-3.6.1.dist-info → janito-3.7.0.dist-info}/licenses/LICENSE +0 -0
  170. {janito-3.6.1.dist-info → janito-3.7.0.dist-info}/top_level.txt +0 -0
@@ -1,124 +0,0 @@
1
- """
2
- File Manager Plugin
3
-
4
- Core file and directory operations for managing project files.
5
- """
6
-
7
- from typing import List, Optional
8
- import os
9
-
10
-
11
- def create_file(path: str, content: str = "", overwrite: bool = False) -> str:
12
- """Create a new file with content"""
13
- return f"create_file(path='{path}', content='{len(content)} chars', overwrite={overwrite})"
14
-
15
-
16
- create_file.tool_name = "create_file"
17
-
18
-
19
- def read_files(paths: List[str]) -> str:
20
- """Read multiple files at once"""
21
- return f"read_files(paths={paths})"
22
-
23
-
24
- read_files.tool_name = "read_files"
25
-
26
-
27
- def view_file(
28
- path: str, from_line: Optional[int] = None, to_line: Optional[int] = None
29
- ) -> str:
30
- """Read specific lines or entire files"""
31
- range_str = f"{from_line}-{to_line}" if from_line or to_line else "all"
32
- return f"view_file(path='{path}', range='{range_str}')"
33
-
34
-
35
- view_file.tool_name = "view_file"
36
-
37
-
38
- def replace_text_in_file(
39
- path: str, search_text: str, replacement_text: str, replace_all: bool = True
40
- ) -> str:
41
- """Find and replace text in files"""
42
- return f"replace_text_in_file(path='{path}', search='{search_text[:20]}...', replace='{replacement_text[:20]}...')"
43
-
44
-
45
- replace_text_in_file.tool_name = "replace_text_in_file"
46
-
47
-
48
- def validate_file_syntax(path: str) -> str:
49
- """Check file syntax (Python/Markdown)"""
50
- return f"validate_file_syntax(path='{path}')"
51
-
52
-
53
- validate_file_syntax.tool_name = "validate_file_syntax"
54
-
55
-
56
- def create_directory(path: str) -> str:
57
- """Create new directories"""
58
- return f"create_directory(path='{path}')"
59
-
60
-
61
- create_directory.tool_name = "create_directory"
62
-
63
-
64
- def remove_directory(path: str, recursive: bool = False) -> str:
65
- """Remove directories (recursive option)"""
66
- return f"remove_directory(path='{path}', recursive={recursive})"
67
-
68
-
69
- remove_directory.tool_name = "remove_directory"
70
-
71
-
72
- def remove_file(path: str) -> str:
73
- """Delete single files"""
74
- return f"remove_file(path='{path}')"
75
-
76
-
77
- remove_file.tool_name = "remove_file"
78
-
79
-
80
- def copy_file(sources: str, target: str, overwrite: bool = False) -> str:
81
- """Copy files or directories"""
82
- return f"copy_file(sources='{sources}', target='{target}', overwrite={overwrite})"
83
-
84
-
85
- copy_file.tool_name = "copy_file"
86
-
87
-
88
- def move_file(src_path: str, dest_path: str, overwrite: bool = False) -> str:
89
- """Move/rename files or directories"""
90
- return f"move_file(src='{src_path}', dest='{dest_path}', overwrite={overwrite})"
91
-
92
-
93
- move_file.tool_name = "move_file"
94
-
95
-
96
- def find_files(
97
- paths: str,
98
- pattern: str,
99
- max_depth: Optional[int] = None,
100
- include_gitignored: bool = False,
101
- ) -> str:
102
- """Search for files by pattern (respects .gitignore)"""
103
- return f"find_files(paths='{paths}', pattern='{pattern}', max_depth={max_depth})"
104
-
105
-
106
- find_files.tool_name = "find_files"
107
-
108
-
109
- # Plugin metadata
110
- __plugin_name__ = "core.filemanager"
111
- __plugin_description__ = "Core file and directory operations"
112
- __plugin_tools__ = [
113
- create_file,
114
- read_files,
115
- view_file,
116
- replace_text_in_file,
117
- validate_file_syntax,
118
- create_directory,
119
- remove_directory,
120
- remove_file,
121
- copy_file,
122
- move_file,
123
- find_files,
124
- ]
@@ -1,87 +0,0 @@
1
- import os
2
- from janito.tools.path_utils import expand_path
3
- import shutil
4
- from typing import List, Union
5
- from janito.tools.adapters.local.adapter import register_local_tool
6
- from janito.tools.tool_base import ToolBase, ToolPermissions
7
- from janito.tools.tool_utils import display_path
8
- from janito.report_events import ReportAction
9
- from janito.i18n import tr
10
-
11
-
12
- @register_local_tool
13
- class CopyFileTool(ToolBase):
14
- """
15
- Copy one or more files to a target directory, or copy a single file to a new file.
16
- Args:
17
- sources (str): Space-separated path(s) to the file(s) to copy.
18
- For multiple sources, provide a single string with paths separated by spaces.
19
- target (str): Destination path. If copying multiple sources, this must be an existing directory.
20
- overwrite (bool, optional): Overwrite existing files. Default: False.
21
- Recommended only after reading the file to be overwritten.
22
- Returns:
23
- str: Status string for each copy operation.
24
- """
25
-
26
- permissions = ToolPermissions(read=True, write=True)
27
- tool_name = "copy_file"
28
-
29
- def run(self, sources: str, target: str, overwrite: bool = False) -> str:
30
- source_list = [expand_path(src) for src in sources.split() if src]
31
- target = expand_path(target)
32
- messages = []
33
- if len(source_list) > 1:
34
- if not os.path.isdir(target):
35
- return tr(
36
- "❗ Target must be an existing directory when copying multiple files: '{target}'",
37
- target=display_path(target),
38
- )
39
- for src in source_list:
40
- if not os.path.isfile(src):
41
- messages.append(
42
- tr(
43
- "❗ Source file does not exist: '{src}'",
44
- src=display_path(src),
45
- )
46
- )
47
- continue
48
- dst = os.path.join(target, os.path.basename(src))
49
- messages.append(self._copy_one(src, dst, overwrite=overwrite))
50
- else:
51
- src = source_list[0]
52
- if os.path.isdir(target):
53
- dst = os.path.join(target, os.path.basename(src))
54
- else:
55
- dst = target
56
- messages.append(self._copy_one(src, dst, overwrite=overwrite))
57
- return "\n".join(messages)
58
-
59
- def _copy_one(self, src, dst, overwrite=False) -> str:
60
- disp_src = display_path(src)
61
- disp_dst = display_path(dst)
62
- if not os.path.isfile(src):
63
- return tr("❗ Source file does not exist: '{src}'", src=disp_src)
64
- if os.path.exists(dst) and not overwrite:
65
- return tr(
66
- "❗ Target already exists: '{dst}'. Set overwrite=True to replace.",
67
- dst=disp_dst,
68
- )
69
- try:
70
- os.makedirs(os.path.dirname(dst), exist_ok=True)
71
- shutil.copy2(src, dst)
72
- note = (
73
- "\n⚠️ Overwrote existing file. (recommended only after reading the file to be overwritten)"
74
- if (os.path.exists(dst) and overwrite)
75
- else ""
76
- )
77
- self.report_success(
78
- tr("✅ Copied '{src}' to '{dst}'", src=disp_src, dst=disp_dst)
79
- )
80
- return tr("✅ Copied '{src}' to '{dst}'", src=disp_src, dst=disp_dst) + note
81
- except Exception as e:
82
- return tr(
83
- "❗ Copy failed from '{src}' to '{dst}': {err}",
84
- src=disp_src,
85
- dst=disp_dst,
86
- err=str(e),
87
- )
@@ -1,87 +0,0 @@
1
- import os
2
- from janito.tools.path_utils import expand_path
3
- from janito.tools.adapters.local.adapter import register_local_tool
4
-
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
- from janito.tools.loop_protection_decorator import protect_against_loops
10
-
11
- from janito.tools.adapters.local.validate_file_syntax.core import validate_file_syntax
12
-
13
-
14
- @register_local_tool
15
- class CreateFileTool(ToolBase):
16
- """
17
- Create a new file with the given content.
18
-
19
- Args:
20
- path (str): Path to the file to create.
21
- content (str): Content to write to the file.
22
- overwrite (bool, optional): Overwrite existing file if True. Default: False. Recommended only after reading the file to be overwritten.
23
- Returns:
24
- str: Status message indicating the result. Example:
25
- - "✅ Successfully created the file at ..."
26
-
27
- Note: Syntax validation is automatically performed after this operation.
28
-
29
- Security: This tool includes loop protection to prevent excessive file creation operations.
30
- Protection: Prevents repeated create calls for the same file path within a short window (1 allowed per 10 seconds).
31
- """
32
-
33
- permissions = ToolPermissions(write=True)
34
- tool_name = "create_file"
35
-
36
- @protect_against_loops(max_calls=1, time_window=10.0, key_field="path")
37
- def run(self, path: str, content: str, overwrite: bool = False) -> str:
38
- path = expand_path(path)
39
- disp_path = display_path(path)
40
- if os.path.exists(path) and not overwrite:
41
- try:
42
- with open(path, "r", encoding="utf-8", errors="replace") as f:
43
- existing_content = f.read()
44
- except Exception as e:
45
- existing_content = f"[Error reading file: {e}]"
46
- return tr(
47
- "❗ Cannot create file: file already exists at '{disp_path}'.\n--- Current file content ---\n{existing_content}",
48
- disp_path=disp_path,
49
- existing_content=existing_content,
50
- )
51
- # Determine if we are overwriting an existing file
52
- is_overwrite = os.path.exists(path) and overwrite
53
- if is_overwrite:
54
- # Overwrite branch: log only overwrite warning (no create message)
55
- self.report_action(
56
- tr("⚠️ Overwriting file '{disp_path}'", disp_path=disp_path),
57
- ReportAction.CREATE,
58
- )
59
- dir_name = os.path.dirname(path)
60
- if dir_name:
61
- os.makedirs(dir_name, exist_ok=True)
62
- if not is_overwrite:
63
- # Create branch: log file creation message
64
- self.report_action(
65
- tr("📝 Create file '{disp_path}' ...", disp_path=disp_path),
66
- ReportAction.CREATE,
67
- )
68
- with open(path, "w", encoding="utf-8", errors="replace") as f:
69
- f.write(content)
70
- new_lines = content.count("\n") + 1 if content else 0
71
- self.report_success(
72
- tr("✅ {new_lines} lines", new_lines=new_lines), ReportAction.CREATE
73
- )
74
- # Perform syntax validation and append result
75
- validation_result = validate_file_syntax(path)
76
- if is_overwrite:
77
- # Overwrite branch: return minimal overwrite info to user
78
- return (
79
- tr("✅ {new_lines} lines", new_lines=new_lines)
80
- + f"\n{validation_result}"
81
- )
82
- else:
83
- # Create branch: return detailed create success to user
84
- return (
85
- tr("✅ Created file {new_lines} lines.", new_lines=new_lines)
86
- + f"\n{validation_result}"
87
- )
@@ -1,131 +0,0 @@
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
@@ -1,58 +0,0 @@
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)