diff --git a/py3status/modules/playerctl.py b/py3status/modules/playerctl.py index 605317a884..8fcf00490b 100644 --- a/py3status/modules/playerctl.py +++ b/py3status/modules/playerctl.py @@ -25,6 +25,11 @@ '[\?if=status=Stopped .. ][[{artist}][\?soft - ][{title}|{player}]]]')* format_player_separator: show separator if more than one player (default ' ') players: list of players to track. An empty list tracks all players (default []) + replacements: specify string replacements to use on placeholders + (default {"artist": [ "([\(\[][^)\]]*?(bonus|demo|edit|explicit|extended|' + 'feat|mono|remaster|stereo|version)[^)\]]*?[\)\]])", + "([\-,;/])([^\-,;/])*(bonus|demo|edit|explicit|extended|feat|mono|' + 'remaster|stereo|version).*"]})* seek_delta: time (in seconds) to change the playback's position by (default 5) thresholds: specify color thresholds to use for different placeholders (default {"status": [("Playing", "good"), ("Paused", "degraded"), ("Stopped", "bad")]}) @@ -99,6 +104,12 @@ class Py3status: ) format_player_separator = " " players = [] + replacements = { + "artist": [ + "([\(\[][^)\]]*?(bonus|demo|edit|explicit|extended|feat|mono|remaster|stereo|version)[^)\]]*?[\)\]])", + "([\-,;/])([^\-,;/])*(bonus|demo|edit|explicit|extended|feat|mono|remaster|stereo|version).*" + ], + } seek_delta = 5 thresholds = {"status": [("Playing", "good"), ("Paused", "degraded"), ("Stopped", "bad")]} volume_delta = 10 @@ -116,6 +127,7 @@ class Meta: def post_config_hook(self): self.thresholds_init = self.py3.get_color_names_list(self.format_player) + self.placeholders = self.py3.get_placeholders_list(self.format_player) self.position = self.py3.format_contains(self.format_player, "position") self.cache_timeout = getattr(self, "cache_timeout", 1) @@ -279,6 +291,11 @@ def playerctl(self): if self.position and player_data["status"] == "Playing" and player_data["position"]: cached_until = self.cache_timeout + # Replace the values + for x in self.placeholders: + for x in player_data: + player_data[x] = self.py3.replace(player_data[x], x) + # Set the color of a player for key in self.thresholds_init: if key in player_data: diff --git a/py3status/py3.py b/py3status/py3.py index 8550f89f1b..0c85cb3ef9 100644 --- a/py3status/py3.py +++ b/py3status/py3.py @@ -1,4 +1,5 @@ import os +import re import shlex import sys import time @@ -102,6 +103,7 @@ def __init__(self, module=None): self._format_placeholders = {} self._format_placeholders_cache = {} self._module = module + self._replacements = None self._report_exception_cache = set() self._thresholds = None self._threshold_gradients = {} @@ -177,7 +179,7 @@ def _thresholds_init(self): except TypeError: pass self._thresholds[None] = [(x[0], self._get_color(x[1])) for x in thresholds] - return + elif isinstance(thresholds, dict): for key, value in thresholds.items(): if isinstance(value, list): @@ -187,6 +189,21 @@ def _thresholds_init(self): pass self._thresholds[key] = [(x[0], self._get_color(x[1])) for x in value] + def _replacements_init(self): + """ + Initiate and check any replacements set + """ + replacements = getattr(self._py3status_module, "replacements", []) + self._replacements = {} + + if isinstance(replacements, list): + self._replacements[None] = [re.compile(x, re.IGNORECASE) for x in replacements] + + elif isinstance(replacements, dict): + for key, value in replacements.items(): + if isinstance(value, list): + self._replacements[key] = [re.compile(x, re.IGNORECASE) for x in value] + def _get_module_info(self, module_name): """ THIS IS PRIVATE AND UNSUPPORTED. @@ -1214,6 +1231,30 @@ def threshold_get_color(self, value, name=None): return color + def replace(self, value, key=None): + """ + Replace string using replacements. + + :param value: string value to be replaced + :param key: accepts a key name + """ + # If first run, then process the replacements data. + if self._replacements is None: + self._replacements_init() + + if not isinstance(value, str): + return value + + name_used = name + if name_used not in self._replacements: + name_used = None + + replacements = self._replacements.get(name_used, []) + for pattern in replacements: + value = re.sub(pattern, "", value) + + return value + def request( self, url,