Refactoring

Break up conditional tree in cell building function
This commit is contained in:
Matthias Bisping 2023-02-01 10:09:31 +01:00
parent f98256d7e9
commit fb69eb7f5c

View File

@ -881,58 +881,67 @@ class RecursiveRandomTable(RandomContentRectangle):
assert self.content.mode == "RGBA"
def fill_cells_with_content(self, cells):
for cell in cells:
def inner(cell):
size = get_size(cell)
if size <= Size.SMALL.value:
words = generate_random_words(1, 3)
return generate_text_block(cell, " ".join(words))
elif size <= Size.MEDIUM.value:
choice = rnd.choice(["plot", "recurse"])
if choice == "plot":
return generate_random_plot(cell)
elif choice == "recurse":
return generate_recursive_random_table(
cell,
border_width=1,
layout=random.choice(["open", "horizontal", "vertical"]),
double_rule=False,
)
else:
return generate_text_block(cell, f"{choice} {size:.0f} {get_size_class(cell).name}")
else:
choice = rnd.choice(["plot", "recurse"])
logger.debug(f"Generating {choice} {size:.0f} {get_size_class(cell).name}")
if choice == "plot" and is_square_like(cell):
return generate_random_plot(cell)
else:
logger.debug(f"recurse {size:.0f} {get_size_class(cell).name}")
return generate_recursive_random_table(
cell,
border_width=1,
layout=random.choice(["open", "horizontal", "vertical"]),
double_rule=False,
)
cell = inner(cell)
cell = self.build_cell(cell)
assert cell.content.mode == "RGBA"
yield cell
def build_cell(self, cell):
size = get_size(cell)
if size <= Size.SMALL.value:
return self.build_small_cell(cell)
elif size <= Size.MEDIUM.value:
return self.build_medium_sized_cell(cell)
else:
return self.build_large_cell(cell)
def build_small_cell(self, cell):
words = generate_random_words(1, 3)
return generate_text_block(cell, " ".join(words))
def build_medium_sized_cell(self, cell):
choice = rnd.choice(["plot", "recurse"])
if choice == "plot":
return generate_random_plot(cell)
elif choice == "recurse":
return generate_recursive_random_table(
cell,
border_width=1,
layout=random.choice(["open", "horizontal", "vertical"]),
double_rule=False,
)
else:
return generate_text_block(cell, f"{choice} {get_size(cell):.0f} {get_size_class(cell).name}")
def build_large_cell(self, cell):
choice = rnd.choice(["plot", "recurse"])
logger.debug(f"Generating {choice} {get_size(cell):.0f} {get_size_class(cell).name}")
if choice == "plot" and is_square_like(cell):
return generate_random_plot(cell)
else:
logger.debug(f"recurse {get_size(cell):.0f} {get_size_class(cell).name}")
return generate_recursive_random_table(
cell,
border_width=1,
layout=random.choice(["open", "horizontal", "vertical"]),
double_rule=False,
)
def draw_cell_borders(self, cells: List[ContentRectangle]):
def draw_edges_based_on_position(cell: Cell, col_idx, row_index):
# Draw the borders of the cell based on its position in the table