fixing iterative changes

This commit is contained in:
Kyattsukuro 2026-03-30 14:12:21 +02:00
parent 1e8704cebf
commit 195cda37bd
8 changed files with 40 additions and 32 deletions

View File

@ -59,7 +59,7 @@ def main():
args = parser.parse_args() args = parser.parse_args()
if args.v: if args.v:
print("1.2") print("1.3")
return return
if not args.type: if not args.type:
parser.error("The --type argument is required.") parser.error("The --type argument is required.")

View File

@ -0,0 +1,8 @@
from dataclasses import dataclass
@dataclass
class Entry:
value: str|int|float|bool|None
bg_color: tuple[str, str, str]
fg_color: tuple[str, str, str]

View File

@ -1,4 +1,5 @@
import re import re
from fledermaus_formatter.table_handler.Entry import Entry
from copy import deepcopy from copy import deepcopy
class FindAndReplace: class FindAndReplace:
@ -16,4 +17,4 @@ class FindAndReplace:
res = correct_name res = correct_name
elif not self.require_full_match: elif not self.require_full_match:
res = res.replace(bad_name, correct_name) res = res.replace(bad_name, correct_name)
return res return Entry(value=res, fg_color=entry.fg_color, bg_color=entry.bg_color)

View File

@ -21,6 +21,7 @@ class RegexEditCol:
return value return value
if self.matching_group_rearange: if self.matching_group_rearange:
return self._reorder(value) entry.value = self._reorder(value)
else:
return regex.sub(self.match, self.replace_with, value) entry.value = regex.sub(self.match, self.replace_with, value)
return entry

View File

@ -15,7 +15,8 @@ class RegexGroupValuebasedChoose:
if not len(values): if not len(values):
return None return None
if self.highest: if self.highest:
return str(values[-1]) entry.value = str(values[-1])
else: else:
return str(values[0]) entry.value = str(values[0])
return entry

View File

@ -1,8 +1,9 @@
import regex import regex
import re import re
from fledermaus_formatter.table_handler.Entry import Entry
class RegexMoveToCol: class RegexMoveToCol:
def __init__(self, match: str, to: str, with_input_iteration: list[str]|None = None): def __init__(self, match: str, to: str, with_input_iteration: list[list[str]]|None = None):
self.match = match self.match = match
self.to = to self.to = to
@ -52,14 +53,14 @@ class RegexMoveToCol:
return regex.sub(repl, value) return regex.sub(repl, value)
def do_changes(self, source_coll: str, used_regex: str, row: dict[str, str], changes={}): def do_changes(self, source_coll: str, used_regex: str, row: dict[str, str], changes={}):
current_to = changes.get(self.to, row[source_coll].value) current_to = changes.get(self.to, row[source_coll]).value
current_source = changes.get(source_coll, row[source_coll].value) current_source = changes.get(source_coll, row[source_coll]).value
pending_res_to = self._get_merged_groups(used_regex, current_to) pending_res_to = self._get_merged_groups(used_regex, current_to)
if pending_res_to is not None: if pending_res_to is not None:
changes[self.to] = pending_res_to changes[self.to] = Entry(value=pending_res_to, fg_color=row[self.to].fg_color, bg_color=row[self.to].bg_color)
changes[source_coll] = self._remove_regex_groups(used_regex, current_source) changes[source_coll] = Entry(value=self._remove_regex_groups(used_regex, current_source), fg_color=row[source_coll].fg_color, bg_color=row[source_coll].bg_color)
return changes return changes
def execute_on_row(self, row: dict[str, str], source_coll: str = None): def execute_on_row(self, row: dict[str, str], source_coll: str = None):
if not source_coll: if not source_coll:

View File

