"Fossies" - the Fresh Open Source Software Archive

Member "cheetah3-3.2.6.post2/Cheetah/Templates/_SkeletonPage.py" (20 Apr 2021, 8431 Bytes) of package /linux/www/cheetah3-3.2.6.post2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "_SkeletonPage.py" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 3-3.1.0_vs_3-3.2.0.

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