From b24dea9dba53bc4e8364126caf4ec0a228479f07 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 | 12 +++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) 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 41bb84e..c6116dd 100755 --- a/scanner.py +++ b/scanner.py @@ -25,6 +25,9 @@ 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): print "scanning", folder.path valid = [x.lower() for x in config.get('base','filetypes').split(',')] @@ -39,7 +42,7 @@ class Scanner: 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 ]: @@ -58,6 +61,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): if path in self.__tracks: tr = self.__tracks[path]