Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MoveOperation bugfix for CLI overrides #36

Merged
merged 9 commits into from
Dec 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ beets/
*.egg-info/
.vscode/
.DS_Store
*.pyc
*.swp
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ python setup.py install

If you get permission errors, try running it with `sudo`.

Update the `config.yaml` to utilize the local plugin with:

```yaml
pluginpath:
- /path/to.../beets-filetote/beetsplug
```

## Configuration

You will need to enable the plugin in beets' `config.yaml`:
Expand Down Expand Up @@ -108,8 +115,10 @@ This plugin supports the same operations as beets:
- `reflink`

These options are mutually exclusive, and there are nuances to how beets (and
thus this plugin) behave when there multiple set. See the [beets documentation](https://beets.readthedocs.io/en/stable/reference/config.html#importer-options)
for more details.
thus this plugin) behave when there multiple set. See the [beets documentation]
and [#36](https://github.com/gtronset/beets-filetote/pull/36) for more details.

[beets documentation]: https://beets.readthedocs.io/en/stable/reference/config.html#importer-options

### Renaming files

Expand Down
15 changes: 7 additions & 8 deletions beetsplug/filetote.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def __init__(self):
}
)

self.operation = self._operation_type()
self.operation = None

self._process_queue = []
self._shared_artifacts = {}
Expand Down Expand Up @@ -61,11 +61,13 @@ def __init__(self):
for move_event in move_events:
self.register_listener(move_event, self.collect_artifacts)

self.register_listener("import_begin", self._register_source)
self.register_listener("import_begin", self._register_session_settings)
self.register_listener("cli_exit", self.process_events)

def _register_source(self, session):
""" """
def _register_session_settings(self, session):
"""Certain settings are only available and/or finalized once the
import session begins."""
self.operation = self._operation_type()
self.paths = os.path.expanduser(session.paths[0])

def _operation_type(self):
Expand Down Expand Up @@ -333,9 +335,6 @@ def process_artifacts(self, source_files, mapping):
util.mkdirall(dest_file)
dest_file = util.bytestring_path(dest_file)

# TODO: detect if beets was called with 'move' and override config
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a legacy TODO that is now resolved.

The beets.config module provides the current config when referenced and not just the values in config.yml. Fixing when the self.operation is obtained deprecates this TODO.

# option here

self.manipulate_artifact(source_file, dest_file)

if self.print_ignored and ignored_files:
Expand Down Expand Up @@ -399,4 +398,4 @@ def manipulate_artifact(self, source_file, dest_file):
elif self.operation == MoveOperation.REFLINK_AUTO:
util.reflink(source_file, dest_file, fallback=True)
else:
assert False, "unknown MoveOperation"
assert False, f"unknown MoveOperation {self.operation}"
13 changes: 8 additions & 5 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 13 additions & 4 deletions tests/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ def capture_log(logger="beets"):


class FiletoteTestCase(_common.TestCase):
# pylint: disable=too-many-instance-attributes, protected-access
# pylint: disable=too-many-instance-attributes
# pylint: disable=protected-access
# pylint: disable=logging-fstring-interpolation
"""
Provides common setup and teardown, a convenience method for exercising the
plugin/importer, tools to setup a library, a directory containing files
Expand Down Expand Up @@ -68,7 +70,7 @@ def setUp(self):
# Install the DummyIO to capture anything directed to stdout
self.in_out.install()

def _run_importer(self):
def _run_importer(self, operation_option=None):
"""
Create an instance of the plugin, run the importer, and
remove/unregister the plugin instance so a new instance can
Expand All @@ -81,6 +83,13 @@ def _run_importer(self):
# Create an instance of the plugin
plugins.find_plugins()

if operation_option == "copy":
config["import"]["copy"] = True
config["import"]["move"] = False
elif operation_option == "move":
config["import"]["copy"] = False
config["import"]["move"] = True

# Exercise
# Run the importer
self.importer.run()
Expand Down Expand Up @@ -305,10 +314,10 @@ def _list_files(self, startpath):
for root, _dirs, files in os.walk(path):
level = root.replace(path, "").count(os.sep)
indent = self._indenter(level)
log.debug("%s%s/", indent, os.path.basename(root))
log.debug(f"{indent}{os.path.basename(root)}/")
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With how the logger is passed around, %s interpolation doesn't occur.

