spv-dev team mailing list archive
-
spv-dev team
-
Mailing list archive
-
Message #00001
[Branch ~bouf10/slidepresenterview/refactor-pres-design] Rev 59: Introduce the URI concept.
------------------------------------------------------------
revno: 59
committer: F.A. Bourbonnais <bouf10pub@xxxxxxxxxxx>
branch nick: refactor-pres-design
timestamp: Sat 2010-05-08 22:56:23 -0400
message:
Introduce the URI concept.
added:
slidepresenterview/tests/unit/utils/
slidepresenterview/tests/unit/utils/__init__.py
slidepresenterview/tests/unit/utils/tests_uri.py
slidepresenterview/utils/uri.py
--
lp:~bouf10/slidepresenterview/refactor-pres-design
https://code.launchpad.net/~bouf10/slidepresenterview/refactor-pres-design
Your team SlidePresenterView Development Team is subscribed to branch lp:~bouf10/slidepresenterview/refactor-pres-design.
To unsubscribe from this branch go to https://code.launchpad.net/~bouf10/slidepresenterview/refactor-pres-design/+edit-subscription
=== added directory 'slidepresenterview/tests/unit/utils'
=== added file 'slidepresenterview/tests/unit/utils/__init__.py'
=== added file 'slidepresenterview/tests/unit/utils/tests_uri.py'
--- slidepresenterview/tests/unit/utils/tests_uri.py 1970-01-01 00:00:00 +0000
+++ slidepresenterview/tests/unit/utils/tests_uri.py 2010-05-09 02:56:23 +0000
@@ -0,0 +1,206 @@
+# -*- coding: utf-8 -*-
+#
+# SlidePresenterView - Console for presenters
+# https://launchpad.net/slidepresenterview
+#
+# Copyright (C) 2010 Felix-Antoine Bourbonnais <bouf10pub _AT@. rubico.info>
+#
+# 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, either version 3 of the License, or
+# (at your option) any later version.
+#
+# 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, see <http://www.gnu.org/licenses/>.
+#
+"""
+Tests for the L{slidepresenterview.utils.uri} module.
+"""
+
+import unittest
+
+from slidepresenterview.utils.uri import URI
+
+class TestGivenFullAsciiUri(unittest.TestCase):
+
+ def setUp(self):
+ self.uri_str = "http://example.com/path/to/resource.pdf?a=1&b"
+ self.uri = URI(self.uri_str)
+
+
+ def should_be_parsed(self):
+ self.assertEquals("http", self.uri.scheme)
+ self.assertEquals("example.com", self.uri.netloc)
+ self.assertEquals("/path/to/resource.pdf", self.uri.path)
+ self.assertEquals("a=1&b", self.uri.query)
+
+
+ def should_be_type_str(self):
+ self.assertTrue(isinstance(self.uri.scheme, str))
+ self.assertTrue(isinstance(self.uri.netloc, str))
+ self.assertTrue(isinstance(self.uri.path, str))
+ self.assertTrue(isinstance(self.uri.query, str))
+
+
+ def test_uri_property_should_return_same_str_and_type(self):
+ self.assertEquals(self.uri_str, self.uri.uri)
+ self.assertTrue(isinstance(self.uri.uri, str))
+
+
+ def test_str_property_should_return_uri(self):
+ self.assertEquals(self.uri_str, str(self.uri))
+
+
+class TestGivenUnicodeUri(unittest.TestCase): #pylint: disable-msg=R0904
+
+ def setUp(self):
+ self.uri_unicode = u"http://exaéple.com/path/t o/résource.pdf?a=é&b"
+ self.uri = URI(self.uri_unicode)
+
+
+ def should_be_parsed(self):
+ self.assertEquals("http", self.uri.scheme)
+ self.assertEquals(u"exaéple.com", self.uri.netloc)
+ self.assertEquals(u"/path/t o/résource.pdf", self.uri.path)
+ self.assertEquals(u"a=é&b", self.uri.query)
+
+
+ def should_be_type_str(self):
+ self.assertTrue(isinstance(self.uri.scheme, unicode))
+ self.assertTrue(isinstance(self.uri.netloc, unicode))
+ self.assertTrue(isinstance(self.uri.path, unicode))
+ self.assertTrue(isinstance(self.uri.query, unicode))
+
+
+ def test_uri_property_should_return_same_str_and_type(self):
+ self.assertEquals(self.uri_unicode, self.uri.uri)
+ self.assertTrue(isinstance(self.uri.uri, unicode))
+
+
+ def test_unicode_property_should_return_uri_in_unicode(self):
+ self.assertEquals(self.uri_unicode, str(self.uri))
+ self.assertTrue(isinstance(unicode(self.uri), unicode))
+
+
+class TestGivenFullFileScheme(unittest.TestCase):
+
+ def setUp(self):
+ self.uri_unicode = u"file:///path/to/résource?a=é"
+ self.uri = URI(self.uri_unicode)
+
+
+ def should_have_file_as_scheme(self):
+ self.assertEquals("file", self.uri.scheme)
+
+
+ def should_have_no_netloc(self):
+ self.assertEquals("", self.uri.netloc)
+
+
+ def should_have_no_query(self):
+ self.assertEquals("", self.uri.query)
+
+
+#pylint: disable-msg=C0103,R0904
+class TestGivenFileSchemeWithNoHeadSlashAndWithBackslash(unittest.TestCase):
+
+ def setUp(self):
+ self.uri_unicode = u"file://c:/a é\a.pdf"
+ self.uri = URI(self.uri_unicode)
+
+
+ def should_have_file_as_scheme(self):
+ self.assertEquals("file", self.uri.scheme)
+
+
+ def should_have_no_netloc(self):
+ self.assertEquals("", self.uri.netloc)
+
+
+ def should_all_is_in_the_path(self):
+ self.assertEquals(u"c:/a é\a.pdf", self.uri.path)
+
+
+ def should_not_nomalised_path(self):
+ self.assertEquals(u"c:/a é\a.pdf", self.uri.path)
+
+
+ def should_have_no_query(self):
+ self.assertEquals("", self.uri.query)
+
+
+ def should_have_the_same_full_uri(self):
+ self.assertEquals(self.uri_unicode, self.uri.uri)
+
+
+ def test_unicode_property_should_have_the_full_uri(self):
+ self.assertEquals(self.uri_unicode, unicode(self.uri))
+
+
+class TestUriComparisons_GivenHttpScheme(unittest.TestCase):
+
+ def setUp(self):
+ self.uri_base = URI(u"http://éxample.com/patéh?a=é")
+ self.uri_same = URI(u"http://éxample.com/patéh?a=é")
+ self.uri_same_str = URI("http://éxample.com/patéh?a=é")
+ self.uri_diff_scheme = URI(u"https://éxample.com/patéh?a=é")
+ self.uri_diff_netloc = URI(u"http://example.com/patéh?a=é")
+ self.uri_diff_path = URI(u"http://éxample.com/pateh?a=é")
+ self.uri_diff_query = URI(u"http://éxample.com/patéh?a=e")
+ self.uri_diff_all = URI(u"https://example.com/pateh?a=e")
+
+
+ def test_same_instance_should_be_equal(self):
+ self.assertTrue(self.uri_base == self.uri_base)
+ self.assertFalse(self.uri_base != self.uri_base) #Tests the != operator
+
+
+ def test_same_uri_should_be_equal(self):
+ self.assertTrue(self.uri_base == self.uri_same)
+ self.assertFalse(self.uri_base != self.uri_same) #Tests the != operator
+
+
+ def test_same_uri_diff_encoding_should_be_equal(self):
+ self.assertTrue(self.uri_base == self.uri_same_str)
+
+
+ def test_one_part_diff_should_not_be_equal(self):
+ self.assertTrue(self.uri_base != self.uri_diff_scheme)
+ self.assertTrue(self.uri_base != self.uri_diff_netloc)
+ self.assertTrue(self.uri_base != self.uri_diff_path)
+ self.assertTrue(self.uri_base != self.uri_diff_query)
+
+
+ def test_all_parts_diff_should_be_not_equals(self):
+ self.assertTrue(self.uri_base != self.uri_diff_all)
+
+
+class TestUriComparisons_GivenFileScheme(unittest.TestCase):
+
+ def setUp(self):
+ self.uri_unix_base = URI(u"file:///path/a é/fé.pdf")
+ self.uri_unix_diff_path = URI(u"file:///path/a e/fé.pdf")
+ self.uri_win_base = URI(u"file://c:\path\a é\fé.pdf")
+ self.uri_win_diff_path = URI(u"file://c:\path\a é\fe.pdf")
+ self.uri_win_base_norm = URI(u"file://c:/path/a é/fé.pdf")
+
+
+ def test_normalisation_should_be_ignored(self):
+ self.assertTrue(self.uri_win_base != self.uri_win_base_norm)
+
+
+ def test_same_instances_should_be_equal(self):
+ self.assertTrue(self.uri_win_base == self.uri_win_base)
+ self.assertTrue(self.uri_unix_base == self.uri_unix_base)
+
+
+ def test_one_part_diff_should_not_be_equal(self):
+ self.assertTrue(self.uri_win_base != self.uri_win_diff_path)
+ self.assertTrue(self.uri_unix_base != self.uri_unix_diff_path)
+
+
\ No newline at end of file
=== added file 'slidepresenterview/utils/uri.py'
--- slidepresenterview/utils/uri.py 1970-01-01 00:00:00 +0000
+++ slidepresenterview/utils/uri.py 2010-05-09 02:56:23 +0000
@@ -0,0 +1,92 @@
+# -*- coding: utf-8 -*-
+#
+# SlidePresenterView - Console for presenters
+# https://launchpad.net/slidepresenterview
+#
+# Copyright (C) 2010 Felix-Antoine Bourbonnais <bouf10pub _AT@. rubico.info>
+#
+# 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, either version 3 of the License, or
+# (at your option) any later version.
+#
+# 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, see <http://www.gnu.org/licenses/>.
+#
+"""
+Utilities to manipulate URIs
+"""
+
+from urllib2 import urlparse
+
+__all__ = ['URI']
+
+
+class URI(object):
+ """
+ Represents an URI.
+
+ The comparison between URI objects is content based so it is done by
+ comparing the uri strings.
+
+ Note: Paths are not normalised (path is as is)
+
+ Encoding
+ --------
+
+ - Every inputs should be in unicode (not str encoded or QString)
+ - Outputs are in the same type as received
+ - No URL decoding is done so encoded characters (like %2F) will remains.
+
+ Decomposition
+ -------------
+
+ URI are decomposed in parts [http://example.com/path/to/resouce.pdf?a=1&b]:
+ - A protocol/scheme (scheme). [http]
+ - A network location (netloc) [example.com]
+ - A resource path (the heading / is kept) [/path/to/resource.pdf]
+ - A query [a=1&b]
+
+ The splitting is done considering the scheme. For instance, with the
+ protocol file, queries will be ignored. In that case, everything
+ following the scheme will be part of the resource path.
+
+ @ivar scheme: The protocol/scheme
+ @ivar netloc: The network location
+ @ivar path: The complete resource path
+ @ivar query: The query
+ @ivar uri: The full uri
+ """
+
+ def __init__(self, uri_string):
+ uri_split = urlparse.urlsplit(uri_string, allow_fragments=False)
+
+ self.scheme = uri_split.scheme
+ if self.scheme == 'file':
+ self.netloc = ''
+ self.path = uri_split.netloc + uri_split.path
+ else:
+ self.netloc = uri_split.netloc
+ self.path = uri_split.path
+ self.query = uri_split.query
+
+
+ @property
+ def uri(self):
+ uri = "%s://%s%s" % (self.scheme, self.netloc, self.path)
+ if len(self.query) > 0:
+ uri = "%s?%s" % (uri, self.query)
+ return uri
+
+
+ def __str__(self):
+ return self.uri
+
+
+ def __cmp__(self, other_uri):
+ return cmp(self.uri, other_uri.uri)
\ No newline at end of file