Skip to content

Commit

Permalink
fix: finish ai edit file
Browse files Browse the repository at this point in the history
  • Loading branch information
aboutmydreams committed May 19, 2024
1 parent 07fa689 commit 7f12111
Show file tree
Hide file tree
Showing 5 changed files with 290 additions and 2 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="way3",
version="0.0.29",
version="1.0.1",
author="aboutmydreams",
author_email="[email protected]",
description="Simplified file path management for Python developers",
Expand Down
76 changes: 75 additions & 1 deletion tests/test_file_op.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
from way3 import create_file, delete_file
from way3 import (
create_file,
delete_file,
read_file,
read_line,
insert_to_file,
modify_line,
replace_string_in_file,
replace_string_in_line,
delete_line,
)

import unittest
import os
Expand All @@ -22,3 +32,67 @@ def test_delete_file(self):
self.assertEqual(
os.path.isfile("test.txt"), False, "result fail: expected False"
)

def test_read_file(self):
file_content = "Hello\nWorld!"
create_file("./test.txt", file_content)
info = read_file("test.txt")
self.assertEqual(info.content, file_content)
self.assertEqual(info.length, len(file_content))
self.assertEqual(info.lines, 2)
self.assertEqual(info.file_type, ".txt")
delete_file("test.txt")

def test_read_line(self):
file_content = "Hello\nWorld!"
create_file("./test.txt", file_content)
line = read_line("test.txt", 1)
self.assertEqual(line, "Hello\n")
line = read_line("test.txt", 2)
self.assertEqual(line, "World!")
delete_file("test.txt")

def test_insert_to_file(self):
file_content = "Hello\nWorld!"
create_file("./test.txt", file_content)
insert_to_file("./test.txt", "Python\n", 1)
with open("./test.txt", "r") as f:
lines = f.readlines()
self.assertEqual(lines[1], "Python\n")
delete_file("test.txt")

def test_modify_line(self):
file_content = "Hello\nWorld!"
create_file("./test.txt", file_content)
modify_line("./test.txt", "Python\n", 0)
with open("./test.txt", "r") as f:
lines = f.readlines()
self.assertEqual(lines[0], "Python\n")
delete_file("test.txt")

def test_replace_string_in_file(self):
file_content = "Hello, Python!"
create_file("./test.txt", file_content)
replace_string_in_file("./test.txt", "Python", "World")
with open("./test.txt", "r") as f:
file_content = f.read()
self.assertEqual(file_content, "Hello, World!")
delete_file("test.txt")

def test_replace_string_in_line(self):
file_content = "Hello,\nPython!"
create_file("./test.txt", file_content)
replace_string_in_line("./test.txt", 1, "Python", "World")
with open("./test.txt", "r") as f:
lines = f.readlines()
self.assertEqual(lines[1], "World!")
delete_file("test.txt")

def test_delete_line(self):
file_content = "Hello,\nPython!"
create_file("./test.txt", file_content)
delete_line("./test.txt", 1)
with open("./test.txt", "r") as f:
lines = f.readlines()
self.assertEqual(len(lines), 1)
delete_file("test.txt")
9 changes: 9 additions & 0 deletions way3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
from .file_op.create_file import create_file as create_file # noqa: E402
from .file_op.create_file import add_to_file_end as add_to_file_end # noqa: E402
from .file_op.create_file import delete_file as delete_file # noqa: E402
from .file_op.edit import insert_to_file as insert_to_file # noqa: E402
from .file_op.edit import modify_line as modify_line # noqa: E402
from .file_op.edit import delete_line as delete_line # noqa: E402
from .file_op.edit import replace_string_in_line as replace_string_in_line # noqa: E402
from .file_op.edit import replace_string_in_file as replace_string_in_file # noqa: E402


# File Find
Expand All @@ -27,3 +32,7 @@
from .folder_op.create_folder import create_directory as create_directory # noqa: E402
from .folder_op.create_folder import rename_directory as rename_directory # noqa: E402
from .folder_op.create_folder import delete_directory as delete_directory # noqa: E402

## read file
from .file_op.read import read_file as read_file # noqa: E402
from .file_op.read import read_line as read_line # noqa: E402
158 changes: 158 additions & 0 deletions way3/file_op/edit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
import os
import logging
from typing import Union


def insert_to_file(
file_name: str,
content: str,
line_number: int = 0,
file_path: Union[str, os.PathLike] = ".",
create_if_not_exist: bool = True,
) -> tuple[bool, str]:
if file_name.startswith(os.sep):
file_path = file_name
else:
file_path = os.path.join(file_path, file_name)

if not create_if_not_exist and not os.path.exists(file_path):
logging.warning("File does not exist: " + file_path)
return False, f"Failure, File does not exist: {file_path}"

