# HG changeset patch # User Jeff Hammel # Date 1309311558 25200 # Node ID b02420253bfdc89fa514c4a2ee5b4d2f1ae165fc # Parent 8d47894191a09ec1dc98371ed6c76af8c672a58a add recomposition and a test for it diff -r 8d47894191a0 -r b02420253bfd tests/doctest.txt --- a/tests/doctest.txt Mon Jun 27 11:22:40 2011 -0700 +++ b/tests/doctest.txt Tue Jun 28 18:39:18 2011 -0700 @@ -20,11 +20,14 @@ >>> sorted(matcher.match('example')) # -> example.* ['http://www.example.com/foo/bar/fleem', 'http://www.example.com/foo/blah', 'https://www.example.com/foo/', 'https://www.example.net/foo/'] -Test decomposition:: +Test decomposition and recomposition:: >>> matcher = UrlMatcher() - >>> sorted(matcher.decompose('http://www.example.com/foo').items()) + >>> decomposed = matcher.decompose('http://www.example.com/foo') + >>> sorted(decomposed.items()) [('domain', ['example', 'com', 'www']), ('path', ['foo']), ('scheme', 'http')] + >>> matcher.recompose(decomposed) + 'http://www.example.com/foo' Test url diffs:: diff -r 8d47894191a0 -r b02420253bfd urlmatch.py --- a/urlmatch.py Mon Jun 27 11:22:40 2011 -0700 +++ b/urlmatch.py Tue Jun 28 18:39:18 2011 -0700 @@ -39,6 +39,31 @@ return urldict + @classmethod + def recompose(cls, url): + """reconstruct a deconstructed url""" + + # must have a domain + assert 'domain' in url + assert url['domain'] + + # reconstruct domain + if len(url['domain']) == 1: + return url['domain'][0] # what else to do? + retval = '%s.%s' % tuple(url['domain'][:2]) + if len(url['domain']) > 2: + retval = '.'.join(reversed(url['domain'][2:])) + '.' + retval + + # add the scheme + if 'scheme' in url: + retval = url['scheme'] + '://' + retval + + # add the path + if 'path' in url: + retval += '/' + '/'.join(url['path']) + + return retval + def add(self, url): if url not in self.urls: self.urls[url] = self.decompose(url)