New archi: domain driven development

Working but need to check out code
This commit is contained in:
2025-12-01 07:10:03 +01:00
parent 2b815502f6
commit 2c8cdd3ab1
73 changed files with 4084 additions and 853 deletions
+1
View File
@@ -0,0 +1 @@
"""Persistence layer - Data storage implementations."""
@@ -0,0 +1,10 @@
"""JSON-based repository implementations."""
from .movie_repository import JsonMovieRepository
from .tvshow_repository import JsonTVShowRepository
from .subtitle_repository import JsonSubtitleRepository
__all__ = [
"JsonMovieRepository",
"JsonTVShowRepository",
"JsonSubtitleRepository",
]
@@ -0,0 +1,115 @@
"""JSON-based movie repository implementation."""
from typing import List, Optional, Dict, Any
import logging
from domain.movies.repositories import MovieRepository
from domain.movies.entities import Movie
from domain.shared.value_objects import ImdbId
from ..memory import Memory
logger = logging.getLogger(__name__)
class JsonMovieRepository(MovieRepository):
"""
JSON-based implementation of MovieRepository.
Stores movies in the memory.json file.
"""
def __init__(self, memory: Memory):
"""
Initialize repository.
Args:
memory: Memory instance for persistence
"""
self.memory = memory
def save(self, movie: Movie) -> None:
"""Save a movie to the repository."""
movies = self._load_all()
# Remove existing movie with same IMDb ID
movies = [m for m in movies if m.get('imdb_id') != str(movie.imdb_id)]
# Add new movie
movies.append(self._to_dict(movie))
# Save to memory
self.memory.set('movies', movies)
logger.debug(f"Saved movie: {movie.imdb_id}")
def find_by_imdb_id(self, imdb_id: ImdbId) -> Optional[Movie]:
"""Find a movie by its IMDb ID."""
movies = self._load_all()
for movie_dict in movies:
if movie_dict.get('imdb_id') == str(imdb_id):
return self._from_dict(movie_dict)
return None
def find_all(self) -> List[Movie]:
"""Get all movies in the repository."""
movies_dict = self._load_all()
return [self._from_dict(m) for m in movies_dict]
def delete(self, imdb_id: ImdbId) -> bool:
"""Delete a movie from the repository."""
movies = self._load_all()
initial_count = len(movies)
# Filter out the movie
movies = [m for m in movies if m.get('imdb_id') != str(imdb_id)]
if len(movies) < initial_count:
self.memory.set('movies', movies)
logger.debug(f"Deleted movie: {imdb_id}")
return True
return False
def exists(self, imdb_id: ImdbId) -> bool:
"""Check if a movie exists in the repository."""
return self.find_by_imdb_id(imdb_id) is not None
def _load_all(self) -> List[Dict[str, Any]]:
"""Load all movies from memory."""
return self.memory.get('movies', [])
def _to_dict(self, movie: Movie) -> Dict[str, Any]:
"""Convert Movie entity to dict for storage."""
return {
'imdb_id': str(movie.imdb_id),
'title': movie.title.value,
'release_year': movie.release_year.value if movie.release_year else None,
'quality': movie.quality.value,
'file_path': str(movie.file_path) if movie.file_path else None,
'file_size': movie.file_size.bytes if movie.file_size else None,
'tmdb_id': movie.tmdb_id,
'overview': movie.overview,
'poster_path': movie.poster_path,
'vote_average': movie.vote_average,
'added_at': movie.added_at.isoformat(),
}
def _from_dict(self, data: Dict[str, Any]) -> Movie:
"""Convert dict from storage to Movie entity."""
from domain.movies.value_objects import MovieTitle, ReleaseYear, Quality
from domain.shared.value_objects import FilePath, FileSize
from datetime import datetime
return Movie(
imdb_id=ImdbId(data['imdb_id']),
title=MovieTitle(data['title']),
release_year=ReleaseYear(data['release_year']) if data.get('release_year') else None,
quality=Quality(data.get('quality', 'unknown')),
file_path=FilePath(data['file_path']) if data.get('file_path') else None,
file_size=FileSize(data['file_size']) if data.get('file_size') else None,
tmdb_id=data.get('tmdb_id'),
overview=data.get('overview'),
poster_path=data.get('poster_path'),
vote_average=data.get('vote_average'),
added_at=datetime.fromisoformat(data['added_at']) if data.get('added_at') else datetime.now(),
)
@@ -0,0 +1,127 @@
"""JSON-based subtitle repository implementation."""
from typing import List, Optional, Dict, Any
import logging
from domain.subtitles.repositories import SubtitleRepository
from domain.subtitles.entities import Subtitle
from domain.subtitles.value_objects import Language, SubtitleFormat, TimingOffset
from domain.shared.value_objects import ImdbId, FilePath
from ..memory import Memory
logger = logging.getLogger(__name__)
class JsonSubtitleRepository(SubtitleRepository):
"""
JSON-based implementation of SubtitleRepository.
Stores subtitles in the memory.json file.
"""
def __init__(self, memory: Memory):
"""
Initialize repository.
Args:
memory: Memory instance for persistence
"""
self.memory = memory
def save(self, subtitle: Subtitle) -> None:
"""Save a subtitle to the repository."""
subtitles = self._load_all()
# Add new subtitle (we allow multiple subtitles for same media)
subtitles.append(self._to_dict(subtitle))
# Save to memory
self.memory.set('subtitles', subtitles)
logger.debug(f"Saved subtitle for: {subtitle.media_imdb_id}")
def find_by_media(
self,
media_imdb_id: ImdbId,
language: Optional[Language] = None,
season: Optional[int] = None,
episode: Optional[int] = None
) -> List[Subtitle]:
"""Find subtitles for a media item."""
subtitles = self._load_all()
results = []
for sub_dict in subtitles:
# Filter by IMDb ID
if sub_dict.get('media_imdb_id') != str(media_imdb_id):
continue
# Filter by language if specified
if language and sub_dict.get('language') != language.value:
continue
# Filter by season/episode if specified
if season is not None and sub_dict.get('season_number') != season:
continue
if episode is not None and sub_dict.get('episode_number') != episode:
continue
results.append(self._from_dict(sub_dict))
return results
def delete(self, subtitle: Subtitle) -> bool:
"""Delete a subtitle from the repository."""
subtitles = self._load_all()
initial_count = len(subtitles)
# Filter out the subtitle (match by file path)
subtitles = [
s for s in subtitles
if s.get('file_path') != str(subtitle.file_path)
]
if len(subtitles) < initial_count:
self.memory.set('subtitles', subtitles)
logger.debug(f"Deleted subtitle: {subtitle.file_path}")
return True
return False
def _load_all(self) -> List[Dict[str, Any]]:
"""Load all subtitles from memory."""
return self.memory.get('subtitles', [])
def _to_dict(self, subtitle: Subtitle) -> Dict[str, Any]:
"""Convert Subtitle entity to dict for storage."""
return {
'media_imdb_id': str(subtitle.media_imdb_id),
'language': subtitle.language.value,
'format': subtitle.format.value,
'file_path': str(subtitle.file_path),
'season_number': subtitle.season_number,
'episode_number': subtitle.episode_number,
'timing_offset': subtitle.timing_offset.milliseconds,
'hearing_impaired': subtitle.hearing_impaired,
'forced': subtitle.forced,
'source': subtitle.source,
'uploader': subtitle.uploader,
'download_count': subtitle.download_count,
'rating': subtitle.rating,
}
def _from_dict(self, data: Dict[str, Any]) -> Subtitle:
"""Convert dict from storage to Subtitle entity."""
return Subtitle(
media_imdb_id=ImdbId(data['media_imdb_id']),
language=Language.from_code(data['language']),
format=SubtitleFormat.from_extension(data['format']),
file_path=FilePath(data['file_path']),
season_number=data.get('season_number'),
episode_number=data.get('episode_number'),
timing_offset=TimingOffset(data.get('timing_offset', 0)),
hearing_impaired=data.get('hearing_impaired', False),
forced=data.get('forced', False),
source=data.get('source'),
uploader=data.get('uploader'),
download_count=data.get('download_count'),
rating=data.get('rating'),
)
@@ -0,0 +1,112 @@
"""JSON-based TV show repository implementation."""
from typing import List, Optional, Dict, Any
import logging
from domain.tv_shows.repositories import TVShowRepository
from domain.tv_shows.entities import TVShow
from domain.tv_shows.value_objects import ShowStatus
from domain.shared.value_objects import ImdbId
from ..memory import Memory
logger = logging.getLogger(__name__)
class JsonTVShowRepository(TVShowRepository):
"""
JSON-based implementation of TVShowRepository.
Stores TV shows in the memory.json file (compatible with existing tv_shows structure).
"""
def __init__(self, memory: Memory):
"""
Initialize repository.
Args:
memory: Memory instance for persistence
"""
self.memory = memory
def save(self, show: TVShow) -> None:
"""Save a TV show to the repository."""
shows = self._load_all()
# Remove existing show with same IMDb ID
shows = [s for s in shows if s.get('imdb_id') != str(show.imdb_id)]
# Add new show
shows.append(self._to_dict(show))
# Save to memory
self.memory.set('tv_shows', shows)
logger.debug(f"Saved TV show: {show.imdb_id}")
def find_by_imdb_id(self, imdb_id: ImdbId) -> Optional[TVShow]:
"""Find a TV show by its IMDb ID."""
shows = self._load_all()
for show_dict in shows:
if show_dict.get('imdb_id') == str(imdb_id):
return self._from_dict(show_dict)
return None
def find_all(self) -> List[TVShow]:
"""Get all TV shows in the repository."""
shows_dict = self._load_all()
return [self._from_dict(s) for s in shows_dict]
def delete(self, imdb_id: ImdbId) -> bool:
"""Delete a TV show from the repository."""
shows = self._load_all()
initial_count = len(shows)
# Filter out the show
shows = [s for s in shows if s.get('imdb_id') != str(imdb_id)]
if len(shows) < initial_count:
self.memory.set('tv_shows', shows)
logger.debug(f"Deleted TV show: {imdb_id}")
return True
return False
def exists(self, imdb_id: ImdbId) -> bool:
"""Check if a TV show exists in the repository."""
return self.find_by_imdb_id(imdb_id) is not None
def _load_all(self) -> List[Dict[str, Any]]:
"""Load all TV shows from memory."""
return self.memory.get('tv_shows', [])
def _to_dict(self, show: TVShow) -> Dict[str, Any]:
"""Convert TVShow entity to dict for storage."""
return {
'imdb_id': str(show.imdb_id),
'title': show.title,
'seasons_count': show.seasons_count,
'status': show.status.value,
'tmdb_id': show.tmdb_id,
'overview': show.overview,
'poster_path': show.poster_path,
'first_air_date': show.first_air_date,
'vote_average': show.vote_average,
'added_at': show.added_at.isoformat(),
}
def _from_dict(self, data: Dict[str, Any]) -> TVShow:
"""Convert dict from storage to TVShow entity."""
from datetime import datetime
return TVShow(
imdb_id=ImdbId(data['imdb_id']),
title=data['title'],
seasons_count=data['seasons_count'],
status=ShowStatus.from_string(data['status']),
tmdb_id=data.get('tmdb_id'),
overview=data.get('overview'),
poster_path=data.get('poster_path'),
first_air_date=data.get('first_air_date'),
vote_average=data.get('vote_average'),
added_at=datetime.fromisoformat(data['added_at']) if data.get('added_at') else datetime.now(),
)
+86
View File
@@ -0,0 +1,86 @@
"""Memory storage - Migrated from agent/memory.py"""
from pathlib import Path
from typing import Any, Dict
import json
from agent.config import settings
from agent.parameters import validate_parameter, get_parameter_schema
class Memory:
"""
Generic memory storage for agent state.
Provides a simple key-value store that persists to JSON.
"""
def __init__(self, path: str = "memory.json"):
self.file = Path(path)
self.data: Dict[str, Any] = {}
self.load()
def load(self) -> None:
"""Load memory from file or initialize with defaults."""
if self.file.exists():
try:
self.data = json.loads(self.file.read_text(encoding="utf-8"))
except (json.JSONDecodeError, IOError) as e:
print(f"Warning: Could not load memory file: {e}")
self.data = {
"config": {},
"tv_shows": [],
"history": [],
}
else:
self.data = {
"config": {},
"tv_shows": [],
"history": [],
}
def save(self) -> None:
self.file.write_text(
json.dumps(self.data, indent=2, ensure_ascii=False),
encoding="utf-8",
)
def get(self, key: str, default: Any = None) -> Any:
"""Get a value from memory by key."""
return self.data.get(key, default)
def set(self, key: str, value: Any) -> None:
"""
Set a value in memory and save.
Validates the value against the parameter schema if one exists.
"""
# Validate if schema exists
is_valid, error_msg = validate_parameter(key, value)
if not is_valid:
print(f'Validation failed for {key}: {error_msg}')
raise ValueError(f"Invalid value for {key}: {error_msg}")
print(f'Setting {key} in memory to: {value}')
self.data[key] = value
self.save()
def has(self, key: str) -> bool:
"""Check if a key exists and has a non-None value."""
return key in self.data and self.data[key] is not None
def append_history(self, role: str, content: str) -> None:
"""
Append a message to conversation history.
Args:
role: Message role ('user' or 'assistant')
content: Message content
"""
if "history" not in self.data:
self.data["history"] = []
self.data["history"].append({
"role": role,
"content": content
})
self.save()