##// END OF EJS Templates
urllib.quote/unquote must be str on Python 2
MinRK -
Show More
@@ -1,66 +1,71 b''
1 1 """Notebook related utilities
2 2
3 3 Authors:
4 4
5 5 * Brian Granger
6 6 """
7 7
8 8 #-----------------------------------------------------------------------------
9 9 # Copyright (C) 2011 The IPython Development Team
10 10 #
11 11 # Distributed under the terms of the BSD License. The full license is in
12 12 # the file COPYING, distributed as part of this software.
13 13 #-----------------------------------------------------------------------------
14 14
15 15 import os
16 16 from urllib import quote, unquote
17 17
18 from IPython.utils import py3compat
19
18 20 #-----------------------------------------------------------------------------
19 21 # Imports
20 22 #-----------------------------------------------------------------------------
21 23
22 24 def url_path_join(*pieces):
23 25 """Join components of url into a relative url
24 26
25 27 Use to prevent double slash when joining subpath. This will leave the
26 28 initial and final / in place
27 29 """
28 30 initial = pieces[0].startswith('/')
29 31 final = pieces[-1].endswith('/')
30 32 stripped = [s.strip('/') for s in pieces]
31 33 result = '/'.join(s for s in stripped if s)
32 34 if initial: result = '/' + result
33 35 if final: result = result + '/'
34 36 if result == '//': result = '/'
35 37 return result
36 38
37 39 def path2url(path):
38 40 """Convert a local file path to a URL"""
39 41 pieces = [ quote(p) for p in path.split(os.path.sep) ]
40 42 # preserve trailing /
41 43 if pieces[-1] == '':
42 44 pieces[-1] = '/'
43 45 url = url_path_join(*pieces)
44 46 return url
45 47
46 48 def url2path(url):
47 49 """Convert a URL to a local file path"""
48 50 pieces = [ unquote(p) for p in url.split('/') ]
49 51 path = os.path.join(*pieces)
50 52 return path
51 53
52 54 def url_escape(path):
53 55 """Escape special characters in a URL path
54 56
55 57 Turns '/foo bar/' into '/foo%20bar/'
56 58 """
57 parts = path.split('/')
58 return '/'.join([quote(p) for p in parts])
59 parts = py3compat.unicode_to_str(path).split('/')
60 return u'/'.join([quote(p) for p in parts])
59 61
60 62 def url_unescape(path):
61 63 """Unescape special characters in a URL path
62 64
63 65 Turns '/foo%20bar/' into '/foo bar/'
64 66 """
65 return '/'.join([unquote(p) for p in path.split('/')])
67 return u'/'.join([
68 py3compat.str_to_unicode(unquote(p))
69 for p in py3compat.unicode_to_str(path).split('/')
70 ])
66 71
General Comments 0
You need to be logged in to leave comments. Login now