diff --git a/config.yaml b/config.yaml index 42cce47..cfc41f8 100644 --- a/config.yaml +++ b/config.yaml @@ -59,7 +59,11 @@ beetslabels: # labels: playlists: - name: exercise - query: ["effortless"] + # and: "label:a label:b" + # or: "label:a , label:b" + # in the future? concat: ["label:a", "label:b"] + query: "label:effortless" + sort: "{effortless}" smartplaylist: relative_to: ~/Music/0beets_playlists diff --git a/plugins/beetslabels.py b/plugins/beetslabels.py index 228991d..f550796 100644 --- a/plugins/beetslabels.py +++ b/plugins/beetslabels.py @@ -16,7 +16,7 @@ Prefixes = dict[str, FieldQueryType] import re from .labels import HasLabelQuery, labels_command, LABELS_FIELD_NAME, LabelValueSort -from .playlists import initialize_playlists, InPlaylistQuery, PlaylistValueSort +from .playlists import initialize_playlists, expand_playlist_query, valid_playlist, PlaylistValueSort class LabelValueSortsDict(dict): """Custom dict that returns LabelValueSort for any label:* key.""" @@ -46,6 +46,22 @@ def parse_sorted_query_override( """Given a list of strings, create the `Query` and `Sort` that they represent. """ + # First, expand any playlist: macros + expanded_parts = [] + for part in parts: + if part.startswith('playlist:') and not re.match(r"[+-]$", part): + playlist_name = part[len("playlist:"):] + + if valid_playlist(playlist_name): + expanded_parts.extend(expand_playlist_query(playlist_name)) + else: + # Unknown playlist, just let it go through + expanded_parts.append(part) + else: + expanded_parts.append(part) + + parts = expanded_parts + # Separate query token and sort token. query_parts = [] sort_parts = [] @@ -94,7 +110,6 @@ class BeetsLabelsPlugin(BeetsPlugin): def __init__(self): super().__init__() self.item_queries = {"label": HasLabelQuery} - self.item_queries = {"playlist": InPlaylistQuery} playlists_config = self.config['playlists'].get() initialize_playlists(playlists_config) diff --git a/plugins/playlists.py b/plugins/playlists.py index 1275476..46587f6 100644 --- a/plugins/playlists.py +++ b/plugins/playlists.py @@ -10,28 +10,16 @@ playlist_config = {} def initialize_playlists(playlists): for playlist in playlists: - playlist_config[playlist["name"]] = \ - playlist["query"] + pl_name = playlist["name"] + query = [playlist["query"], ",", f"label:{pl_name}"] + playlist_config[pl_name] = query -class InPlaylistQuery(FieldQuery): - def __init__(self, _, pattern: str, __): - super().__init__(LABELS_FIELD_NAME, pattern, False) +def valid_playlist(playlist_name): + return playlist_name in playlist_config - @classmethod - def value_match(self, pattern, jsonstr): - if jsonstr is not None: - playlist = pattern - labels = json.loads(jsonstr) - - if playlist in labels: - # This song has been explicitly added to the playlist - return True - - for label in playlist_config[playlist]: - if label in labels: - return True - - return False +def expand_playlist_query(playlist_name): + print(playlist_config[playlist_name]) + return playlist_config[playlist_name] class PlaylistValueSort(SlowFieldSort): def __init__(self, field, ascending=True, case_insensitive=True):