working trailing defaults sites
authorTero Marttila <terom@fixme.fi>
Sat, 07 Feb 2009 20:40:56 +0200
branchsites
changeset 38 9737b6ca2295
parent 37 1f13c384508e
child 39 82df0bb66ca7
working trailing defaults
sites/irclogs.qmsk.net/urls.py
--- a/sites/irclogs.qmsk.net/urls.py	Sat Feb 07 20:34:07 2009 +0200
+++ b/sites/irclogs.qmsk.net/urls.py	Sat Feb 07 20:40:56 2009 +0200
@@ -75,10 +75,13 @@
         # invalid
         raise URLError("Invalid label: %r" % (mask, ))
     
-    def match (self, value) :
+    def match (self, value=None) :
         """
             Match this label against the given value, returning either True to match without a value, a LabelValue
-            object, or boolean false to not match
+            object, or boolean false to not match.
+
+            If value is None, this means that only a default value should be accepted. XXX: currently returned default
+            is not used.
         """
 
         abstract
@@ -95,11 +98,16 @@
 
         return isinstance(other, EmptyLabel)
     
-    def match (self, value) :
+    def match (self, value=None) :
         """
             Match empty string -> True
         """
-
+        
+        # no default
+        if value is None :
+            return False
+        
+        # only empty segments
         if value == '' :
             return True
 
@@ -127,11 +135,16 @@
 
         return isinstance(other, StaticLabel) and self.name == other.name
     
-    def match (self, value) :
+    def match (self, value=None) :
         """
             Match exactly -> True
         """
 
+        # no defaults
+        if value is None :
+            return False
+        
+        # match name
         if value == self.name :
             return True
 
@@ -174,14 +187,18 @@
 
         super(SimpleValueLabel, self).__init__(key, default)
     
-    def match (self, value) :
+    def match (self, value=None) :
         """
             Match -> LabelValue
-
-            XXX: empty string?
         """
-
-        return LabelValue(self, value)
+        
+        # default?
+        if value is None and self.default :
+            return LabelValue(self, self.default)
+        
+        # only non-empty values!
+        elif value :
+            return LabelValue(self, value)
 
     def __str__ (self) :
         if self.default :
@@ -326,6 +343,12 @@
                 # this URL is the best match
                 return (self.url, [])
             
+            # look for default-only values, DFS
+            for child in self.children :
+                # does the child's label accept a default match?
+                if child.label.match() :
+                    return child.match(label_path)
+
             else :
                 # incomplete URL
                 raise URLError("no URL handler defined for this Node")