author | Tero Marttila <terom@paivola.fi> |
Sun, 14 Sep 2014 02:52:40 +0300 | |
changeset 68 | 023b9a9d6c76 |
parent 65 | 67d8600fa0a1 |
child 69 | 4b8814ff6d6d |
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 |
||
123 |
def list (self): |
|
124 |
""" |
|
125 |
Lists all Trees and Pages for this Tree |
|
126 |
""" |
|
127 |
||
128 |
for path, file_name, file_type in self.scan(): |
|
129 |
# trees |
|
130 |
if os.path.isdir(path): |
|
65
67d8600fa0a1
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
64
diff
changeset
|
131 |
yield self.url(tree=file_name), file_name, None |
63 | 132 |
|
133 |
if file_name == self.INDEX: |
|
134 |
continue |
|
135 |
||
136 |
# pages |
|
137 |
if not file_type: |
|
138 |
continue |
|
139 |
||
140 |
if file_type not in TYPES: |
|
141 |
continue |
|
142 |
||
65
67d8600fa0a1
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
64
diff
changeset
|
143 |
yield self.url(page=file_name), file_name, file_type |
63 | 144 |
|
62 | 145 |
def page (self, name): |
146 |
""" |
|
147 |
Scans through tree looking for a matching page. |
|
148 |
||
68
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
149 |
Returns Page or None. |
62 | 150 |
""" |
63 | 151 |
|
152 |
if not name: |
|
153 |
name = self.INDEX |
|
62 | 154 |
|
63 | 155 |
for path, file_name, file_type in self.scan(): |
156 |
# match on name |
|
62 | 157 |
if file_name != name: |
158 |
continue |
|
159 |
||
63 | 160 |
# match on type |
161 |
if not file_type: |
|
162 |
continue |
|
163 |
||
62 | 164 |
page_type = TYPES.get(file_type) |
165 |
||
166 |
if not page_type: |
|
167 |
continue |
|
63 | 168 |
|
169 |
# out |
|
62 | 170 |
return page_type( |
171 |
path = path, |
|
172 |
name = name, |
|
173 |
tree = self, |
|
68
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
174 |
parents = self.parents + (self, ), |
62 | 175 |
) |
176 |
||
60
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
177 |
class Page (object): |
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
178 |
ENCODING = 'utf-8' |
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
179 |
|
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
180 |
@classmethod |
65
67d8600fa0a1
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
64
diff
changeset
|
181 |
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
|
182 |
""" |
68
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
183 |
Returns Page. |
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
184 |
|
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
185 |
Raises NotFound |
60
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
186 |
""" |
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
187 |
|
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
188 |
log.info("page=%r", page) |
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
189 |
|
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
190 |
if page: |
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
191 |
parts = page.split('/') |
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
192 |
else: |
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
193 |
parts = [ ] |
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
194 |
|
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
195 |
if parts: |
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
196 |
page_name = parts.pop(-1) |
62 | 197 |
tree_parts = parts |
60
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
198 |
else: |
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
199 |
page_name = '' |
62 | 200 |
tree_parts = [] |
60
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
201 |
|
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
202 |
# scan dir |
65
67d8600fa0a1
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
64
diff
changeset
|
203 |
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
|
204 |
|
62 | 205 |
# scan page |
206 |
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
|
207 |
|
62 | 208 |
if not page: |
68
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
209 |
raise NotFound() |
62 | 210 |
|
211 |
return page |
|
212 |
||
68
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
213 |
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
|
214 |
self.path = path |
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
215 |
self.name = name |
62 | 216 |
self.tree = tree |
68
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
217 |
self.parents = parents |
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
218 |
|
60
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
219 |
self.encoding = encoding |
68
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
220 |
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
|
221 |
|
64 | 222 |
def url (self): |
223 |
return self.tree.url(page=self.name) |
|
224 |
||
60
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
225 |
def open (self): |
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
226 |
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
|
227 |
|
65
67d8600fa0a1
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
64
diff
changeset
|
228 |
def stat (self): |
67d8600fa0a1
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
64
diff
changeset
|
229 |
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
|
230 |
|
61 | 231 |
def breadcrumb (self): |
68
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
232 |
for tree in self.tree.hierarchy(): |
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
233 |
yield tree.url(), tree.title |
63 | 234 |
|
235 |
if self.name != self.tree.INDEX: |
|
68
023b9a9d6c76
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
65
diff
changeset
|
236 |
yield self.url(), self.title |
61 | 237 |
|
65
67d8600fa0a1
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
64
diff
changeset
|
238 |
def modified (self): |
67d8600fa0a1
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
64
diff
changeset
|
239 |
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
|
240 |
|
60
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
241 |
def render (self, request): |
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
242 |
raise NotImplementedError() |
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
243 |
|
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
244 |
class HTML_Page (Page): |
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
245 |
def render (self, request): |
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
246 |
return self.open().read() |
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
247 |
|
65
67d8600fa0a1
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
64
diff
changeset
|
248 |
SITE = Site.lookup() |
67d8600fa0a1
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
64
diff
changeset
|
249 |
|
60
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
250 |
TYPES = { |
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
251 |
'html': HTML_Page, |
bd9cea0b5b11
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
252 |
} |
65
67d8600fa0a1
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
64
diff
changeset
|
253 |
|
67d8600fa0a1
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
64
diff
changeset
|
254 |
def page (page): |
67d8600fa0a1
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
64
diff
changeset
|
255 |
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
|
256 |