Show More
@@ -12,6 +12,9 b' Authors:' | |||
|
12 | 12 | # the file COPYING, distributed as part of this software. |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | |
|
15 | import os | |
|
16 | from urllib import quote, unquote | |
|
17 | ||
|
15 | 18 | #----------------------------------------------------------------------------- |
|
16 | 19 | # Imports |
|
17 | 20 | #----------------------------------------------------------------------------- |
@@ -24,9 +27,25 b' def url_path_join(*pieces):' | |||
|
24 | 27 | """ |
|
25 | 28 | initial = pieces[0].startswith('/') |
|
26 | 29 | final = pieces[-1].endswith('/') |
|
27 | striped = [s.strip('/') for s in pieces] | |
|
28 | result = '/'.join(s for s in striped if s) | |
|
30 | stripped = [s.strip('/') for s in pieces] | |
|
31 | result = '/'.join(s for s in stripped if s) | |
|
29 | 32 | if initial: result = '/' + result |
|
30 | 33 | if final: result = result + '/' |
|
31 | 34 | if result == '//': result = '/' |
|
32 | 35 | return result |
|
36 | ||
|
37 | def path2url(path): | |
|
38 | """Convert a local file path to a URL""" | |
|
39 | pieces = [ quote(p) for p in path.split(os.path.sep) ] | |
|
40 | # preserve trailing / | |
|
41 | if pieces[-1] == '': | |
|
42 | pieces[-1] = '/' | |
|
43 | url = url_path_join(*pieces) | |
|
44 | return url | |
|
45 | ||
|
46 | def url2path(url): | |
|
47 | """Convert a URL to a local file path""" | |
|
48 | pieces = [ unquote(p) for p in url.split('/') ] | |
|
49 | path = os.path.join(*pieces) | |
|
50 | return path | |
|
51 |
General Comments 0
You need to be logged in to leave comments.
Login now