author | Tero Marttila <terom@paivola.fi> |
Sun, 14 Sep 2014 04:41:46 +0300 | |
changeset 215 | d72e797a8977 |
parent 211 | 437c611c1caa |
child 83 | f167c68d9f34 |
permissions | -rw-r--r-- |
197
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
1 |
from django.conf import settings |
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
2 |
|
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
3 |
import codecs |
202
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
4 |
import datetime |
197
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
5 |
import logging; log = logging.getLogger('qmsk_www_pages.pages') |
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
6 |
import os, os.path |
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
7 |
|
205
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
8 |
class NotFound (Exception): |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
9 |
pass |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
10 |
|
202
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
11 |
class Site (object): |
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
12 |
@classmethod |
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
13 |
def lookup (cls): |
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
14 |
return cls( |
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
15 |
root = settings.QMSK_WWW_PAGES_DIR, |
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
16 |
name = settings.QMSK_WWW_PAGES_SITE, |
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
17 |
) |
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
18 |
|
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
19 |
def __init__ (self, root, name): |
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
20 |
self.root = root |
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
21 |
self.name = name |
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
22 |
|
199 | 23 |
class Tree (object): |
200 | 24 |
INDEX = 'index' |
25 |
||
199 | 26 |
@classmethod |
202
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
27 |
def lookup (cls, site, parts): |
205
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
28 |
""" |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
29 |
Returns Tree |
199 | 30 |
|
205
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
31 |
Raises NotFound |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
32 |
""" |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
33 |
|
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
34 |
parents = ( ) |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
35 |
tree = cls(site.root, None, parents, site, |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
36 |
title = site.name, |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
37 |
) |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
38 |
|
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
39 |
for name in parts: |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
40 |
if name.startswith('.'): |
199 | 41 |
# evil |
205
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
42 |
raise NotFound() |
199 | 43 |
|
205
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
44 |
if not name: |
199 | 45 |
continue |
46 |
||
205
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
47 |
path = os.path.join(tree.path, name) |
199 | 48 |
|
49 |
if not os.path.exists(path): |
|
205
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
50 |
raise NotFound() |
199 | 51 |
|
52 |
if not os.path.isdir(path): |
|
205
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
53 |
raise NotFound() |
199 | 54 |
|
215 | 55 |
# title |
56 |
title = tree.item_title(name) |
|
57 |
||
205
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
58 |
parents += (tree, ) |
215 | 59 |
tree = cls(path, name, parents, site, |
60 |
title = title, |
|
61 |
) |
|
205
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
62 |
|
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
63 |
return tree |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
64 |
|
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
65 |
def __init__ (self, path, name, parents, site, |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
66 |
title = None, |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
67 |
): |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
68 |
""" |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
69 |
path: filesystem path |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
70 |
name: subtree name, or None for root |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
71 |
parents: (Tree) |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
72 |
site: Site |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
73 |
""" |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
74 |
|
199 | 75 |
self.path = path |
205
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
76 |
self.name = name |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
77 |
self.parents = parents |
202
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
78 |
self.site = site |
200 | 79 |
|
205
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
80 |
self.title = title or name |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
81 |
|
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
82 |
def hierarchy (self): |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
83 |
""" |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
84 |
Yield Tree. |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
85 |
""" |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
86 |
|
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
87 |
for tree in self.parents: |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
88 |
yield tree |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
89 |
|
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
90 |
yield self |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
91 |
|
200 | 92 |
def url (self, tree=None, page=None): |
205
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
93 |
path = '/'.join(tree.name for tree in self.hierarchy() if tree.name is not None) |
200 | 94 |
|
95 |
if path: |
|
96 |
path += '/' |
|
97 |
||
98 |
if tree: |
|
99 |
path = tree + '/' |
|
100 |
||
101 |
if page: |
|
102 |
path += page |
|
103 |
||
104 |
return path |
|
105 |
||
106 |
def scan (self): |
|
107 |
""" |
|
108 |
Scan for files in tree. |
|
109 |
""" |
|
110 |
||
111 |
for filename in os.listdir(self.path): |
|
112 |
if filename.startswith('.'): |
|
113 |
continue |
|
114 |
||
115 |
if '.' in filename: |
|
116 |
file_name, file_type = filename.rsplit('.', 1) |
|
117 |
else: |
|
118 |
file_name = filename |
|
119 |
file_type = None |
|
120 |
||
121 |
if not file_name: |
|
122 |
continue |
|
123 |
||
124 |
path = os.path.join(self.path, filename) |
|
125 |
||
126 |
yield path, file_name, file_type |
|
127 |
||
209 | 128 |
def item_title (self, name): |
129 |
""" |
|
130 |
Lookup item title if exists. |
|
131 |
""" |
|
132 |
||
133 |
title_path = os.path.join(self.path, name + '.title') |
|
134 |
||
135 |
log.info("%s: %s title_path=%s", self, name, title_path) |
|
136 |
||
137 |
if os.path.exists(title_path): |
|
138 |
return open(title_path).read().strip() |
|
139 |
else: |
|
140 |
return None |
|
141 |
||
200 | 142 |
def list (self): |
143 |
""" |
|
211 | 144 |
Lists all Trees and Pages for this Tree. |
145 |
||
146 |
Yields (name, url, page_type or None, title) |
|
200 | 147 |
""" |
148 |
||
209 | 149 |
for path, name, file_type in self.scan(): |
150 |
title = self.item_title(name) or name |
|
151 |
||
200 | 152 |
# trees |
153 |
if os.path.isdir(path): |
|
211 | 154 |
yield name, self.url(tree=name), None, title |
200 | 155 |
|
209 | 156 |
if name == self.INDEX: |
200 | 157 |
continue |
158 |
||
159 |
# pages |
|
160 |
if not file_type: |
|
161 |
continue |
|
162 |
||
163 |
if file_type not in TYPES: |
|
164 |
continue |
|
209 | 165 |
|
211 | 166 |
yield name, self.url(page=name), file_type, title |
167 |
||
168 |
def list_sorted (self): |
|
169 |
return sorted(list(self.list())) |
|
200 | 170 |
|
199 | 171 |
def page (self, name): |
172 |
""" |
|
173 |
Scans through tree looking for a matching page. |
|
174 |
||
205
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
175 |
Returns Page or None. |
199 | 176 |
""" |
200 | 177 |
|
178 |
if not name: |
|
179 |
name = self.INDEX |
|
215 | 180 |
title_default = self.title |
181 |
else: |
|
182 |
title_default = None |
|
199 | 183 |
|
200 | 184 |
for path, file_name, file_type in self.scan(): |
185 |
# match on name |
|
199 | 186 |
if file_name != name: |
187 |
continue |
|
188 |
||
200 | 189 |
# match on type |
190 |
if not file_type: |
|
191 |
continue |
|
192 |
||
199 | 193 |
page_type = TYPES.get(file_type) |
194 |
||
195 |
if not page_type: |
|
196 |
continue |
|
200 | 197 |
|
198 |
# out |
|
215 | 199 |
title = self.item_title(file_name) or title_default |
209 | 200 |
|
199 | 201 |
return page_type( |
202 |
path = path, |
|
203 |
name = name, |
|
204 |
tree = self, |
|
205
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
205 |
parents = self.parents + (self, ), |
209 | 206 |
|
207 |
title = title, |
|
199 | 208 |
) |
209 |
||
197
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
210 |
class Page (object): |
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
211 |
ENCODING = 'utf-8' |
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
212 |
|
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
213 |
@classmethod |
202
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
214 |
def lookup (cls, site, page): |
197
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
215 |
""" |
205
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
216 |
Returns Page. |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
217 |
|
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
218 |
Raises NotFound |
197
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
219 |
""" |
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
220 |
|
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
221 |
log.info("page=%r", page) |
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
222 |
|
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
223 |
if page: |
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
224 |
parts = page.split('/') |
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
225 |
else: |
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
226 |
parts = [ ] |
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
227 |
|
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
228 |
if parts: |
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
229 |
page_name = parts.pop(-1) |
199 | 230 |
tree_parts = parts |
197
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
231 |
else: |
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
232 |
page_name = '' |
199 | 233 |
tree_parts = [] |
197
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
234 |
|
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
235 |
# scan dir |
202
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
236 |
tree = Tree.lookup(site, tree_parts) |
197
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
237 |
|
199 | 238 |
# scan page |
239 |
page = tree.page(page_name) |
|
197
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
240 |
|
199 | 241 |
if not page: |
205
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
242 |
raise NotFound() |
199 | 243 |
|
244 |
return page |
|
245 |
||
205
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
246 |
def __init__ (self, path, name, tree, parents=(), encoding=ENCODING, title=None): |
197
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
247 |
self.path = path |
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
248 |
self.name = name |
199 | 249 |
self.tree = tree |
205
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
250 |
self.parents = parents |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
251 |
|
197
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
252 |
self.encoding = encoding |
205
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
253 |
self.title = title or name |
197
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
254 |
|
206
440e6769976c
pages: page.hierarchy() for navigation
Tero Marttila <terom@paivola.fi>
parents:
205
diff
changeset
|
255 |
def hierarchy (self): |
440e6769976c
pages: page.hierarchy() for navigation
Tero Marttila <terom@paivola.fi>
parents:
205
diff
changeset
|
256 |
""" |
440e6769976c
pages: page.hierarchy() for navigation
Tero Marttila <terom@paivola.fi>
parents:
205
diff
changeset
|
257 |
Yield (Tree, name) pairs |
440e6769976c
pages: page.hierarchy() for navigation
Tero Marttila <terom@paivola.fi>
parents:
205
diff
changeset
|
258 |
""" |
440e6769976c
pages: page.hierarchy() for navigation
Tero Marttila <terom@paivola.fi>
parents:
205
diff
changeset
|
259 |
|
440e6769976c
pages: page.hierarchy() for navigation
Tero Marttila <terom@paivola.fi>
parents:
205
diff
changeset
|
260 |
parent = None |
440e6769976c
pages: page.hierarchy() for navigation
Tero Marttila <terom@paivola.fi>
parents:
205
diff
changeset
|
261 |
|
440e6769976c
pages: page.hierarchy() for navigation
Tero Marttila <terom@paivola.fi>
parents:
205
diff
changeset
|
262 |
for tree in self.parents: |
440e6769976c
pages: page.hierarchy() for navigation
Tero Marttila <terom@paivola.fi>
parents:
205
diff
changeset
|
263 |
if parent: |
440e6769976c
pages: page.hierarchy() for navigation
Tero Marttila <terom@paivola.fi>
parents:
205
diff
changeset
|
264 |
yield parent, tree.name |
440e6769976c
pages: page.hierarchy() for navigation
Tero Marttila <terom@paivola.fi>
parents:
205
diff
changeset
|
265 |
|
440e6769976c
pages: page.hierarchy() for navigation
Tero Marttila <terom@paivola.fi>
parents:
205
diff
changeset
|
266 |
parent = tree |
440e6769976c
pages: page.hierarchy() for navigation
Tero Marttila <terom@paivola.fi>
parents:
205
diff
changeset
|
267 |
|
440e6769976c
pages: page.hierarchy() for navigation
Tero Marttila <terom@paivola.fi>
parents:
205
diff
changeset
|
268 |
yield parent, self.name |
440e6769976c
pages: page.hierarchy() for navigation
Tero Marttila <terom@paivola.fi>
parents:
205
diff
changeset
|
269 |
|
201 | 270 |
def url (self): |
271 |
return self.tree.url(page=self.name) |
|
272 |
||
197
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
273 |
def open (self): |
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
274 |
return codecs.open(self.path, encoding=self.encoding) |
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
275 |
|
202
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
276 |
def stat (self): |
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
277 |
return os.stat(self.path) |
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
278 |
|
198 | 279 |
def breadcrumb (self): |
205
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
280 |
for tree in self.tree.hierarchy(): |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
281 |
yield tree.url(), tree.title |
200 | 282 |
|
283 |
if self.name != self.tree.INDEX: |
|
205
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
284 |
yield self.url(), self.title |
198 | 285 |
|
202
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
286 |
def modified (self): |
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
287 |
return datetime.datetime.utcfromtimestamp(self.stat().st_mtime) |
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
288 |
|
197
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
289 |
def render (self, request): |
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
290 |
raise NotImplementedError() |
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
291 |
|
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
292 |
class HTML_Page (Page): |
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
293 |
def render (self, request): |
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
294 |
return self.open().read() |
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
295 |
|
202
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
296 |
SITE = Site.lookup() |
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
297 |
|
197
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
298 |
TYPES = { |
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
299 |
'html': HTML_Page, |
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
300 |
} |
202
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
301 |
|
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
302 |
def page (page): |
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
303 |
return Page.lookup(SITE, page) |
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
304 |