praisonaiagents 0.0.23__py3-none-any.whl → 0.0.24__py3-none-any.whl
Sign up to get free protection for your applications and to get access to all the features.
- praisonaiagents/tools/__init__.py +165 -2
- praisonaiagents/tools/arxiv_tools.py +292 -0
- praisonaiagents/tools/calculator_tools.py +278 -0
- praisonaiagents/tools/csv_tools.py +266 -0
- praisonaiagents/tools/duckdb_tools.py +268 -0
- praisonaiagents/tools/duckduckgo_tools.py +52 -0
- praisonaiagents/tools/excel_tools.py +310 -0
- praisonaiagents/tools/file_tools.py +274 -0
- praisonaiagents/tools/json_tools.py +515 -0
- praisonaiagents/tools/newspaper_tools.py +354 -0
- praisonaiagents/tools/pandas_tools.py +326 -0
- praisonaiagents/tools/python_tools.py +423 -0
- praisonaiagents/tools/shell_tools.py +278 -0
- praisonaiagents/tools/spider_tools.py +431 -0
- praisonaiagents/tools/test.py +56 -0
- praisonaiagents/tools/tools.py +5 -36
- praisonaiagents/tools/wikipedia_tools.py +272 -0
- praisonaiagents/tools/xml_tools.py +498 -0
- praisonaiagents/tools/yaml_tools.py +417 -0
- praisonaiagents/tools/yfinance_tools.py +213 -0
- {praisonaiagents-0.0.23.dist-info → praisonaiagents-0.0.24.dist-info}/METADATA +1 -1
- praisonaiagents-0.0.24.dist-info/RECORD +42 -0
- praisonaiagents-0.0.23.dist-info/RECORD +0 -24
- {praisonaiagents-0.0.23.dist-info → praisonaiagents-0.0.24.dist-info}/WHEEL +0 -0
- {praisonaiagents-0.0.23.dist-info → praisonaiagents-0.0.24.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,274 @@
|
|
1
|
+
"""File handling tools for basic file operations.
|
2
|
+
|
3
|
+
Usage:
|
4
|
+
from praisonaiagents.tools import file_tools
|
5
|
+
content = file_tools.read_file("example.txt")
|
6
|
+
file_tools.write_file("output.txt", "Hello World")
|
7
|
+
|
8
|
+
or
|
9
|
+
from praisonaiagents.tools import read_file, write_file, list_files
|
10
|
+
content = read_file("example.txt")
|
11
|
+
"""
|
12
|
+
|
13
|
+
import os
|
14
|
+
import json
|
15
|
+
from typing import List, Dict, Union, Optional
|
16
|
+
from pathlib import Path
|
17
|
+
import shutil
|
18
|
+
import logging
|
19
|
+
|
20
|
+
class FileTools:
|
21
|
+
"""Tools for file operations including read, write, list, and information."""
|
22
|
+
|
23
|
+
@staticmethod
|
24
|
+
def read_file(filepath: str, encoding: str = 'utf-8') -> str:
|
25
|
+
"""
|
26
|
+
Read content from a file.
|
27
|
+
|
28
|
+
Args:
|
29
|
+
filepath: Path to the file
|
30
|
+
encoding: File encoding (default: utf-8)
|
31
|
+
|
32
|
+
Returns:
|
33
|
+
str: Content of the file
|
34
|
+
"""
|
35
|
+
try:
|
36
|
+
with open(filepath, 'r', encoding=encoding) as f:
|
37
|
+
return f.read()
|
38
|
+
except Exception as e:
|
39
|
+
error_msg = f"Error reading file {filepath}: {str(e)}"
|
40
|
+
logging.error(error_msg)
|
41
|
+
return error_msg
|
42
|
+
|
43
|
+
@staticmethod
|
44
|
+
def write_file(filepath: str, content: str, encoding: str = 'utf-8') -> bool:
|
45
|
+
"""
|
46
|
+
Write content to a file.
|
47
|
+
|
48
|
+
Args:
|
49
|
+
filepath: Path to the file
|
50
|
+
content: Content to write
|
51
|
+
encoding: File encoding (default: utf-8)
|
52
|
+
|
53
|
+
Returns:
|
54
|
+
bool: True if successful, False otherwise
|
55
|
+
"""
|
56
|
+
try:
|
57
|
+
# Create directory if it doesn't exist
|
58
|
+
os.makedirs(os.path.dirname(filepath), exist_ok=True)
|
59
|
+
with open(filepath, 'w', encoding=encoding) as f:
|
60
|
+
f.write(content)
|
61
|
+
return True
|
62
|
+
except Exception as e:
|
63
|
+
error_msg = f"Error writing to file {filepath}: {str(e)}"
|
64
|
+
logging.error(error_msg)
|
65
|
+
return False
|
66
|
+
|
67
|
+
@staticmethod
|
68
|
+
def list_files(directory: str, pattern: Optional[str] = None) -> List[Dict[str, Union[str, int]]]:
|
69
|
+
"""
|
70
|
+
List files in a directory with optional pattern matching.
|
71
|
+
|
72
|
+
Args:
|
73
|
+
directory: Directory path
|
74
|
+
pattern: Optional glob pattern (e.g., "*.txt")
|
75
|
+
|
76
|
+
Returns:
|
77
|
+
List[Dict]: List of file information dictionaries
|
78
|
+
"""
|
79
|
+
try:
|
80
|
+
path = Path(directory)
|
81
|
+
if pattern:
|
82
|
+
files = path.glob(pattern)
|
83
|
+
else:
|
84
|
+
files = path.iterdir()
|
85
|
+
|
86
|
+
result = []
|
87
|
+
for file in files:
|
88
|
+
if file.is_file():
|
89
|
+
stat = file.stat()
|
90
|
+
result.append({
|
91
|
+
'name': file.name,
|
92
|
+
'path': str(file),
|
93
|
+
'size': stat.st_size,
|
94
|
+
'modified': stat.st_mtime,
|
95
|
+
'created': stat.st_ctime
|
96
|
+
})
|
97
|
+
return result
|
98
|
+
except Exception as e:
|
99
|
+
error_msg = f"Error listing files in {directory}: {str(e)}"
|
100
|
+
logging.error(error_msg)
|
101
|
+
return [{'error': error_msg}]
|
102
|
+
|
103
|
+
@staticmethod
|
104
|
+
def get_file_info(filepath: str) -> Dict[str, Union[str, int]]:
|
105
|
+
"""
|
106
|
+
Get detailed information about a file.
|
107
|
+
|
108
|
+
Args:
|
109
|
+
filepath: Path to the file
|
110
|
+
|
111
|
+
Returns:
|
112
|
+
Dict: File information including size, dates, etc.
|
113
|
+
"""
|
114
|
+
try:
|
115
|
+
path = Path(filepath)
|
116
|
+
if not path.exists():
|
117
|
+
return {'error': f'File not found: {filepath}'}
|
118
|
+
|
119
|
+
stat = path.stat()
|
120
|
+
return {
|
121
|
+
'name': path.name,
|
122
|
+
'path': str(path),
|
123
|
+
'size': stat.st_size,
|
124
|
+
'modified': stat.st_mtime,
|
125
|
+
'created': stat.st_ctime,
|
126
|
+
'is_file': path.is_file(),
|
127
|
+
'is_dir': path.is_dir(),
|
128
|
+
'extension': path.suffix,
|
129
|
+
'parent': str(path.parent)
|
130
|
+
}
|
131
|
+
except Exception as e:
|
132
|
+
error_msg = f"Error getting file info for {filepath}: {str(e)}"
|
133
|
+
logging.error(error_msg)
|
134
|
+
return {'error': error_msg}
|
135
|
+
|
136
|
+
@staticmethod
|
137
|
+
def copy_file(src: str, dst: str) -> bool:
|
138
|
+
"""
|
139
|
+
Copy a file from source to destination.
|
140
|
+
|
141
|
+
Args:
|
142
|
+
src: Source file path
|
143
|
+
dst: Destination file path
|
144
|
+
|
145
|
+
Returns:
|
146
|
+
bool: True if successful, False otherwise
|
147
|
+
"""
|
148
|
+
try:
|
149
|
+
# Create destination directory if it doesn't exist
|
150
|
+
os.makedirs(os.path.dirname(dst), exist_ok=True)
|
151
|
+
shutil.copy2(src, dst)
|
152
|
+
return True
|
153
|
+
except Exception as e:
|
154
|
+
error_msg = f"Error copying file from {src} to {dst}: {str(e)}"
|
155
|
+
logging.error(error_msg)
|
156
|
+
return False
|
157
|
+
|
158
|
+
@staticmethod
|
159
|
+
def move_file(src: str, dst: str) -> bool:
|
160
|
+
"""
|
161
|
+
Move a file from source to destination.
|
162
|
+
|
163
|
+
Args:
|
164
|
+
src: Source file path
|
165
|
+
dst: Destination file path
|
166
|
+
|
167
|
+
Returns:
|
168
|
+
bool: True if successful, False otherwise
|
169
|
+
"""
|
170
|
+
try:
|
171
|
+
# Create destination directory if it doesn't exist
|
172
|
+
os.makedirs(os.path.dirname(dst), exist_ok=True)
|
173
|
+
shutil.move(src, dst)
|
174
|
+
return True
|
175
|
+
except Exception as e:
|
176
|
+
error_msg = f"Error moving file from {src} to {dst}: {str(e)}"
|
177
|
+
logging.error(error_msg)
|
178
|
+
return False
|
179
|
+
|
180
|
+
@staticmethod
|
181
|
+
def delete_file(filepath: str) -> bool:
|
182
|
+
"""
|
183
|
+
Delete a file.
|
184
|
+
|
185
|
+
Args:
|
186
|
+
filepath: Path to the file
|
187
|
+
|
188
|
+
Returns:
|
189
|
+
bool: True if successful, False otherwise
|
190
|
+
"""
|
191
|
+
try:
|
192
|
+
os.remove(filepath)
|
193
|
+
return True
|
194
|
+
except Exception as e:
|
195
|
+
error_msg = f"Error deleting file {filepath}: {str(e)}"
|
196
|
+
logging.error(error_msg)
|
197
|
+
return False
|
198
|
+
|
199
|
+
# Create instance for direct function access
|
200
|
+
_file_tools = FileTools()
|
201
|
+
read_file = _file_tools.read_file
|
202
|
+
write_file = _file_tools.write_file
|
203
|
+
list_files = _file_tools.list_files
|
204
|
+
get_file_info = _file_tools.get_file_info
|
205
|
+
copy_file = _file_tools.copy_file
|
206
|
+
move_file = _file_tools.move_file
|
207
|
+
delete_file = _file_tools.delete_file
|
208
|
+
|
209
|
+
if __name__ == "__main__":
|
210
|
+
# Example usage
|
211
|
+
print("\n==================================================")
|
212
|
+
print("FileTools Demonstration")
|
213
|
+
print("==================================================\n")
|
214
|
+
|
215
|
+
# Create a test directory
|
216
|
+
test_dir = os.path.join(os.getcwd(), "test_files")
|
217
|
+
os.makedirs(test_dir, exist_ok=True)
|
218
|
+
|
219
|
+
# Create test files
|
220
|
+
test_file = os.path.join(test_dir, "test_file.txt")
|
221
|
+
test_content = "Hello, this is a test file!"
|
222
|
+
|
223
|
+
print("1. Writing to file")
|
224
|
+
print("------------------------------")
|
225
|
+
success = write_file(test_file, test_content)
|
226
|
+
print(f"Write successful: {success}\n")
|
227
|
+
|
228
|
+
print("2. Reading from file")
|
229
|
+
print("------------------------------")
|
230
|
+
content = read_file(test_file)
|
231
|
+
print(f"Content: {content}\n")
|
232
|
+
|
233
|
+
print("3. File Information")
|
234
|
+
print("------------------------------")
|
235
|
+
info = get_file_info(test_file)
|
236
|
+
print(json.dumps(info, indent=2))
|
237
|
+
print()
|
238
|
+
|
239
|
+
print("4. Listing Files")
|
240
|
+
print("------------------------------")
|
241
|
+
files = list_files(test_dir, "*.txt")
|
242
|
+
for file in files:
|
243
|
+
print(f"Found: {file['name']} ({file['size']} bytes)")
|
244
|
+
print()
|
245
|
+
|
246
|
+
print("5. Copying File")
|
247
|
+
print("------------------------------")
|
248
|
+
copy_file_path = os.path.join(test_dir, "test_file_copy.txt")
|
249
|
+
copy_success = copy_file(test_file, copy_file_path)
|
250
|
+
print(f"Copy successful: {copy_success}\n")
|
251
|
+
|
252
|
+
print("6. Moving File")
|
253
|
+
print("------------------------------")
|
254
|
+
move_file_path = os.path.join(test_dir, "test_file_moved.txt")
|
255
|
+
move_success = move_file(copy_file_path, move_file_path)
|
256
|
+
print(f"Move successful: {move_success}\n")
|
257
|
+
|
258
|
+
print("7. Deleting Files")
|
259
|
+
print("------------------------------")
|
260
|
+
delete_success = delete_file(test_file)
|
261
|
+
print(f"Delete original successful: {delete_success}")
|
262
|
+
delete_success = delete_file(move_file_path)
|
263
|
+
print(f"Delete moved file successful: {delete_success}\n")
|
264
|
+
|
265
|
+
# Clean up test directory
|
266
|
+
try:
|
267
|
+
shutil.rmtree(test_dir)
|
268
|
+
print("Test directory cleaned up successfully")
|
269
|
+
except Exception as e:
|
270
|
+
print(f"Error cleaning up test directory: {str(e)}")
|
271
|
+
|
272
|
+
print("\n==================================================")
|
273
|
+
print("Demonstration Complete")
|
274
|
+
print("==================================================")
|