Show More
@@ -1,66 +1,71 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 | from urllib import quote, unquote | |
17 |
|
17 | |||
|
18 | from IPython.utils import py3compat | |||
|
19 | ||||
18 | #----------------------------------------------------------------------------- |
|
20 | #----------------------------------------------------------------------------- | |
19 | # Imports |
|
21 | # Imports | |
20 | #----------------------------------------------------------------------------- |
|
22 | #----------------------------------------------------------------------------- | |
21 |
|
23 | |||
22 | def url_path_join(*pieces): |
|
24 | def url_path_join(*pieces): | |
23 | """Join components of url into a relative url |
|
25 | """Join components of url into a relative url | |
24 |
|
26 | |||
25 | Use to prevent double slash when joining subpath. This will leave the |
|
27 | Use to prevent double slash when joining subpath. This will leave the | |
26 | initial and final / in place |
|
28 | initial and final / in place | |
27 | """ |
|
29 | """ | |
28 | initial = pieces[0].startswith('/') |
|
30 | initial = pieces[0].startswith('/') | |
29 | final = pieces[-1].endswith('/') |
|
31 | final = pieces[-1].endswith('/') | |
30 | stripped = [s.strip('/') for s in pieces] |
|
32 | stripped = [s.strip('/') for s in pieces] | |
31 | result = '/'.join(s for s in stripped if s) |
|
33 | result = '/'.join(s for s in stripped if s) | |
32 | if initial: result = '/' + result |
|
34 | if initial: result = '/' + result | |
33 | if final: result = result + '/' |
|
35 | if final: result = result + '/' | |
34 | if result == '//': result = '/' |
|
36 | if result == '//': result = '/' | |
35 | return result |
|
37 | return result | |
36 |
|
38 | |||
37 | def path2url(path): |
|
39 | def path2url(path): | |
38 | """Convert a local file path to a URL""" |
|
40 | """Convert a local file path to a URL""" | |
39 | pieces = [ quote(p) for p in path.split(os.path.sep) ] |
|
41 | pieces = [ quote(p) for p in path.split(os.path.sep) ] | |
40 | # preserve trailing / |
|
42 | # preserve trailing / | |
41 | if pieces[-1] == '': |
|
43 | if pieces[-1] == '': | |
42 | pieces[-1] = '/' |
|
44 | pieces[-1] = '/' | |
43 | url = url_path_join(*pieces) |
|
45 | url = url_path_join(*pieces) | |
44 | return url |
|
46 | return url | |
45 |
|
47 | |||
46 | def url2path(url): |
|
48 | def url2path(url): | |
47 | """Convert a URL to a local file path""" |
|
49 | """Convert a URL to a local file path""" | |
48 | pieces = [ unquote(p) for p in url.split('/') ] |
|
50 | pieces = [ unquote(p) for p in url.split('/') ] | |
49 | path = os.path.join(*pieces) |
|
51 | path = os.path.join(*pieces) | |
50 | return path |
|
52 | return path | |
51 |
|
53 | |||
52 | def url_escape(path): |
|
54 | def url_escape(path): | |
53 | """Escape special characters in a URL path |
|
55 | """Escape special characters in a URL path | |
54 |
|
56 | |||
55 | Turns '/foo bar/' into '/foo%20bar/' |
|
57 | Turns '/foo bar/' into '/foo%20bar/' | |
56 | """ |
|
58 | """ | |
57 | parts = path.split('/') |
|
59 | parts = py3compat.unicode_to_str(path).split('/') | |
58 | return '/'.join([quote(p) for p in parts]) |
|
60 | return u'/'.join([quote(p) for p in parts]) | |
59 |
|
61 | |||
60 | def url_unescape(path): |
|
62 | def url_unescape(path): | |
61 | """Unescape special characters in a URL path |
|
63 | """Unescape special characters in a URL path | |
62 |
|
64 | |||
63 | Turns '/foo%20bar/' into '/foo bar/' |
|
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