From 9ba7b98fada6bdffc3f27d77c7a71ddd4d3f5713 Mon Sep 17 00:00:00 2001 From: Kyattsukuro Date: Mon, 30 Mar 2026 14:12:21 +0200 Subject: [PATCH] fixing iterative changes --- fledermaus_formatter/table_handler/Entry.py | 8 +++++ .../commands/find_and_replace.py | 3 +- .../table_handler/commands/regex_edit_col.py | 7 +++-- .../commands/regex_group_valuebased_choose.py | 5 ++-- .../commands/regex_move_to_col.py | 13 ++++---- .../commands/row_color_to_value.py | 4 ++- .../table_handler/serializer.py | 30 ++++++++----------- 7 files changed, 39 insertions(+), 31 deletions(-) create mode 100644 fledermaus_formatter/table_handler/Entry.py diff --git a/fledermaus_formatter/table_handler/Entry.py b/fledermaus_formatter/table_handler/Entry.py new file mode 100644 index 0000000..df803d2 --- /dev/null +++ b/fledermaus_formatter/table_handler/Entry.py @@ -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] \ No newline at end of file diff --git a/fledermaus_formatter/table_handler/commands/find_and_replace.py b/fledermaus_formatter/table_handler/commands/find_and_replace.py index ee562e3..bfbd21e 100644 --- a/fledermaus_formatter/table_handler/commands/find_and_replace.py +++ b/fledermaus_formatter/table_handler/commands/find_and_replace.py @@ -1,4 +1,5 @@ import re +from fledermaus_formatter.table_handler.Entry import Entry from copy import deepcopy class FindAndReplace: @@ -16,4 +17,4 @@ class FindAndReplace: res = correct_name elif not self.require_full_match: res = res.replace(bad_name, correct_name) - return res \ No newline at end of file + return Entry(value=res, fg_color=entry.fg_color, bg_color=entry.bg_color) \ No newline at end of file diff --git a/fledermaus_formatter/table_handler/commands/regex_edit_col.py b/fledermaus_formatter/table_handler/commands/regex_edit_col.py index 249b75c..b7e89bb 100644 --- a/fledermaus_formatter/table_handler/commands/regex_edit_col.py +++ b/fledermaus_formatter/table_handler/commands/regex_edit_col.py @@ -21,6 +21,7 @@ class RegexEditCol: return value if self.matching_group_rearange: - return self._reorder(value) - - return regex.sub(self.match, self.replace_with, value) \ No newline at end of file + entry.value = self._reorder(value) + else: + entry.value = regex.sub(self.match, self.replace_with, value) + return entry \ No newline at end of file diff --git a/fledermaus_formatter/table_handler/commands/regex_group_valuebased_choose.py b/fledermaus_formatter/table_handler/commands/regex_group_valuebased_choose.py index 09a7de6..4feded5 100644 --- a/fledermaus_formatter/table_handler/commands/regex_group_valuebased_choose.py +++ b/fledermaus_formatter/table_handler/commands/regex_group_valuebased_choose.py @@ -15,7 +15,8 @@ class RegexGroupValuebasedChoose: if not len(values): return None if self.highest: - return str(values[-1]) + entry.value = str(values[-1]) else: - return str(values[0]) + entry.value = str(values[0]) + return entry diff --git a/fledermaus_formatter/table_handler/commands/regex_move_to_col.py b/fledermaus_formatter/table_handler/commands/regex_move_to_col.py index f47d7b2..e911604 100644 --- a/fledermaus_formatter/table_handler/commands/regex_move_to_col.py +++ b/fledermaus_formatter/table_handler/commands/regex_move_to_col.py @@ -1,8 +1,9 @@ import regex import re +from fledermaus_formatter.table_handler.Entry import Entry 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.to = to @@ -52,14 +53,14 @@ class RegexMoveToCol: return regex.sub(repl, value) 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_source = changes.get(source_coll, row[source_coll].value) + current_to = changes.get(self.to, 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) if pending_res_to is not None: - changes[self.to] = pending_res_to - changes[source_coll] = self._remove_regex_groups(used_regex, current_source) - return changes + 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] = 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 def execute_on_row(self, row: dict[str, str], source_coll: str = None): if not source_coll: diff --git a/fledermaus_formatter/table_handler/commands/row_color_to_value.py b/fledermaus_formatter/table_handler/commands/row_color_to_value.py index 4e0e305..c33007f 100644 --- a/fledermaus_formatter/table_handler/commands/row_color_to_value.py +++ b/fledermaus_formatter/table_handler/commands/row_color_to_value.py @@ -1,3 +1,5 @@ +from fledermaus_formatter.table_handler.Entry import Entry + class RowColorToCalue: def __init__(self, target: str, value: str, @@ -52,5 +54,5 @@ class RowColorToCalue: color_pace_results.append(self._check_if_collection_matchers(color_collection, color_place)) 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 {} \ No newline at end of file diff --git a/fledermaus_formatter/table_handler/serializer.py b/fledermaus_formatter/table_handler/serializer.py index d03ed57..bee5ceb 100644 --- a/fledermaus_formatter/table_handler/serializer.py +++ b/fledermaus_formatter/table_handler/serializer.py @@ -1,9 +1,9 @@ from openpyxl import Workbook, load_workbook -from dataclasses import dataclass from pprint import pprint as print import fledermaus_formatter.instructions.mappings as mappings +from fledermaus_formatter.table_handler.Entry import Entry from .commands.find_and_replace import FindAndReplace from .commands.regex_edit_col import RegexEditCol @@ -22,14 +22,6 @@ def hex_to_rgb(hex): hex= "{:06}".format(str(hex).lstrip("#")) 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: def __init__(self, file: str, expected_input_schema: list[str], expected_output_schema: list[str]): self.wb: Workbook = load_workbook(file, read_only=True) @@ -84,7 +76,7 @@ class TableSerializer: # write rows 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): - 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) @@ -104,27 +96,29 @@ class TableSerializer: return value 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 res = None res_row = {} match instruct.get("command"): 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": 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": 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": res = RegexGroupValuebasedChoose(instruct["match"], instruct["highest"])\ - .execute_on_cell(row[source_cell]) + .execute_on_cell(in_place_working_row[source_cell]) case "CONDITIONAL_COPY": 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": 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 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) if not self.implicit_copy: continue - for col in self. expected_output_schema: + for col in self.expected_output_schema: if col in working_row: continue - working_row[col] = source_row.get(col).value + working_row[col] = source_row.get(col) self.serialized_output[rid] = working_row