From 603b57ef3c353c48d97645b32efdc607a51283e0 Mon Sep 17 00:00:00 2001 From: Thomas Adamcik Date: Wed, 27 Nov 2013 22:49:07 +0100 Subject: [PATCH] utils: Remove find_uris and update find_files - find_uris is no more - find_files now returns file paths relative to path being searched - find_files now only works on directories - find_files tests have been updated to reflect changes - local scanner has gotten a minimal update to reflect this alteration --- mopidy/backends/local/commands.py | 5 +-- mopidy/utils/path.py | 28 +++++++---------- tests/audio/scan_test.py | 35 ++++++++++++--------- tests/data/{ => find}/.blank.mp3 | Bin tests/data/{ => find}/.hidden/.gitignore | 0 tests/data/find/baz/file | 0 tests/data/find/foo/bar/file | 0 tests/data/find/foo/file | 0 tests/utils/path_test.py | 38 ++++------------------- 9 files changed, 40 insertions(+), 66 deletions(-) rename tests/data/{ => find}/.blank.mp3 (100%) rename tests/data/{ => find}/.hidden/.gitignore (100%) create mode 100644 tests/data/find/baz/file create mode 100644 tests/data/find/foo/bar/file create mode 100644 tests/data/find/foo/file diff --git a/mopidy/backends/local/commands.py b/mopidy/backends/local/commands.py index c2ef143c..c0d6d23a 100644 --- a/mopidy/backends/local/commands.py +++ b/mopidy/backends/local/commands.py @@ -68,12 +68,13 @@ class ScanCommand(commands.Command): local_updater.remove(uri) logger.info('Checking %s for unknown tracks.', media_dir) - for uri in path.find_uris(media_dir): - file_extension = os.path.splitext(path.uri_to_path(uri))[1] + for relpath in path.find_files(media_dir): + file_extension = os.path.splitext(relpath)[1] if file_extension.lower() in excluded_file_extensions: logger.debug('Skipped %s: File extension excluded.', uri) continue + uri = path.path_to_uri(os.path.join(media_dir, relpath)) if uri not in uris_library: uris_update.add(uri) diff --git a/mopidy/utils/path.py b/mopidy/utils/path.py index 32dcb721..b8dcc589 100644 --- a/mopidy/utils/path.py +++ b/mopidy/utils/path.py @@ -119,26 +119,20 @@ def find_files(path): path = path.encode('utf-8') if os.path.isfile(path): - if not os.path.basename(path).startswith(b'.'): - yield path - else: - for dirpath, dirnames, filenames in os.walk(path, followlinks=True): - for dirname in dirnames: - if dirname.startswith(b'.'): - # Skip hidden dirs by modifying dirnames inplace - dirnames.remove(dirname) + return - for filename in filenames: - if filename.startswith(b'.'): - # Skip hidden files - continue + for dirpath, dirnames, filenames in os.walk(path, followlinks=True): + for dirname in dirnames: + if dirname.startswith(b'.'): + # Skip hidden dirs by modifying dirnames inplace + dirnames.remove(dirname) - yield os.path.join(dirpath, filename) + for filename in filenames: + if filename.startswith(b'.'): + # Skip hidden files + continue - -def find_uris(path): - for p in find_files(path): - yield path_to_uri(p) + yield os.path.relpath(os.path.join(dirpath, filename), path) def check_file_path_is_inside_base_dir(file_path, base_path): diff --git a/tests/audio/scan_test.py b/tests/audio/scan_test.py index 4acbecb6..ed3f8e01 100644 --- a/tests/audio/scan_test.py +++ b/tests/audio/scan_test.py @@ -1,5 +1,6 @@ from __future__ import unicode_literals +import os import unittest from mopidy import exceptions @@ -240,11 +241,15 @@ class ScannerTest(unittest.TestCase): self.errors = {} self.data = {} - def scan(self, path): - paths = path_lib.find_files(path_to_data_dir(path)) - uris = (path_lib.path_to_uri(p) for p in paths) + def find(self, path): + media_dir = path_to_data_dir(path) + for path in path_lib.find_files(media_dir): + yield os.path.join(media_dir, path) + + def scan(self, paths): scanner = scan.Scanner() - for uri in uris: + for path in paths: + uri = path_lib.path_to_uri(path) key = uri[len('file://'):] try: self.data[key] = scanner.scan(uri) @@ -256,15 +261,15 @@ class ScannerTest(unittest.TestCase): self.assertEqual(self.data[name][key], value) def test_data_is_set(self): - self.scan('scanner/simple') + self.scan(self.find('scanner/simple')) self.assert_(self.data) def test_errors_is_not_set(self): - self.scan('scanner/simple') + self.scan(self.find('scanner/simple')) self.assert_(not self.errors) def test_uri_is_set(self): - self.scan('scanner/simple') + self.scan(self.find('scanner/simple')) self.check( 'scanner/simple/song1.mp3', 'uri', 'file://%s' % path_to_data_dir('scanner/simple/song1.mp3')) @@ -273,39 +278,39 @@ class ScannerTest(unittest.TestCase): 'file://%s' % path_to_data_dir('scanner/simple/song1.ogg')) def test_duration_is_set(self): - self.scan('scanner/simple') + self.scan(self.find('scanner/simple')) self.check('scanner/simple/song1.mp3', 'duration', 4680000000) self.check('scanner/simple/song1.ogg', 'duration', 4680000000) def test_artist_is_set(self): - self.scan('scanner/simple') + self.scan(self.find('scanner/simple')) self.check('scanner/simple/song1.mp3', 'artist', 'name') self.check('scanner/simple/song1.ogg', 'artist', 'name') def test_album_is_set(self): - self.scan('scanner/simple') + self.scan(self.find('scanner/simple')) self.check('scanner/simple/song1.mp3', 'album', 'albumname') self.check('scanner/simple/song1.ogg', 'album', 'albumname') def test_track_is_set(self): - self.scan('scanner/simple') + self.scan(self.find('scanner/simple')) self.check('scanner/simple/song1.mp3', 'title', 'trackname') self.check('scanner/simple/song1.ogg', 'title', 'trackname') def test_nonexistant_dir_does_not_fail(self): - self.scan('scanner/does-not-exist') + self.scan(self.find('scanner/does-not-exist')) self.assert_(not self.errors) def test_other_media_is_ignored(self): - self.scan('scanner/image') + self.scan(self.find('scanner/image')) self.assert_(self.errors) def test_log_file_that_gst_thinks_is_mpeg_1_is_ignored(self): - self.scan('scanner/example.log') + self.scan([path_to_data_dir('scanner/example.log')]) self.assert_(self.errors) def test_empty_wav_file_is_ignored(self): - self.scan('scanner/empty.wav') + self.scan([path_to_data_dir('scanner/empty.wav')]) self.assert_(self.errors) @unittest.SkipTest diff --git a/tests/data/.blank.mp3 b/tests/data/find/.blank.mp3 similarity index 100% rename from tests/data/.blank.mp3 rename to tests/data/find/.blank.mp3 diff --git a/tests/data/.hidden/.gitignore b/tests/data/find/.hidden/.gitignore similarity index 100% rename from tests/data/.hidden/.gitignore rename to tests/data/find/.hidden/.gitignore diff --git a/tests/data/find/baz/file b/tests/data/find/baz/file new file mode 100644 index 00000000..e69de29b diff --git a/tests/data/find/foo/bar/file b/tests/data/find/foo/bar/file new file mode 100644 index 00000000..e69de29b diff --git a/tests/data/find/foo/file b/tests/data/find/foo/file new file mode 100644 index 00000000..e69de29b diff --git a/tests/utils/path_test.py b/tests/utils/path_test.py index 673fda73..316b4f38 100644 --- a/tests/utils/path_test.py +++ b/tests/utils/path_test.py @@ -221,9 +221,12 @@ class FindFilesTest(unittest.TestCase): self.assertEqual(self.find('does-not-exist'), []) def test_file(self): - files = self.find('blank.mp3') - self.assertEqual(len(files), 1) - self.assertEqual(files[0], path_to_data_dir('blank.mp3')) + self.assertEqual([], self.find('blank.mp3')) + + def test_files(self): + files = self.find('find') + excepted = [b'foo/bar/file', b'foo/file', b'baz/file'] + self.assertItemsEqual(excepted, files) def test_names_are_bytestrings(self): is_bytes = lambda f: isinstance(f, bytes) @@ -231,35 +234,6 @@ class FindFilesTest(unittest.TestCase): self.assert_( is_bytes(name), '%s is not bytes object' % repr(name)) - def test_ignores_hidden_dirs(self): - self.assertEqual(self.find('.hidden'), []) - - def test_ignores_hidden_files(self): - self.assertEqual(self.find('.blank.mp3'), []) - - -class FindUrisTest(unittest.TestCase): - def find(self, value): - return list(path.find_uris(path_to_data_dir(value))) - - def test_basic_dir(self): - self.assert_(self.find('')) - - def test_nonexistant_dir(self): - self.assertEqual(self.find('does-not-exist'), []) - - def test_file(self): - uris = self.find('blank.mp3') - expected = path.path_to_uri(path_to_data_dir('blank.mp3')) - self.assertEqual(len(uris), 1) - self.assertEqual(uris[0], expected) - - def test_ignores_hidden_dirs(self): - self.assertEqual(self.find('.hidden'), []) - - def test_ignores_hidden_files(self): - self.assertEqual(self.find('.blank.mp3'), []) - # TODO: kill this in favour of just os.path.getmtime + mocks class MtimeTest(unittest.TestCase):