lib/filesystem/page.py
author Tero Marttila <terom@fixme.fi>
Sat, 07 Feb 2009 16:33:27 +0200
branchsites
changeset 172 0797aa26beaf
parent 167 lib/page.py@f2504700e273
child 173 f1d36d7b361e
permissions -rw-r--r--
bloat code with even more layers of indirection, split off the filesystem-based stuff into a separate lib.filesystem package (next, move it to sites/www.qmsk.net)
5
9ed4c7d2bdd2 older work
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
149
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents: 148
diff changeset
     2
"""
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents: 148
diff changeset
     3
    Handling page requests
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents: 148
diff changeset
     4
"""
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents: 148
diff changeset
     5
150
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
     6
# for filesystem ops
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
     7
import os, os.path
163
5cefbd93926a some unicode fixes, layout tweaks, a link icon
Tero Marttila <terom@fixme.fi>
parents: 158
diff changeset
     8
import time
149
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents: 148
diff changeset
     9
172
0797aa26beaf bloat code with even more layers of indirection, split off the filesystem-based stuff into a separate lib.filesystem package (next, move it to sites/www.qmsk.net)
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    10
from lib import http, template, config
150
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
    11
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
    12
class PageError (http.ResponseError) :
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
    13
    """
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
    14
        Error looking up/handling a page
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
    15
    """
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
    16
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
    17
    pass
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
    18
153
01f5ef2890c4 funky PageTree stuff
Tero Marttila <terom@fixme.fi>
parents: 152
diff changeset
    19
# XXX: should inherit from PageInfo
150
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
    20
class Page (object) :
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
    21
    """
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
    22
        This object represents the information about our attempt to render some specific page
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
    23
    """
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
    24
172
0797aa26beaf bloat code with even more layers of indirection, split off the filesystem-based stuff into a separate lib.filesystem package (next, move it to sites/www.qmsk.net)
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    25
    def __init__ (self, fs, url, path, basename, url_tail, charset='utf8') :
150
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
    26
        """
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
    27
            Initialize the page at the given location
172
0797aa26beaf bloat code with even more layers of indirection, split off the filesystem-based stuff into a separate lib.filesystem package (next, move it to sites/www.qmsk.net)
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    28
            
0797aa26beaf bloat code with even more layers of indirection, split off the filesystem-based stuff into a separate lib.filesystem package (next, move it to sites/www.qmsk.net)
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    29
            @param fs the FilesysteMapper
150
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
    30
            @param url the URL leading to this page
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
    31
            @param path the filesystem path to this page's file
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
    32
            @param basename the filesystem name of this page's file, without the file extension
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
    33
            @param url_trail trailing URL for this page
163
5cefbd93926a some unicode fixes, layout tweaks, a link icon
Tero Marttila <terom@fixme.fi>
parents: 158
diff changeset
    34
            @param charset file charset
150
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
    35
        """
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
    36
        
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
    37
        # store
172
0797aa26beaf bloat code with even more layers of indirection, split off the filesystem-based stuff into a separate lib.filesystem package (next, move it to sites/www.qmsk.net)
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    38
        self.fs = fs
150
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
    39
        self.url = url
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
    40
        self.path = path
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
    41
        self.basename = basename
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
    42
        self.url_tail = url_tail
163
5cefbd93926a some unicode fixes, layout tweaks, a link icon
Tero Marttila <terom@fixme.fi>
parents: 158
diff changeset
    43
        self.charset = charset
150
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
    44
152
5e62fd2ed7f7 some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents: 151
diff changeset
    45
        # unbound
5e62fd2ed7f7 some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents: 151
diff changeset
    46
        self.request = None
5e62fd2ed7f7 some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents: 151
diff changeset
    47
150
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
    48
        # sub-init
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
    49
        self._init()
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
    50
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
    51
    def _init (self) :
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
    52
        """
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
    53
            Do initial data loading, etc
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
    54
        """
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
    55
        
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
    56
        pass
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
    57
152
5e62fd2ed7f7 some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents: 151
diff changeset
    58
    def bind_request (self, request) :
5e62fd2ed7f7 some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents: 151
diff changeset
    59
        """
5e62fd2ed7f7 some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents: 151
diff changeset
    60
            Bind this page-render to the given request
5e62fd2ed7f7 some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents: 151
diff changeset
    61
        """
5e62fd2ed7f7 some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents: 151
diff changeset
    62
5e62fd2ed7f7 some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents: 151
diff changeset
    63
        self.request = request
5e62fd2ed7f7 some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents: 151
diff changeset
    64
151
f2600dda89c5 page list + menu
Tero Marttila <terom@fixme.fi>
parents: 150
diff changeset
    65
    @property
f2600dda89c5 page list + menu
Tero Marttila <terom@fixme.fi>
parents: 150
diff changeset
    66
    def title (self) :
150
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
    67
        """
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
    68
            Return the page's title
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
    69
151
f2600dda89c5 page list + menu
Tero Marttila <terom@fixme.fi>
parents: 150
diff changeset
    70
            Defaults to the retreiving the page title from page_list, or basename in Titlecase.
150
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
    71
        """
151
f2600dda89c5 page list + menu
Tero Marttila <terom@fixme.fi>
parents: 150
diff changeset
    72
        
172
0797aa26beaf bloat code with even more layers of indirection, split off the filesystem-based stuff into a separate lib.filesystem package (next, move it to sites/www.qmsk.net)
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    73
        # lookup in PageTree
0797aa26beaf bloat code with even more layers of indirection, split off the filesystem-based stuff into a separate lib.filesystem package (next, move it to sites/www.qmsk.net)
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    74
        page_info = self.fs.tree.get_page(self.url)
