Skip to content

Commit

Permalink
make self.py3.replace helper
Browse files Browse the repository at this point in the history
  • Loading branch information
lasers committed Mar 27, 2024
1 parent 781e4f6 commit 9f0ba08
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
17 changes: 17 additions & 0 deletions py3status/modules/playerctl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")]})
Expand Down Expand Up @@ -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
Expand All @@ -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)

Expand Down Expand Up @@ -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:
Expand Down
43 changes: 42 additions & 1 deletion py3status/py3.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import re
import shlex
import sys
import time
Expand Down Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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):
Expand All @@ -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.
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 9f0ba08

Please sign in to comment.