83 lines
4.3 KiB
Python
83 lines
4.3 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Path mapping utilities for NFOGuard
|
|
Handles conversion between external service paths and container paths
|
|
"""
|
|
import os
|
|
import re
|
|
from pathlib import Path
|
|
|
|
class PathMapper:
|
|
"""Handles path mapping between different environments"""
|
|
|
|
def __init__(self):
|
|
# Load path mappings from environment variables
|
|
self.radarr_roots = self._parse_path_list(os.environ.get("RADARR_ROOT_FOLDERS", ""))
|
|
self.sonarr_roots = self._parse_path_list(os.environ.get("SONARR_ROOT_FOLDERS", ""))
|
|
self.movie_paths = self._parse_path_list(os.environ.get("MOVIE_PATHS", ""))
|
|
self.tv_paths = self._parse_path_list(os.environ.get("TV_PATHS", ""))
|
|
|
|
def _parse_path_list(self, path_string: str) -> list:
|
|
"""Parse comma-separated path string into list"""
|
|
if not path_string:
|
|
return []
|
|
return [p.strip() for p in path_string.split(",") if p.strip()]
|
|
|
|
def sonarr_path_to_container_path(self, sonarr_path: str) -> str:
|
|
"""Convert Sonarr path to container path using environment mappings"""
|
|
print(f"DEBUG: sonarr_path_to_container_path input: {sonarr_path}")
|
|
print(f"DEBUG: sonarr_roots: {self.sonarr_roots}")
|
|
print(f"DEBUG: tv_paths: {self.tv_paths}")
|
|
|
|
# Sort roots by length (longest first) to avoid substring matching issues
|
|
indexed_roots = [(i, root) for i, root in enumerate(self.sonarr_roots)]
|
|
indexed_roots.sort(key=lambda x: len(x[1]), reverse=True)
|
|
|
|
# Try to match against configured Sonarr root folders (longest first)
|
|
for original_index, sonarr_root in indexed_roots:
|
|
print(f"DEBUG: Checking sonarr_root[{original_index}]: {sonarr_root}")
|
|
if sonarr_path.startswith(sonarr_root + '/') or sonarr_path == sonarr_root:
|
|
print(f"DEBUG: Match found! Index {original_index}")
|
|
# Map to corresponding TV path
|
|
if original_index < len(self.tv_paths):
|
|
container_root = self.tv_paths[original_index]
|
|
relative_path = sonarr_path[len(sonarr_root):].lstrip('/')
|
|
result = str(Path(container_root) / relative_path) if relative_path else container_root
|
|
print(f"DEBUG: Mapped to: {result}")
|
|
return result
|
|
|
|
print(f"DEBUG: No match found, returning original: {sonarr_path}")
|
|
# No fallback - if path mapping fails, return original and let validation catch it
|
|
return sonarr_path
|
|
|
|
def radarr_path_to_container_path(self, radarr_path: str) -> str:
|
|
"""Convert Radarr path to container path using environment mappings"""
|
|
print(f"DEBUG: radarr_path_to_container_path input: {radarr_path}")
|
|
print(f"DEBUG: radarr_roots: {self.radarr_roots}")
|
|
print(f"DEBUG: movie_paths: {self.movie_paths}")
|
|
|
|
# Sort roots by length (longest first) to avoid substring matching issues
|
|
indexed_roots = [(i, root) for i, root in enumerate(self.radarr_roots)]
|
|
indexed_roots.sort(key=lambda x: len(x[1]), reverse=True)
|
|
|
|
# Try to match against configured Radarr root folders (longest first)
|
|
for original_index, radarr_root in indexed_roots:
|
|
print(f"DEBUG: Checking radarr_root[{original_index}]: {radarr_root}")
|
|
if radarr_path.startswith(radarr_root + '/') or radarr_path == radarr_root:
|
|
print(f"DEBUG: Match found! Index {original_index}")
|
|
# Map to corresponding movie path
|
|
if original_index < len(self.movie_paths):
|
|
container_root = self.movie_paths[original_index]
|
|
relative_path = radarr_path[len(radarr_root):].lstrip('/')
|
|
result = str(Path(container_root) / relative_path) if relative_path else container_root
|
|
print(f"DEBUG: Mapped to: {result}")
|
|
return result
|
|
|
|
print(f"DEBUG: No match found, returning original: {radarr_path}")
|
|
# No fallback - if path mapping fails, return original and let validation catch it
|
|
return radarr_path
|
|
|
|
def container_path_to_host_path(self, container_path: str) -> str:
|
|
"""Convert container path back to host path if needed"""
|
|
# This might be needed for file operations
|
|
return container_path |