|
| 1 | +# This file is part of beets. |
| 2 | +# Copyright 2023, Max Rumpf. |
| 3 | +# |
| 4 | +# Permission is hereby granted, free of charge, to any person obtaining |
| 5 | +# a copy of this software and associated documentation files (the |
| 6 | +# "Software"), to deal in the Software without restriction, including |
| 7 | +# without limitation the rights to use, copy, modify, merge, publish, |
| 8 | +# distribute, sublicense, and/or sell copies of the Software, and to |
| 9 | +# permit persons to whom the Software is furnished to do so, subject to |
| 10 | +# the following conditions: |
| 11 | +# |
| 12 | +# The above copyright notice and this permission notice shall be |
| 13 | +# included in all copies or substantial portions of the Software. |
| 14 | + |
| 15 | +"""Plugin to rewrite fields based on a given query.""" |
| 16 | + |
| 17 | +from collections import defaultdict |
| 18 | +import shlex |
| 19 | + |
| 20 | +import confuse |
| 21 | +from beets import ui |
| 22 | +from beets.dbcore import AndQuery, query_from_strings |
| 23 | +from beets.library import Item, Album |
| 24 | +from beets.plugins import BeetsPlugin |
| 25 | + |
| 26 | + |
| 27 | +def rewriter(field, rules): |
| 28 | + """Template field function factory. |
| 29 | +
|
| 30 | + Create a template field function that rewrites the given field |
| 31 | + with the given rewriting rules. |
| 32 | + ``rules`` must be a list of (query, replacement) pairs. |
| 33 | + """ |
| 34 | + def fieldfunc(item): |
| 35 | + value = item._values_fixed[field] |
| 36 | + for query, replacement in rules: |
| 37 | + if query.match(item): |
| 38 | + # Rewrite activated. |
| 39 | + return replacement |
| 40 | + # Not activated; return original value. |
| 41 | + return value |
| 42 | + |
| 43 | + return fieldfunc |
| 44 | + |
| 45 | + |
| 46 | +class AdvancedRewritePlugin(BeetsPlugin): |
| 47 | + """Plugin to rewrite fields based on a given query.""" |
| 48 | + |
| 49 | + def __init__(self): |
| 50 | + """Parse configuration and register template fields for rewriting.""" |
| 51 | + super().__init__() |
| 52 | + |
| 53 | + template = confuse.Sequence({ |
| 54 | + 'match': str, |
| 55 | + 'field': str, |
| 56 | + 'replacement': str, |
| 57 | + }) |
| 58 | + |
| 59 | + # Gather all the rewrite rules for each field. |
| 60 | + rules = defaultdict(list) |
| 61 | + for rule in self.config.get(template): |
| 62 | + query = query_from_strings(AndQuery, Item, prefixes={}, |
| 63 | + query_parts=shlex.split(rule['match'])) |
| 64 | + fieldname = rule['field'] |
| 65 | + replacement = rule['replacement'] |
| 66 | + if fieldname not in Item._fields: |
| 67 | + raise ui.UserError( |
| 68 | + "invalid field name (%s) in rewriter" % fieldname) |
| 69 | + self._log.debug('adding template field {0} → {1}', |
| 70 | + fieldname, replacement) |
| 71 | + rules[fieldname].append((query, replacement)) |
| 72 | + if fieldname == 'artist': |
| 73 | + # Special case for the artist field: apply the same |
| 74 | + # rewrite for "albumartist" as well. |
| 75 | + rules['albumartist'].append((query, replacement)) |
| 76 | + |
| 77 | + # Replace each template field with the new rewriter function. |
| 78 | + for fieldname, fieldrules in rules.items(): |
| 79 | + getter = rewriter(fieldname, fieldrules) |
| 80 | + self.template_fields[fieldname] = getter |
| 81 | + if fieldname in Album._fields: |
| 82 | + self.album_template_fields[fieldname] = getter |
0 commit comments