From ba9dcb559c91d7bf44a5f79da42d963dfb21e85d Mon Sep 17 00:00:00 2001 From: RaNaN Date: Sat, 27 Nov 2010 00:20:04 +0100 Subject: relpath for 2.5 --- module/web/pyload/templatetags/quotepath.py | 16 +++++++++++++++- module/web/pyload/views.py | 13 +++++++++---- 2 files changed, 24 insertions(+), 5 deletions(-) (limited to 'module/web') diff --git a/module/web/pyload/templatetags/quotepath.py b/module/web/pyload/templatetags/quotepath.py index 6c461b445..f3d9d7b2c 100644 --- a/module/web/pyload/templatetags/quotepath.py +++ b/module/web/pyload/templatetags/quotepath.py @@ -1,6 +1,8 @@ +import os +from posixpath import curdir, sep, pardir, join + from django.template.defaultfilters import stringfilter from django import template -import os register = template.Library() @@ -46,6 +48,18 @@ def path_make_relative(path): else: return p + os.path.sep +def relpath(path, start=curdir): + """Return a relative version of a path""" + if not path: + raise ValueError("no path specified") + start_list = posixpath.abspath(start).split(sep) + path_list = posixpath.abspath(path).split(sep) + # Work out how much of the filepath is shared by start and path. + i = len(posixpath.commonprefix([start_list, path_list])) + rel_list = [pardir] * (len(start_list)-i) + path_list[i:] + if not rel_list: + return curdir + return join(*rel_list) register.filter(path_make_relative) diff --git a/module/web/pyload/views.py b/module/web/pyload/views.py index 40d3b8207..62c405339 100644 --- a/module/web/pyload/views.py +++ b/module/web/pyload/views.py @@ -17,6 +17,11 @@ from copy import deepcopy from operator import itemgetter from pyload.templatetags import quotepath +try: + from os.path import relpath +except: + from pyload.templatetags import relpath + from django.conf import settings from django.contrib.auth.decorators import login_required from django.http import HttpResponse @@ -405,7 +410,7 @@ def path(request, path, type): cwd = os.path.abspath(path) abs = True else: - cwd = os.path.relpath(path) + cwd = relpath(path) else: cwd = os.getcwd() @@ -418,10 +423,10 @@ def path(request, path, type): parentdir = os.path.dirname(cwd) if not abs: if os.path.abspath(cwd) == "/": - cwd = os.path.relpath(cwd) + cwd = relpath(cwd) else: - cwd = os.path.relpath(cwd) + os.path.sep - parentdir = os.path.relpath(parentdir) + os.path.sep + cwd = relpath(cwd) + os.path.sep + parentdir = relpath(parentdir) + os.path.sep if os.path.abspath(cwd) == "/": parentdir = "" -- cgit v1.2.3