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