From d422f7423b2ebdedd06e2be266c1d56be7d8d4c3 Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Tue, 8 Jun 2010 16:23:46 -0700 Subject: [PATCH] importer now walks directories in sorted order --- beets/autotag/__init__.py | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/beets/autotag/__init__.py b/beets/autotag/__init__.py index af5afbad0..e380b1ae1 100644 --- a/beets/autotag/__init__.py +++ b/beets/autotag/__init__.py @@ -71,13 +71,39 @@ def _first_n(it, n): break yield v +def _sorted_walk(path): + """Like os.walk, but yields things in sorted, breadth-first + order. + """ + # Get all the directories and files at this level. + dirs = [] + files = [] + for base in os.listdir(path): + cur = os.path.join(path, base) + if os.path.isdir(cur): + dirs.append(base) + else: + files.append(base) + + # Sort lists and yield the current level. + dirs.sort() + files.sort() + yield (path, dirs, files) + + # Recurse into directories. + for base in dirs: + cur = os.path.join(path, base) + # yield from _sorted_walk(cur) + for res in _sorted_walk(cur): + yield res + def albums_in_dir(path): """Recursively searches the given directory and returns an iterable of lists of items where each list is probably an album. Specifically, any folder containing any media files is an album. """ path = library._unicode_path(path) - for root, dirs, files in os.walk(path): + for root, dirs, files in _sorted_walk(path): # Get a list of items in the directory. items = [] for filename in files: