Skip to content

Commit

Permalink
Backport posixpath.relpath for Python 2.5
Browse files Browse the repository at this point in the history
Together with the last few commits, this fixes #20 and fixes #19.
  • Loading branch information
SimonSapin committed Aug 13, 2012
1 parent 1301c36 commit 92eb1a1
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion flask_frozen/__init__.py
Expand Up @@ -38,6 +38,23 @@ def is_mapping(obj):
def is_mapping(obj):
return hasattr(obj, 'keys')

try:
from posixpath import relpath as posix_relpath
except ImportError:
# Python 2.5
def posix_relpath(path, start):
sep = posixpath.sep
start_list = [x for x in posixpath.abspath(start).split(sep) if x]
path_list = [x for x in posixpath.abspath(path).split(sep) if x]

# Work out how much of the filepath is shared by start and path.
i = len(posixpath.commonprefix([start_list, path_list]))

rel_list = [posixpath.pardir] * (len(start_list)-i) + path_list[i:]
if not rel_list:
return posixpath.curdir
return posixpath.join(*rel_list)


__all__ = ['Freezer']

Expand Down Expand Up @@ -437,7 +454,7 @@ def relative_url_for(endpoint, **values):
if not request_path.endswith('/'):
request_path = posixpath.dirname(request_path)

return posixpath.relpath(url, request_path)
return posix_relpath(url, request_path)


def unwrap_method(method):
Expand Down

0 comments on commit 92eb1a1

Please sign in to comment.