Repository: mopidy/mopidy-local

Issue:
[Feature request] Opposite of excluded_file_extensions 
My music collection has a lot of extra files cluttering it up, so it took me a while to build up the `local/excluded_file_extensions` option to skip all of them. I thought it would be more useful to have an `included_file_extensions` option, because a list of typical music file extensions is smaller. That could give you a bit of a problem if both options are used, but you could probably just deprecate and ignore `excluded_file_extensions`.

Discussion:
codecov[bot]: # [Codecov](https://codecov.io/gh/mopidy/mopidy-local/pull/32?src=pr&el=h1) Report
> Merging [#32](https://codecov.io/gh/mopidy/mopidy-local/pull/32?src=pr&el=desc) into [master](https://codecov.io/gh/mopidy/mopidy-local/commit/6865fac7254674170d1d2a92e3302dcd10b337da?src=pr&el=desc) will **decrease** coverage by `0.92%`.
> The diff coverage is `0%`.

[![Impacted file tree graph](https://codecov.io/gh/mopidy/mopidy-local/pull/32/graphs/tree.svg?width=650&token=rEF2NwRisd&height=150&src=pr)](https://codecov.io/gh/mopidy/mopidy-local/pull/32?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master      #32      +/-   ##
==========================================
- Coverage   51.12%   50.19%   -0.93%     
==========================================
  Files           9        9              
  Lines         757      777      +20     
==========================================
+ Hits          387      390       +3     
- Misses        370      387      +17
```


| [Impacted Files](https://codecov.io/gh/mopidy/mopidy-local/pull/32?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [mopidy\_local/commands.py](https://codecov.io/gh/mopidy/mopidy-local/pull/32/diff?src=pr&el=tree#diff-bW9waWR5X2xvY2FsL2NvbW1hbmRzLnB5) | `0% <0%> (ø)` | :arrow_up: |
| [mopidy\_local/storage.py](https://codecov.io/gh/mopidy/mopidy-local/pull/32/diff?src=pr&el=tree#diff-bW9waWR5X2xvY2FsL3N0b3JhZ2UucHk=) | `44.56% <0%> (-0.07%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/mopidy/mopidy-local/pull/32?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/mopidy/mopidy-local/pull/32?src=pr&el=footer). Last update [6865fac...402e924](https://codecov.io/gh/mopidy/mopidy-local/pull/32?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

jodal: One more thing: Would be nice with a changelog update together with the change, so the changelog always is up to date and ready for release :-)
jodal: And, by running `tox` locally, you run the project through approximately the same test setup as the CircleCI build which is currently failing. That makes for a lot faster iteration!
kingosticks: Sorry I'm late to the review party! Great PR here, my comments are mostly nitpicks. 
kingosticks: Except this needs to be rebased on master. 
jessesheidlower: I hope I did the rebase correctly. If so, I think it's ready....
kingosticks: Looks like it needs another go through `black`.
jessesheidlower: > Looks like it needs another go through `black`. It can be annoying until you get in the habit of running `tox` before pushing.

<sigh> Yup, got it now. Sorry!

Patch:
diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index 32bf2bb..9576cfa 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -10,8 +10,9 @@ v3.1.0 (UNRELEASED)
 - Add ``.cue`` to the list of excluded file extensions. (PR: #29)
 - Replace ``os.path`` usage with ``pathlib`` to handle arbitrary file path
   encodings better. (#20, PR: #30)
+- Add an ``included_files_extensions`` config. (#8, PR: #32)
 
-
+  
 v3.0.0 (2019-12-22)
 ===================
 
diff --git a/README.rst b/README.rst
index 6adafaa..e32fe4d 100644
--- a/README.rst
+++ b/README.rst
@@ -91,9 +91,16 @@ The following configuration values are available:
   library it should try and store its progress so far. Some libraries might not
   respect this setting. Set this to zero to disable flushing.
 
+- ``local/included_file_extensions``: File extensions to include when scanning
+  the media directory. Values should be separated by either comma or newline.
+  Each file extension should start with a dot, .e.g. ``.flac``. Setting any
+  values here will override the existence of ``local/excluded_file_extensions``.
+
 - ``local/excluded_file_extensions``: File extensions to exclude when scanning
   the media directory. Values should be separated by either comma or newline.
-  Each file extension should start with a dot, .e.g. ``.html``.
+  Each file extension should start with a dot, .e.g. ``.html``. Defaults to a
+  list of common non-audio file extensions often found in music collections.
+  This config value has no effect if ``local/included_file_extensions`` is set.
 
 - ``local/directories``: List of top-level directory names and URIs
   for browsing.
diff --git a/mopidy_local/__init__.py b/mopidy_local/__init__.py
index d4d881a..225ecd2 100644
--- a/mopidy_local/__init__.py
+++ b/mopidy_local/__init__.py
@@ -26,6 +26,7 @@ class Extension(ext.Extension):
         schema["scan_timeout"] = config.Integer(minimum=1000, maximum=1000 * 60 * 60)
         schema["scan_flush_threshold"] = config.Integer(minimum=0)
         schema["scan_follow_symlinks"] = config.Boolean()
+        schema["included_file_extensions"] = config.List(optional=True)
         schema["excluded_file_extensions"] = config.List(optional=True)
         schema["directories"] = config.List()
         schema["timeout"] = config.Integer(optional=True, minimum=1)
diff --git a/mopidy_local/commands.py b/mopidy_local/commands.py
index 5dc213d..f195614 100644
--- a/mopidy_local/commands.py
+++ b/mopidy_local/commands.py
@@ -81,6 +81,10 @@ class ScanCommand(commands.Command):
                 media_dir=media_dir,
                 file_mtimes=file_mtimes,
                 files_in_library=files_in_library,
+                included_file_exts=[
+                    file_ext.lower()
+                    for file_ext in config["local"]["included_file_extensions"]
+                ],
                 excluded_file_exts=[
                     file_ext.lower()
                     for file_ext in config["local"]["excluded_file_extensions"]
@@ -143,19 +147,56 @@ class ScanCommand(commands.Command):
         return files_to_update, files_in_library
 
     def _find_files_to_scan(
-        self, *, media_dir, file_mtimes, files_in_library, excluded_file_exts
+        self,
+        *,
+        media_dir,
+        file_mtimes,
+        files_in_library,
+        included_file_exts,
+        excluded_file_exts,
     ):
         files_to_update = set()
 
+        def _is_hidden_file(relative_path, file_uri):
+            if any(p.startswith(".") for p in relative_path.parts):
+                logger.debug(f"Skipped {file_uri}: Hidden directory/file")
+                return True
+            else:
+                return False
+
+        def _extension_filters(
+            relative_path, file_uri, included_file_exts, excluded_file_exts
+        ):
+            if included_file_exts:
+                if relative_path.suffix.lower() in included_file_exts:
+                    logger.debug(f"Added {file_uri}: File extension on included list")
+                    return True
+                else:
+                    logger.debug(
+                        f"Skipped {file_uri}: File extension not on included list"
+                    )
+                    return False
+            else:
+                if relative_path.suffix.lower() in excluded_file_exts:
+                    logger.debug(f"Skipped {file_uri}: File extension on excluded list")
+                    return False
+                else:
+                    logger.debug(
+                        f"Included {file_uri}: File extension not on excluded list"
+                    )
+                    return True
+
         for absolute_path in file_mtimes:
             relative_path = absolute_path.relative_to(media_dir)
             file_uri = absolute_path.as_uri()
 
-            if any(p.startswith(".") for p in relative_path.parts):
-                logger.debug(f"Skipped {file_uri}: Hidden directory/file")
-            elif relative_path.suffix.lower() in excluded_file_exts:
-                logger.debug(f"Skipped {file_uri}: File extension excluded")
-            elif absolute_path not in files_in_library:
+            if (
+                not _is_hidden_file(relative_path, file_uri)
+                and _extension_filters(
+                    relative_path, file_uri, included_file_exts, excluded_file_exts
+                )
+                and absolute_path not in files_in_library
+            ):
                 files_to_update.add(absolute_path)
 
         logger.info(f"Found {len(files_to_update)} tracks which need to be updated")
diff --git a/mopidy_local/ext.conf b/mopidy_local/ext.conf
index cbe97df..08e1f85 100644
--- a/mopidy_local/ext.conf
+++ b/mopidy_local/ext.conf
@@ -5,6 +5,7 @@ media_dir = $XDG_MUSIC_DIR
 scan_timeout = 1000
 scan_flush_threshold = 100
 scan_follow_symlinks = false
+included_file_extensions =
 excluded_file_extensions =
   .cue
   .directory
@@ -18,6 +19,7 @@ excluded_file_extensions =
   .txt
   .zip
 
+
 # top-level directories for browsing, as <name> <uri>
 directories =
     Albums                  local:directory?type=album


Test patch:
diff --git a/tests/test_extension.py b/tests/test_extension.py
index e11c4a1..ff79cc2 100644
--- a/tests/test_extension.py
+++ b/tests/test_extension.py
@@ -23,6 +23,7 @@ def test_get_config_schema():
     assert "scan_timeout" in schema
     assert "scan_flush_threshold" in schema
     assert "scan_follow_symlinks" in schema
+    assert "included_file_extensions" in schema
     assert "excluded_file_extensions" in schema
     # from mopidy-local-sqlite
     assert "directories" in schema