lib/map.py
branchsites
changeset 30 a86a25a9f75b
child 31 107062ebb6f9
equal deleted inserted replaced
29:b06ff4c05d42 30:a86a25a9f75b
       
     1 """
       
     2     Handles mapping URLs to request handlers
       
     3 """
       
     4 
       
     5 import http
       
     6 import handler
       
     7 
       
     8 class MappingError (http.ResponseError) :
       
     9     """
       
    10         URL could not be mapped
       
    11     """
       
    12 
       
    13     def __init__ (self, url) :
       
    14         super(MappingError, self).__init__("URL not found: %s" % (url, ), status='404 Not Found')
       
    15 
       
    16 class Mapper (object) :
       
    17     """
       
    18         Translates requests to handlers
       
    19     """
       
    20 
       
    21     def map_request (self, request) :
       
    22         """
       
    23             Map the given request, returning a Handler
       
    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 StaticMapping (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 map_request (self, request) :
       
    72         """
       
    73             Returns the appropriate handler
       
    74         """
       
    75 
       
    76         # just test each mapping in turn
       
    77         for mapping in self.mappings :
       
    78             handler = mapping.test(request)
       
    79 
       
    80             if handler :
       
    81                 return handler
       
    82         
       
    83         # fail, not found
       
    84         raise MappingError(request.get_page_name())
       
    85         
       
    86 class FilesystemMapper (Mapper) :
       
    87     """
       
    88         Translates requests to handlers based on a filesystem directory containing various kinds of files
       
    89     """
       
    90 
       
    91     def __init__ (self, path) :
       
    92         """
       
    93             Create, path is where the pages are stored
       
    94         """
       
    95         
       
    96         # store
       
    97         self.path = path
       
    98     
       
    99     def map_request (self, request) :
       
   100         """
       
   101             Looks up the appropriate Page, and then returns a generic Handler
       
   102         """
       
   103 
       
   104         # XXX: harcoded
       
   105         return handler.Handler(handler.handle_request)
       
   106 
       
   107 # "friendly" names
       
   108 fstree  = FilesystemMapper
       
   109 
       
   110 map     = SimpleMapping
       
   111 mapre   = RegexpMapping
       
   112