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