with open(file_path, "r") as file:
lines = file.readlines()

if line_number < 0:
line_number += len(lines) + 1

lines.insert(line_number, content + "\n")

with open(file_path, "w") as file:
file.writelines(lines)

return True, os.path.abspath(file_path)


def modify_line(
file_name: str,
content: str,
line_number: int,
file_path: Union[str, os.PathLike] = ".",
) -> tuple[bool, str]:
if file_name.startswith(os.sep):
file_path = file_name
else:
file_path = os.path.join(file_path, file_name)

if not os.path.exists(file_path):
logging.warning("File does not exist: " + file_path)
return False, f"Failure, File does not exist: {file_path}"

with open(file_path, "r") as file:
lines = file.readlines()

if line_number < 0:
line_number += len(lines)

if abs(line_number) > len(lines):
logging.warning("Line number exceeds the number of lines in the file.")
return False, "Failure, Line number exceeds the number of lines in the file."

lines[line_number] = content + "\n"

with open(file_path, "w") as file:
file.writelines(lines)

return True, os.path.abspath(file_path)


def delete_line(
file_name: str,
line_number: int,
file_path: Union[str, os.PathLike] = ".",
) -> tuple[bool, str]:
if file_name.startswith(os.sep):
file_path = file_name
else:
file_path = os.path.join(file_path, file_name)

if not os.path.exists(file_path):
logging.warning("File does not exist: " + file_path)
return False, f"Failure, File does not exist: {file_path}"

with open(file_path, "r") as file:
lines = file.readlines()

if line_number < 0:
line_number += len(lines)

if abs(line_number) > len(lines):
logging.warning("Line number exceeds the number of lines in the file.")
return False, "Failure, Line number exceeds the number of lines in the file."

del lines[line_number]

with open(file_path, "w") as file:
file.writelines(lines)

return True, os.path.abspath(file_path)


def replace_string_in_file(
file_name: str,
old_string: str,
new_string: str,
file_path: Union[str, os.PathLike] = ".",
) -> tuple[bool, str]:
if file_name.startswith(os.sep):
file_path = file_name
else:
file_path = os.path.join(file_path, file_name)

if not os.path.exists(file_path):
logging.warning("File does not exist: " + file_path)
return False, f"Failure, File does not exist: {file_path}"

with open(file_path, "r") as file:
file_content = file.read()

file_content = file_content.replace(old_string, new_string)

with open(file_path, "w") as file:
file.write(file_content)

return True, os.path.abspath(file_path)


def replace_string_in_line(
file_name: str,
line_number: int,
old_string: str,
new_string: str,
file_path: Union[str, os.PathLike] = ".",
) -> tuple[bool, str]:
if file_name.startswith(os.sep):
file_path = file_name
else:
file_path = os.path.join(file_path, file_name)

if not os.path.exists(file_path):
logging.warning("File does not exist: " + file_path)
return False, f"Failure, File does not exist: {file_path}"

with open(file_path, "r") as file:
lines = file.readlines()

if line_number < 0:
line_number += len(lines)

if abs(line_number) > len(lines):
logging.warning("Line number exceeds the number of lines in the file.")
return False, "Failure, Line number exceeds the number of lines in the file."

lines[line_number] = lines[line_number].replace(old_string, new_string)

with open(file_path, "w") as file:
file.writelines(lines)

return True, os.path.abspath(file_path)
47 changes: 47 additions & 0 deletions way3/file_op/read.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from typing import Union, NamedTuple
import os
import logging


class FileInfo(NamedTuple):
content: str
length: int
lines: int
file_type: str


def read_file(file_name: str, file_path: Union[str, os.PathLike] = ".") -> FileInfo:
if file_name.startswith(os.sep):
file_path = file_name
else:
file_path = os.path.join(file_path, file_name)

if not os.path.exists(file_path):
logging.warning("File does not exist: " + file_path)
return FileInfo(content="", length=0, lines=0, file_type="")

with open(file_path, "r") as file:
content = file.read()
length = len(content)
lines = content.count("\n") + 1
file_type = os.path.splitext(file_path)[1]

return FileInfo(content=content, length=length, lines=lines, file_type=file_type)


def read_line(
file_name: str, line_number: int, file_path: Union[str, os.PathLike] = "."
) -> str:
if file_name.startswith(os.sep):
file_path = file_name
else:
file_path = os.path.join(file_path, file_name)

if not os.path.exists(file_path):
logging.warning("File does not exist: " + file_path)
return ""

with open(file_path, "r") as file:
for _ in range(line_number - 1):
file.readline()
return file.readline()

0 comments on commit 7f12111

Please sign in to comment.