From d4d3ad450a354974d23b4f3c962e13a0413dd138 Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Thu, 10 Jan 2013 10:17:34 -0500 Subject: [PATCH] Optimize the filesystem scanning slightly --- archiver/archive.py | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/archiver/archive.py b/archiver/archive.py index 5312a48..b1590f3 100644 --- a/archiver/archive.py +++ b/archiver/archive.py @@ -43,7 +43,7 @@ class Archive (models.Model): "Scan the archive's root filesystem and add any new songs without adding metadata, delete songs that exist no more" #This method is implemented since the other scan methods all need to use the same code #DRY FTW - import re, os + import re, os, itertools from django.core.exceptions import ObjectDoesNotExist from Melodia.melodia_settings import SUPPORTED_AUDIO_EXTENSIONS from Melodia.melodia_settings import HASH_FUNCTION as hash @@ -60,23 +60,21 @@ class Archive (models.Model): #Add new songs for dirname, dirnames, filenames in os.walk(self.root_folder): - #For each filename - for filename in filenames: - #If the filename is a supported audio extension - if re.match(regex, filename): - #Make sure that `filename` is in the database - try: - rel_url = os.path.join(dirname, filename) - full_url = os.path.abspath(rel_url) - self.songs.get(url = full_url) + #For each filename that is supported + for filename in itertools.ifilter(lambda filename: re.match(regex, filename), filenames): + #Make sure that `filename` is in the database + try: + rel_url = os.path.join(dirname, filename) + full_url = os.path.abspath(rel_url) + self.songs.get(url = full_url) - except ObjectDoesNotExist, e: - #Song needs to be added to database - rel_url = os.path.join(dirname, filename) - full_url = os.path.abspath(rel_url) - new_song = Song(url = full_url) - new_song.save() - self.songs.add(new_song) + except ObjectDoesNotExist, e: + #Song needs to be added to database + rel_url = os.path.join(dirname, filename) + full_url = os.path.abspath(rel_url) + new_song = Song(url = full_url) + new_song.save() + self.songs.add(new_song) def _update_song_metadata(self, use_echonest = False, progress_callback = lambda x, y: None): """Scan every song in this archive (database only) and make sure all songs are correct