Turn the actual query into a macro
This commit is contained in:
parent
ddcbe8fbab
commit
c45b06f734
3 changed files with 30 additions and 23 deletions
|
|
@ -59,7 +59,11 @@ beetslabels:
|
||||||
# labels:
|
# labels:
|
||||||
playlists:
|
playlists:
|
||||||
- name: exercise
|
- 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:
|
smartplaylist:
|
||||||
relative_to: ~/Music/0beets_playlists
|
relative_to: ~/Music/0beets_playlists
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ Prefixes = dict[str, FieldQueryType]
|
||||||
import re
|
import re
|
||||||
|
|
||||||
from .labels import HasLabelQuery, labels_command, LABELS_FIELD_NAME, LabelValueSort
|
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):
|
class LabelValueSortsDict(dict):
|
||||||
"""Custom dict that returns LabelValueSort for any label:* key."""
|
"""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
|
"""Given a list of strings, create the `Query` and `Sort` that they
|
||||||
represent.
|
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.
|
# Separate query token and sort token.
|
||||||
query_parts = []
|
query_parts = []
|
||||||
sort_parts = []
|
sort_parts = []
|
||||||
|
|
@ -94,7 +110,6 @@ class BeetsLabelsPlugin(BeetsPlugin):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.item_queries = {"label": HasLabelQuery}
|
self.item_queries = {"label": HasLabelQuery}
|
||||||
self.item_queries = {"playlist": InPlaylistQuery}
|
|
||||||
|
|
||||||
playlists_config = self.config['playlists'].get()
|
playlists_config = self.config['playlists'].get()
|
||||||
initialize_playlists(playlists_config)
|
initialize_playlists(playlists_config)
|
||||||
|
|
|
||||||
|
|
@ -10,28 +10,16 @@ playlist_config = {}
|
||||||
|
|
||||||
def initialize_playlists(playlists):
|
def initialize_playlists(playlists):
|
||||||
for playlist in playlists:
|
for playlist in playlists:
|
||||||
playlist_config[playlist["name"]] = \
|
pl_name = playlist["name"]
|
||||||
playlist["query"]
|
query = [playlist["query"], ",", f"label:{pl_name}"]
|
||||||
|
playlist_config[pl_name] = query
|
||||||
|
|
||||||
class InPlaylistQuery(FieldQuery):
|
def valid_playlist(playlist_name):
|
||||||
def __init__(self, _, pattern: str, __):
|
return playlist_name in playlist_config
|
||||||
super().__init__(LABELS_FIELD_NAME, pattern, False)
|
|
||||||
|
|
||||||
@classmethod
|
def expand_playlist_query(playlist_name):
|
||||||
def value_match(self, pattern, jsonstr):
|
print(playlist_config[playlist_name])
|
||||||
if jsonstr is not None:
|
return playlist_config[playlist_name]
|
||||||
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
|
|
||||||
|
|
||||||
class PlaylistValueSort(SlowFieldSort):
|
class PlaylistValueSort(SlowFieldSort):
|
||||||
def __init__(self, field, ascending=True, case_insensitive=True):
|
def __init__(self, field, ascending=True, case_insensitive=True):
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue