24 lines
983 B
Python
24 lines
983 B
Python
import re
|
|
from fledermaus_formatter.table_handler.Entry import Entry
|
|
from copy import deepcopy
|
|
|
|
class FindAndReplace:
|
|
def __init__(self, map: dict[str, list[str]], reqire_full_match: bool = False):
|
|
self.map = map
|
|
self.require_full_match = reqire_full_match
|
|
|
|
def execute_on_cell(self, entry) -> str:
|
|
res = deepcopy(entry.value)
|
|
|
|
for correct_name, bad_names in self.map.items():
|
|
for bad_name in bad_names:
|
|
if self.require_full_match and bad_name == res:
|
|
# Todo remove random leading/trailing spaces before if
|
|
res = correct_name
|
|
break
|
|
elif not self.require_full_match:
|
|
prior_val = res
|
|
res = res.replace(bad_name, correct_name)
|
|
if prior_val != res or correct_name in res:
|
|
break
|
|
return Entry(value=res, fg_color=entry.fg_color, bg_color=entry.bg_color) |