Improve save_path handling and use ConfigManager
Normalize path input to string, ensure parent directories are created (mkdir with parents=True), and check writability using the normalized path. Replace manual JSON file write with ConfigManager.set("download_path", ...) and add import for ConfigManager. Adjust logging to use logger.error for failures and replace logger.exception where appropriate. These changes improve robustness when saving the download path and centralize config persistence.
This commit is contained in:
@@ -402,25 +402,26 @@ def load_saved_path(main_window_instance: Any) -> None:
|
|||||||
main_window_instance.last_path = tempfile.gettempdir()
|
main_window_instance.last_path = tempfile.gettempdir()
|
||||||
|
|
||||||
|
|
||||||
|
from ..utils.ytsage_config_manager import ConfigManager
|
||||||
|
|
||||||
def save_path(main_window_instance: Any, path: Union[str, Path]) -> bool:
|
def save_path(main_window_instance: Any, path: Union[str, Path]) -> bool:
|
||||||
"""Save download path with enhanced error handling."""
|
"""Save download path with enhanced error handling."""
|
||||||
try:
|
try:
|
||||||
# Verify the path is valid and writable
|
# Verify the path is valid and writable
|
||||||
if not Path(path).exists():
|
path_str = str(path)
|
||||||
|
if not Path(path_str).exists():
|
||||||
try:
|
try:
|
||||||
Path(path).mkdir(exist_ok=True)
|
Path(path_str).mkdir(parents=True, exist_ok=True)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.exception(f"Error creating directory: {e}")
|
logger.error(f"Error creating directory: {e}")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if not os.access(path, os.W_OK):
|
if not os.access(path_str, os.W_OK):
|
||||||
logger.info("Path is not writable")
|
logger.error("Path is not writable")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# Save the config
|
# Save the config using ConfigManager
|
||||||
config = {"download_path": path}
|
ConfigManager.set("download_path", path_str)
|
||||||
with open(APP_CONFIG_FILE, "w", encoding="utf-8") as f:
|
|
||||||
json.dump(config, f, ensure_ascii=False)
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|||||||
Reference in New Issue
Block a user