Rev 18: Add URL mapping facility. in file:///home/robertc/source/baz/plugins/dbus/trunk/

Robert Collins robertc at robertcollins.net
Sun Mar 25 22:57:00 BST 2007


At file:///home/robertc/source/baz/plugins/dbus/trunk/

------------------------------------------------------------
revno: 18
revision-id: robertc at robertcollins.net-20070325215659-e0jswadjeeef7rfs
parent: robertc at robertcollins.net-20070325215425-39iagmg6u6gto911
committer: Robert Collins <robertc at robertcollins.net>
branch nick: trunk
timestamp: Mon 2007-03-26 07:56:59 +1000
message:
  Add URL mapping facility.
added:
  mapper.py                      mapper.py-20070325215439-9swnwkfh7afa1ugy-1
  tests/test_mapper.py           test_mapper.py-20070325215439-9swnwkfh7afa1ugy-2
modified:
  tests/__init__.py              __init__.py-20070206032729-2b5teqiwctqrfgei-7
=== added file 'mapper.py'
--- a/mapper.py	1970-01-01 00:00:00 +0000
+++ b/mapper.py	2007-03-25 21:56:59 +0000
@@ -0,0 +1,52 @@
+# bzr-dbus: dbus support for bzr/bzrlib.
+# Copyright (C) 2007 Canonical Limited.
+#   Author: Robert Collins.
+# 
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; version 2 of the License.
+# 
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+# 
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+# 
+
+"""URL mapping facility."""
+
+
+class URLMapper(object):
+    """A class to help map private URL's to public ones.
+    
+    Instances have the following attributes:
+    maps: A dict of source_url:[target_url] relationships. When mapping
+        a url, all source_urls that are a prefix of the url being mapped
+        are used. To prevent false matches, its recommended that source_url end
+        in a / (unless of course you want the behaviour of replacing other
+        directories with the same prefix.
+    """
+
+    def __init__(self):
+        """Create a new URLMapper."""
+        self.maps = {}
+
+    def map(self, url):
+        """map url to a list of translated urls.
+
+        :param url: The url to map.
+        :result: A list of mapped urls sorted by the inverse length of the
+            source_url used to perform the mapping. When no mapping is found,
+            the an empty list is returned.
+        """
+        maps = reversed(sorted(self.maps.items(), key=lambda x:len(x)))
+        result = []
+        for source, targets in maps:
+            if url.startswith(source):
+                suffix = url[len(source):]
+                for target in targets:
+                    result.append(target + suffix)
+        return result

=== added file 'tests/test_mapper.py'
--- a/tests/test_mapper.py	1970-01-01 00:00:00 +0000
+++ b/tests/test_mapper.py	2007-03-25 21:56:59 +0000
@@ -0,0 +1,73 @@
+# bzr-dbus: dbus support for bzr/bzrlib.
+# Copyright (C) 2007 Canonical Limited.
+#   Author: Robert Collins.
+# 
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; version 2 of the License.
+# 
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+# 
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+# 
+
+"""Tests for the url mapping logic.."""
+
+from bzrlib.tests import TestCase
+
+from bzrlib.plugins.dbus import mapper
+
+
+class TestMapper(TestCase):
+
+    def test_init(self):
+        a_mapper = mapper.URLMapper()
+        # an empty map should have no maps.
+        self.assertEqual({}, a_mapper.maps)
+
+    def test_single_map(self):
+        a_mapper = mapper.URLMapper()
+        a_mapper.maps['foo/'] = ['bar/']
+        self.assertEqual([], a_mapper.map('foo'))
+        self.assertEqual(['bar/'], a_mapper.map('foo/'))
+        self.assertEqual(['bar/thomolew'], a_mapper.map('foo/thomolew'))
+
+    def test_multi_target_map(self):
+        a_mapper = mapper.URLMapper()
+        a_mapper.maps['foo/'] = ['bar/', 'gam/']
+        self.assertEqual([], a_mapper.map('foo'))
+        self.assertEqual(['bar/', 'gam/'], a_mapper.map('foo/'))
+        self.assertEqual(['bar/thomolew', 'gam/thomolew'],
+            a_mapper.map('foo/thomolew'))
+
+    def test_multi_source_matches(self):
+        a_mapper = mapper.URLMapper()
+        a_mapper.maps['file:///'] = ['http://host/']
+        # longer == more specific, give it both shorter and longer outputs.
+        a_mapper.maps['file:///tmp/'] = ['bzr://host/', 'bzr+ssh://host/tmp/']
+        self.assertEqual([], a_mapper.map('memory:///'))
+        self.assertEqual(
+            ['bzr://host/',
+             'bzr+ssh://host/tmp/',
+             'http://host/tmp/'
+            ],
+            a_mapper.map('file:///tmp/'))
+        self.assertEqual(
+            ['bzr://host/suffix',
+             'bzr+ssh://host/tmp/suffix',
+             'http://host/tmp/suffix'
+            ],
+            a_mapper.map('file:///tmp/suffix'))
+
+    def test_irrelevant_maps_ignored(self):
+        a_mapper = mapper.URLMapper()
+        a_mapper.maps['a/'] = ['b/']
+        a_mapper.maps['b/'] = ['c/']
+        self.assertEqual([], a_mapper.map(''))
+        self.assertEqual(['b/'], a_mapper.map('a/'))
+        self.assertEqual(['c/'], a_mapper.map('b/'))

=== modified file 'tests/__init__.py'
--- a/tests/__init__.py	2007-02-06 04:16:36 +0000
+++ b/tests/__init__.py	2007-03-25 21:56:59 +0000
@@ -27,6 +27,7 @@
     module_names = [
         'bzrlib.plugins.dbus.tests.test_activity',
         'bzrlib.plugins.dbus.tests.test_hook',
+        'bzrlib.plugins.dbus.tests.test_mapper',
         ]
     loader = TestLoader()
     return loader.loadTestsFromModuleNames(module_names)



More information about the bazaar-commits mailing list