1 from ..grid
import symbols
3 class GridRenderer(object):
4 """Class that renders a grid in some human-consumable form.
6 This base class's methods result in a flat string of symbols, but it's easy
7 to create a more useful rendering by just overriding a few methods.
10 def render_grid(self
, grid
):
11 """Renders the entire grid. Code on the outside probably only needs to
18 parts
.append(self
.before_grid(grid
))
19 for idx
, row
in enumerate(grid
.rows
):
20 if idx
and idx % grid
.box_height
== 0:
21 parts
.append(self
.inside_grid(grid
))
22 parts
.append(self
.render_row(row
))
23 parts
.append(self
.after_grid(grid
))
27 def render_row(self
, row
):
28 """Renders a single row."""
31 parts
.append(self
.before_row(row
))
32 for idx
, cell
in enumerate(row
.cells
):
33 if idx
and idx % row
.grid
.box_width
== 0:
34 parts
.append(self
.inside_row(row
))
35 parts
.extend(self
.render_cell(cell
))
36 parts
.append(self
.after_row(row
))
40 def render_cell(self
, cell
):
41 """Renders a single cell."""
43 self
.before_cell(cell
),
44 self
.inside_cell(cell
),
45 self
.after_cell(cell
),
49 def before_grid(self
, grid
):
50 """Content prepended before the entire grid."""
51 return self
.inside_grid(grid
)
53 def inside_grid(self
, grid
):
54 """Content inserted within the grid, between boxes."""
57 def after_grid(self
, grid
):
58 """Content appended after the entire grid."""
59 return self
.inside_grid(grid
)
62 def before_row(self
, row
):
63 """Content prepended before each row."""
64 return self
.inside_row(row
)
66 def inside_row(self
, row
):
67 """Content inserted within each row, between boxes."""
70 def after_row(self
, row
):
71 """Content appended after each row.
73 Note that newlines are not assumed; you must provide one if you want
74 one included in the rendering.
77 return self
.inside_row(row
)
80 def before_cell(self
, cell
):
81 """Content prepended before each cell in a row."""
84 def inside_cell(self
, cell
):
85 """Content used to represent the actual cell value."""
87 return symbols
[cell
.value
]
91 def after_cell(self
, cell
):
92 """Content appended after each row in a cell."""