home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / pyshared / httplib2 / iri2uri.py < prev   
Encoding:
Python Source  |  2009-12-28  |  3.8 KB  |  111 lines

  1. """
  2. iri2uri
  3.  
  4. Converts an IRI to a URI.
  5.  
  6. """
  7. __author__ = "Joe Gregorio (joe@bitworking.org)"
  8. __copyright__ = "Copyright 2006, Joe Gregorio"
  9. __contributors__ = []
  10. __version__ = "1.0.0"
  11. __license__ = "MIT"
  12. __history__ = """
  13. """
  14.  
  15. import urlparse
  16.  
  17.  
  18. # Convert an IRI to a URI following the rules in RFC 3987
  19. # The characters we need to enocde and escape are defined in the spec:
  20. #
  21. # iprivate =  %xE000-F8FF / %xF0000-FFFFD / %x100000-10FFFD
  22. # ucschar = %xA0-D7FF / %xF900-FDCF / %xFDF0-FFEF
  23. #         / %x10000-1FFFD / %x20000-2FFFD / %x30000-3FFFD
  24. #         / %x40000-4FFFD / %x50000-5FFFD / %x60000-6FFFD
  25. #         / %x70000-7FFFD / %x80000-8FFFD / %x90000-9FFFD
  26. #         / %xA0000-AFFFD / %xB0000-BFFFD / %xC0000-CFFFD
  27. #         / %xD0000-DFFFD / %xE1000-EFFFD
  28.  
  29. escape_range = [
  30.    (0xA0, 0xD7FF ),
  31.    (0xE000, 0xF8FF ),
  32.    (0xF900, 0xFDCF ),
  33.    (0xFDF0, 0xFFEF),
  34.    (0x10000, 0x1FFFD ),
  35.    (0x20000, 0x2FFFD ),
  36.    (0x30000, 0x3FFFD),
  37.    (0x40000, 0x4FFFD ),
  38.    (0x50000, 0x5FFFD ),
  39.    (0x60000, 0x6FFFD),
  40.    (0x70000, 0x7FFFD ),
  41.    (0x80000, 0x8FFFD ),
  42.    (0x90000, 0x9FFFD),
  43.    (0xA0000, 0xAFFFD ),
  44.    (0xB0000, 0xBFFFD ),
  45.    (0xC0000, 0xCFFFD),
  46.    (0xD0000, 0xDFFFD ),
  47.    (0xE1000, 0xEFFFD),
  48.    (0xF0000, 0xFFFFD ),
  49.    (0x100000, 0x10FFFD)
  50. ]
  51.  
  52. def encode(c):
  53.     retval = c
  54.     i = ord(c)
  55.     for low, high in escape_range:
  56.         if i < low:
  57.             break
  58.         if i >= low and i <= high:
  59.             retval = "".join(["%%%2X" % ord(o) for o in c.encode('utf-8')])
  60.             break
  61.     return retval
  62.  
  63.  
  64. def iri2uri(uri):
  65.     """Convert an IRI to a URI. Note that IRIs must be 
  66.     passed in a unicode strings. That is, do not utf-8 encode
  67.     the IRI before passing it into the function.""" 
  68.     if isinstance(uri ,unicode):
  69.         (scheme, authority, path, query, fragment) = urlparse.urlsplit(uri)
  70.         authority = authority.encode('idna')
  71.         # For each character in 'ucschar' or 'iprivate'
  72.         #  1. encode as utf-8
  73.         #  2. then %-encode each octet of that utf-8 
  74.         uri = urlparse.urlunsplit((scheme, authority, path, query, fragment))
  75.         uri = "".join([encode(c) for c in uri])
  76.     return uri
  77.         
  78. if __name__ == "__main__":
  79.     import unittest
  80.  
  81.     class Test(unittest.TestCase):
  82.  
  83.         def test_uris(self):
  84.             """Test that URIs are invariant under the transformation."""
  85.             invariant = [ 
  86.                 u"ftp://ftp.is.co.za/rfc/rfc1808.txt",
  87.                 u"http://www.ietf.org/rfc/rfc2396.txt",
  88.                 u"ldap://[2001:db8::7]/c=GB?objectClass?one",
  89.                 u"mailto:John.Doe@example.com",
  90.                 u"news:comp.infosystems.www.servers.unix",
  91.                 u"tel:+1-816-555-1212",
  92.                 u"telnet://192.0.2.16:80/",
  93.                 u"urn:oasis:names:specification:docbook:dtd:xml:4.1.2" ]
  94.             for uri in invariant:
  95.                 self.assertEqual(uri, iri2uri(uri))
  96.             
  97.         def test_iri(self):
  98.             """ Test that the right type of escaping is done for each part of the URI."""
  99.             self.assertEqual("http://xn--o3h.com/%E2%98%84", iri2uri(u"http://\N{COMET}.com/\N{COMET}"))
  100.             self.assertEqual("http://bitworking.org/?fred=%E2%98%84", iri2uri(u"http://bitworking.org/?fred=\N{COMET}"))
  101.             self.assertEqual("http://bitworking.org/#%E2%98%84", iri2uri(u"http://bitworking.org/#\N{COMET}"))
  102.             self.assertEqual("#%E2%98%84", iri2uri(u"#\N{COMET}"))
  103.             self.assertEqual("/fred?bar=%E2%98%9A#%E2%98%84", iri2uri(u"/fred?bar=\N{BLACK LEFT POINTING INDEX}#\N{COMET}"))
  104.             self.assertEqual("/fred?bar=%E2%98%9A#%E2%98%84", iri2uri(iri2uri(u"/fred?bar=\N{BLACK LEFT POINTING INDEX}#\N{COMET}")))
  105.             self.assertNotEqual("/fred?bar=%E2%98%9A#%E2%98%84", iri2uri(u"/fred?bar=\N{BLACK LEFT POINTING INDEX}#\N{COMET}".encode('utf-8')))
  106.  
  107.     unittest.main()
  108.  
  109.     
  110.