generated from aboutmydreams/quick-pylib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
07fa689
commit 7f12111
Showing
5 changed files
with
290 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |