Merge branch 'master' of ssh://wwwjail.house/usr/local/git/base/kiosk
[kiosk.git] / page_builder.py
1 #!/usr/bin/env python3
2
3 import sys
4
5
6 class page_builder(object):
7     LAYOUT_AUTO = 0
8     LAYOUT_ONE_ITEM = 1
9     LAYOUT_FOUR_ITEMS = 2
10     LAYOUT_MANY_ITEMS = 3
11     LAYOUT_TEXT_ONE_COLUMN = 4
12     LAYOUT_TEXT_TWO_COLUMNS = 5
13
14     def __init__(self):
15         self.title = None
16         self.style = ""
17         self.layout = page_builder.LAYOUT_AUTO
18         self.items = []
19         self.debug_info = None
20         self.custom_html = None
21
22     def set_layout(self, layout: int):
23         self.layout = layout
24         return self
25
26     def set_title(self, title: str):
27         self.title = title
28         return self
29
30     def set_style(self, style: str):
31         self.style = style
32         return self
33
34     def add_item(self, item: str):
35         self.items.append(item)
36         return self
37
38     def set_debug_info(self, debug_info: bool):
39         self.debug_info = debug_info
40         return self
41
42     def __pick_layout(self) -> None:
43         if len(self.items) == 1:
44             self.layout = page_builder.LAYOUT_ONE_ITEM
45         elif len(self.items) <= 4:
46             self.layout = page_builder.LAYOUT_FOUR_ITEMS
47         else:
48             self.layout = page_builder.LAYOUT_MANY_ITEMS
49
50     def __render_custom_html(self, f) -> None:
51         if self.custom_html is not None:
52             f.write(self.custom_html)
53
54     def __render_header(self, f) -> None:
55         if self.title is not None:
56             f.write("<H1>%s</H1>\n" % self.title)
57         f.write("<HR>\n<TABLE WIDTH=99% BORDER=0>\n<TR>\n")
58         if self.style is not None:
59             f.write(self.style)
60
61     def __render_footer(self, f) -> None:
62         f.write("</TR>\n</TABLE>\n")
63
64     def render_html(self, f) -> None:
65         if self.layout == page_builder.LAYOUT_AUTO or self.layout is None:
66             self.__pick_layout()
67         self.__render_custom_html(f)
68         self.__render_header(f)
69
70         items_per_row = None
71
72         if self.layout == page_builder.LAYOUT_ONE_ITEM:
73             items_per_row = 1
74
75         elif self.layout == page_builder.LAYOUT_FOUR_ITEMS:
76             items_per_row = 2
77
78         elif self.layout == page_builder.LAYOUT_MANY_ITEMS:
79             items_per_row = 3
80
81         elif self.layout == page_builder.LAYOUT_TEXT_TWO_COLUMNS:
82             items_per_row = 1
83
84         elif self.layout == page_builder.LAYOUT_TEXT_ONE_COLUMN:
85             items_per_row = 1
86
87         else:
88             print("Error, unknown layout type: %d" % self.layout)
89             return
90
91         count = 0
92         self.items.sort(key=len, reverse=True)
93         for item in self.items:
94             f.write('<TD WIDTH=50%% STYLE="padding: 10px;">\n%s\n</TD>\n' % item)
95             count += 1
96             if count % items_per_row == 0:
97                 f.write("</TR>\n<TR>\n")
98         self.__render_footer(f)
99
100     def set_custom_html(self, html):
101         self.custom_html = html
102
103
104 # x = page_builder()
105 # x.set_title("title").add_item("item1").add_item("item2").add_item("item3").render_html(sys.stdout)