@ -1,3 +1,5 @@
from fledermaus_formatter.table_handler.Entry import Entry
class RowColorToCalue: class RowColorToCalue:
def __init__(self, target: str, def __init__(self, target: str,
value: str, value: str,
@ -52,5 +54,5 @@ class RowColorToCalue:
color_pace_results.append(self._check_if_collection_matchers(color_collection, color_place)) color_pace_results.append(self._check_if_collection_matchers(color_collection, color_place))
if any(color_pace_results): if any(color_pace_results):
return {self.target: self.value} return {self.target: Entry(value=self.value, fg_color=row[self.target].fg_color, bg_color=row[self.target].bg_color)}
return {} return {}

View File

@ -1,9 +1,9 @@
from openpyxl import Workbook, load_workbook from openpyxl import Workbook, load_workbook
from dataclasses import dataclass
from pprint import pprint as print from pprint import pprint as print
import fledermaus_formatter.instructions.mappings as mappings import fledermaus_formatter.instructions.mappings as mappings
from fledermaus_formatter.table_handler.Entry import Entry
from .commands.find_and_replace import FindAndReplace from .commands.find_and_replace import FindAndReplace
from .commands.regex_edit_col import RegexEditCol from .commands.regex_edit_col import RegexEditCol
@ -22,14 +22,6 @@ def hex_to_rgb(hex):
hex= "{:06}".format(str(hex).lstrip("#")) hex= "{:06}".format(str(hex).lstrip("#"))
return tuple(int(hex[i:i+2],16) for i in (0, 2, 4)) return tuple(int(hex[i:i+2],16) for i in (0, 2, 4))
@dataclass
class Entry:
value: str|int|float|bool|None
bg_color: tuple[str, str, str]
fg_color: tuple[str, str, str]
class TableSerializer: class TableSerializer:
def __init__(self, file: str, expected_input_schema: list[str], expected_output_schema: list[str]): def __init__(self, file: str, expected_input_schema: list[str], expected_output_schema: list[str]):
self.wb: Workbook = load_workbook(file, read_only=True) self.wb: Workbook = load_workbook(file, read_only=True)
@ -84,7 +76,7 @@ class TableSerializer:
# write rows # write rows
for row_i, (row_id, row_data) in enumerate(self.serialized_output.items(), start=2 if self.output_header else 1): for row_i, (row_id, row_data) in enumerate(self.serialized_output.items(), start=2 if self.output_header else 1):
for col_i, col_name in enumerate(self.expected_output_schema, start=1): for col_i, col_name in enumerate(self.expected_output_schema, start=1):
ows.cell(row=row_i, column=col_i, value=row_data.get(col_name)) ows.cell(row=row_i, column=col_i, value=row_data.get(col_name).value if row_data.get(col_name) else "")
owb.save(destination_file) owb.save(destination_file)
@ -104,27 +96,29 @@ class TableSerializer:
return value return value
def _extecute_instruct(self, source_cell: str, instruct: dict, row: dict[str, any], working_row: dict[str, any]): def _extecute_instruct(self, source_cell: str, instruct: dict, row: dict[str, any], working_row: dict[str, any]):
in_place_working_row = {**row, **working_row}
# execute changes # execute changes
res = None res = None
res_row = {} res_row = {}
match instruct.get("command"): match instruct.get("command"):
case "FIND_AND_REPLACE": case "FIND_AND_REPLACE":
res = FindAndReplace(instruct["map"], instruct.get("reqire_full_match", False)).execute_on_cell(row[source_cell]) res = FindAndReplace(instruct["map"], instruct.get("reqire_full_match", False)).execute_on_cell(in_place_working_row[source_cell])
case "REGEX_MOVE_TO_COL": case "REGEX_MOVE_TO_COL":
res_row = RegexMoveToCol(instruct["match"], instruct["to"], instruct.get("with_input_iteration")) \ res_row = RegexMoveToCol(instruct["match"], instruct["to"], instruct.get("with_input_iteration")) \
.execute_on_row(row, source_cell) .execute_on_row(in_place_working_row, source_cell)
case "REGEX_EDIT_COL": case "REGEX_EDIT_COL":
res = RegexEditCol(instruct["match"], instruct["replace_with"], instruct.get("matching_group_rearange"))\ res = RegexEditCol(instruct["match"], instruct["replace_with"], instruct.get("matching_group_rearange"))\
.execute_on_cell(row[source_cell]) .execute_on_cell(in_place_working_row[source_cell])
case "REGEX_GROUP_VALUEBASED_CHOOS": case "REGEX_GROUP_VALUEBASED_CHOOS":
res = RegexGroupValuebasedChoose(instruct["match"], instruct["highest"])\ res = RegexGroupValuebasedChoose(instruct["match"], instruct["highest"])\
.execute_on_cell(row[source_cell]) .execute_on_cell(in_place_working_row[source_cell])
case "CONDITIONAL_COPY": case "CONDITIONAL_COPY":
res_row = Copy(instruct["source"], instruct["target"], replace=instruct.get("replace"))\ res_row = Copy(instruct["source"], instruct["target"], replace=instruct.get("replace"))\
.execute_on_row(row) .execute_on_row(in_place_working_row)
case "ROW_COLOR_TO_BOOL": case "ROW_COLOR_TO_BOOL":
res_row = RowColorToCalue(instruct["target"], instruct["value"], instruct.get("fg_colors"), instruct.get("bg_colors"), instruct.get("approximate"), instruct.get("threshold"))\ res_row = RowColorToCalue(instruct["target"], instruct["value"], instruct.get("fg_colors"), instruct.get("bg_colors"), instruct.get("approximate"), instruct.get("threshold"))\
.execute_on_row(row) .execute_on_row(in_place_working_row)
# Merge row_changes to working_row # Merge row_changes to working_row
for target_cel, change in res_row.items(): for target_cel, change in res_row.items():
@ -161,8 +155,8 @@ class TableSerializer:
working_row = self._extecute_instruct(col, instruct, source_row, working_row) working_row = self._extecute_instruct(col, instruct, source_row, working_row)
if not self.implicit_copy: if not self.implicit_copy:
continue continue
for col in self. expected_output_schema: for col in self.expected_output_schema:
if col in working_row: if col in working_row:
continue continue
working_row[col] = source_row.get(col).value working_row[col] = source_row.get(col)
self.serialized_output[rid] = working_row self.serialized_output[rid] = working_row