subindent = self._indenter(level + 1)
for filename in files:
log.debug("%s%s", subindent, filename)
log.debug(f"{subindent}{filename}")

def assert_in_lib_dir(self, *segments):
"""
Expand Down
132 changes: 132 additions & 0 deletions tests/test_moveoperation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
# pylint: disable=duplicate-code

import os

import beets
from beets import config

from tests.helper import FiletoteTestCase


class FiletoteMoveOperation(FiletoteTestCase):
"""
Tests to check handling of the operation (copy, move, etc.) can be
overridden by the CLI.
"""

def setUp(self):
"""Provides shared setup for tests."""
super().setUp()

self._set_import_dir()
self.album_path = os.path.join(self.import_dir, b"the_album")
self.rsrc_mp3 = b"full.mp3"
os.makedirs(self.album_path)

config["filetote"]["extensions"] = ".file"

def test_import_config_copy_false_import_op_copy(self):
"""Tests that when config does not have an operation set, that
providing it as `--copy` in the CLI correctly overrides."""

self._setup_import_session(copy=False, autotag=False)

self._create_file(
self.album_path, beets.util.bytestring_path("\xe4rtifact.file")
)
medium = self._create_medium(
os.path.join(self.album_path, b"track_1.mp3"), self.rsrc_mp3
)
self.import_media = [medium]

self._run_importer(operation_option="copy")

self.assert_in_import_dir(
b"the_album",
beets.util.bytestring_path("\xe4rtifact.file"),
)

self.assert_in_lib_dir(
b"Tag Artist",
b"Tag Album",
beets.util.bytestring_path("\xe4rtifact.file"),
)

def test_import_config_copy_false_import_op_move(self):
"""Tests that when config does not have an operation set, that
providing it as `--move` in the CLI correctly overrides."""
self._setup_import_session(copy=False, autotag=False)

self._create_file(
self.album_path, beets.util.bytestring_path("\xe4rtifact.file")
)
medium = self._create_medium(
os.path.join(self.album_path, b"track_1.mp3"), self.rsrc_mp3
)
self.import_media = [medium]

self._run_importer(operation_option="move")

self.assert_not_in_import_dir(
b"the_album",
beets.util.bytestring_path("\xe4rtifact.file"),
)

self.assert_in_lib_dir(
b"Tag Artist",
b"Tag Album",
beets.util.bytestring_path("\xe4rtifact.file"),
)

def test_import_config_copy_true_import_op_move(self):
"""Tests that when config operation is set to `copy`, that providing
`--move` in the CLI correctly overrides."""

self._setup_import_session(copy=True, autotag=False)

self._create_file(
self.album_path, beets.util.bytestring_path("\xe4rtifact.file")
)
medium = self._create_medium(
os.path.join(self.album_path, b"track_1.mp3"), self.rsrc_mp3
)
self.import_media = [medium]

self._run_importer(operation_option="move")

self.assert_not_in_import_dir(
b"the_album",
beets.util.bytestring_path("\xe4rtifact.file"),
)

self.assert_in_lib_dir(
b"Tag Artist",
b"Tag Album",
beets.util.bytestring_path("\xe4rtifact.file"),
)

def test_import_config_move_true_import_op_copy(self):
"""Tests that when config operation is set to `move`, that providing
`--copy` in the CLI correctly overrides."""
self._setup_import_session(move=True, autotag=False)

self._create_file(
self.album_path, beets.util.bytestring_path("\xe4rtifact.file")
)
medium = self._create_medium(
os.path.join(self.album_path, b"track_1.mp3"), self.rsrc_mp3
)
self.import_media = [medium]

self._run_importer(operation_option="copy")

self.assert_in_import_dir(
b"the_album",
beets.util.bytestring_path("\xe4rtifact.file"),
)

self.assert_in_lib_dir(
b"Tag Artist",
b"Tag Album",
beets.util.bytestring_path("\xe4rtifact.file"),
)