Skip to content

Commit 7f12111

Browse files
committed
fix: finish ai edit file
1 parent 07fa689 commit 7f12111

File tree

5 files changed

+290
-2
lines changed

5 files changed

+290
-2
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name="way3",
8-
version="0.0.29",
8+
version="1.0.1",
99
author="aboutmydreams",
1010
author_email="aboutmydreams@163.com",
1111
description="Simplified file path management for Python developers",

tests/test_file_op.py

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1-
from way3 import create_file, delete_file
1+
from way3 import (
2+
create_file,
3+
delete_file,
4+
read_file,
5+
read_line,
6+
insert_to_file,
7+
modify_line,
8+
replace_string_in_file,
9+
replace_string_in_line,
10+
delete_line,
11+
)
212

313
import unittest
414
import os
@@ -22,3 +32,67 @@ def test_delete_file(self):
2232
self.assertEqual(
2333
os.path.isfile("test.txt"), False, "result fail: expected False"
2434
)
35+
36+
def test_read_file(self):
37+
file_content = "Hello\nWorld!"
38+
create_file("./test.txt", file_content)
39+
info = read_file("test.txt")
40+
self.assertEqual(info.content, file_content)
41+
self.assertEqual(info.length, len(file_content))
42+
self.assertEqual(info.lines, 2)
43+
self.assertEqual(info.file_type, ".txt")
44+
delete_file("test.txt")
45+
46+
def test_read_line(self):
47+
file_content = "Hello\nWorld!"
48+
create_file("./test.txt", file_content)
49+
line = read_line("test.txt", 1)
50+
self.assertEqual(line, "Hello\n")
51+
line = read_line("test.txt", 2)
52+
self.assertEqual(line, "World!")
53+
delete_file("test.txt")
54+
55+
def test_insert_to_file(self):
56+
file_content = "Hello\nWorld!"
57+
create_file("./test.txt", file_content)
58+
insert_to_file("./test.txt", "Python\n", 1)
59+
with open("./test.txt", "r") as f:
60+
lines = f.readlines()
61+
self.assertEqual(lines[1], "Python\n")
62+
delete_file("test.txt")
63+
64+
def test_modify_line(self):
65+
file_content = "Hello\nWorld!"
66+
create_file("./test.txt", file_content)
67+
modify_line("./test.txt", "Python\n", 0)
68+
with open("./test.txt", "r") as f:
69+
lines = f.readlines()
70+
self.assertEqual(lines[0], "Python\n")
71+
delete_file("test.txt")
72+
73+
def test_replace_string_in_file(self):
74+
file_content = "Hello, Python!"
75+
create_file("./test.txt", file_content)
76+
replace_string_in_file("./test.txt", "Python", "World")
77+
with open("./test.txt", "r") as f:
78+
file_content = f.read()
79+
self.assertEqual(file_content, "Hello, World!")
80+
delete_file("test.txt")
81+
82+
def test_replace_string_in_line(self):
83+
file_content = "Hello,\nPython!"
84+
create_file("./test.txt", file_content)
85+
replace_string_in_line("./test.txt", 1, "Python", "World")
86+
with open("./test.txt", "r") as f:
87+
lines = f.readlines()
88+
self.assertEqual(lines[1], "World!")
89+
delete_file("test.txt")
90+
91+
def test_delete_line(self):
92+
file_content = "Hello,\nPython!"
93+
create_file("./test.txt", file_content)
94+
delete_line("./test.txt", 1)
95+
with open("./test.txt", "r") as f:
96+
lines = f.readlines()
97+
self.assertEqual(len(lines), 1)
98+
delete_file("test.txt")

way3/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
from .file_op.create_file import create_file as create_file # noqa: E402
55
from .file_op.create_file import add_to_file_end as add_to_file_end # noqa: E402
66
from .file_op.create_file import delete_file as delete_file # noqa: E402
7+
from .file_op.edit import insert_to_file as insert_to_file # noqa: E402
8+
from .file_op.edit import modify_line as modify_line # noqa: E402
9+
from .file_op.edit import delete_line as delete_line # noqa: E402
10+
from .file_op.edit import replace_string_in_line as replace_string_in_line # noqa: E402
11+
from .file_op.edit import replace_string_in_file as replace_string_in_file # noqa: E402
712

813

914
# File Find
@@ -27,3 +32,7 @@
2732
from .folder_op.create_folder import create_directory as create_directory # noqa: E402
2833
from .folder_op.create_folder import rename_directory as rename_directory # noqa: E402
2934
from .folder_op.create_folder import delete_directory as delete_directory # noqa: E402
35+
36+
## read file
37+
from .file_op.read import read_file as read_file # noqa: E402
38+
from .file_op.read import read_line as read_line # noqa: E402

