From d7acc14e1aea2c7ce295c4486747718a57e10414 Mon Sep 17 00:00:00 2001 From: Bob Swift Date: Mon, 4 Jul 2022 13:07:05 -0600 Subject: [PATCH] Add option to use regular expressions for match strings --- plugins/genre_mapper/__init__.py | 21 ++++++++------- plugins/genre_mapper/options_genre_mapper.ui | 27 ++++++++++++++++++- .../genre_mapper/ui_options_genre_mapper.py | 16 ++++++++++- 3 files changed, 52 insertions(+), 12 deletions(-) diff --git a/plugins/genre_mapper/__init__.py b/plugins/genre_mapper/__init__.py index c4fb7653..8bfdddfc 100644 --- a/plugins/genre_mapper/__init__.py +++ b/plugins/genre_mapper/__init__.py @@ -28,7 +28,7 @@

Please see the user guide on GitHub for more information. ''' -PLUGIN_VERSION = '0.3' +PLUGIN_VERSION = '0.4' PLUGIN_API_VERSIONS = ['2.0', '2.1', '2.2', '2.3', '2.6', '2.7', '2.8'] PLUGIN_LICENSE = "GPL-2.0" PLUGIN_LICENSE_URL = "https://www.gnu.org/licenses/gpl-2.0.txt" @@ -59,6 +59,7 @@ OPT_MATCH_ENABLED = 'genre_mapper_enabled' OPT_MATCH_PAIRS = 'genre_mapper_replacement_pairs' OPT_MATCH_FIRST = 'genre_mapper_apply_first_match_only' +OPT_MATCH_REGEX = 'genre_mapper_use_regex' class GenreMappingPairs(): @@ -66,7 +67,8 @@ class GenreMappingPairs(): @classmethod def refresh(cls): - log.debug("%s: Refreshing the genre replacement maps processing pairs.", PLUGIN_NAME,) + log.debug("%s: Refreshing the genre replacement maps processing pairs using '%s' translation.", + PLUGIN_NAME, 'RegEx' if config.Option.exists("setting", OPT_MATCH_REGEX) and config.setting[OPT_MATCH_REGEX] else 'Simple',) if not config.Option.exists("setting", OPT_MATCH_PAIRS): log.warning("%s: Unable to read the '%s' setting.", PLUGIN_NAME, OPT_MATCH_PAIRS,) return @@ -74,17 +76,13 @@ def refresh(cls): def _make_re(map_string): # Replace period with temporary placeholder character (newline) re_string = str(map_string).strip().replace('.', '\n') - # Convert wildcard characters to regular expression equivalents re_string = re_string.replace('*', '.*').replace('?', '.') - - # Escape carat and dollar sign in regular expression + # Escape carat and dollar sign for regular expression re_string = re_string.replace('^', '\\^').replace('$', '\\$') - # Replace temporary placeholder characters with escaped periods - # and wrap expression with '^' and '$' to force full match re_string = '^' + re_string.replace('\n', '\\.') + '$' - + # Return regular expression with carat and dollar sign to force match condition on full string return re_string cls.pairs = [] @@ -96,7 +94,7 @@ def _make_re(map_string): if not original: continue replacement = replacement.strip() - cls.pairs.append((_make_re(original), replacement)) + cls.pairs.append((original if config.setting[OPT_MATCH_REGEX] else _make_re(original), replacement)) log.debug('%s: Add genre mapping pair: "%s" = "%s"', PLUGIN_NAME, original, replacement,) if not cls.pairs: log.debug("%s: No genre replacement maps defined.", PLUGIN_NAME,) @@ -112,6 +110,7 @@ class GenreMapperOptionsPage(OptionsPage): config.TextOption("setting", OPT_MATCH_PAIRS, ''), config.BoolOption("setting", OPT_MATCH_FIRST, False), config.BoolOption("setting", OPT_MATCH_ENABLED, False), + config.BoolOption("setting", OPT_MATCH_REGEX, False), ] def __init__(self, parent=None): @@ -126,6 +125,7 @@ def load(self): self.ui.genre_mapper_replacement_pairs.setPlainText(config.setting[OPT_MATCH_PAIRS]) self.ui.genre_mapper_first_match_only.setChecked(config.setting[OPT_MATCH_FIRST]) self.ui.cb_enable_genre_mapping.setChecked(config.setting[OPT_MATCH_ENABLED]) + self.ui.cb_use_regex.setChecked(config.setting[OPT_MATCH_REGEX]) self.ui.cb_enable_genre_mapping.stateChanged.connect(self._set_enabled_state) self._set_enabled_state() @@ -134,6 +134,7 @@ def save(self): config.setting[OPT_MATCH_PAIRS] = self.ui.genre_mapper_replacement_pairs.toPlainText() config.setting[OPT_MATCH_FIRST] = self.ui.genre_mapper_first_match_only.isChecked() config.setting[OPT_MATCH_ENABLED] = self.ui.cb_enable_genre_mapping.isChecked() + config.setting[OPT_MATCH_REGEX] = self.ui.cb_use_regex.isChecked() GenreMappingPairs.refresh() @@ -151,7 +152,7 @@ def track_genre_mapper(album, metadata, *args): metadata_genres = str(metadata['genre']).split(MULTI_VALUED_JOINER) for genre in metadata_genres: for (original, replacement) in GenreMappingPairs.pairs: - if genre and re.fullmatch(original, genre, re.IGNORECASE): + if genre and re.search(original, genre, re.IGNORECASE): genre = replacement if config.setting[OPT_MATCH_FIRST]: break diff --git a/plugins/genre_mapper/options_genre_mapper.ui b/plugins/genre_mapper/options_genre_mapper.ui index ba18753c..bb85ee51 100644 --- a/plugins/genre_mapper/options_genre_mapper.ui +++ b/plugins/genre_mapper/options_genre_mapper.ui @@ -73,7 +73,7 @@ - <html><head/><body><p>These are the original / replacement pairs used to map one genre entry to another. Each pair must be entered on a separate line in the form:</p><p><span style=" font-weight:600;">[genre match test string]=[replacement genre]</span></p><p>Supported wildcards in the test string part of the mapping include '*' and '?' to match any number of characters and a single character respectively. Blank lines and lines beginning with an equals sign (=) will be ignored. Case-insensitive tests are used when matching. Replacements will be made in the order they are found in the list. An example for mapping all types of Rock genres (e.g. Country Rock, Hard Rock, Progressive Rock) to &quot;Rock&quot; would be done using the following line:</p><pre style=" margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'Courier New'; font-size:10pt; font-weight:600;">*rock*=Rock</span></pre><p>For more information please see the <a href="https://github.com/rdswift/picard-plugins/blob/2.0_RDS_Plugins/plugins/genre_mapper/docs/README.md"><span style=" text-decoration: underline; color:#0000ff;">User Guide</span></a> on GitHub.<br/></p></body></html> + <html><head/><body><p>These are the original / replacement pairs used to map one genre entry to another. Each pair must be entered on a separate line in the form:</p><p><span style=" font-weight:600;">[genre match test string]=[replacement genre]</span></p><p>Unless the &quot;regular expressions&quot; option is enabled, supported wildcards in the test string part of the mapping include '*' and '?' to match any number of characters and a single character respectively. An example for mapping all types of Rock genres (e.g. Country Rock, Hard Rock, Progressive Rock) to &quot;Rock&quot; would be done using the following line:</p><p><span style=" font-family:'Courier New'; font-size:10pt; font-weight:600;">*rock*=Rock</span></p><p>Blank lines and lines beginning with an equals sign (=) will be ignored. Case-insensitive tests are used when matching. Replacements will be made in the order they are found in the list.</p><p>For more information please see the <a href="https://github.com/rdswift/picard-plugins/blob/2.0_RDS_Plugins/plugins/genre_mapper/docs/README.md"><span style=" text-decoration: underline; color:#0000ff;">User Guide</span></a> on GitHub.<br/></p></body></html> Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop @@ -120,6 +120,19 @@ Replacement Pairs + + + + + 50 + false + + + + Match tests are entered as regular expressions + + + @@ -156,6 +169,12 @@ 10 + + true + + + QPlainTextEdit::NoWrap + Enter replacement pairs (one per line) @@ -168,6 +187,12 @@ + + cb_enable_genre_mapping + cb_use_regex + genre_mapper_first_match_only + genre_mapper_replacement_pairs + diff --git a/plugins/genre_mapper/ui_options_genre_mapper.py b/plugins/genre_mapper/ui_options_genre_mapper.py index 19e83c30..8b481eb9 100644 --- a/plugins/genre_mapper/ui_options_genre_mapper.py +++ b/plugins/genre_mapper/ui_options_genre_mapper.py @@ -58,6 +58,7 @@ def setupUi(self, GenreMapperOptionsPage): self.cb_enable_genre_mapping.setObjectName("cb_enable_genre_mapping") self.verticalLayout_4.addWidget(self.cb_enable_genre_mapping) self.gm_replacement_pairs = QtWidgets.QGroupBox(GenreMapperOptionsPage) + self.gm_replacement_pairs.setEnabled(True) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.MinimumExpanding) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) @@ -71,6 +72,13 @@ def setupUi(self, GenreMapperOptionsPage): self.gm_replacement_pairs.setObjectName("gm_replacement_pairs") self.verticalLayout_3 = QtWidgets.QVBoxLayout(self.gm_replacement_pairs) self.verticalLayout_3.setObjectName("verticalLayout_3") + self.cb_use_regex = QtWidgets.QCheckBox(self.gm_replacement_pairs) + font = QtGui.QFont() + font.setBold(False) + font.setWeight(50) + self.cb_use_regex.setFont(font) + self.cb_use_regex.setObjectName("cb_use_regex") + self.verticalLayout_3.addWidget(self.cb_use_regex) self.genre_mapper_first_match_only = QtWidgets.QCheckBox(self.gm_replacement_pairs) font = QtGui.QFont() font.setBold(False) @@ -90,6 +98,8 @@ def setupUi(self, GenreMapperOptionsPage): font.setFamily("Courier New") font.setPointSize(10) self.genre_mapper_replacement_pairs.setFont(font) + self.genre_mapper_replacement_pairs.setTabChangesFocus(True) + self.genre_mapper_replacement_pairs.setLineWrapMode(QtWidgets.QPlainTextEdit.NoWrap) self.genre_mapper_replacement_pairs.setObjectName("genre_mapper_replacement_pairs") self.verticalLayout_3.addWidget(self.genre_mapper_replacement_pairs) self.verticalLayout_4.addWidget(self.gm_replacement_pairs) @@ -97,12 +107,16 @@ def setupUi(self, GenreMapperOptionsPage): self.retranslateUi(GenreMapperOptionsPage) QtCore.QMetaObject.connectSlotsByName(GenreMapperOptionsPage) + GenreMapperOptionsPage.setTabOrder(self.cb_enable_genre_mapping, self.cb_use_regex) + GenreMapperOptionsPage.setTabOrder(self.cb_use_regex, self.genre_mapper_first_match_only) + GenreMapperOptionsPage.setTabOrder(self.genre_mapper_first_match_only, self.genre_mapper_replacement_pairs) def retranslateUi(self, GenreMapperOptionsPage): _translate = QtCore.QCoreApplication.translate self.gm_description.setTitle(_translate("GenreMapperOptionsPage", "Genre Mapper")) - self.format_description.setText(_translate("GenreMapperOptionsPage", "

These are the original / replacement pairs used to map one genre entry to another. Each pair must be entered on a separate line in the form:

[genre match test string]=[replacement genre]

Supported wildcards in the test string part of the mapping include \'*\' and \'?\' to match any number of characters and a single character respectively. Blank lines and lines beginning with an equals sign (=) will be ignored. Case-insensitive tests are used when matching. Replacements will be made in the order they are found in the list. An example for mapping all types of Rock genres (e.g. Country Rock, Hard Rock, Progressive Rock) to "Rock" would be done using the following line:

*rock*=Rock

For more information please see the User Guide on GitHub.

")) + self.format_description.setText(_translate("GenreMapperOptionsPage", "

These are the original / replacement pairs used to map one genre entry to another. Each pair must be entered on a separate line in the form:

[genre match test string]=[replacement genre]

Unless the "regular expressions" option is enabled, supported wildcards in the test string part of the mapping include \'*\' and \'?\' to match any number of characters and a single character respectively. An example for mapping all types of Rock genres (e.g. Country Rock, Hard Rock, Progressive Rock) to "Rock" would be done using the following line:

*rock*=Rock

Blank lines and lines beginning with an equals sign (=) will be ignored. Case-insensitive tests are used when matching. Replacements will be made in the order they are found in the list.

For more information please see the User Guide on GitHub.

")) self.cb_enable_genre_mapping.setText(_translate("GenreMapperOptionsPage", "Enable genre mapping")) self.gm_replacement_pairs.setTitle(_translate("GenreMapperOptionsPage", "Replacement Pairs")) + self.cb_use_regex.setText(_translate("GenreMapperOptionsPage", "Match tests are entered as regular expressions")) self.genre_mapper_first_match_only.setText(_translate("GenreMapperOptionsPage", "Apply only the first matching replacement")) self.genre_mapper_replacement_pairs.setPlaceholderText(_translate("GenreMapperOptionsPage", "Enter replacement pairs (one per line)"))