fixing iterative changes
This commit is contained in:
parent
1e8704cebf
commit
9ba7b98fad
8
fledermaus_formatter/table_handler/Entry.py
Normal file
8
fledermaus_formatter/table_handler/Entry.py
Normal 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]
|
||||
@ -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
|
||||
return Entry(value=res, fg_color=entry.fg_color, bg_color=entry.bg_color)
|
||||
@ -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)
|
||||
entry.value = self._reorder(value)
|
||||
else:
|
||||
entry.value = regex.sub(self.match, self.replace_with, value)
|
||||
return entry
|
||||
@ -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
|
||||
|
||||
|
||||
@ -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,13 +53,13 @@ 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)
|
||||
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):
|
||||
|
||||
@ -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 {}
|
||||
@ -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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user