| 1 |
"""A baseclass for the SkeletonPage template""" |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
import os |
| 7 |
import time |
| 8 |
|
| 9 |
|
| 10 |
from Cheetah.Template import Template |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
class _SkeletonPage(Template): |
| 17 |
"""A baseclass for the SkeletonPage template""" |
| 18 |
|
| 19 |
docType = '<!DOCTYPE HTML PUBLIC ' \ |
| 20 |
'"-//W3C//DTD HTML 4.01 Transitional//EN" ' \ |
| 21 |
'"http://www.w3.org/TR/html4/loose.dtd">' |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
title = '' |
| 28 |
siteDomainName = 'www.example.com' |
| 29 |
siteCredits = 'Designed & Implemented by Tavis Rudd' |
| 30 |
siteCopyrightName = "Tavis Rudd" |
| 31 |
htmlTag = '<html>' |
| 32 |
|
| 33 |
def __init__(self, *args, **KWs): |
| 34 |
Template.__init__(self, *args, **KWs) |
| 35 |
self._metaTags = { |
| 36 |
'HTTP-EQUIV': { |
| 37 |
'keywords': 'Cheetah', |
| 38 |
'Content-Type': 'text/html; charset=iso-8859-1', |
| 39 |
}, |
| 40 |
'NAME': { |
| 41 |
'generator': 'Cheetah: The Python-Powered Template Engine' |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
self._stylesheets = {} |
| 49 |
|
| 50 |
self._stylesheetsOrder = [] |
| 51 |
|
| 52 |
self._stylesheetLibs = {} |
| 53 |
|
| 54 |
self._javascriptLibs = {} |
| 55 |
self._javascriptTags = {} |
| 56 |
|
| 57 |
self._bodyTagAttribs = {} |
| 58 |
|
| 59 |
def metaTags(self): |
| 60 |
""" |
| 61 |
Return a formatted vesion of the self._metaTags dictionary, using the |
| 62 |
formatMetaTags function from Cheetah.Macros.HTML""" |
| 63 |
|
| 64 |
return self.formatMetaTags(self._metaTags) |
| 65 |
|
| 66 |
def stylesheetTags(self): |
| 67 |
"""Return a formatted version of the self._stylesheetLibs and |
| 68 |
self._stylesheets dictionaries. The keys in self._stylesheets must |
| 69 |
be listed in the order that they should appear in the list |
| 70 |
self._stylesheetsOrder, to ensure that the style rules are defined in |
| 71 |
the correct order.""" |
| 72 |
|
| 73 |
stylesheetTagsTxt = '' |
| 74 |
for title, src in self._stylesheetLibs.items(): |
| 75 |
stylesheetTagsTxt += \ |
| 76 |
'<link rel="stylesheet" type="text/css" href="' \ |
| 77 |
+ str(src) + '" />\n' |
| 78 |
|
| 79 |
if not self._stylesheetsOrder: |
| 80 |
return stylesheetTagsTxt |
| 81 |
|
| 82 |
stylesheetTagsTxt += '<style type="text/css"><!--\n' |
| 83 |
for identifier in self._stylesheetsOrder: |
| 84 |
if identifier not in self._stylesheets: |
| 85 |
warning = '# the identifier ' + identifier + \ |
| 86 |
'was in stylesheetsOrder, but not in stylesheets' |
| 87 |
print(warning) |
| 88 |
stylesheetTagsTxt += warning |
| 89 |
continue |
| 90 |
|
| 91 |
attribsDict = self._stylesheets[identifier] |
| 92 |
cssCode = '' |
| 93 |
attribCode = '' |
| 94 |
for k, v in attribsDict.items(): |
| 95 |
attribCode += str(k) + ': ' + str(v) + '; ' |
| 96 |
attribCode = attribCode[:-2] |
| 97 |
|
| 98 |
cssCode = '\n' + identifier + ' {' + attribCode + '}' |
| 99 |
stylesheetTagsTxt += cssCode |
| 100 |
|
| 101 |
stylesheetTagsTxt += '\n//--></style>\n' |
| 102 |
|
| 103 |
return stylesheetTagsTxt |
| 104 |
|
| 105 |
def javascriptTags(self): |
| 106 |
"""Return a formatted version of the javascriptTags and |
| 107 |
javascriptLibs dictionaries |
| 108 |
|
| 109 |
Each value in javascriptTags should be a either a code string |
| 110 |
to include, or a list containing the JavaScript version number |
| 111 |
and the code string. The keys can be anything. The same applies |
| 112 |
for javascriptLibs, but the string should be the SRC filename |
| 113 |
rather than a code string. |
| 114 |
""" |
| 115 |
|
| 116 |
javascriptTagsTxt = [] |
| 117 |
for key, details in self._javascriptTags.items(): |
| 118 |
if not isinstance(details, (list, tuple)): |
| 119 |
details = ['', details] |
| 120 |
|
| 121 |
javascriptTagsTxt += ['<script language="JavaScript', |
| 122 |
str(details[0]), |
| 123 |
'" type="text/javascript"><!--\n', |
| 124 |
str(details[0]), '\n//--></script>\n'] |
| 125 |
|
| 126 |
for key, details in self._javascriptLibs.items(): |
| 127 |
if not isinstance(details, (list, tuple)): |
| 128 |
details = ['', details] |
| 129 |
|
| 130 |
javascriptTagsTxt += ['<script language="JavaScript', |
| 131 |
str(details[0]), |
| 132 |
'" type="text/javascript" src="', |
| 133 |
str(details[1]), '" />\n'] |
| 134 |
return ''.join(javascriptTagsTxt) |
| 135 |
|
| 136 |
def bodyTag(self): |
| 137 |
"""Create a body tag from the entries in the dict bodyTagAttribs.""" |
| 138 |
return self.formHTMLTag('body', self._bodyTagAttribs) |
| 139 |
|
| 140 |
def imgTag(self, src, alt='', width=None, height=None, border=0): |
| 141 |
"""Dynamically generate an image tag |
| 142 |
|
| 143 |
Cheetah will try to convert the src argument to a WebKit serverSidePath |
| 144 |
relative to the servlet's location. If width and height aren't |
| 145 |
specified they are calculated using PIL or ImageMagick if available. |
| 146 |
""" |
| 147 |
|
| 148 |
src = self.normalizePath(src) |
| 149 |
|
| 150 |
if not width or not height: |
| 151 |
try: |
| 152 |
import Image |
| 153 |
im = Image.open(src) |
| 154 |
calcWidth, calcHeight = im.size |
| 155 |
del im |
| 156 |
if not width: |
| 157 |
width = calcWidth |
| 158 |
if not height: |
| 159 |
height = calcHeight |
| 160 |
|
| 161 |
except Exception: |
| 162 |
try: |
| 163 |
calcWidth, calcHeight = os.popen( |
| 164 |
'identify -format "%w,%h" ' + src).read().split(',') |
| 165 |
if not width: |
| 166 |
width = calcWidth |
| 167 |
if not height: |
| 168 |
height = calcHeight |
| 169 |
|
| 170 |
except Exception: |
| 171 |
pass |
| 172 |
|
| 173 |
if width and height: |
| 174 |
return ''.join(['<img src="', src, '" width="', |
| 175 |
str(width), '" height="', str(height), |
| 176 |
'" alt="', alt, '" border="', str(border), '" />']) |
| 177 |
elif width: |
| 178 |
return ''.join(['<img src="', src, '" width="', str(width), |
| 179 |
'" alt="', alt, '" border="', str(border), '" />']) |
| 180 |
elif height: |
| 181 |
return ''.join(['<img src="', src, '" height="', str(height), |
| 182 |
'" alt="', alt, '" border="', str(border), '" />']) |
| 183 |
else: |
| 184 |
return ''.join(['<img src="', src, '" alt="', alt, |
| 185 |
'" border="', str(border), '" />']) |
| 186 |
|
| 187 |
def currentYr(self): |
| 188 |
"""Return a string representing the current yr.""" |
| 189 |
return time.strftime("%Y", time.localtime(time.time())) |
| 190 |
|
| 191 |
def currentDate(self, formatString="%b %d, %Y"): |
| 192 |
"""Return a string representing the current localtime.""" |
| 193 |
return time.strftime(formatString, time.localtime(time.time())) |
| 194 |
|
| 195 |
def spacer(self, width=1, height=1): |
| 196 |
return '<img src="spacer.gif" width="%s" height="%s" alt="" />' \ |
| 197 |
% (str(width), str(height)) |
| 198 |
|
| 199 |
def formHTMLTag(self, tagName, attributes={}): |
| 200 |
"""returns a string containing an HTML <tag> """ |
| 201 |
tagTxt = ['<', tagName.lower()] |
| 202 |
for name, val in attributes.items(): |
| 203 |
tagTxt += [' ', name.lower(), '="', str(val), '"'] |
| 204 |
tagTxt.append('>') |
| 205 |
return ''.join(tagTxt) |
| 206 |
|
| 207 |
def formatMetaTags(self, metaTags): |
| 208 |
"""format a dict of metaTag definitions into an HTML version""" |
| 209 |
metaTagsTxt = [] |
| 210 |
if 'HTTP-EQUIV' in metaTags: |
| 211 |
for http_equiv, contents in metaTags['HTTP-EQUIV'].items(): |
| 212 |
metaTagsTxt += ['<meta http-equiv="', str(http_equiv), |
| 213 |
'" content="', str(contents), '" />\n'] |
| 214 |
|
| 215 |
if 'NAME' in metaTags: |
| 216 |
for name, contents in metaTags['NAME'].items(): |
| 217 |
metaTagsTxt += ['<meta name="', str(name), '" content="', |
| 218 |
str(contents), '" />\n'] |
| 219 |
return ''.join(metaTagsTxt) |