Add more hooks for podcasts/episodes

Add the following hooks in hooks.py:

 - on_podcast_subscribe
 - on_podcast_update_failed
 - on_podcast_delete
 - on_episode_delete
 - on_episode_remove_from_podcast

Call them as appropriate (in model.py)
This commit is contained in:
Neal H. Walfield 2011-07-23 23:26:05 +02:00 committed by Thomas Perl
parent f87d88a510
commit cabc680f50
2 changed files with 58 additions and 0 deletions

View File

@ -117,6 +117,14 @@ class HookManager(object):
# the same function defined in them. If the handler functions here contain
# any code, it will be called after all the hooks have been called.
@call_hooks
def on_podcast_subscribe(self, podcast):
"""Called when the user subscribes to a new podcast feed.
@param podcast: A gpodder.model.PodcastChannel instance
"""
pass
@call_hooks
def on_podcast_updated(self, podcast):
"""Called when a podcast feed was updated
@ -127,6 +135,16 @@ class HookManager(object):
"""
pass
@call_hooks
def on_podcast_update_failed(self, podcast, exception):
"""Called when a podcast update failed.
@param podcast: A gpodder.model.PodcastChannel instance
@param exception: The reason.
"""
pass
@call_hooks
def on_podcast_save(self, podcast):
"""Called when a podcast is saved to the database
@ -138,6 +156,14 @@ class HookManager(object):
"""
pass
@call_hooks
def on_podcast_delete(self, podcast):
"""Called when a podcast is deleted from the database
@param podcast: A gpodder.model.PodcastChannel instance
"""
pass
@call_hooks
def on_episode_save(self, episode):
"""Called when an episode is saved to the database
@ -176,3 +202,18 @@ class HookManager(object):
"""
pass
@call_hooks
def on_episode_delete(self, episode, filename):
"""Called just before the episode's disk file is about to be
deleted."""
pass
@call_hooks
def on_episode_removed_from_podcast(self, episode):
"""Called just before the episode is about to be removed from
the podcast channel, e.g., when the episode has not been
downloaded and it disappears from the feed.
@param podcast: A gpodder.model.PodcastChannel instance
"""
pass

View File

@ -405,6 +405,8 @@ class PodcastEpisode(PodcastModelObject):
def delete_from_disk(self):
filename = self.local_filename(create=False, check_only=True)
if filename is not None:
if gpodder.user_hooks is not None:
gpodder.user_hooks.on_episode_delete(self, filename)
util.delete_file(filename)
self.set_state(gpodder.STATE_DELETED)
@ -1012,6 +1014,8 @@ class PodcastChannel(PodcastModelObject):
for episode in episodes_to_purge:
logger.debug('Episode removed from feed: %s (%s)',
episode.title, episode.guid)
if gpodder.user_hooks is not None:
gpodder.user_hooks.on_episode_removed_from_podcast(episode)
self.db.delete_episode_by_guid(episode.guid, self.id)
# Remove the episode from the "children" episodes list
@ -1067,6 +1071,8 @@ class PodcastChannel(PodcastModelObject):
#feedcore.NotFound
#feedcore.InvalidFeed
#feedcore.UnknownStatusCode
if gpodder.user_hooks is not None:
gpodder.user_hooks.on_podcast_update_failed(self, e)
raise
if gpodder.user_hooks is not None:
@ -1075,6 +1081,8 @@ class PodcastChannel(PodcastModelObject):
self.db.commit()
def delete(self):
if gpodder.user_hooks is not None:
gpodder.user_hooks.on_podcast_delete(self)
self.db.delete_podcast(self)
def save(self):
@ -1082,6 +1090,9 @@ class PodcastChannel(PodcastModelObject):
self.get_save_dir()
if gpodder.user_hooks is not None:
if self.id is None:
gpodder.user_hooks.on_podcast_subscribe(self)
gpodder.user_hooks.on_podcast_save(self)
self.db.save_podcast(self)
@ -1210,6 +1221,12 @@ class PodcastChannel(PodcastModelObject):
def remove_downloaded(self):
# Remove the download directory
if gpodder.user_hooks is not None:
for episode in self.get_downloaded_episodes():
filename = episode.local_filename(create=False, check_only=True)
if filename is not None:
gpodder.user_hooks.on_episode_delete(episode, filename)
shutil.rmtree(self.save_dir, True)
@property