lib/page.py
changeset 21 b05979822dee
parent 16 4a40718c7b4b
child 26 9d3beac1b196
equal deleted inserted replaced
20:d40c339d3778 21:b05979822dee
     3     Handling page requests
     3     Handling page requests
     4 """
     4 """
     5 
     5 
     6 # for filesystem ops
     6 # for filesystem ops
     7 import os, os.path
     7 import os, os.path
       
     8 import time
     8 
     9 
     9 # for ResponseError
    10 # for ResponseError
    10 import http
    11 import http
    11 
    12 
    12 # for TemplatePage
    13 # for TemplatePage
    28 class Page (object) :
    29 class Page (object) :
    29     """
    30     """
    30         This object represents the information about our attempt to render some specific page
    31         This object represents the information about our attempt to render some specific page
    31     """
    32     """
    32 
    33 
    33     def __init__ (self, url, path, basename, url_tail) :
    34     def __init__ (self, url, path, basename, url_tail, charset='utf8') :
    34         """
    35         """
    35             Initialize the page at the given location
    36             Initialize the page at the given location
    36 
    37 
    37             @param url the URL leading to this page
    38             @param url the URL leading to this page
    38             @param path the filesystem path to this page's file
    39             @param path the filesystem path to this page's file
    39             @param basename the filesystem name of this page's file, without the file extension
    40             @param basename the filesystem name of this page's file, without the file extension
    40             @param url_trail trailing URL for this page
    41             @param url_trail trailing URL for this page
       
    42             @param charset file charset
    41         """
    43         """
    42         
    44         
    43         # store
    45         # store
    44         self.url = url
    46         self.url = url
    45         self.path = path
    47         self.path = path
    46         self.basename = basename
    48         self.basename = basename
    47         self.url_tail = url_tail
    49         self.url_tail = url_tail
       
    50         self.charset = charset
    48 
    51 
    49         # unbound
    52         # unbound
    50         self.request = None
    53         self.request = None
    51 
    54 
    52         # sub-init
    55         # sub-init
    91         """
    94         """
    92             Return the page content as a string
    95             Return the page content as a string
    93         """
    96         """
    94 
    97 
    95         abstract
    98         abstract
       
    99     
       
   100     @property
       
   101     def modified (self) :
       
   102         """
       
   103             Returns the page modification timestamp
       
   104         """
       
   105         
       
   106         # stat
       
   107         timestamp = os.stat(self.path).st_mtime
       
   108 
       
   109         return time.strftime("%Y/%m/%d %H:%M %Z", time.gmtime(timestamp))
    96 
   110 
    97 class HTMLPage (Page) :
   111 class HTMLPage (Page) :
    98     """
   112     """
    99         A simple .html page that's just passed through directly
   113         A simple .html page that's just passed through directly
   100     """
   114     """
   103     def content (self) :
   117     def content (self) :
   104         """
   118         """
   105             Opens the .html file, reads and returns contents
   119             Opens the .html file, reads and returns contents
   106         """
   120         """
   107 
   121 
   108         return open(self.path, 'rb').read()
   122         return open(self.path, 'rb').read().decode(self.charset)
   109 
   123 
   110 class TemplatePage (Page) :
   124 class TemplatePage (Page) :
   111     """
   125     """
   112         A template that's rendered using our template library
   126         A template that's rendered using our template library
   113     """
   127     """