Skip to content

Commit

Permalink
Fix #42: Skip cache for local feeds.
Browse files Browse the repository at this point in the history
  • Loading branch information
tkem committed Jan 10, 2018
1 parent c4cd77e commit 50bd288
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
9 changes: 5 additions & 4 deletions mopidy_podcast/backend.py
Expand Up @@ -21,21 +21,22 @@ class PodcastFeedCache(cachetools.TTLCache):
pykka_traversable = True

def __init__(self, config):
# FIXME: "missing" parameter will be deprecated in cachetools v1.2
super(PodcastFeedCache, self).__init__(
maxsize=config[Extension.ext_name]['cache_size'],
ttl=config[Extension.ext_name]['cache_ttl'],
missing=self.__missing
ttl=config[Extension.ext_name]['cache_ttl']
)
self.__opener = Extension.get_url_opener(config)
self.__timeout = config[Extension.ext_name]['timeout']

def __missing(self, uri):
def __missing__(self, uri):
ext_name, _, feedurl = uri.partition('+')
assert ext_name == Extension.ext_name
f = self.__opener.open(feedurl, timeout=self.__timeout)
with contextlib.closing(f) as source:
feed = feeds.parse(source)
# do not cache local URIs so updates are available immediately
if not feedurl.startswith('file:'):
self[uri] = feed
return feed


Expand Down
9 changes: 6 additions & 3 deletions mopidy_podcast/library.py
Expand Up @@ -50,14 +50,17 @@ def root_directory(self):
return None
elif root.startswith(('file:', 'http:', 'https:')):
uri = uritools.uridefrag('podcast+' + root).uri
return models.Ref.directory(name='Podcasts', uri=uri)
elif os.path.isabs(root):
uri = uritools.uricompose('podcast+file', '', root)
return models.Ref.directory(name='Podcasts', uri=uri)
elif self.__config_dir:
uri = uritools.uricompose('podcast+file', '',
os.path.join(self.__config_dir, root))
path = os.path.join(self.__config_dir, root)
uri = uritools.uricompose('podcast+file', '', path)
return models.Ref.directory(name='Podcasts', uri=uri)
else:
logger.error('Cannot retrieve Podcast root directory')
return None
return models.Ref.directory(name='Podcasts', uri=uri)

def browse(self, uri):
try:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -26,7 +26,7 @@ def get_version(filename):
'setuptools',
'Mopidy >= 1.1.1',
'Pykka >= 1.1',
'cachetools >= 1.0',
'cachetools >= 2.0',
'uritools >= 1.0'
],
entry_points={
Expand Down
9 changes: 4 additions & 5 deletions tests/test_library.py
Expand Up @@ -16,7 +16,6 @@ def test_browse(config, library, filename, abspath):
feed = feeds.parse(abspath(filename))
newest_first = config['podcast']['browse_order'] == 'desc'
assert library.browse(feed.uri) == list(feed.items(newest_first))
assert feed.uri in library.backend.feeds


@pytest.mark.parametrize('uri,expected', [
Expand All @@ -38,7 +37,6 @@ def test_get_images(library, filename, abspath):
assert library.get_images([uri]) == {uri: images}
images = {uri: images for uri, images in feed.images()}
assert library.get_images(list(images)) == images
assert feed.uri in library.backend.feeds


@pytest.mark.parametrize('uris,expected', [
Expand All @@ -60,7 +58,6 @@ def test_lookup(config, library, filename, abspath):
assert library.lookup(track.uri) == [track]
newest_first = config['podcast']['lookup_order'] == 'desc'
assert library.lookup(feed.uri) == list(feed.tracks(newest_first))
assert feed.uri in library.backend.feeds


@pytest.mark.parametrize('uri,expected', [
Expand All @@ -79,10 +76,12 @@ def test_lookup_error(library, uri, expected):
def test_refresh(library, filename, abspath):
feed = feeds.parse(abspath(filename))
tracks = library.lookup(feed.uri)
assert feed.uri not in library.backend.feeds # local feeds not cached!
library.backend.feeds[feed.uri] = feed
assert feed.uri in library.backend.feeds
library.refresh(tracks[0].uri)
assert feed.uri not in library.backend.feeds
library.lookup(tracks[0].uri)
assert feed.uri in library.backend.feeds
library.backend.feeds[feed.uri] = feed
assert library.backend.feeds
library.refresh()
assert not library.backend.feeds

0 comments on commit 50bd288

Please sign in to comment.