add path2url and url2path to html.utils

MinRK 13 years ago
parent 2a946c62ab
commit ed66b8b8fe

@ -12,6 +12,9 @@ Authors:
# the file COPYING, distributed as part of this software.
#-----------------------------------------------------------------------------
import os
from urllib import quote, unquote
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
@ -24,9 +27,25 @@ def url_path_join(*pieces):
"""
initial = pieces[0].startswith('/')
final = pieces[-1].endswith('/')
striped = [s.strip('/') for s in pieces]
result = '/'.join(s for s in striped if s)
stripped = [s.strip('/') for s in pieces]
result = '/'.join(s for s in stripped if s)
if initial: result = '/' + result
if final: result = result + '/'
if result == '//': result = '/'
return result
def path2url(path):
"""Convert a local file path to a URL"""
pieces = [ quote(p) for p in path.split(os.path.sep) ]
# preserve trailing /
if pieces[-1] == '':
pieces[-1] = '/'
url = url_path_join(*pieces)
return url
def url2path(url):
"""Convert a URL to a local file path"""
pieces = [ unquote(p) for p in url.split('/') ]
path = os.path.join(*pieces)
return path

Loading…
Cancel
Save