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