Refactor code structure for improved readability and maintainability
This commit is contained in:
BIN
dist/renamer-0.5.9-py3-none-any.whl
vendored
Normal file
BIN
dist/renamer-0.5.9-py3-none-any.whl
vendored
Normal file
Binary file not shown.
@@ -1,6 +1,6 @@
|
|||||||
[project]
|
[project]
|
||||||
name = "renamer"
|
name = "renamer"
|
||||||
version = "0.5.8"
|
version = "0.5.9"
|
||||||
description = "Terminal-based media file renamer and metadata viewer"
|
description = "Terminal-based media file renamer and metadata viewer"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
requires-python = ">=3.11"
|
requires-python = ">=3.11"
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ if os.getenv('FORMATTER_LOG', '0') == '1':
|
|||||||
logging.basicConfig(filename='formatter.log', level=logging.INFO,
|
logging.basicConfig(filename='formatter.log', level=logging.INFO,
|
||||||
format='%(asctime)s - %(levelname)s - %(message)s')
|
format='%(asctime)s - %(levelname)s - %(message)s')
|
||||||
else:
|
else:
|
||||||
logging.basicConfig(level=logging.CRITICAL) # Disable logging
|
logging.basicConfig(level=logging.INFO) # Enable logging for debugging
|
||||||
|
|
||||||
|
|
||||||
class RenamerApp(App):
|
class RenamerApp(App):
|
||||||
|
|||||||
@@ -26,7 +26,8 @@ class ProposedNameFormatter:
|
|||||||
return self.rename_line()
|
return self.rename_line()
|
||||||
|
|
||||||
def rename_line(self) -> str:
|
def rename_line(self) -> str:
|
||||||
return f"{self.__order}{self.__title} {self.__year}{self.__special_info}{self.__source} [{self.__frame_class}{self.__hdr},{self.__audio_langs}]{self.__db_info}.{self.__extension}"
|
result = f"{self.__order}{self.__title} {self.__year}{self.__special_info}{self.__source} [{self.__frame_class}{self.__hdr},{self.__audio_langs}]{self.__db_info}.{self.__extension}"
|
||||||
|
return result.replace("/", "-").replace("\\", "-")
|
||||||
|
|
||||||
def rename_line_formatted(self, file_path) -> str:
|
def rename_line_formatted(self, file_path) -> str:
|
||||||
"""Format the proposed name for display with color"""
|
"""Format the proposed name for display with color"""
|
||||||
|
|||||||
@@ -129,8 +129,8 @@ class RenameConfirmScreen(Screen):
|
|||||||
def __init__(self, old_path: Path, new_name: str):
|
def __init__(self, old_path: Path, new_name: str):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.old_path = old_path
|
self.old_path = old_path
|
||||||
self.new_name = new_name
|
self.new_name = new_name.replace("/", "-").replace("\\", "-")
|
||||||
self.new_path = old_path.parent / new_name
|
self.new_path = old_path.parent / self.new_name
|
||||||
self.was_edited = False
|
self.was_edited = False
|
||||||
|
|
||||||
def compose(self):
|
def compose(self):
|
||||||
@@ -178,12 +178,19 @@ Do you want to proceed with renaming?
|
|||||||
def on_button_pressed(self, event):
|
def on_button_pressed(self, event):
|
||||||
if event.button.id == "rename":
|
if event.button.id == "rename":
|
||||||
try:
|
try:
|
||||||
logging.info(f"Renaming {self.old_path} to {self.new_path}")
|
logging.info(f"Starting rename: old_path={self.old_path}, new_path={self.new_path}")
|
||||||
|
logging.info(f"Old file name: {self.old_path.name}")
|
||||||
|
logging.info(f"New file name: {self.new_name}")
|
||||||
|
logging.info(f"New path parent: {self.new_path.parent}, Old path parent: {self.old_path.parent}")
|
||||||
|
if "/" in self.new_name or "\\" in self.new_name:
|
||||||
|
logging.warning(f"New name contains path separators: {self.new_name}")
|
||||||
self.old_path.rename(self.new_path)
|
self.old_path.rename(self.new_path)
|
||||||
|
logging.info(f"Rename successful: {self.old_path} -> {self.new_path}")
|
||||||
# Update the tree node
|
# Update the tree node
|
||||||
self.app.update_renamed_file(self.old_path, self.new_path) # type: ignore
|
self.app.update_renamed_file(self.old_path, self.new_path) # type: ignore
|
||||||
self.app.pop_screen()
|
self.app.pop_screen()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
logging.error(f"Rename failed: {self.old_path} -> {self.new_path}, error: {str(e)}")
|
||||||
# Show error
|
# Show error
|
||||||
content = self.query_one("#confirm_content", Static)
|
content = self.query_one("#confirm_content", Static)
|
||||||
content.update(f"Error renaming file: {str(e)}")
|
content.update(f"Error renaming file: {str(e)}")
|
||||||
@@ -228,12 +235,19 @@ Do you want to proceed with renaming?
|
|||||||
if event.key == "y":
|
if event.key == "y":
|
||||||
# Trigger rename
|
# Trigger rename
|
||||||
try:
|
try:
|
||||||
logging.info(f"Hotkey renaming {self.old_path} to {self.new_path}")
|
logging.info(f"Hotkey rename: old_path={self.old_path}, new_path={self.new_path}")
|
||||||
|
logging.info(f"Old file name: {self.old_path.name}")
|
||||||
|
logging.info(f"New file name: {self.new_name}")
|
||||||
|
logging.info(f"New path parent: {self.new_path.parent}, Old path parent: {self.old_path.parent}")
|
||||||
|
if "/" in self.new_name or "\\" in self.new_name:
|
||||||
|
logging.warning(f"New name contains path separators: {self.new_name}")
|
||||||
self.old_path.rename(self.new_path)
|
self.old_path.rename(self.new_path)
|
||||||
|
logging.info(f"Hotkey rename successful: {self.old_path} -> {self.new_path}")
|
||||||
# Update the tree node
|
# Update the tree node
|
||||||
self.app.update_renamed_file(self.old_path, self.new_path) # type: ignore
|
self.app.update_renamed_file(self.old_path, self.new_path) # type: ignore
|
||||||
self.app.pop_screen()
|
self.app.pop_screen()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
logging.error(f"Hotkey rename failed: {self.old_path} -> {self.new_path}, error: {str(e)}")
|
||||||
# Show error
|
# Show error
|
||||||
content = self.query_one("#confirm_content", Static)
|
content = self.query_one("#confirm_content", Static)
|
||||||
content.update(f"Error renaming file: {str(e)}")
|
content.update(f"Error renaming file: {str(e)}")
|
||||||
|
|||||||
Reference in New Issue
Block a user