sites/irclogs.qmsk.net/urltree.py
branchsites
changeset 40 71ab68f31a1c
parent 39 82df0bb66ca7
child 41 9585441a4bfb
equal deleted inserted replaced
39:82df0bb66ca7 40:71ab68f31a1c
   261         """
   261         """
   262             Invoke the handler, using the given label values
   262             Invoke the handler, using the given label values
   263         """
   263         """
   264         
   264         
   265         # start with the defaults
   265         # start with the defaults
   266         kwargs = self.defaults()
   266         kwargs = self.defaults.copy()
   267 
   267 
   268         # then add all the values
   268         # then add all the values
   269         for label_value in label_values :
   269         for label_value in label_values :
   270             kwargs[label_value.label.key] = label_value.value
   270             kwargs[label_value.label.key] = label_value.value
   271             
   271             
   401             # ok, but continue looking to make sure there's no ambiguous URLs
   401             # ok, but continue looking to make sure there's no ambiguous URLs
   402             match = child
   402             match = child
   403         
   403         
   404         # found something?
   404         # found something?
   405         if not match :
   405         if not match :
   406             raise URLError("No child found for label")
   406             raise URLError("No child found for label: %s + %s + %s" % (self.get_url(), label, '/'.join(str(l) for l in label_path)))
   407 
   407 
   408         # ok, recurse into the match
   408         # ok, recurse into the match
   409         url, label_value = match.match(label_path)
   409         url, label_value = match.match(label_path)
   410 
   410 
   411         # add our value?
   411         # add our value?
   412         if isinstance(value, LabelValue) :
   412         if isinstance(value, LabelValue) :
   413             label_value.append(value)
   413             label_value.append(value)
   414 
   414 
   415         # return the match
   415         # return the match
   416         return url, label_value
   416         return url, label_value
   417     
   417 
       
   418     def get_url (self) :
       
   419         """
       
   420             Returns the URL for this node, by iterating over our parents
       
   421         """
       
   422         
       
   423         # URL segments in reverse order
       
   424         segments = ['']
       
   425         
       
   426         # start with ourself
       
   427         node = self
       
   428         
       
   429         # iterate up to root
       
   430         while node :
       
   431             segments.append(str(node.label))
       
   432 
       
   433             node = node.parent
       
   434 
       
   435         # reverse
       
   436         segments.reverse()
       
   437 
       
   438         # return
       
   439         return '/'.join(segments)
       
   440 
   418     def dump (self, indent=0) :
   441     def dump (self, indent=0) :
   419         """
   442         """
   420             Returns a multi-line string representation of this Node
   443             Returns a multi-line string representation of this Node
   421         """
   444         """
   422 
   445 
   468             Find the URL object best corresponding to the given url, matching any ValueLabels.
   491             Find the URL object best corresponding to the given url, matching any ValueLabels.
   469 
   492 
   470             Returns an (URL, [LabelValue]) tuple.
   493             Returns an (URL, [LabelValue]) tuple.
   471         """
   494         """
   472 
   495 
   473         # normalize the URL
       
   474         url = os.path.normpath(url)
       
   475 
       
   476         # split it into labels
   496         # split it into labels
   477         path = url.split('/')
   497         path = url.split('/')
   478 
   498         
   479         # ensure that it starts with a /
   499         # empty URL is empty
   480         root_label = path.pop(0)
   500         if url :
   481         assert self.root.label.match(root_label), "URL must begin with root"
   501             # ensure that it doesn't start with a /
       
   502             assert not self.root.label.match(path[0]), "URL must not begin with root"
   482 
   503 
   483         # just match starting at root
   504         # just match starting at root
   484         return self.root.match(path)
   505         return self.root.match(path)
   485 
   506 
   486     def handle_request (self, request) :
   507     def handle_request (self, request) :
   487         """
   508         """
   488             Looks up the request's URL, and invokes its handler
   509             Looks up the request's URL, and invokes its handler
   489         """
   510         """
   490         
   511         
   491         # get the request's URL path
   512         # get the requested URL
   492         url, label_values = self.match(request.get_page_name())
   513         request_url = request.get_page_name()
       
   514 
       
   515         # find the URL+values to use
       
   516         url, label_values = self.match(request_url)
   493 
   517 
   494         # let the URL handle it
   518         # let the URL handle it
   495         url.execute(request, label_values)
   519         return url.execute(request, label_values)
   496 
   520 
   497 
   521