From 0cde586da280cebfa10927e7382cd6db0fbd25be Mon Sep 17 00:00:00 2001 From: Max Goltzsche Date: Fri, 15 Dec 2023 22:07:46 +0100 Subject: [PATCH] smartplaylist: change option --extm3u to --output The boolean flags `--extm3u` and `--no-extm3u` are replaced with a string option `--output=m3u|m3u8`. This reduces the amount of options and allows to evolve the CLI to support more playlist output formats in the future (e.g. JSON) without polluting the CLI at that point. --- beetsplug/smartplaylist.py | 29 ++++++++++++++--------------- docs/changelog.rst | 2 +- docs/plugins/smartplaylist.rst | 4 ++-- test/plugins/test_smartplaylist.py | 4 ++-- 4 files changed, 19 insertions(+), 20 deletions(-) diff --git a/beetsplug/smartplaylist.py b/beetsplug/smartplaylist.py index ab561e094a..120361d311 100644 --- a/beetsplug/smartplaylist.py +++ b/beetsplug/smartplaylist.py @@ -49,7 +49,7 @@ def __init__(self): "prefix": "", "urlencode": False, "pretend_paths": False, - "extm3u": False, + "output": "m3u", } ) @@ -91,7 +91,7 @@ def commands(self): dest="relative_to", metavar="PATH", type="string", - help="Generate playlist item paths relative to this path.", + help="generate playlist item paths relative to this path.", ) spl_update.parser.add_option( "--prefix", @@ -102,7 +102,7 @@ def commands(self): "--forward-slash", action="store_true", dest="forward_slash", - help="Force forward slash in paths within playlists.", + help="force forward slash in paths within playlists.", ) spl_update.parser.add_option( "--urlencode", @@ -110,15 +110,9 @@ def commands(self): help="URL-encode all paths.", ) spl_update.parser.add_option( - "--extm3u", - action="store_true", - help="generate extm3u/m3u8 playlists.", - ) - spl_update.parser.add_option( - "--no-extm3u", - action="store_false", - dest="extm3u", - help="generate extm3u/m3u8 playlists.", + "--output", + type="string", + help="specify the playlist format: m3u|m3u8.", ) spl_update.func = self.update_cmd return [spl_update] @@ -299,9 +293,14 @@ def update_playlists(self, lib, pretend=False): os.path.join(playlist_dir, bytestring_path(m3u)) ) mkdirall(m3u_path) - extm3u = self.config["extm3u"] + pl_format = self.config["output"].get() + if pl_format != "m3u" and pl_format != "m3u8": + msg = "Unsupported output format '{}' provided! " + msg += "Supported: m3u, m3u8" + raise Exception(msg.format(pl_format)) + m3u8 = pl_format == "m3u8" with open(syspath(m3u_path), "wb") as f: - if extm3u: + if m3u8: f.write(b"#EXTM3U\n") for entry in m3us[m3u]: path = entry["path"] @@ -311,7 +310,7 @@ def update_playlists(self, lib, pretend=False): if self.config["urlencode"]: path = bytestring_path(pathname2url(path)) comment = "" - if extm3u: + if m3u8: comment = "#EXTINF:{},{} - {}\n".format( int(item.length), item.artist, item.title ) diff --git a/docs/changelog.rst b/docs/changelog.rst index 3295fb5d34..c88a100928 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -148,7 +148,7 @@ New features: `synced` option to prefer synced lyrics over plain lyrics. * :ref:`import-cmd`: Expose import.quiet_fallback as CLI option. * :ref:`import-cmd`: Expose `import.incremental_skip_later` as CLI option. -* :doc:`/plugins/smartplaylist`: Add new config option `smartplaylist.extm3u`. +* :doc:`/plugins/smartplaylist`: Add new config option `smartplaylist.output`. * :doc:`/plugins/smartplaylist`: Expose config options as CLI options. Bug fixes: diff --git a/docs/plugins/smartplaylist.rst b/docs/plugins/smartplaylist.rst index 1d4de4eb55..365b5af321 100644 --- a/docs/plugins/smartplaylist.rst +++ b/docs/plugins/smartplaylist.rst @@ -118,9 +118,9 @@ other configuration options are: - **urlencode**: URL-encode all paths. Default: ``no``. - **pretend_paths**: When running with ``--pretend``, show the actual file paths that will be written to the m3u file. Default: ``false``. -- **extm3u**: Generate extm3u/m3u8 playlists. Default ``ǹo``. +- **output**: Specify the playlist format: m3u|m3u8. Default ``m3u``. For many configuration options, there is a corresponding CLI option, e.g. ``--playlist-dir``, ``--relative-to``, ``--prefix``, ``--forward-slash``, -``--urlencode``, ``--extm3u``, ``--pretend-paths``. +``--urlencode``, ``--output``, ``--pretend-paths``. CLI options take precedence over those specified within the configuration file. diff --git a/test/plugins/test_smartplaylist.py b/test/plugins/test_smartplaylist.py index f36601267d..f2f9faade9 100644 --- a/test/plugins/test_smartplaylist.py +++ b/test/plugins/test_smartplaylist.py @@ -191,7 +191,7 @@ def test_playlist_update(self): self.assertEqual(content, b"/tagada.mp3\n") - def test_playlist_update_extm3u(self): + def test_playlist_update_format_m3u8(self): spl = SmartPlaylistPlugin() i = MagicMock() @@ -215,7 +215,7 @@ def test_playlist_update_extm3u(self): spl._matched_playlists = [pl] dir = bytestring_path(mkdtemp()) - config["smartplaylist"]["extm3u"] = True + config["smartplaylist"]["output"] = "m3u8" config["smartplaylist"]["prefix"] = "http://beets:8337/files" config["smartplaylist"]["relative_to"] = False config["smartplaylist"]["playlist_dir"] = py3_path(dir)