From 82992047ddac572a8482d33aeb610d991c57a138 Mon Sep 17 00:00:00 2001 From: spl0k Date: Sat, 2 Nov 2013 19:52:02 +0100 Subject: [PATCH] Added configurable extension whitelist on the scanner Closes #8 --- README.md | 2 ++ scanner.py | 16 ++++++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 8be2654..cce1c70 100755 --- a/README.md +++ b/README.md @@ -42,6 +42,8 @@ Available settings are: I personnaly use SQLite (`sqlite:////var/supysonic/supysonic.db`), but it might not be the brightest idea for large libraries. * **cache_dir**: path to a cache folder. Mostly used for resized cover art images. Defaults to `/supysonic`. * **log_file**: path and base name of a rolling log file. + * **scanner_extensions**: space-separated list of file extensions the scanner is restricted to. If omitted, files will be scanned + regardless of their extension * Section **lastfm**: * **api_key**: Last.FM [API key](http://www.last.fm/api/accounts) to enable scrobbling * **secret**: Last.FM API secret matching the key. diff --git a/scanner.py b/scanner.py index 304d1a5..3893b1d 100755 --- a/scanner.py +++ b/scanner.py @@ -22,14 +22,19 @@ class Scanner: self.__deleted_albums = 0 self.__deleted_tracks = 0 + extensions = config.get('base', 'scanner_extensions') + self.__extensions = map(str.lower, extensions.split()) if extensions else None + def scan(self, folder): for root, subfolders, files in os.walk(folder.path): for f in files: - self.__scan_file(os.path.join(root, f), folder) + path = os.path.join(root, f) + if self.__is_valid_path(path): + self.__scan_file(path, folder) folder.last_scan = int(time.time()) def prune(self, folder): - for track in [ t for t in self.__tracks if t.root_folder.id == folder.id and not os.path.exists(t.path) ]: + for track in [ t for t in self.__tracks if t.root_folder.id == folder.id and not self.__is_valid_path(t.path) ]: self.__remove_track(track) for album in [ album for artist in self.__artists for album in artist.albums if len(album.tracks) == 0 ]: @@ -48,6 +53,13 @@ class Scanner: for f in folder.children: self.check_cover_art(f) + def __is_valid_path(self, path): + if not os.path.exists(path): + return False + if not self.__extensions: + return True + return os.path.splitext(path)[1][1:].lower() in self.__extensions + def __scan_file(self, path, folder): tr = filter(lambda t: t.path == path, self.__tracks) if tr: