Skip to content

Commit

Permalink
Added an included_file_extensions configuration: closes #8 (#32)
Browse files Browse the repository at this point in the history
* added an included_file_extensions configuration

* reformatted using black

* Several fixes to address jodal comments

* regularized wording of debug log

* reformatted with black

* fix order
  • Loading branch information
jessesheidlower authored and jodal committed Jan 8, 2020
1 parent 6865fac commit e337e9f
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 8 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
===================

Expand Down
9 changes: 8 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions mopidy_local/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def get_config_schema(self):
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)
Expand Down
53 changes: 47 additions & 6 deletions mopidy_local/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ def run(self, args, config):
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"]
Expand Down Expand Up @@ -143,19 +147,56 @@ def _check_tracks_in_library(
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")
Expand Down
2 changes: 2 additions & 0 deletions mopidy_local/ext.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -18,6 +19,7 @@ excluded_file_extensions =
.txt
.zip


# top-level directories for browsing, as <name> <uri>
directories =
Albums local:directory?type=album
Expand Down
1 change: 1 addition & 0 deletions tests/test_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e337e9f

Please sign in to comment.