Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
(
[[TableCell("Main title", colspan=3)], ["ISBN", "Title", "Author"]],
[],
"default",
"""+------+-------+--------+
| Main title |
+------+-------+--------+
| ISBN | Title | Author |
+------+-------+--------+
""",
),
(
[],
[
[
TableCell("1", colspan=3),
TableCell("2", colspan=2),
TableCell("3", colspan=2),
TableCell("4", colspan=2),
]
],
"default",
"""+--+--+--+--+--+--+--+--+--+
| 1 | 2 | 3 | 4 |
+--+--+--+--+--+--+--+--+--+
""",
),
]
@property
def render_data(self):
return copy.deepcopy(self._render_data)
[[TableCell("Main title", colspan=3)], ["ISBN", "Title", "Author"]],
[],
"default",
"""+------+-------+--------+
| Main title |
+------+-------+--------+
| ISBN | Title | Author |
+------+-------+--------+
""",
),
(
[],
[
[
TableCell("1", colspan=3),
TableCell("2", colspan=2),
TableCell("3", colspan=2),
TableCell("4", colspan=2),
]
],
"default",
"""+--+--+--+--+--+--+--+--+--+
| 1 | 2 | 3 | 4 |
+--+--+--+--+--+--+--+--+--+
""",
),
]
@property
def render_data(self):
return copy.deepcopy(self._render_data)
"default",
"""+------+-------+--------+
| Main title |
+------+-------+--------+
| ISBN | Title | Author |
+------+-------+--------+
""",
),
(
[],
[
[
TableCell("1", colspan=3),
TableCell("2", colspan=2),
TableCell("3", colspan=2),
TableCell("4", colspan=2),
]
],
"default",
"""+--+--+--+--+--+--+--+--+--+
| 1 | 2 | 3 | 4 |
+--+--+--+--+--+--+--+--+--+
""",
),
]
@property
def render_data(self):
return copy.deepcopy(self._render_data)
def setUp(self):
self.stream = BytesIO()
[TableCell("9971-5-0210-0", rowspan=3, colspan=1), "Dante Alighieri"],
[TableSeparator()],
["Charles Dickens"],
],
"default",
"""+---------------+-----------------+
| ISBN | Author |
+---------------+-----------------+
| 9971-5-0210-0 | Dante Alighieri |
| |-----------------|
| | Charles Dickens |
+---------------+-----------------+
""",
),
(
[[TableCell("Main title", colspan=3)], ["ISBN", "Title", "Author"]],
[],
"default",
"""+------+-------+--------+
| Main title |
+------+-------+--------+
| ISBN | Title | Author |
+------+-------+--------+
""",
),
(
[],
[
[
TableCell("1", colspan=3),
TableCell("2", colspan=2),
TableCell("3", colspan=2),
| 021 | |
| 0-0 | |
+-----------------+-------+-----------------+
""",
),
(
["ISBN", "Title", "Author"],
[
[
TableCell("9971\n-5-\n021\n0-0", rowspan=2, colspan=2),
"Dante Alighieri",
],
["Charles Dickens"],
[
"Dante Alighieri",
TableCell("9971\n-5-\n021\n0-0", rowspan=2, colspan=2),
],
["Charles Dickens"],
],
"default",
"""+-----------------+-------+-----------------+
| ISBN | Title | Author |
+-----------------+-------+-----------------+
| 9971 | Dante Alighieri |
| -5- | Charles Dickens |
| 021 | |
| 0-0 | |
| Dante Alighieri | 9971 |
| Charles Dickens | -5- |
| | 021 |
| | 0-0 |
+-----------------+-------+-----------------+
:rtype: list
"""
unmerged_rows = OrderedDict()
for column, cell in enumerate(rows[line]):
if isinstance(cell, TableCell) and cell.rowspan > 1:
nb_lines = cell.rowspan - 1
lines = [cell]
if '\n' in cell:
lines = cell.split('\n')
if len(lines) > nb_lines:
nb_lines = cell.count('\n')
rows[line][column] = TableCell(lines[0], colspan=cell.colspan)
# Create a two dimensional array (rowspan x colspan)
placeholder = OrderedDict([(k, OrderedDict()) for k in range(line + 1, line + 1 + nb_lines)])
for k, v in unmerged_rows.items():
if k in placeholder:
for l, m in unmerged_rows[k].items():
if l in placeholder[k]:
placeholder[k][l].update(m)
else:
placeholder[k][l] = m
else:
placeholder[k] = v
unmerged_rows = placeholder
for unmerged_row_key, unmerged_row in unmerged_rows.items():
def _fill_cells(self, row):
"""
Fill cells for a row that contains colspan > 1.
:type row: list
:rtype: list
"""
new_row = []
for column, cell in enumerate(row):
new_row.append(cell)
if isinstance(cell, TableCell) and cell.colspan > 1:
for position in range(column + 1, column + cell.colspan):
# insert empty value at column position
new_row.append('')
if new_row:
return new_row
return row
def _copy_row(self, rows, line):
"""
Copy a row
:type rows: list
:type line: int
:rtype: list
"""
row = [x for x in rows[line]]
for cell_key, cell_value in enumerate(row):
row[cell_key] = ''
if isinstance(cell_value, TableCell):
row[cell_key] = TableCell('', colspan=cell_value.colspan)
return row
for l, m in unmerged_rows[k].items():
if l in placeholder[k]:
placeholder[k][l].update(m)
else:
placeholder[k][l] = m
else:
placeholder[k] = v
unmerged_rows = placeholder
for unmerged_row_key, unmerged_row in unmerged_rows.items():
value = ''
if unmerged_row_key - line < len(lines):
value = lines[unmerged_row_key - line]
unmerged_rows[unmerged_row_key][column] = TableCell(value, colspan=cell.colspan)
for unmerged_row_key, unmerged_row in unmerged_rows.items():
# we need to know if unmerged_row will be merged or inserted into rows
if (unmerged_row_key < len(rows)
and isinstance(rows[unmerged_row_key], list)
and (self._get_number_of_columns(rows[unmerged_row_key])
+ self._get_number_of_columns(list(unmerged_rows[unmerged_row_key].values()))
<= self._number_of_columns)):
# insert cell into row at cell_key position
for cell_key, cell in unmerged_row.items():
rows[unmerged_row_key].insert(cell_key, cell)
else:
row = self._copy_row(rows, unmerged_row_key - 1)
for column, cell in unmerged_row.items():
if len(cell):
row[column] = unmerged_row[column]