From a8458720ee83775e546e05449504e1f5cf365539 Mon Sep 17 00:00:00 2001 From: Thomas Adamcik Date: Tue, 14 Jan 2014 22:07:20 +0100 Subject: [PATCH] local: Review fixes --- mopidy/local/json.py | 10 ++++------ tests/local/json_test.py | 2 +- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/mopidy/local/json.py b/mopidy/local/json.py index 626dbbb5..9a7d02f8 100644 --- a/mopidy/local/json.py +++ b/mopidy/local/json.py @@ -46,6 +46,7 @@ def write_library(json_file, data): class _BrowseCache(object): encoding = sys.getfilesystemencoding() + splitpath_re = re.compile(r'([^/]+)') def __init__(self, uris): """Create a dictionary tree for quick browsing. @@ -58,7 +59,7 @@ class _BrowseCache(object): for uri in uris: path = translator.local_track_uri_to_path(uri, b'/') - parts = self.split(path.decode(self.encoding)) + parts = self.splitpath_re.findall(path.decode(self.encoding)) filename = parts.pop() node = self._root for part in parts: @@ -66,14 +67,11 @@ class _BrowseCache(object): ref = models.Ref.track(uri=uri, name=filename) node.setdefault(None, []).append(ref) - def split(self, path): - return re.findall(r'([^/]+)', path) - def lookup(self, path): results = [] node = self._root - for part in self.split(path): + for part in self.splitpath_re.findall(path): node = node.get(part, {}) for key, value in node.items(): @@ -99,7 +97,7 @@ class JsonLibrary(local.Library): def browse(self, path): if not self._browse_cache: - return + return [] return self._browse_cache.lookup(path) def load(self): diff --git a/tests/local/json_test.py b/tests/local/json_test.py index 09f84ab7..af606c05 100644 --- a/tests/local/json_test.py +++ b/tests/local/json_test.py @@ -30,7 +30,7 @@ class BrowseCacheTest(unittest.TestCase): def test_lookup_foo_baz(self): self.assertEqual([], self.cache.lookup('/foo/baz')) - def test_lookup_normalise_slashes(self): + def test_lookup_normalize_slashes(self): expected = [Ref.track(uri=self.uris[0], name='song1'), Ref.track(uri=self.uris[1], name='song2')] self.assertEqual(expected, self.cache.lookup('/foo//bar/'))