ext.urltree: initial import from qmsk.web
authorTero Marttila <terom@fixme.fi>
Sun, 09 Jan 2011 00:50:21 +0200
changeset 44 30af52a271a1
parent 43 fabb71550e51
child 45 e3001377e9dc
ext.urltree: initial import from qmsk.web
svv/ext/__init__.py
svv/ext/urltree.py
tests/ext_urltree.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/svv/ext/urltree.py	Sun Jan 09 00:50:21 2011 +0200
@@ -0,0 +1,941 @@
+"""
+    Tree-based URL mapping
+"""
+
+import re
+import os.path
+
+import werkzeug
+
+class URLError (Exception) :
+    """
+        Error with an URL definition
+    """
+
+    pass
+
+class LabelValue (object) :
+    """
+        Represents the value of a ValueLabel... love these names
+    """
+
+    def __init__ (self, label, value, is_default) :
+        """
+            Just store
+        """
+
+        self.label = label
+        self.value = value
+        self.is_default = is_default
+    
+    def __str__ (self) :
+        return "%s%s" % (self.label.key, "=%r" % (self.value, ) if not self.is_default else '')
+
+    def __repr__ (self) :
+        return "<%s>" % self
+
+class Label (object) :
+    """
+        Base class for URL labels (i.e. the segments of the URL between /s)
+    """
+
+    @staticmethod
+    def parse (mask, defaults, config) :
+        """
+            Parse the given label-segment, and return a *Label instance. Config is the URLConfig to use
+        """
+
+        # empty?
+        if not mask :
+            return EmptyLabel()
+
+        # simple value?
+        match = SimpleValueLabel.EXPR.match(mask)
+
+        if match :
+            # key
+            key = match.group('key')
+
+            # type
+            type_name = match.group("type")
+            
+            # lookup type, None -> default
+            type = config.get_type(type_name)
+
+            # defaults?
+            default = defaults.get(key)
+
+            if not default :
+                default = match.group('default')
+
+                if default :
+                    # apply type to default
+                    default = type.parse(default)
+
+            # build
+            return SimpleValueLabel(key, type_name, type, default)
+        
+        # static?
+        match = StaticLabel.EXPR.match(mask)
+
+        if match :
+            return StaticLabel(match.group('name'))
+
+        # invalid
+        raise URLError("Invalid label: %r" % (mask, ))
+    
+    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.
+
+            If value is None, this means that only a default value should be returned.
+        """
+
+        abstract
+    
+    def build (self, value_dict) :
+        """
+            Return a string representing this label, using the values in the given value_dict if needed
+        """
+
+        abstract
+
+    def build_default (self, value_dict) :
+        """
+            Return an (is_default, value) tuple
+        """
+
+        abstract
+
+
+class EmptyLabel (Label) :
+    """
+        An empty label, i.e. just a slash in the URL
+    """
+    
+    def __eq__ (self, other) :
+        """
+            Just compares type
+        """
+
+        return isinstance(other, EmptyLabel)
+    
+    def match (self, value=None) :
+        """
+            Match empty string -> True
+        """
+        
+        # no default
+        if value is None :
+            return False
+        
+        # only empty segments
+        if value == '' :
+            return True
+    
+    def build (self, values) :
+        return ''
+
+    def build_default (self, values) :
+        return (False, '')
+
+    def __str__ (self) :
+        return ''
+
+class StaticLabel (Label) :
+    """
+        A simple literal Label, used for fixed terms in the URL
+    """
+
+    EXPR = re.compile(r'^(?P<name>[a-zA-Z0-9_.-]+)$')
+
+    def __init__ (self, name) :
+        """
+            The given name is the literal name of this label
+        """
+
+        self.name = name
+
+    def __eq__ (self, other) :
+        """
+            Compares names
+        """
+
+        return isinstance(other, StaticLabel) and self.name == other.name
+    
+    def match (self, value=None) :
+        """
+            Match exactly -> True
+        """
+
+        # no defaults
+        if value is None :
+            return False
+        
+        # match name
+        if value == self.name :
+            return True
+
+    def build (self, values) :
+        return self.name
+
+    def build_default (self, values) :
+        return (False, self.name)
+
+    def __str__ (self) :
+        return self.name
+
+class ValueLabel (Label) :
+    """
+        A label with a key and a value
+
+        XXX: do we even need this?
+    """
+
+    def __init__ (self, key, default) :
+        """
+            Set the key and default value. Default value may be None if there is no default value defined
+        """
+
+        self.key = key
+        self.default = default
+
+    def __eq__ (self, other) :
+        """
+            Compares keys
+        """
+
+        return isinstance(other, ValueLabel) and self.key == other.key
+    
+    def build (self, values) :
+        """
+            Return either the assigned value from values, our default value, or raise an error
+        """
+        
+        # just proxy to build_default
+        return self.build_default(values)[1]
+
+    def build_default (self, values) :
+        """
+            Check if we have a value in values, and return based on that
+        """
+ 
+        # state
+        is_default = False
+        
+        # value given?
+        if self.key not in values or values[self.key] is None :
+            # error on missing non-default
+            if self.default is None :
+                raise URLError("No value given for label %r" % (self.key, ))
+            
+            # use default
+            else :
+                is_default = True
+                value = self.default
+        
+        else :
+            # lookup the value obj to use
+            value = values[self.key]
+            
+            # default?
+            is_default = bool(value == self.default)
+        
+        # convert value back to str
+        value = self.type.build(value)
+        
+        # return
+        return (is_default, value)
+
+class SimpleValueLabel (ValueLabel) :
+    """
+        A label that has a name and a simple string value
+    """
+
+    EXPR = re.compile(r'^\{(?P<key>[a-zA-Z_][a-zA-Z0-9_]*)(:(?P<type>[a-zA-Z_][a-zA-Z0-9_]*))?(=(?P<default>[^}]*))?\}$')
+
+    def __init__ (self, key, type_name, type, default) :
+        """
+            The given key is the name of this label's value.
+
+            The given type_name is is None for the default type, otherwise the type's name. Type is a URLType.
+        """
+
+        # type
+        self.type_name = type_name
+        self.type = type
+
+        # store
+        self.key = key
+        self.default = default
+        
+    def match (self, value=None) :
+        """
+            Match -> LabelValue
+        """
+        
+        # default?
+        if value is None and self.default is not None :
+            return LabelValue(self, self.default, True)
+        
+        # only non-empty values!
+        elif value :
+            # test
+            if not self.type.test(value) :
+                return False
+
+            # convert with type
+            value = self.type.parse(value)
+
+            return LabelValue(self, value, False)
+
+    def __str__ (self) :
+        return '{%s%s%s}' % (
+            self.key, 
+            (':%s' % (self.type_name, ) if self.type_name is not None else ''),
+            '=%s' % (self.default, ) if self.default is not None else '',
+        )
+
+class URLType (object) :
+    """
+        Handles the type-ness of values in the URL
+    """
+
+    def test (self, value) :
+        """
+            Tests if the given value is valid for this type.
+
+            Defaults to calling parse(), and returning False on errors, True otherwise
+        """
+        
+        try :
+            self.parse(value)
+
+        except :
+            return False
+
+        else :
+            return True
+    
+    def parse (self, value) :
+        """
+            Parse the given value, which was tested earlier with test(), and return the value object
+        """
+
+        abstract
+    
+    def append (self, old_value, value) :
+        """
+            Handle multiple values for this type, by combining the given old value and new value (both from parse).
+
+            Defaults to raise an error
+        """
+
+        raise URLError("Multiple values for argument")
+   
+    def build (self, obj) :
+        """
+            Reverse of parse(), return an url-value built from the given object
+        """
+
+        abstract
+
+    def build_multi (self, obj) :
+        """
+            Return a list of string values for the given object value (as from parse/append).
+
+            Defaults to return [self.build(obj)]
+        """
+
+        return [self.build(obj)]
+
+class URLStringType (URLType) :
+    """
+        The default URLType, just plain strings.
+        
+        XXX: decodeing here, or elsewhere?
+    """
+
+    def parse (self, value) :
+        """
+            Identitiy
+        """
+
+        return value
+
+    def build (self, obj) :
+        return str(obj)
+    
+class URLIntegerType (URLType) :
+    """
+        A URLType for simple integers
+    """
+
+    def __init__ (self, allow_negative=True, allow_zero=True, max=None) :
+        """
+            Pass in allow_negative=False to disallow negative numbers, allow_zero=False to disallow zero, or non-zero
+            max to specifiy maximum value
+        """
+
+        self.allow_negative = allow_negative
+        self.allow_zero = allow_zero
+        self.max = max
+    
+    def _validate (self, value) :
+        """
+            Test to make sure value fits our criteria
+        """
+
+        # negative?
+        if not self.allow_negative and value < 0 :
+            raise ValueError("value is negative")
+        
+        # zero?
+        if not self.allow_zero and value == 0 :
+            raise ValueError("value is zero")
+        
+        # max?
+        if self.max is not None and value > self.max :
+            raise ValueError("value is too large: %d" % value)
+        
+        return value
+
+    def parse (self, value) :
+        """
+            Convert str -> int
+        """
+
+        return self._validate(int(value))
+    
+    def build (self, obj) :
+        """
+            Convert int -> str
+        """
+
+        return unicode(self._validate(obj))
+    
+class URLListType (URLType) :
+    """
+        A list of strings
+    """
+
+    def parse (self, value) :
+        return [value]
+    
+    def append (self, old_value, value) :
+        return old_value + value
+
+    def build_multi (self, obj) :
+        return obj
+
+class URLConfig (object) :
+    """
+        Global configuration relevant to all URLs. This can be used to construct a set of URLs and then create an
+        URLTree out of them. Simply call the url_config() instace with the normal URL arguments (except, of course,
+        config), and finally just pass the url_config to URLTree (it's iterable).
+
+        XXX: rename to URLFactory?
+    """
+
+    # built-in type codes
+    BUILTIN_TYPES = {
+        # default - string
+        None    : URLStringType(),
+
+        # string
+        'str'   : URLStringType(),
+
+        # integer
+        'int'   : URLIntegerType(),
+
+        # list of strs
+        'list'  : URLListType(),
+    }
+
+    def __init__ (self, type_dict=None, ignore_extra_args=True) :
+        """
+            Create an URLConfig for use with URL
+
+            If type_dict is given, it should be a dict of { type_names: URLType }, and they will be available for
+            type specifications in addition to the defaults. This will call type._init_name with the key, so do
+            *not* initialize the name yourself.
+
+            If ignore_extra_args is given, unrecognized query arguments will be ignored.
+        """
+
+        # build our type_dict
+        self.type_dict = self.BUILTIN_TYPES.copy()
+        
+        # apply the given type_dict
+        if type_dict :
+            # merge
+            self.type_dict.update(type_dict)
+
+        # init
+        self.ignore_extra_args = ignore_extra_args
+        self.urls = []
+        
+    def get_type (self, type_name=None) :
+        """
+            Lookup an URLType by type_name, None for default.
+        """
+        
+        # lookup + return
+        return self.type_dict[type_name]
+
+    def __call__ (self, *args, **kwargs) :
+        """
+            Return new URL object with this config and the given args, adding it to our list of urls
+        """
+        
+        # build
+        url = URL(self, *args, **kwargs)
+        
+        # store
+        self.urls.append(url)
+
+        # return
+        return url
+    
+    def __iter__ (self) :
+        """
+            Returns all defined URLs
+        """
+
+        return iter(self.urls)
+
+class URL (object) :
+    """
+        Represents a specific URL
+    """
+
+
+    def __init__ (self, config, url_mask, handler, **defaults) :
+        """
+            Create an URL using the given URLConfig, with the given url mask, handler, and default values.
+        """
+
+        # store
+        self.config = config
+        self.url_mask = url_mask
+        self.handler = handler
+        self.defaults = defaults
+
+        # query string
+        self.query_args = dict()
+
+        # remove prepending root /
+        url_mask = url_mask.lstrip('/')
+        
+        # parse any query string
+        # XXX: conflicts with regexp syntax
+        if '/?' in url_mask :
+            url_mask, query_mask = url_mask.split('/?')
+        
+        else :
+            query_mask = None
+
+        # build our label path
+        self.label_path = [Label.parse(mask, defaults, config) for mask in url_mask.split('/')]
+
+        # build our query args list
+        if query_mask :
+            # split into items
+            for query_item in query_mask.split('&') :
+                # parse default
+                if '=' in query_item :
+                    query_item, default = query_item.split('=')
+
+                else :
+                    default = None
+                
+                # parse type
+                if ':' in query_item :
+                    query_item, type = query_item.split(':')
+                else :
+                    type = None
+                
+                # parse key
+                key = query_item
+
+                # type
+                type = self.config.get_type(type)
+
+                # add to query_args as (type, default) tuple
+                self.query_args[key] = (type, type.parse(default) if default else default)
+         
+    def get_label_path (self) :
+        """
+            Returns a list containing the labels in this url
+        """
+        
+        # copy self.label_path
+        return list(self.label_path)
+
+    def execute (self, request, label_values) :
+        """
+            Invoke the handler, using the given label values
+        """
+        
+        # start with the defaults
+        kwargs = self.defaults.copy()
+
+        # ...dict of those label values which are set to defaults
+        default_labels = {}
+
+        # then add all the values
+        for label_value in label_values :
+            kwargs[label_value.label.key] = label_value.value
+            
+            # add key to default_values?
+            if label_value.is_default :
+                default_labels[label_value.label.key] = label_value.label
+       
+        # then parse all query args
+        for key, value in request.get_args() :
+            # lookup in our defined query args
+            if key in self.query_args :
+                # lookup spec
+                type, default = self.query_args[key]
+            
+            # override URL params if they were not given
+            elif key in default_labels :
+                type, default = default_labels[key].type, None
+            
+            # be strict about extraneous query args?
+            elif not self.config.ignore_extra_args :
+                raise URLError("Unrecognized query argument: %r" % (key, ))
+            
+            # ignore
+            else :
+                continue
+
+            # normalize empty value to None
+            if not value :
+                value = None
+
+            else :
+                # parse value
+                value = type.parse(value)
+            
+            # XXX: this should set it to True or something... it's a flag, "/foo?bar"
+            # set default?
+            if value is None :
+                if default == '' :
+                    # do not pass key at all
+                    continue
+
+                elif default is not None :
+                    # pass default value
+                    value = default
+
+                else :
+                    # required arg, no default
+                    raise URLError("No value given for required argument: %r" % (key, ))
+            
+            # already have a non-default value?
+            if key in kwargs and key not in default_labels :
+                # append to old value
+                kwargs[key] = type.append(kwargs[key], value)
+
+            else :
+                # set key
+                kwargs[key] = value
+        
+        # then check all query args
+        for key, (type, default) in self.query_args.iteritems() :
+            # skip those already present
+            if key in kwargs :
+                continue
+
+            # apply default?
+            if default is None :
+                raise URLError("Missing required argument: %r" % (key, ))
+            
+            elif default == '' :
+                # skip empty default
+                continue
+
+            else :
+                # set default
+                kwargs[key] = default
+
+        # execute the handler
+        return self.handler(request, **kwargs)
+    
+    def build (self, request, **values) :
+        """
+            Build an absolute URL pointing to this target, with the given values. Default values are left off if they
+            are at the end of the URL.
+
+            Values given as None are ignored.
+        """
+        
+        # collect segments as a list of (is_default, segment) values
+        segments = [(False, request.page_prefix)] + [label.build_default(values) for label in self.label_path]
+        
+        # trim default items off the end
+        for is_default, segment in segments[::-1] :
+            if segment is None or is_default :
+                segments.pop(-1)
+            
+            else :
+                break
+
+        assert segments
+        
+        # join
+        url = '/'.join(segment for is_default, segment in segments)
+        
+        # build query args as { key -> [value] }
+        query_args = [(key, type.build_multi(values[key])) for key, (type, default) in self.query_args.iteritems() if key in values and values[key] is not None]
+
+        return "%s%s" % (url, '?%s' % ('&'.join('%s=%s' % (key, value) for key, values in query_args for value in values)) if query_args else '')
+
+    def __str__ (self) :
+        return '/'.join(str(label) for label in self.label_path)
+    
+    def __repr__ (self) :
+        return "URL(%r, %r)" % (str(self), self.handler)
+
+class URLNode (object) :
+    """
+        Represents a node in the URLTree
+    """
+
+    def __init__ (self, parent, label) :
+        """
+            Initialize with the given parent and label, empty children dict
+        """
+        
+        # the parent URLNode
+        self.parent = parent
+
+        # this node's Label
+        self.label = label
+
+        # list of child URLNodes
+        self.children = []
+
+        # this node's URL, set by add_url for an empty label_path
+        self.url = None
+
+    def _build_child (self, label) :
+        """
+            Build, insert and return a new child Node
+        """
+        
+        # build new child
+        child = URLNode(self, label)
+        
+        # add to children
+        self.children.append(child)
+
+        # return
+        return child
+
+    def add_url (self, url, label_path) :
+        """
+            Add a URL object to this node under the given path. Uses recursion to process the path.
+
+            The label_path argument is a (partial) label path as returned by URL.get_label_path.
+
+            If label_path is empty (len zero, or begins with EmptyLabel), then the given url is assigned to this node, if no
+            url was assigned before.
+        """
+        
+        # matches this node?
+        if not label_path or isinstance(label_path[0], EmptyLabel) :
+            if self.url :
+                raise URLError(url, "node already defined")
+
+            else :
+                # set
+                self.url = url
+
+        else :
+            # pop child label from label_path
+            child_label = label_path.pop(0)
+
+            # look for the child to recurse into
+            child = None
+
+            # look for an existing child with that label
+            for child in self.children :
+                if child.label == child_label :
+                    # found, use this
+                    break
+
+            else :
+                # build a new child
+                child = self._build_child(child_label)
+
+            # recurse to handle the rest of the label_path
+            child.add_url(url, label_path)
+    
+    def match (self, label_path) :
+        """
+            Locate the URL object corresponding to the given label_path value under this node.
+
+            Returns a (url, label_values) tuple
+        """
+
+        # determine value to use
+        value = None
+
+        # empty label_path?
+        if not label_path or label_path[0] == '' :
+            # the search ends at this node
+            if self.url :
+                # this URL is the best match
+                return (self.url, [])
+            
+            elif not self.children :
+                # incomplete URL
+                raise URLError("no URL handler defined for this Node")
+            
+            else :
+                # use default value, i.e. Label.match(None)
+                label = None
+
+        else :
+            # pop the next label from the label path
+            label = label_path.pop(0)
+
+        # return one match...
+        match = value = None
+
+        # recurse through our children, DFS
+        for child in self.children :
+            # match value
+            value = child.label.match(label)
+
+            # skip those that don't match at all
+            if not value :
+                continue;
+            
+            # already found a match? :/
+            if match :
+                raise URLError("Ambiguous URL")
+
+            # ok, but continue looking to make sure there's no ambiguous URLs
+            match = child
+        
+        # found something?
+        if not match :
+            raise URLError("No child found for label: %s + %s + %s" % (self.get_url(), label, '/'.join(str(l) for l in label_path)))
+
+        # ok, recurse into the match
+        url, label_value = match.match(label_path)
+
+        # add our value?
+        if isinstance(value, LabelValue) :
+            label_value.append(value)
+
+        # return the match
+        return url, label_value
+
+    def get_url (self) :
+        """
+            Returns the URL for this node, by iterating over our parents
+        """
+        
+        # URL segments in reverse order
+        segments = ['']
+        
+        # start with ourself
+        node = self
+        
+        # iterate up to root
+        while node :
+            segments.append(str(node.label))
+
+            node = node.parent
+
+        # reverse
+        segments.reverse()
+
+        # return
+        return '/'.join(segments)
+
+    def dump (self, indent=0) :
+        """
+            Returns a multi-line string representation of this Node
+        """
+
+        return '\n'.join([
+            "%-45s%s" % (
+                ' '*indent + str(self.label) + ('/' if self.children else ''), 
+                (' -> %r' % self.url) if self.url else ''
+            )
+        ] + [
+            child.dump(indent + 4) for child in self.children
+        ])
+
+    def __str__ (self) :
+        return "%s/[%s]" % (self.label, ','.join(str(child) for child in self.children))
+
+class URLTree (handler.RequestHandler) :
+    """
+        Map requests to handlers, using a defined tree of URLs
+    """
+
+    def __init__ (self, url_list) :
+        """
+            Initialize the tree using the given list of URLs
+        """
+
+        # root node
+        self.root = URLNode(None, EmptyLabel())
+        
+        # just add each URL
+        for url in url_list :
+            self.add_url(url)
+
+    def add_url (self, url) :
+        """
+            Adds the given URL to the tree. The URL must begin with a root slash.
+        """
+
+        # get url's label path
+        path = url.get_label_path()
+
+        # add to root
+        self.root.add_url(url, path)
+        
+    def match (self, url) :
+        """
+            Find the URL object best corresponding to the given url, matching any ValueLabels.
+
+            Returns an (URL, [LabelValue]) tuple.
+
+            XXX: handle unicode on URLs
+        """
+
+        # split it into labels
+        path = url.split('/')
+        
+        # empty URL is empty
+        if url :
+            # ensure that it doesn't start with a /
+            assert not self.root.label.match(path[0]), "URL must not begin with root"
+
+        # just match starting at root
+        return self.root.match(path)
+
+    def handle_request (self, request) :
+        """
+            Looks up the request's URL, and invokes its handler
+        """
+        
+        # get the requested URL
+        request_url = request.get_page_name()
+
+        # find the URL+values to use
+        url, label_values = self.match(request_url)
+
+        # let the URL handle it
+        return url.execute(request, label_values)
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/ext_urltree.py	Sun Jan 09 00:50:21 2011 +0200
@@ -0,0 +1,625 @@
+# :set encoding=utf8
+"""
+    Unit tests for svv.ext.urltree
+"""
+
+import unittest
+
+from svv.ext import urltree
+
+class TestLabelValue (unittest.TestCase) :
+    class dummylabel :
+        key = 'foo'
+
+    def test_str_default (self) :
+        self.assertEqual(str(urltree.LabelValue(self.dummylabel, 'bar', True)), "foo")
+
+    def test_str_value (self) :
+        self.assertEqual(str(urltree.LabelValue(self.dummylabel, 'bar', False)), "foo='bar'")
+
+class TestLabel (unittest.TestCase) :
+    def setUp (self) :
+        self.config = urltree.URLConfig()
+
+    def _test_parse (self, mask, _type, defaults={}, **attrs) :
+        label = urltree.Label.parse(mask, defaults, self.config)
+
+        self.assertTrue(isinstance(label, _type))
+        
+        for k, v in attrs.iteritems() :
+            self.assertEqual(getattr(label, k), v)
+
+    def test_parse_empty (self) :
+        self._test_parse("", urltree.EmptyLabel)
+
+    def test_parse_static (self) :
+        self._test_parse("foo", urltree.StaticLabel, name="foo")
+
+    def test_parse_static_valid (self) :
+        self._test_parse("foo_bar", urltree.StaticLabel, name="foo_bar")
+        self._test_parse("foo2", urltree.StaticLabel, name="foo2")
+        self._test_parse("2foo.q", urltree.StaticLabel, name="2foo.q")
+    
+    def test_parse_static_invalid (self) :
+        self.assertRaises(urltree.URLError, urltree.Label.parse, "foo/bar", {}, self.config)
+
+    def test_parse_value_plain (self) :
+        self._test_parse("{foo}", urltree.SimpleValueLabel, key="foo", default=None, type_name=None, type=urltree.URLConfig.BUILTIN_TYPES[None])
+
+    def test_parse_value_default1 (self) :
+        self._test_parse("{foo=bar1}", urltree.SimpleValueLabel, key="foo", default="bar1", type_name=None)
+
+    def test_parse_value_default2 (self) :
+        self._test_parse("{foo}", urltree.SimpleValueLabel, dict(foo="bar2"), key="foo", default="bar2", type_name=None)
+
+    def test_parse_value_default3 (self) :
+        self._test_parse("{foo=bar1}", urltree.SimpleValueLabel, dict(foo="bar3"), key="foo", default="bar3", type_name=None)
+
+    def test_parse_value_type_str (self) :
+        self._test_parse("{foo:str}", urltree.SimpleValueLabel, key="foo", type_name="str", type=urltree.URLConfig.BUILTIN_TYPES['str'])
+
+    def test_parse_value_type_int (self) :
+        self._test_parse("{foo:int}", urltree.SimpleValueLabel, key="foo", type_name="int", type=urltree.URLConfig.BUILTIN_TYPES['int'])
+    
+    def test_parse_invalid_type (self) :
+        self.assertRaises(KeyError, self._test_parse, "{foo:bar}", None)
+
+class TestEmptyLabel (unittest.TestCase) :
+    def setUp (self) :
+        self.label = urltree.EmptyLabel()
+
+    def test_eq (self) :
+        self.assertTrue(self.label == urltree.EmptyLabel())
+        self.assertFalse(self.label == urltree.StaticLabel("foo"))
+
+    def test_match (self) :
+        self.assertFalse(self.label.match())
+        self.assertFalse(self.label.match("foo"))
+        self.assertTrue(self.label.match(""))
+
+    def test_build (self) :
+        self.assertEqual(self.label.build({}), "")
+
+    def test_build_default (self) :
+        self.assertEqual(self.label.build_default({}), (False, ""))
+
+    def test_str (self) :
+        self.assertEqual(str(self.label), "")
+
+class TestStaticLabel (unittest.TestCase) :
+    def setUp (self) :
+        self.label = urltree.StaticLabel("test")
+
+    def test_eq (self) :
+        self.assertTrue(self.label == urltree.StaticLabel("test"))
+        self.assertFalse(self.label == urltree.StaticLabel("Test"))
+        self.assertFalse(self.label == urltree.EmptyLabel())
+
+    def test_match (self) :
+        self.assertFalse(self.label.match())
+        self.assertFalse(self.label.match("foo"))
+        self.assertTrue(self.label.match("test"))
+
+    def test_build (self) :
+        self.assertEqual(self.label.build({}), "test")
+
+    def test_build_default (self) :
+        self.assertEqual(self.label.build_default({}), (False, "test"))
+
+    def test_str (self) :
+        self.assertEqual(str(self.label), "test")
+
+class TestSimpleValueLabel (unittest.TestCase) :
+    def setUp (self) :
+        self.label = urltree.SimpleValueLabel("test", 'int', urltree.URLConfig.BUILTIN_TYPES['int'], None)
+        self.label_default_0 = urltree.SimpleValueLabel("test", 'int', urltree.URLConfig.BUILTIN_TYPES['int'], 0)
+        self.label_default_1 = urltree.SimpleValueLabel("test", 'int', urltree.URLConfig.BUILTIN_TYPES['int'], 1)
+        self.label_str = urltree.SimpleValueLabel("test", None, urltree.URLConfig.BUILTIN_TYPES[None], None)
+        self.label_str_default = urltree.SimpleValueLabel("test", None, urltree.URLConfig.BUILTIN_TYPES[None], 'def')
+
+    def test_eq (self) :
+        self.assertTrue(self.label == urltree.SimpleValueLabel("test", 'str', None, 1))
+        self.assertFalse(self.label == urltree.StaticLabel("Test"))
+        self.assertFalse(self.label == urltree.EmptyLabel())
+    
+    def _check_value (self, label, label_value, value, is_default) :
+        self.assertTrue(isinstance(label_value, urltree.LabelValue))
+        self.assertEqual(label_value.label, label)
+        self.assertEqual(label_value.value, value)
+        self.assertEqual(label_value.is_default, is_default)
+
+    def test_match_default_none (self) :
+        self.assertEquals(self.label.match(), None)
+
+    def test_match_default_value (self) :
+        self._check_value(self.label_default_0, self.label_default_0.match(), 0, True)
+        self._check_value(self.label_default_1, self.label_default_1.match(), 1, True)
+    
+    def test_match_invalid (self) :
+        self.assertEquals(self.label.match("foo"), False)
+        self.assertEquals(self.label_default_0.match("foo"), False)
+        self.assertEquals(self.label_default_1.match("foo"), False)
+
+    def test_match_valid (self) :
+        self._check_value(self.label, self.label.match("0"), 0, False)
+        self._check_value(self.label, self.label.match("1"), 1, False)
+        self._check_value(self.label_default_0, self.label_default_0.match("1"), 1, False)
+        self._check_value(self.label_default_1, self.label_default_1.match("0"), 0, False)
+
+    def test_build (self) :
+        self.assertEqual(self.label.build(dict(test=1)), "1")
+    
+    def test_build_nodefault (self) :
+        self.assertRaises(urltree.URLError, self.label.build, {})
+
+    def test_build_none (self) :
+        self.assertRaises(urltree.URLError, self.label.build, dict(test=None))
+    
+    def test_build_default (self) :
+        self.assertEqual(self.label_default_0.build_default({}), (True, "0"))
+        self.assertEqual(self.label_default_1.build_default({}), (True, "1"))
+        self.assertEqual(self.label_default_0.build_default(dict(test=0)), (True, "0"))
+    
+    def test_build_nonedefault (self) :
+        self.assertEqual(self.label_default_1.build_default(dict(test=None)), (True, "1"))
+
+    def test_build_value (self) :
+        self.assertEqual(self.label.build_default(dict(test=0)), (False, "0"))
+        self.assertEqual(self.label.build_default(dict(test=1)), (False, "1"))
+        self.assertEqual(self.label_default_0.build_default(dict(test=1)), (False, "1"))
+
+    def test_str (self) :
+        self.assertEqual(str(self.label), "{test:int}")
+        self.assertEqual(str(self.label_default_0), "{test:int=0}")
+        self.assertEqual(str(self.label_default_1), "{test:int=1}")
+        self.assertEqual(str(self.label_str), "{test}")
+        self.assertEqual(str(self.label_str_default), "{test=def}")
+
+class TestStringType (unittest.TestCase) :
+    def setUp (self) :
+        self.type = urltree.URLStringType()
+
+    def test_test (self) :
+        self.assertTrue(self.type.test(""))
+        self.assertTrue(self.type.test("xxx"))
+    
+    def test_parse (self) :
+        self.assertEqual(self.type.parse(""), "")
+        self.assertEqual(self.type.parse("xxx"), "xxx")
+    
+    def test_build (self) :
+        self.assertEqual(self.type.build(""), "")
+        self.assertEqual(self.type.build("xxx"), "xxx")
+        self.assertEqual(self.type.build("äää"), "äää")
+
+class TestIntegerType (unittest.TestCase) :
+    def setUp (self) :
+        self.type = urltree.URLIntegerType()
+        self.type_positive = urltree.URLIntegerType(allow_negative=False)
+        self.type_nonzero = urltree.URLIntegerType(allow_zero=False)
+        self.type_max_5 = urltree.URLIntegerType(max=5)
+
+    def test_test (self) :
+        self.assertTrue(self.type.test("1"))
+        self.assertFalse(self.type.test("xx"))
+        self.assertTrue(self.type_positive.test("1"))
+        self.assertFalse(self.type_positive.test("-1"))
+        self.assertTrue(self.type_nonzero.test("1"))
+        self.assertFalse(self.type_nonzero.test("0"))
+        self.assertTrue(self.type_max_5.test("5"))
+        self.assertFalse(self.type_max_5.test("6"))
+    
+    def test_parse_invalid (self) :
+        self.assertRaises(ValueError, self.type.parse, "xx")
+        self.assertRaises(ValueError, self.type_nonzero.parse, "0")
+
+    def test_parse_valid (self) :
+        self.assertEqual(self.type.parse("0"), 0)
+        self.assertEqual(self.type.parse("2"), 2)
+        self.assertEqual(self.type_nonzero.parse("3"), 3)
+    
+    def test_append (self) :
+        self.assertRaises(urltree.URLError, self.type.append, 0, 1)
+
+    def test_build (self) :
+        self.assertEqual(self.type.build(0), "0")
+        self.assertEqual(self.type.build(5), "5")
+        self.assertEqual(self.type_positive.build(1), "1")
+        self.assertEqual(self.type_nonzero.build(1), "1")
+        self.assertEqual(self.type_max_5.build(5), "5")
+    
+    def test_build_invalid (self) :
+        self.assertRaises(ValueError, self.type_positive.build, -1)
+        self.assertRaises(ValueError, self.type_nonzero.build, 0)
+        self.assertRaises(ValueError, self.type_max_5.build, 6)
+
+    def test_build_multi (self) :
+        self.assertEqual(self.type.build_multi(0), ["0"])
+   
+class TestListType (unittest.TestCase) :
+    def setUp (self) :
+        self.type = urltree.URLListType()
+
+    def test_parse (self) :
+        self.assertEqual(self.type.parse("x"), ["x"])
+        self.assertEqual(self.type.parse(""), [""])
+    
+    def test_append (self) :
+        self.assertEqual(self.type.append(["x"], ["y"]), ["x", "y"])
+
+    def test_build_multi (self) :
+        self.assertEqual(self.type.build_multi(["x", "y"]), ["x", "y"])
+
+class TestConfig (unittest.TestCase) :
+    def test_init (self) :
+        urltree.URLConfig(type_dict=dict(foo=None), ignore_extra_args=True)
+
+    def test_get_type (self) :
+        self.assertEquals(urltree.URLConfig(dict(foo='xxx')).get_type('foo'), 'xxx')
+    
+    def test_get_type_invalid (self) :
+        self.assertRaises(KeyError, urltree.URLConfig(dict(foo='xxx')).get_type, 'xxx')
+
+    def test_call (self) :
+        config = urltree.URLConfig()
+        url = config("foo", None)
+
+        self.assertTrue(isinstance(url, urltree.URL))
+        self.assertTrue(url in config.urls)
+    
+    def test_iter (self) :
+        config = urltree.URLConfig()
+        url1 = config("foo1", None)
+        url2 = config("foo2", None)
+
+        urls = list(config)
+
+        self.assertTrue(urls[0].url_mask == "foo1")
+        self.assertTrue(urls[1].url_mask == "foo2")
+
+class TestURL (unittest.TestCase) :
+    def setUp (self) :
+        self.config = urltree.URLConfig(ignore_extra_args=True)
+        self.config_strict = urltree.URLConfig(ignore_extra_args=False)
+    
+    def _test_label_path (self, mask, *path, **qargs) :
+        url = self.config(mask, None)
+        
+        # right label path
+        self.assertEquals(url.label_path, list(path))
+        
+        # right qargs keys
+        self.assertEquals(set(url.query_args), set(qargs))
+        
+        # right qargs values
+        for key, value in qargs.iteritems() :
+            self.assertEquals(url.query_args[key], value)
+    
+    # __init__
+    def test_label_path_empty (self) :
+        self._test_label_path("", urltree.EmptyLabel())
+    
+    def test_label_path_root (self) :
+        self._test_label_path("/", urltree.EmptyLabel())
+
+    def test_label_path_static (self) :
+        self._test_label_path("/foo", urltree.StaticLabel("foo"))
+
+    def test_label_path_static2 (self) :
+        self._test_label_path("/foo/bar/", urltree.StaticLabel("foo"), urltree.StaticLabel("bar"), urltree.EmptyLabel())
+    
+    def test_label_path_mix (self) :
+        self._test_label_path("/foo/{bar}", urltree.StaticLabel("foo"), urltree.SimpleValueLabel("bar", None, None, None))
+    
+#    def test_query_args_root_empty (self) :
+#        self._test_label_path("/?", urltree.EmptyLabel())
+
+    def test_query_args_simple (self) :
+        self._test_label_path("/x/?foo", urltree.StaticLabel("x"), foo=(self.config.get_type(None), None))
+
+    def test_query_args_multi (self) :
+        self._test_label_path("/x/?foo=0&bar&tee:int=&eee:int", urltree.StaticLabel("x"), 
+            foo = (self.config.get_type(None), "0"),
+            bar = (self.config.get_type(None), None),
+            tee = (self.config.get_type('int'), ''),
+            eee = (self.config.get_type('int'), None),
+        )
+    
+    def test_label_path_mutate (self) :
+        l = self.config("xxx", None)
+
+        lp = l.get_label_path()
+
+        lp.pop(0)
+
+        self.assertTrue(len(l.label_path) > len(lp))
+    
+    def _setup_handler (self) :
+        def _handler (req, **kwargs) :
+            return kwargs
+
+        return _handler
+    
+    def _setup_execute (self, mask, config) :
+        _handler = self._setup_handler()
+
+        url = config(mask, _handler)
+
+        return url
+    
+    class dummyrequest :
+        def __init__ (self, qargs) : self.qargs = qargs
+        def get_args (self) : return self.qargs
+    
+    class dummy_label :
+        def __init__ (self, key, type) :
+            self.key = key
+            self.type = type
+
+    class dummy_labelvalue :
+        def __init__ (self, key, value, is_default, type) : 
+            self.label = TestURL.dummy_label(key, type)
+            self.value = value
+            self.is_default = is_default
+
+    def _test_execute (self, mask, values={}, qargs={}, qlist=[], config=None) :
+        if not config :
+            config = self.config
+
+        # setup
+        url = self._setup_execute(mask, config)
+        req = self.dummyrequest(qargs.items() + qlist)
+        values = [self.dummy_labelvalue(k, v, d, config.get_type()) for k, (v, d) in values.iteritems()]
+
+        # exec
+        out_args = url.execute(req, values)
+        
+        return out_args
+    
+    # execute
+    def test_execute_empty (self) :
+        self.assertEquals(set(self._test_execute("/")), set())
+
+    def test_execute_plain (self) :
+        self.assertEquals(set(self._test_execute("/foo")), set())
+
+    def test_execute_simple (self) :
+        self.assertEquals(self._test_execute("/foo/{bar}", dict(bar=(0, False))), dict(bar=0))
+
+    def test_execute_multi (self) :
+        self.assertEquals(self._test_execute("/foo/{bar}/{quux}", dict(bar=(1, False), quux=(2, False))), dict(bar=1, quux=2))
+
+    def test_execute_default (self) :
+        self.assertEquals(self._test_execute("/foo/{bar=0}", dict(bar=("0", True))), dict(bar="0"))
+    
+    def test_execute_qargs_default (self) :
+        self.assertEquals(self._test_execute("/{foo}/?bar=0", dict(foo=("x", False))), dict(foo="x", bar="0"))
+    
+    def test_execute_qargs_default_type (self) :
+        self.assertEquals(self._test_execute("/{foo}/?bar:int=0", dict(foo=("x", False))), dict(foo="x", bar=0))
+
+    def test_execute_qargs_default_none (self) :
+        self.assertEquals(self._test_execute("/{foo}/?bar=", dict(foo=("x", False))), dict(foo="x"))
+
+    def test_execute_qargs_missing (self) :
+        self.assertRaises(urltree.URLError, self._test_execute, "/{foo}/?bar", dict(foo=("x", False)))
+
+    def test_execute_qargs_invalid (self) :
+        self.assertRaises(ValueError, self._test_execute, "/{foo}/?bar:int", dict(foo=("x", False)), dict(bar="x"))
+
+    def test_execute_qargs_simple (self) :
+        self.assertEquals(self._test_execute("/{foo}/?bar", dict(foo=("x", False)), dict(bar="y")), dict(foo="x", bar="y"))
+
+    def test_execute_qargs_novalue (self) :
+        self.assertRaises(urltree.URLError, self._test_execute, "/{foo}/?bar", dict(foo=("x", False)), dict(bar=''))
+
+    def test_execute_qargs_multi_invalid (self) :
+        self.assertRaises(urltree.URLError, self._test_execute, "/{foo}/?bar", dict(foo=("x", False)), qlist=[('bar', 'a'), ('bar', 'b')])
+
+    def test_execute_qargs_multi_list (self) :
+        self.assertEqual(self._test_execute("/{foo}/?bar:list", dict(foo=("x", False)), qlist=[('bar', 'a'), ('bar', 'b')]), dict(foo='x', bar=['a', 'b']))
+
+    def test_execute_qarg_override_strict (self) :
+        self.assertRaises(urltree.URLError, self._test_execute, "/{foo}", dict(foo=("x1", False)), dict(foo="x2"), config=self.config_strict)
+
+    def test_execute_qarg_override_ignore (self) :
+        self.assertEqual(self._test_execute("/{foo}", dict(foo=("x1", False)), dict(foo="x2")), dict(foo="x1"))
+        
+    def test_execute_qarg_override_ok (self) :
+        self.assertEqual(self._test_execute("/{foo=x1}", dict(foo=("x1", True)), dict(foo="x2")), dict(foo="x2"))
+    
+    # build
+    class dummyrequest_page :
+        def __init__ (self, page_prefix) :
+            self.page_prefix = page_prefix
+    
+    def _test_build (self, mask, url, **args) :
+        self.assertEquals(self.config(mask, None).build(self.dummyrequest_page("/index.cgi"), **args), "/index.cgi" + url)
+    
+    def _test_build_fails (self, err, mask, **args) :
+        self.assertRaises(err, self.config(mask, None).build, self.dummyrequest_page("/index.cgi"), **args)
+
+    def test_build_empty (self) :
+        self._test_build("/", "/")
+
+    def test_build_static (self) :
+        self._test_build("/foo", "/foo")
+
+    def test_build_simple (self) :
+        self._test_build("/foo/{bar}", "/foo/x", bar="x")
+
+    def test_build_multi (self) :
+        self._test_build("/foo/{bar}/{quux}", "/foo/x/y", bar="x", quux="y")
+    
+    def test_build_missing (self) :
+        self._test_build_fails(urltree.URLError, "/foo/{bar}/{quux}", bar="x")
+    
+    def test_build_unknown (self) :
+        self._test_build_fails(urltree.URLError, "/foo/{bar}/{quux}", bar="x", quux="y", frob="???")
+
+    def test_build_long (self) :
+        self._test_build("/foo/{bar=a}/{quux=b}", "/foo/x/y", bar="x", quux="y")
+    
+    def test_build_short (self) :
+        self._test_build("/foo/{bar=a}/{quux=b}", "/foo/x", bar="x", quux="b")
+    
+    def test_build_with_none (self) :
+        self._test_build("/foo/{bar=a}/{quux=b}", "/foo/x", bar="x", quux=None)
+ 
+    def test_build_default (self) :
+        self._test_build("/foo/{bar=a}/{quux=b}", "/foo/x", bar="x")
+    
+    def test_build_qargs (self) :
+        self._test_build("/foo/{bar}/?quux", "/foo/x?quux=a", bar="x", quux="a")
+
+    def test_build_qargs_default (self) :
+        self._test_build("/foo/{bar}/?quux", "/foo/x?quux=a", bar="x", quux="a")
+    
+    # XXX: this just becomes ...?quux=['a', 'b'] like from str(list)
+#    def test_build_qargs_multi_invalid (self) :
+#        self._test_build_fails(urltree.URLError, "/foo/{bar}/?quux", bar="x", quux=["a", "b"])
+    
+    def test_build_qargs_multi_list (self) :
+        self._test_build("/foo/{bar}/?quux:list", "/foo/x?quux=a&quux=b", bar="x", quux=["a", "b"])
+
+    def test_build_qargs_none (self) :
+        self._test_build("/foo/{bar}/?quux", "/foo/x", bar="x", quux=None)
+
+class TestTreeBuild (unittest.TestCase) :
+    def setUp (self) :
+        self.config = urltree.URLConfig(ignore_extra_args=True)
+
+    def test_simple_root (self) :
+        self.config("/", None)
+        self.assertEqual(str(urltree.URLTree(self.config).root), "/[]")
+
+    def test_simple_static (self) :
+        self.config("/foo/bar", None)
+        self.assertEqual(str(urltree.URLTree(self.config).root), "/[foo/[bar/[]]]")
+
+    def test_multi_static (self) :
+        self.config("/foo/bar", None)
+        self.config("/foo/quux", None)
+        self.config("/asdf", None)
+        self.assertEqual(str(urltree.URLTree(self.config).root), "/[foo/[bar/[],quux/[]],asdf/[]]")
+
+    def test_simple_value (self) :
+        self.config("/foo/{bar}", None)
+        self.assertEqual(str(urltree.URLTree(self.config).root), "/[foo/[{bar}/[]]]")
+
+    def test_deep (self) :
+        self.config("/foo/{cc}/a", None)
+        self.config("/foo/{cc}/b", None)
+        self.assertEqual(str(urltree.URLTree(self.config).root), "/[foo/[{cc}/[a/[],b/[]]]]")
+
+    def test_deep2 (self) :
+        self.config("/foo/{cc}/a/x", None)
+        self.config("/foo/{cc}/b", None)
+        self.assertEqual(str(urltree.URLTree(self.config).root), "/[foo/[{cc}/[a/[x/[]],b/[]]]]")
+    
+    def test_ambig_simple (self) :
+        self.config("/foo", None)
+        self.config("/foo", None)
+
+        self.assertRaises(urltree.URLError, urltree.URLTree, self.config)
+
+class TestTreeMatch (unittest.TestCase) :
+    def setUp (self) :
+        self.config = urltree.URLConfig(ignore_extra_args=True)
+        
+        self.root =self.config("/", None)
+        self.bar = self.config("/bar", None)
+        self.quux = self.config("/quux/{xyz}", None)
+        self.quux_boo = self.config("/quux/{xyz}/boo/{opt=no}", None)
+        self.quux_yes = self.config("/quux/{xyz}/yes", None)
+        
+        self.tree = urltree.URLTree(self.config)
+    
+    def _test_match (self, path, url, **values) :
+        t_url, t_values = self.tree.match(path)
+
+        self.assertEqual(t_url, url)
+        
+        self.assertEqual(set(v.label.key for v in t_values), set(values))
+
+        for v in t_values :
+            self.assertEqual(v.value, values[v.label.key])
+
+    def test_root (self) :
+        self._test_match("", self.root)
+
+    def test_bar (self) :
+        self._test_match("bar", self.bar)
+    
+    def test_bar_slash (self) :
+        self._test_match("bar/", self.bar)
+
+    def test_quux (self) :
+        self._test_match("quux/a", self.quux, xyz="a")
+ 
+    def test_quux_missing (self) :
+        self.assertRaises(urltree.URLError, self._test_match, "quux/", None)
+    
+    def test_quux_boo (self) :
+        self._test_match("quux/a/boo/x", self.quux_boo, xyz="a", opt="x")
+
+    def test_quux_default (self) :
+        self._test_match("quux/a/boo", self.quux_boo, xyz="a", opt="no")
+
+    def test_yes (self) :
+        self._test_match("quux/a/yes", self.quux_yes, xyz="a")
+    
+class TestTreeHandler (unittest.TestCase) :
+    def _build_handler (self, name) :
+        def _handler (req, **args) :
+            return name, args
+        
+        return _handler
+
+    def setUp (self) :
+        self.config = urltree.URLConfig(ignore_extra_args=True)
+
+        self.root =self.config("/", self._build_handler('root'))
+        self.bar = self.config("/bar", self._build_handler('bar'))
+        self.quux = self.config("/quux/{xyz}", self._build_handler('quux'))
+        self.quux_boo = self.config("/quux/{xyz}/boo/{opt=no}", self._build_handler('quux_boo'))
+
+        self.tree = urltree.URLTree(self.config)
+
+    class dummyrequest_page :
+        def __init__ (self, page_name, qargs) :
+            self.page_name = page_name
+            self.qargs = qargs
+
+        def get_page_name (self) : return self.page_name    
+        def get_args (self) : return self.qargs
+
+    def _test_handle (self, path, name, qargs={}, **args) :
+        req = self.dummyrequest_page(path, qargs.iteritems())
+
+        h_name, h_args = self.tree.handle_request(req)
+
+        self.assertEqual(h_name, name)
+        self.assertEqual(h_args, args)
+    
+    def test_root (self) :
+        self._test_handle("", 'root')
+
+    def test_bar (self) :
+        self._test_handle("bar", 'bar')
+
+    def test_quux (self) :
+        self._test_handle("quux/a", 'quux', xyz='a')
+
+    def test_quux_boo (self) :
+        self._test_handle("quux/a/boo/b", 'quux_boo', xyz='a', opt='b')
+
+    def test_quux_boo_default (self) :
+        self._test_handle("quux/a/boo", 'quux_boo', xyz='a', opt='no')
+
+    def test_quux_boo_qarg (self) :
+        self._test_handle("quux/a/boo", 'quux_boo', dict(opt='yes'), xyz='a', opt='yes')
+
+if __name__ == '__main__' :
+    unittest.main()
+