lib/map.py
branchsites
changeset 31 107062ebb6f9
parent 30 a86a25a9f75b
child 32 be954df4f0e8
equal deleted inserted replaced
30:a86a25a9f75b 31:107062ebb6f9
    11     """
    11     """
    12 
    12 
    13     def __init__ (self, url) :
    13     def __init__ (self, url) :
    14         super(MappingError, self).__init__("URL not found: %s" % (url, ), status='404 Not Found')
    14         super(MappingError, self).__init__("URL not found: %s" % (url, ), status='404 Not Found')
    15 
    15 
    16 class Mapper (object) :
    16 class Mapper (handler.RequestHandler) :
    17     """
    17     """
    18         Translates requests to handlers
    18         Looks up the handler to use based on the URL
    19     """
    19     """
    20 
    20 
    21     def map_request (self, request) :
    21     def handle_request (self, request) :
    22         """
    22         """
    23             Map the given request, returning a Handler
    23             Map the given request
    24         """
    24         """
    25 
    25 
    26         abstract
    26         abstract
    27 
    27 
    28 class Mapping (object) :
    28 class Mapping (object) :
    71     def map_request (self, request) :
    71     def map_request (self, request) :
    72         """
    72         """
    73             Returns the appropriate handler
    73             Returns the appropriate handler
    74         """
    74         """
    75 
    75 
       
    76         # find handler to use
       
    77         handler = None
       
    78 
    76         # just test each mapping in turn
    79         # just test each mapping in turn
    77         for mapping in self.mappings :
    80         for mapping in self.mappings :
    78             handler = mapping.test(request)
    81             handler = mapping.test(request)
    79 
    82 
    80             if handler :
    83             if handler :
    81                 return handler
    84                 break
    82         
    85         
    83         # fail, not found
    86         if not handler :
    84         raise MappingError(request.get_page_name())
    87             # fail, not found
       
    88             raise MappingError(request.get_page_name())
    85         
    89         
    86 class FilesystemMapper (Mapper) :
    90         # passthrough
    87     """
    91         return handler.handle_request(request)
    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 
    92 
   107 # "friendly" names
    93 # "friendly" names
   108 fstree  = FilesystemMapper
       
   109 
       
   110 map     = SimpleMapping
    94 map     = SimpleMapping
   111 mapre   = RegexpMapping
    95 mapre   = RegexpMapping
   112 
    96