From ae0889ddb743930ffc283f91e3e8924658e03287 Mon Sep 17 00:00:00 2001 From: Jacob Pavlock Date: Sun, 18 Dec 2022 18:07:47 -0800 Subject: [PATCH] refactor!: remove sync plugin The original idea of the sync plugin to sync multiple metadata sources with one command has some implementation barriers that were not fully fleshed out. Instead, each plugin should just implement their own sync commands. --- docs/cli.rst | 22 --------------- docs/configuration.rst | 2 +- docs/developers/api/core.rst | 8 ------ moe/config.py | 1 - moe/sync/__init__.py | 18 ------------- moe/sync/sync_cli.py | 40 --------------------------- moe/sync/sync_core.py | 29 -------------------- tests/sync/test_sync_cli.py | 52 ------------------------------------ tests/sync/test_sync_core.py | 46 ------------------------------- 9 files changed, 1 insertion(+), 217 deletions(-) delete mode 100644 moe/sync/__init__.py delete mode 100644 moe/sync/sync_cli.py delete mode 100644 moe/sync/sync_core.py delete mode 100644 tests/sync/test_sync_cli.py delete mode 100644 tests/sync/test_sync_core.py diff --git a/docs/cli.rst b/docs/cli.rst index 6bf1067c..db13e0a8 100644 --- a/docs/cli.rst +++ b/docs/cli.rst @@ -148,25 +148,3 @@ Optional Arguments Query for matching albums instead of tracks. ``-e, --extra`` Query for matching extras instead of tracks. - -sync -==== -Syncs any changes to your music from connected metadata sources. - -.. code-block:: bash - - moe sync [-h] [-a | -e] [-p] query - -Positional Arguments --------------------- -``query`` - Query your library for items to sync. See the :doc:`query docs <../query>` for more info. - -Optional Arguments ------------------- -``-h, --help`` - Display the help message. -``-a, --album`` - Query for matching albums instead of tracks. -``-e, --extra`` - Query for matching extras instead of tracks. diff --git a/docs/configuration.rst b/docs/configuration.rst index 2b67a24a..cccf108f 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -18,7 +18,7 @@ Global Options ============== Most configuration options reside in their relevant plugin, however there are the following global options: -``default_plugins = ["add", "edit", "info", "ls", "move", "rm", "write"]`` +``default_plugins = ["add", "cli", "duplicate", "edit", "import", "list", "move", "remove", "write"]`` Overrides the list of default plugins. ``disable_plugins = []`` diff --git a/docs/developers/api/core.rst b/docs/developers/api/core.rst index aa8fd45a..e88916fa 100644 --- a/docs/developers/api/core.rst +++ b/docs/developers/api/core.rst @@ -41,7 +41,6 @@ Query :members: :undoc-members: - Plugins ======= ``moe`` @@ -96,13 +95,6 @@ Remove .. automodule:: moe.remove :members: -Sync ----- -``moe.sync`` - -.. automodule:: moe.sync - :members: - Write ----- ``moe.write`` diff --git a/moe/config.py b/moe/config.py index 466d5856..426ad559 100644 --- a/moe/config.py +++ b/moe/config.py @@ -46,7 +46,6 @@ "list": "moe.list", "move": "moe.move", "read": "moe.read", - "sync": "moe.sync", "remove": "moe.remove", "write": "moe.write", } diff --git a/moe/sync/__init__.py b/moe/sync/__init__.py deleted file mode 100644 index 67e1a459..00000000 --- a/moe/sync/__init__.py +++ /dev/null @@ -1,18 +0,0 @@ -"""Syncs library metadata with external sources.""" - -import moe -from moe import config - -from . import sync_cli, sync_core -from .sync_core import * - -__all__ = [] -__all__.extend(sync_core.__all__) - - -@moe.hookimpl -def plugin_registration(): - """Only register the cli sub-plugin if the cli is enabled.""" - config.CONFIG.pm.register(sync_core, "sync_core") - if config.CONFIG.pm.has_plugin("cli"): - config.CONFIG.pm.register(sync_cli, "sync_cli") diff --git a/moe/sync/sync_cli.py b/moe/sync/sync_cli.py deleted file mode 100644 index 4ce6bdab..00000000 --- a/moe/sync/sync_cli.py +++ /dev/null @@ -1,40 +0,0 @@ -"""Adds the ``sync`` command to sync library metadata.""" - -import argparse -import logging - -import moe -import moe.cli -from moe import sync as moe_sync -from moe.util.cli import cli_query, query_parser - -log = logging.getLogger("moe.cli.sync") - -__all__: list[str] = [] - - -@moe.hookimpl -def add_command(cmd_parsers: argparse._SubParsersAction): - """Adds the ``add`` command to Moe's CLI.""" - add_parser = cmd_parsers.add_parser( - "sync", - description="Sync music metadata.", - help="sync music metadata", - parents=[query_parser], - ) - add_parser.set_defaults(func=_parse_args) - - -def _parse_args(args: argparse.Namespace): - """Parses the given commandline arguments. - - Args: - args: Commandline arguments to parse. - - Raises: - SystemExit: Invalid query given or query returned no items. - """ - items = cli_query(args.query, query_type=args.query_type) - - for item in items: - moe_sync.sync_item(item) diff --git a/moe/sync/sync_core.py b/moe/sync/sync_core.py deleted file mode 100644 index 122d5dd2..00000000 --- a/moe/sync/sync_core.py +++ /dev/null @@ -1,29 +0,0 @@ -"""Sync metadata from external sources.""" - -import logging - -import moe -from moe import config -from moe.library import LibItem - -log = logging.getLogger("moe.sync") - -__all__ = ["sync_item"] - - -class Hooks: - """Sync plugin hook specifications.""" - - @staticmethod - @moe.hookspec - def sync_metadata(item: LibItem): - """Implement specific metadata syncing for ``item``.""" - - -def sync_item(item: LibItem): - """Syncs metadata from external sources and merges changes into ``item``.""" - log.debug(f"Syncing metadata. [{item=!r}]") - - config.CONFIG.pm.hook.sync_metadata(item=item) - - log.debug(f"Synced metadata. [{item=!r}]") diff --git a/tests/sync/test_sync_cli.py b/tests/sync/test_sync_cli.py deleted file mode 100644 index 72d1b809..00000000 --- a/tests/sync/test_sync_cli.py +++ /dev/null @@ -1,52 +0,0 @@ -"""Test sync plugin cli.""" - -from unittest.mock import call, patch - -import pytest - -import moe -import moe.cli -from tests.conftest import track_factory - - -@pytest.fixture -def mock_sync(): - """Mock the `sync_item()` api call.""" - with patch("moe.sync.sync_cli.moe_sync.sync_item", autospec=True) as mock_sync: - yield mock_sync - - -@pytest.fixture -def mock_query(): - """Mock a database query call. - - Use ``mock_query.return_value` to set the return value of a query. - - Yields: - Mock query - """ - with patch("moe.sync.sync_cli.cli_query", autospec=True) as mock_query: - yield mock_query - - -@pytest.fixture -def _tmp_sync_config(tmp_config): - """A temporary config for the list plugin with the cli.""" - tmp_config('default_plugins = ["cli", "sync"]') - - -@pytest.mark.usefixtures("_tmp_sync_config") -class TestCommand: - """Test the `sync` command.""" - - def test_items(self, mock_query, mock_sync): - """Tracks are synced with a valid query.""" - track1 = track_factory() - track2 = track_factory() - cli_args = ["sync", "*"] - mock_query.return_value = [track1, track2] - - moe.cli.main(cli_args) - - mock_query.assert_called_once_with("*", query_type="track") - mock_sync.assert_has_calls([call(track1), call(track2)]) diff --git a/tests/sync/test_sync_core.py b/tests/sync/test_sync_core.py deleted file mode 100644 index 7fffaf5f..00000000 --- a/tests/sync/test_sync_core.py +++ /dev/null @@ -1,46 +0,0 @@ -"""Test sync plugin core.""" - -import moe -from moe import config -from moe import sync as moe_sync -from moe.config import ExtraPlugin -from moe.library import Track -from tests.conftest import track_factory - - -class SyncPlugin: - """Test plugin for sync hookspecs.""" - - @staticmethod - @moe.hookimpl - def sync_metadata(item): - """Syncs item metadata for testing.""" - if isinstance(item, Track): - item.title = "synced" - - -class TestHooks: - """Test sync hookspecs.""" - - def test_sync_metadata(self, tmp_config): - """Plugins can implement the `sync_metadata` hook.""" - tmp_config( - settings="default_plugins=['sync']", - extra_plugins=[ExtraPlugin(SyncPlugin, "sync_plugin")], - ) - - track = track_factory() - - config.CONFIG.pm.hook.sync_metadata(item=track) - - -class TestSyncItems: - """Test ``sync_item()``.""" - - def test_sync_item(self): - """Call the `sync_metadata` hook when syncing items.""" - track = track_factory() - - moe_sync.sync_item(track) - - config.CONFIG.pm.hook.sync_metadata.assert_called_once_with(item=track)