d
This commit is contained in:
@@ -6,6 +6,7 @@ import re
|
||||
from pathlib import Path
|
||||
from typing import Any, Dict, Optional
|
||||
from SYS.logger import log
|
||||
from SYS.utils import expand_path
|
||||
|
||||
DEFAULT_CONFIG_FILENAME = "config.conf"
|
||||
SCRIPT_DIR = Path(__file__).resolve().parent
|
||||
@@ -13,6 +14,11 @@ SCRIPT_DIR = Path(__file__).resolve().parent
|
||||
_CONFIG_CACHE: Dict[str, Dict[str, Any]] = {}
|
||||
|
||||
|
||||
def clear_config_cache() -> None:
|
||||
"""Clear the configuration cache."""
|
||||
_CONFIG_CACHE.clear()
|
||||
|
||||
|
||||
def _strip_inline_comment(line: str) -> str:
|
||||
# Strip comments in a way that's friendly to common .conf usage:
|
||||
# - Full-line comments starting with '#' or ';'
|
||||
@@ -438,7 +444,7 @@ def resolve_output_dir(config: Dict[str, Any]) -> Path:
|
||||
temp_value = config.get("temp")
|
||||
if temp_value:
|
||||
try:
|
||||
path = Path(str(temp_value)).expanduser()
|
||||
path = expand_path(temp_value)
|
||||
# Verify we can access it (not a system directory with permission issues)
|
||||
if path.exists() or path.parent.exists():
|
||||
return path
|
||||
@@ -449,7 +455,7 @@ def resolve_output_dir(config: Dict[str, Any]) -> Path:
|
||||
outfile_value = config.get("outfile")
|
||||
if outfile_value:
|
||||
try:
|
||||
return Path(str(outfile_value)).expanduser()
|
||||
return expand_path(outfile_value)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
@@ -480,7 +486,7 @@ def get_local_storage_path(config: Dict[str, Any]) -> Optional[Path]:
|
||||
if isinstance(default_config, dict):
|
||||
path_str = default_config.get("path")
|
||||
if path_str:
|
||||
return Path(str(path_str)).expanduser()
|
||||
return expand_path(path_str)
|
||||
|
||||
# Fall back to storage.local.path format
|
||||
storage = config.get("storage", {})
|
||||
@@ -489,14 +495,14 @@ def get_local_storage_path(config: Dict[str, Any]) -> Optional[Path]:
|
||||
if isinstance(local_config, dict):
|
||||
path_str = local_config.get("path")
|
||||
if path_str:
|
||||
return Path(str(path_str)).expanduser()
|
||||
return expand_path(path_str)
|
||||
|
||||
# Fall back to old Local format
|
||||
local_config = config.get("Local", {})
|
||||
if isinstance(local_config, dict):
|
||||
path_str = local_config.get("path")
|
||||
if path_str:
|
||||
return Path(str(path_str)).expanduser()
|
||||
return expand_path(path_str)
|
||||
|
||||
return None
|
||||
|
||||
@@ -606,9 +612,9 @@ def resolve_cookies_path(
|
||||
for value in values:
|
||||
if not value:
|
||||
continue
|
||||
candidate = Path(str(value)).expanduser()
|
||||
candidate = expand_path(value)
|
||||
if not candidate.is_absolute():
|
||||
candidate = (base_dir / candidate).expanduser()
|
||||
candidate = expand_path(base_dir / candidate)
|
||||
if candidate.is_file():
|
||||
return candidate
|
||||
|
||||
@@ -622,7 +628,7 @@ def resolve_debug_log(config: Dict[str, Any]) -> Optional[Path]:
|
||||
value = config.get("download_debug_log")
|
||||
if not value:
|
||||
return None
|
||||
path = Path(str(value)).expanduser()
|
||||
path = expand_path(value)
|
||||
if not path.is_absolute():
|
||||
path = Path.cwd() / path
|
||||
return path
|
||||
|
||||
11
SYS/utils.py
11
SYS/utils.py
@@ -11,11 +11,12 @@ try:
|
||||
import ffmpeg # type: ignore
|
||||
except Exception:
|
||||
ffmpeg = None # type: ignore
|
||||
import os
|
||||
import base64
|
||||
import logging
|
||||
import time
|
||||
from pathlib import Path
|
||||
from typing import Any, Iterable
|
||||
from typing import Any, Iterable, Optional
|
||||
from datetime import datetime
|
||||
from dataclasses import dataclass, field
|
||||
from fnmatch import fnmatch
|
||||
@@ -32,6 +33,14 @@ CHUNK_SIZE = 1024 * 1024 # 1 MiB
|
||||
_format_logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def expand_path(p: str | Path | None) -> Path:
|
||||
"""Expand ~ and environment variables in path."""
|
||||
if p is None:
|
||||
return None # type: ignore
|
||||
expanded = os.path.expandvars(str(p))
|
||||
return Path(expanded).expanduser()
|
||||
|
||||
|
||||
def ensure_directory(path: Path) -> None:
|
||||
"""Ensure *path* exists as a directory."""
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user