From 222b3a34f98b6de2c04e660cb8761ac910210c5e Mon Sep 17 00:00:00 2001 From: Max Goltzsche Date: Thu, 14 Dec 2023 02:36:29 +0100 Subject: [PATCH] smartplaylist: expose config as CLI options Add CLI options to `splupdate` command: * `--playlist-dir`, `-d` * `--relative-to` * `--prefix` * `--urlencode` * `--forward-slash` * `--pretend-paths` --- beetsplug/smartplaylist.py | 53 ++++++++++++++++++++++++++++++---- docs/changelog.rst | 1 + docs/plugins/smartplaylist.rst | 7 ++++- 3 files changed, 55 insertions(+), 6 deletions(-) diff --git a/beetsplug/smartplaylist.py b/beetsplug/smartplaylist.py index c892a6040..ab561e094 100644 --- a/beetsplug/smartplaylist.py +++ b/beetsplug/smartplaylist.py @@ -72,16 +72,53 @@ class SmartPlaylistPlugin(BeetsPlugin): action="store_true", help="display query results but don't write playlist files.", ) + spl_update.parser.add_option( + "--pretend-paths", + action="store_true", + dest="pretend_paths", + help="in pretend mode, log the playlist item URIs/paths.", + ) + spl_update.parser.add_option( + "-d", + "--playlist-dir", + dest="playlist_dir", + metavar="PATH", + type="string", + help="directory to write the generated playlist files to.", + ) + spl_update.parser.add_option( + "--relative-to", + dest="relative_to", + metavar="PATH", + type="string", + help="Generate playlist item paths relative to this path.", + ) + spl_update.parser.add_option( + "--prefix", + type="string", + help="prepend string to every path in the playlist file.", + ) + spl_update.parser.add_option( + "--forward-slash", + action="store_true", + dest="forward_slash", + help="Force forward slash in paths within playlists.", + ) + spl_update.parser.add_option( + "--urlencode", + action="store_true", + help="URL-encode all paths.", + ) spl_update.parser.add_option( "--extm3u", action="store_true", - help="add artist/title as m3u8 comments to playlists.", + help="generate extm3u/m3u8 playlists.", ) spl_update.parser.add_option( "--no-extm3u", action="store_false", dest="extm3u", - help="do not add artist/title as extm3u comments to playlists.", + help="generate extm3u/m3u8 playlists.", ) spl_update.func = self.update_cmd return [spl_update] @@ -111,7 +148,13 @@ class SmartPlaylistPlugin(BeetsPlugin): else: self._matched_playlists = self._unmatched_playlists - self.update_playlists(lib, opts.extm3u, opts.pretend) + self.__apply_opts_to_config(opts) + self.update_playlists(lib, opts.pretend) + + def __apply_opts_to_config(self, opts): + for k, v in opts.__dict__.items(): + if v is not None and k in self.config: + self.config[k] = v def build_queries(self): """ @@ -197,7 +240,7 @@ class SmartPlaylistPlugin(BeetsPlugin): self._unmatched_playlists -= self._matched_playlists - def update_playlists(self, lib, extm3u=None, pretend=False): + def update_playlists(self, lib, pretend=False): if pretend: self._log.info( "Showing query results for {0} smart playlists...", @@ -256,7 +299,7 @@ class SmartPlaylistPlugin(BeetsPlugin): os.path.join(playlist_dir, bytestring_path(m3u)) ) mkdirall(m3u_path) - extm3u = extm3u is None and self.config["extm3u"] or extm3u + extm3u = self.config["extm3u"] with open(syspath(m3u_path), "wb") as f: if extm3u: f.write(b"#EXTM3U\n") diff --git a/docs/changelog.rst b/docs/changelog.rst index 9259b5933..66782408a 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -149,6 +149,7 @@ New features: * :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`: Expose config options as CLI options. Bug fixes: diff --git a/docs/plugins/smartplaylist.rst b/docs/plugins/smartplaylist.rst index 6a78124e1..1d4de4eb5 100644 --- a/docs/plugins/smartplaylist.rst +++ b/docs/plugins/smartplaylist.rst @@ -115,7 +115,12 @@ other configuration options are: - **prefix**: Prepend this string to every path in the playlist file. For example, you could use the URL for a server where the music is stored. Default: empty string. -- **urlencoded**: URL-encode all paths. Default: ``no``. +- **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``. + +For many configuration options, there is a corresponding CLI option, e.g. +``--playlist-dir``, ``--relative-to``, ``--prefix``, ``--forward-slash``, +``--urlencode``, ``--extm3u``, ``--pretend-paths``. +CLI options take precedence over those specified within the configuration file.