Skip to content

Commit

Permalink
py3: add new helper: get_replacements_list
Browse files Browse the repository at this point in the history
  • Loading branch information
lasers committed Dec 23, 2024
1 parent 5d73d00 commit 4feb55b
Showing 1 changed file with 67 additions and 1 deletion.
68 changes: 67 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,25 @@ 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[0], re.IGNORECASE), x[1]) for x in replacements
]

elif isinstance(replacements, dict):
for key, value in replacements.items():
if isinstance(value, list):
self._replacements[key] = [
(re.compile(x[0], re.IGNORECASE), x[1]) for x in value
]

def _get_module_info(self, module_name):
"""
THIS IS PRIVATE AND UNSUPPORTED.
Expand Down Expand Up @@ -710,6 +731,28 @@ def get_color_names_list(self, format_string, matches=None):
found.add(name)
return list(found)

def get_replacements_list(self, format_string):
"""
If possible, returns a list of filtered placeholders in ``format_string``.
"""
replacements = getattr(self._py3status_module, "replacements", None)
if not replacements or not format_string:
return []

if format_string not in self._format_placeholders:
placeholders = self._formatter.get_placeholders(format_string)
self._format_placeholders[format_string] = placeholders
else:
placeholders = self._format_placeholders[format_string]

# filter placeholders
found = set()
for replacement in replacements:
for placeholder in placeholders:
if placeholder == replacement:
found.add(placeholder)
return list(found or placeholders)

def get_placeholders_list(self, format_string, matches=None):
"""
Returns a list of placeholders in ``format_string``.
Expand Down Expand Up @@ -1214,6 +1257,29 @@ def threshold_get_color(self, value, name=None):

return color

def replace(self, value, name=None):
"""
Replace string using replacements.
:param value: string value to be replaced
:param name: accepts a name
"""
# If first run, then process the replacements data.
if self._replacements is None:
self._replacements_init()

if not value or not isinstance(value, str):
return value

name_used = name
if name_used not in self._replacements:
name_used = None

for pattern, new_replacement in self._replacements.get(name_used, []):
value = re.sub(pattern, new_replacement, value)

return value

def request(
self,
url,
Expand Down

0 comments on commit 4feb55b

Please sign in to comment.