13 import template |
13 import template |
14 |
14 |
15 # path to directory containing the page heirarcy |
15 # path to directory containing the page heirarcy |
16 PAGE_DIR = "pages" |
16 PAGE_DIR = "pages" |
17 |
17 |
|
18 # path to directory containing the list of visible pages |
|
19 PAGE_LIST_FILE = os.path.join(PAGE_DIR, "list") |
|
20 |
18 class PageError (http.ResponseError) : |
21 class PageError (http.ResponseError) : |
19 """ |
22 """ |
20 Error looking up/handling a page |
23 Error looking up/handling a page |
21 """ |
24 """ |
22 |
25 |
23 pass |
26 pass |
|
27 |
|
28 class PageList (object) : |
|
29 """ |
|
30 The list of pages |
|
31 """ |
|
32 |
|
33 def __init__ (self) : |
|
34 """ |
|
35 Loads the page list from the list file |
|
36 """ |
|
37 |
|
38 # initialize list of pages |
|
39 self.pages = [] |
|
40 |
|
41 # load from file |
|
42 self._load(PAGE_LIST_FILE) |
|
43 |
|
44 def _load (self, path) : |
|
45 """ |
|
46 Processes the lines in the given file |
|
47 """ |
|
48 |
|
49 for line in open(path, 'rb') : |
|
50 # ignore whitespace |
|
51 line = line.strip() |
|
52 |
|
53 # ignore empty lines |
|
54 if not line : |
|
55 continue |
|
56 |
|
57 # parse line |
|
58 url, title = line.split(':') |
|
59 |
|
60 # add |
|
61 self._add_item(url.strip(), title.strip()) |
|
62 |
|
63 def _add_item (self, url, title) : |
|
64 """ |
|
65 Add item to pages list |
|
66 """ |
|
67 |
|
68 self.pages.append((url, title)) |
|
69 |
|
70 def get_title (self, page) : |
|
71 """ |
|
72 Gets the title for the given page, or None if not found |
|
73 """ |
|
74 |
|
75 return dict(self.pages).get(page) |
|
76 |
|
77 def get_siblings (self, page) : |
|
78 """ |
|
79 Gets the (url, title) tuple list of siblings (including the given page itself) for the given page |
|
80 """ |
|
81 |
|
82 siblings = [] |
|
83 |
|
84 # parent url |
|
85 parent = os.path.split(page.url)[0] |
|
86 |
|
87 # how many segments in the page name |
|
88 segment_count = len(page.url.split('/')) |
|
89 |
|
90 # go through all pages |
|
91 for url, title in self.pages : |
|
92 # it's a sibling if the parent is the same, and the number of segments it the same |
|
93 if url.startswith(parent) and len(url.split('/')) == segment_count : |
|
94 siblings.append((url, title)) |
|
95 |
|
96 # return |
|
97 return siblings |
|
98 |
|
99 # global singleton instance |
|
100 page_list = PageList() |
24 |
101 |
25 class Page (object) : |
102 class Page (object) : |
26 """ |
103 """ |
27 This object represents the information about our attempt to render some specific page |
104 This object represents the information about our attempt to render some specific page |
28 """ |
105 """ |
51 Do initial data loading, etc |
128 Do initial data loading, etc |
52 """ |
129 """ |
53 |
130 |
54 pass |
131 pass |
55 |
132 |
56 def get_title (self) : |
133 @property |
|
134 def title (self) : |
57 """ |
135 """ |
58 Return the page's title |
136 Return the page's title |
59 |
137 |
60 Defaults to the Titlecase'd file basename |
138 Defaults to the retreiving the page title from page_list, or basename in Titlecase. |
61 """ |
139 """ |
62 |
140 |
63 return self.basename.title() |
141 # lookup in page_list |
64 |
142 title = page_list.get_title(self.url) |
65 def get_content (self) : |
143 |
|
144 # fallback to titlecase |
|
145 if not title : |
|
146 title = self.basename.title() |
|
147 |
|
148 return title |
|
149 |
|
150 @property |
|
151 def content (self) : |
66 """ |
152 """ |
67 Return the page content as a string |
153 Return the page content as a string |
68 """ |
154 """ |
69 |
155 |
70 abstract |
156 abstract |
72 class HTMLPage (Page) : |
158 class HTMLPage (Page) : |
73 """ |
159 """ |
74 A simple .html page that's just passed through directly |
160 A simple .html page that's just passed through directly |
75 """ |
161 """ |
76 |
162 |
77 def get_content (self) : |
163 @property |
|
164 def content (self) : |
78 """ |
165 """ |
79 Opens the .html file, reads and returns contents |
166 Opens the .html file, reads and returns contents |
80 """ |
167 """ |
81 |
168 |
82 return open(self.path, 'rb').read() |
169 return open(self.path, 'rb').read() |
83 |
170 |
84 class TemplatePage (Page) : |
171 class TemplatePage (Page) : |
85 """ |
172 """ |
86 A template that's rendered using our template library |
173 A template that's rendered using our template library |
87 """ |
174 """ |
88 |
175 |
89 def get_content (self) : |
176 @property |
|
177 def content (self) : |
90 """ |
178 """ |
91 Loads the .tmpl file, and renders it |
179 Loads the .tmpl file, and renders it |
92 """ |
180 """ |
93 |
181 |
94 return template.render_file(self.path) |
182 return template.render_file(self.path) |