lib/map.py
changeset 46 185504387370
parent 45 e94ab812c0c8
child 47 3d59c9eeffaa
equal deleted inserted replaced
45:e94ab812c0c8 46:185504387370
     1 """
       
     2     Handles mapping URLs to request handlers
       
     3 """
       
     4 
       
     5 import http
       
     6 import handler
       
     7 
       
     8 class MapperError (http.ResponseError) :
       
     9     """
       
    10         URL could not be mapped
       
    11     """
       
    12 
       
    13     def __init__ (self, url) :
       
    14         super(MapperError, self).__init__("URL not found: %s" % (url, ), status='404 Not Found')
       
    15 
       
    16 class Mapper (handler.RequestHandler) :
       
    17     """
       
    18         Looks up the handler to use based on the URL
       
    19     """
       
    20 
       
    21     def handle_request (self, request) :
       
    22         """
       
    23             Map the given request
       
    24         """
       
    25 
       
    26         abstract
       
    27 
       
    28 class Mapping (object) :
       
    29     """
       
    30         A mapping object for StaticMapping
       
    31     """
       
    32 
       
    33     def test (self, request) :
       
    34         """
       
    35             Either return a handler, or None
       
    36         """
       
    37 
       
    38         abstract
       
    39 
       
    40 class RegexpMapping (object) :
       
    41     """
       
    42         A mapping object that uses regular expressions
       
    43     """
       
    44 
       
    45     def __init__ (self, regexp, handler) :
       
    46         pass
       
    47 
       
    48     def test (self, request) :
       
    49         xxx
       
    50 
       
    51 class SimpleMapping (object) :
       
    52     """
       
    53         A mapping object that uses simple expressions
       
    54     """
       
    55 
       
    56     def __init__ (self, expression, handler) :
       
    57         pass
       
    58 
       
    59     def test (self, request) :
       
    60         xxx
       
    61 
       
    62 class StaticMapper (Mapper) :
       
    63     """
       
    64         Translates requests to handlers using a list of pre-determined Mapping's
       
    65     """
       
    66 
       
    67     def __init__ (self, mappings) :
       
    68         # store
       
    69         self.mappings = mappings
       
    70 
       
    71     def handle_request (self, request) :
       
    72         """
       
    73             Returns the appropriate handler
       
    74         """
       
    75 
       
    76         # find handler to use
       
    77         handler = None
       
    78 
       
    79         # just test each mapping in turn
       
    80         for mapping in self.mappings :
       
    81             handler = mapping.test(request)
       
    82 
       
    83             if handler :
       
    84                 break
       
    85         
       
    86         if not handler :
       
    87             # fail, not found
       
    88             raise MapperError(request.get_page_name())
       
    89         
       
    90         # passthrough
       
    91         return handler.handle_request(request)
       
    92 
       
    93 # "friendly" names
       
    94 map     = SimpleMapping
       
    95 mapre   = RegexpMapping
       
    96