151
f2600dda89c5 page list + menu
Tero Marttila <terom@fixme.fi>
parents: 150
diff changeset
    75
        
f2600dda89c5 page list + menu
Tero Marttila <terom@fixme.fi>
parents: 150
diff changeset
    76
        # fallback to titlecase
153
01f5ef2890c4 funky PageTree stuff
Tero Marttila <terom@fixme.fi>
parents: 152
diff changeset
    77
        if page_info :
01f5ef2890c4 funky PageTree stuff
Tero Marttila <terom@fixme.fi>
parents: 152
diff changeset
    78
            title = page_info.title
01f5ef2890c4 funky PageTree stuff
Tero Marttila <terom@fixme.fi>
parents: 152
diff changeset
    79
01f5ef2890c4 funky PageTree stuff
Tero Marttila <terom@fixme.fi>
parents: 152
diff changeset
    80
        else :
151
f2600dda89c5 page list + menu
Tero Marttila <terom@fixme.fi>
parents: 150
diff changeset
    81
            title = self.basename.title()
150
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
    82
151
f2600dda89c5 page list + menu
Tero Marttila <terom@fixme.fi>
parents: 150
diff changeset
    83
        return title
f2600dda89c5 page list + menu
Tero Marttila <terom@fixme.fi>
parents: 150
diff changeset
    84
    
f2600dda89c5 page list + menu
Tero Marttila <terom@fixme.fi>
parents: 150
diff changeset
    85
    @property
f2600dda89c5 page list + menu
Tero Marttila <terom@fixme.fi>
parents: 150
diff changeset
    86
    def content (self) :
150
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
    87
        """
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
    88
            Return the page content as a string
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
    89
        """
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
    90
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
    91
        abstract
163
5cefbd93926a some unicode fixes, layout tweaks, a link icon
Tero Marttila <terom@fixme.fi>
parents: 158
diff changeset
    92
    
5cefbd93926a some unicode fixes, layout tweaks, a link icon
Tero Marttila <terom@fixme.fi>
parents: 158
diff changeset
    93
    @property
5cefbd93926a some unicode fixes, layout tweaks, a link icon
Tero Marttila <terom@fixme.fi>
parents: 158
diff changeset
    94
    def modified (self) :
5cefbd93926a some unicode fixes, layout tweaks, a link icon
Tero Marttila <terom@fixme.fi>
parents: 158
diff changeset
    95
        """
5cefbd93926a some unicode fixes, layout tweaks, a link icon
Tero Marttila <terom@fixme.fi>
parents: 158
diff changeset
    96
            Returns the page modification timestamp
5cefbd93926a some unicode fixes, layout tweaks, a link icon
Tero Marttila <terom@fixme.fi>
parents: 158
diff changeset
    97
        """
5cefbd93926a some unicode fixes, layout tweaks, a link icon
Tero Marttila <terom@fixme.fi>
parents: 158
diff changeset
    98
        
5cefbd93926a some unicode fixes, layout tweaks, a link icon
Tero Marttila <terom@fixme.fi>
parents: 158
diff changeset
    99
        # stat
5cefbd93926a some unicode fixes, layout tweaks, a link icon
Tero Marttila <terom@fixme.fi>
parents: 158
diff changeset
   100
        timestamp = os.stat(self.path).st_mtime
5cefbd93926a some unicode fixes, layout tweaks, a link icon
Tero Marttila <terom@fixme.fi>
parents: 158
diff changeset
   101
167
f2504700e273 remove foo.html page, and rice up the footer a bit
Tero Marttila <terom@fixme.fi>
parents: 163
diff changeset
   102
        return time.strftime(config.DATETIME_FMT, time.gmtime(timestamp))
150
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
   103
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
   104
class HTMLPage (Page) :
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
   105
    """
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
   106
        A simple .html page that's just passed through directly
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
   107
    """
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
   108
151
f2600dda89c5 page list + menu
Tero Marttila <terom@fixme.fi>
parents: 150
diff changeset
   109
    @property
f2600dda89c5 page list + menu
Tero Marttila <terom@fixme.fi>
parents: 150
diff changeset
   110
    def content (self) :
150
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
   111
        """
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
   112
            Opens the .html file, reads and returns contents
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
   113
        """
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
   114
163
5cefbd93926a some unicode fixes, layout tweaks, a link icon
Tero Marttila <terom@fixme.fi>
parents: 158
diff changeset
   115
        return open(self.path, 'rb').read().decode(self.charset)
150
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
   116
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
   117
class TemplatePage (Page) :
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
   118
    """
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
   119
        A template that's rendered using our template library
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
   120
    """
151
f2600dda89c5 page list + menu
Tero Marttila <terom@fixme.fi>
parents: 150
diff changeset
   121
    
f2600dda89c5 page list + menu
Tero Marttila <terom@fixme.fi>
parents: 150
diff changeset
   122
    @property
f2600dda89c5 page list + menu
Tero Marttila <terom@fixme.fi>
parents: 150
diff changeset
   123
    def content (self) :
150
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
   124
        """
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
   125
            Loads the .tmpl file, and renders it
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
   126
        """
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents: 149
diff changeset
   127
152
5e62fd2ed7f7 some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents: 151
diff changeset
   128
        return template.render_file(self.path,
5e62fd2ed7f7 some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents: 151
diff changeset
   129
            request     = self.request,
172
0797aa26beaf bloat code with even more layers of indirection, split off the filesystem-based stuff into a separate lib.filesystem package (next, move it to sites/www.qmsk.net)
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
   130
            page_tree   = self.fs.tree
152
5e62fd2ed7f7 some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents: 151
diff changeset
   131
        )
149
0538176eb172 YES YES MOAR WSGI - Hello World
Tero Marttila <terom@fixme.fi>
parents: 148
diff changeset
   132