map.py
author Tero Marttila <terom@fixme.fi>
Thu, 12 Feb 2009 01:38:29 +0200
changeset 58 a4261592020a
parent 46 54c5f5f340de
permissions -rw-r--r--
support empty defaults for labels, and add a URLListType for use with query strings
30
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
"""
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     2
    Handles mapping URLs to request handlers
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     3
"""
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     4
46
54c5f5f340de reduce to qmsk.web lib
Tero Marttila <terom@fixme.fi>
parents: 35
diff changeset
     5
from qmsk.web import http, handler
30
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     6
35
79ea0e2a6cc2 move pages/ -> sites/www.qmsk.net/pages/
Tero Marttila <terom@fixme.fi>
parents: 32
diff changeset
     7
class MapperError (http.ResponseError) :
30
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     8
    """
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     9
        URL could not be mapped
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    10
    """
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    11
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    12
    def __init__ (self, url) :
35
79ea0e2a6cc2 move pages/ -> sites/www.qmsk.net/pages/
Tero Marttila <terom@fixme.fi>
parents: 32
diff changeset
    13
        super(MapperError, self).__init__("URL not found: %s" % (url, ), status='404 Not Found')
30
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    14
31
107062ebb6f9 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: 30
diff changeset
    15
class Mapper (handler.RequestHandler) :
30
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    16
    """
31
107062ebb6f9 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: 30
diff changeset
    17
        Looks up the handler to use based on the URL
30
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    18
    """
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    19
31
107062ebb6f9 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: 30
diff changeset
    20
    def handle_request (self, request) :
30
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    21
        """
31
107062ebb6f9 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: 30
diff changeset
    22
            Map the given request
30
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    23
        """
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    24
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    25
        abstract
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    26
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    27
class Mapping (object) :
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    28
    """
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    29
        A mapping object for StaticMapping
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    30
    """
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    31
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    32
    def test (self, request) :
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    33
        """
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    34
            Either return a handler, or None
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    35
        """
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    36
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    37
        abstract
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    38
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    39
class RegexpMapping (object) :
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    40
    """
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    41
        A mapping object that uses regular expressions
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    42
    """
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    43
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    44
    def __init__ (self, regexp, handler) :
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    45
        pass
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    46
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    47
    def test (self, request) :
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    48
        xxx
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    49
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    50
class SimpleMapping (object) :
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    51
    """
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    52
        A mapping object that uses simple expressions
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    53
    """
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    54
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    55
    def __init__ (self, expression, handler) :
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    56
        pass
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    57
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    58
    def test (self, request) :
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    59
        xxx
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    60
35
79ea0e2a6cc2 move pages/ -> sites/www.qmsk.net/pages/
Tero Marttila <terom@fixme.fi>
parents: 32
diff changeset
    61
class StaticMapper (Mapper) :
30
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    62
    """
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    63
        Translates requests to handlers using a list of pre-determined Mapping's
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    64
    """
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    65
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    66
    def __init__ (self, mappings) :
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    67
        # store
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    68
        self.mappings = mappings
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    69
32
be954df4f0e8 move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
    70
    def handle_request (self, request) :
30
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    71
        """
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    72
            Returns the appropriate handler
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    73
        """
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    74
31
107062ebb6f9 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: 30
diff changeset
    75
        # find handler to use
107062ebb6f9 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: 30
diff changeset
    76
        handler = None
107062ebb6f9 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: 30
diff changeset
    77
30
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    78
        # just test each mapping in turn
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    79
        for mapping in self.mappings :
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    80
            handler = mapping.test(request)
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    81
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    82
            if handler :
31
107062ebb6f9 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: 30
diff changeset
    83
                break
30
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    84
        
31
107062ebb6f9 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: 30
diff changeset
    85
        if not handler :
107062ebb6f9 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: 30
diff changeset
    86
            # fail, not found
35
79ea0e2a6cc2 move pages/ -> sites/www.qmsk.net/pages/
Tero Marttila <terom@fixme.fi>
parents: 32
diff changeset
    87
            raise MapperError(request.get_page_name())
30
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    88
        
31
107062ebb6f9 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: 30
diff changeset
    89
        # passthrough
107062ebb6f9 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: 30
diff changeset
    90
        return handler.handle_request(request)
30
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    91
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    92
# "friendly" names
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    93
map     = SimpleMapping
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    94
mapre   = RegexpMapping
a86a25a9f75b route requests through sites/www.qmsk.net, although still hardcoded
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    95