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