Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Allow plugin to unregister itself #1914

Merged
merged 4 commits into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions plextraktsync/sync/ClearCollectedPlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

if TYPE_CHECKING:
from plextraktsync.config.SyncConfig import SyncConfig
from plextraktsync.sync.plugin import SyncPluginManager
from plextraktsync.sync.Sync import Sync
from plextraktsync.trakt.TraktApi import TraktApi
from plextraktsync.trakt.types import TraktMedia
Expand All @@ -20,7 +21,6 @@ def __init__(self, trakt: TraktApi):
self.trakt = trakt
self.episode_trakt_ids = set()
self.movie_trakt_ids = set()
self.is_partial = None

@staticmethod
def enabled(config: SyncConfig):
Expand All @@ -31,24 +31,20 @@ def factory(cls, sync: Sync):
return cls(sync.trakt)

@hookimpl
def init(self, is_partial: bool):
self.is_partial = is_partial
if is_partial:
self.logger.warning("Running partial library sync. Clear collected will be disabled.")
def init(self, pm: SyncPluginManager, is_partial: bool):
if not is_partial:
return

self.logger.warning("Disabling Clear Collected: Running partial library sync")
pm.unregister(self)

@hookimpl
def fini(self, dry_run: bool):
if self.is_partial:
return

self.clear_collected(self.trakt.movie_collection, self.movie_trakt_ids, dry_run=dry_run)
self.clear_collected(self.trakt.episodes_collection, self.episode_trakt_ids, dry_run=dry_run)

@hookimpl
def walk_movie(self, movie: Media):
if self.is_partial:
return

self.movie_trakt_ids.add(movie.trakt_id)

def clear_collected(self, existing_items: Iterable[TraktMedia], keep_ids: set[int], dry_run):
Expand Down
2 changes: 1 addition & 1 deletion plextraktsync/sync/Sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def sync(self, walker: Walker, dry_run=False):
pm = SyncPluginManager()
pm.register_plugins(self)

pm.hook.init(sync=self, trakt_lists=trakt_lists, is_partial=is_partial, dry_run=dry_run)
pm.hook.init(sync=self, pm=pm, trakt_lists=trakt_lists, is_partial=is_partial, dry_run=dry_run)

if self.config.need_library_walk:
for movie in walker.find_movies():
Expand Down
3 changes: 2 additions & 1 deletion plextraktsync/sync/plugin/SyncPluginInterface.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
if TYPE_CHECKING:
from plextraktsync.media.Media import Media
from plextraktsync.plan.Walker import Walker
from plextraktsync.sync.plugin import SyncPluginManager
from plextraktsync.sync.Sync import Sync
from plextraktsync.trakt.TraktUserListCollection import \
TraktUserListCollection
Expand All @@ -16,7 +17,7 @@ class SyncPluginInterface:
"""A hook specification namespace."""

@hookspec
def init(self, sync: Sync, trakt_lists: TraktUserListCollection, is_partial: bool, dry_run: bool):
def init(self, pm: SyncPluginManager, sync: Sync, trakt_lists: TraktUserListCollection, is_partial: bool, dry_run: bool):
"""Hook called at sync process initialization"""

@hookspec
Expand Down
4 changes: 4 additions & 0 deletions plextraktsync/sync/plugin/SyncPluginManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ def pm(self):
def hook(self):
return self.pm.hook

@cached_property
def unregister(self):
return self.pm.unregister

@property
def plugins(self):
from ..AddCollectionPlugin import AddCollectionPlugin
Expand Down
Loading