way3/file_op/edit.py

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
import os
2+
import logging
3+
from typing import Union
4+
5+
6+
def insert_to_file(
7+
file_name: str,
8+
content: str,
9+
line_number: int = 0,
10+
file_path: Union[str, os.PathLike] = ".",
11+
create_if_not_exist: bool = True,
12+
) -> tuple[bool, str]:
13+
if file_name.startswith(os.sep):
14+
file_path = file_name
15+
else:
16+
file_path = os.path.join(file_path, file_name)
17+
18+
if not create_if_not_exist and not os.path.exists(file_path):
19+
logging.warning("File does not exist: " + file_path)
20+
return False, f"Failure, File does not exist: {file_path}"
21+
22+
with open(file_path, "r") as file:
23+
lines = file.readlines()
24+
25+
if line_number < 0:
26+
line_number += len(lines) + 1
27+
28+
lines.insert(line_number, content + "\n")
29+
30+
with open(file_path, "w") as file:
31+
file.writelines(lines)
32+
33+
return True, os.path.abspath(file_path)
34+
35+
36+
def modify_line(
37+
file_name: str,
38+
content: str,
39+
line_number: int,
40+
file_path: Union[str, os.PathLike] = ".",
41+
) -> tuple[bool, str]:
42+
if file_name.startswith(os.sep):
43+
file_path = file_name
44+
else:
45+
file_path = os.path.join(file_path, file_name)
46+
47+
if not os.path.exists(file_path):
48+
logging.warning("File does not exist: " + file_path)
49+
return False, f"Failure, File does not exist: {file_path}"
50+
51+
with open(file_path, "r") as file:
52+
lines = file.readlines()
53+
54+
if line_number < 0:
55+
line_number += len(lines)
56+
57+
if abs(line_number) > len(lines):
58+
logging.warning("Line number exceeds the number of lines in the file.")
59+
return False, "Failure, Line number exceeds the number of lines in the file."
60+
61+
lines[line_number] = content + "\n"
62+
63+
with open(file_path, "w") as file:
64+
file.writelines(lines)
65+
66+
return True, os.path.abspath(file_path)
67+
68+
69+
def delete_line(
70+
file_name: str,
71+
line_number: int,
72+
file_path: Union[str, os.PathLike] = ".",
73+
) -> tuple[bool, str]:
74+
if file_name.startswith(os.sep):
75+
file_path = file_name
76+
else:
77+
file_path = os.path.join(file_path, file_name)
78+
79+
if not os.path.exists(file_path):
80+
logging.warning("File does not exist: " + file_path)
81+
return False, f"Failure, File does not exist: {file_path}"
82+
83+
with open(file_path, "r") as file:
84+
lines = file.readlines()
85+
86+
if line_number < 0:
87+
line_number += len(lines)
88+
89+
if abs(line_number) > len(lines):
90+
logging.warning("Line number exceeds the number of lines in the file.")
91+
return False, "Failure, Line number exceeds the number of lines in the file."
92+
93+
del lines[line_number]
94+
95+
with open(file_path, "w") as file:
96+
file.writelines(lines)
97+
98+
return True, os.path.abspath(file_path)
99+
100+
101+
def replace_string_in_file(
102+
file_name: str,
103+
old_string: str,
104+
new_string: str,
105+
file_path: Union[str, os.PathLike] = ".",
106+
) -> tuple[bool, str]:
107+
if file_name.startswith(os.sep):
108+
file_path = file_name
109+
else:
110+
file_path = os.path.join(file_path, file_name)
111+
112+
if not os.path.exists(file_path):
113+
logging.warning("File does not exist: " + file_path)
114+
return False, f"Failure, File does not exist: {file_path}"
115+
116+
with open(file_path, "r") as file:
117+
file_content = file.read()
118+
119+
file_content = file_content.replace(old_string, new_string)
120+
121+
with open(file_path, "w") as file:
122+
file.write(file_content)
123+
124+
return True, os.path.abspath(file_path)
125+
126+
127+
def replace_string_in_line(
128+
file_name: str,
129+
line_number: int,
130+
old_string: str,
131+
new_string: str,
132+
file_path: Union[str, os.PathLike] = ".",
133+
) -> tuple[bool, str]:
134+
if file_name.startswith(os.sep):
135+
file_path = file_name
136+
else:
137+
file_path = os.path.join(file_path, file_name)
138+
139+
if not os.path.exists(file_path):
140+
logging.warning("File does not exist: " + file_path)
141+
return False, f"Failure, File does not exist: {file_path}"
142+
143+
with open(file_path, "r") as file:
144+
lines = file.readlines()
145+
146+
if line_number < 0:
147+
line_number += len(lines)
148+
149+
if abs(line_number) > len(lines):
150+
logging.warning("Line number exceeds the number of lines in the file.")
151+
return False, "Failure, Line number exceeds the number of lines in the file."
152+
153+
lines[line_number] = lines[line_number].replace(old_string, new_string)
154+
155+
with open(file_path, "w") as file:
156+
file.writelines(lines)
157+
158+
return True, os.path.abspath(file_path)

way3/file_op/read.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from typing import Union, NamedTuple
2+
import os
3+
import logging
4+
5+
6+
class FileInfo(NamedTuple):
7+
content: str
8+
length: int
9+
lines: int
10+
file_type: str
11+
12+
13+
def read_file(file_name: str, file_path: Union[str, os.PathLike] = ".") -> FileInfo:
14+
if file_name.startswith(os.sep):
15+
file_path = file_name
16+
else:
17+
file_path = os.path.join(file_path, file_name)
18+
19+
if not os.path.exists(file_path):
20+
logging.warning("File does not exist: " + file_path)
21+
return FileInfo(content="", length=0, lines=0, file_type="")
22+
23+
with open(file_path, "r") as file:
24+
content = file.read()
25+
length = len(content)
26+
lines = content.count("\n") + 1
27+
file_type = os.path.splitext(file_path)[1]
28+
29+
return FileInfo(content=content, length=length, lines=lines, file_type=file_type)
30+
31+
32+
def read_line(
33+
file_name: str, line_number: int, file_path: Union[str, os.PathLike] = "."
34+
) -> str:
35+
if file_name.startswith(os.sep):
36+
file_path = file_name
37+
else:
38+
file_path = os.path.join(file_path, file_name)
39+
40+
if not os.path.exists(file_path):
41+
logging.warning("File does not exist: " + file_path)
42+
return ""
43+
44+
with open(file_path, "r") as file:
45+
for _ in range(line_number - 1):
46+
file.readline()
47+
return file.readline()

0 commit comments

Comments
 (0)