"""RelPathRedux update to relpath """ __author__ = "Josh English (english@spiritone.com)" __version__ = "$Revision: 0.2 $" __date__ = "$Date: 2003/08/85 16:33:42 $" __copyright__ = "Copyright (c) 2003 Josh English" __license__ = "Python" __history__ = """ 0.1 2003-08-05: Initial release 0.2 2004-07-24: Updated code to use urlparse, added ability to detect fragments """ import urlparse class RelPathError(Exception): pass class NotFullPathError(RelPathError): pass def relPathTo(Here,There): """relPathTo(Here,There) Returns a relative path from one page to another. Both Here and There must be absolute URL's """ herelist = urlparse.urlsplit(Here) therelist = urlparse.urlsplit(There) res = '' #print herelist #print therelist if not herelist[0]: raise NotFullPathError, "%s is not a full path" % Here if not therelist[0]: raise NotFullPathError, "%s is not a full path" % There #schemes are different, so return There as a full url if herelist[0].lower() != therelist[0].lower(): return urlparse.urlunsplit(therelist) if herelist[1].lower() != therelist[1].lower(): return urlparse.urlunsplit(therelist) else: ### same netlocations herepath = herelist[2].split('/')[1:] therepath = therelist[2].split('/')[1:] if herepath[0].startswith('~') and therepath[0].startswith('~'): if herepath[0].lower() != therepath[0].lower(): return urlparse.urlunsplit(therelist) #remove all elements that are the same while herepath[0]==therepath[0]: d =herepath.pop(0) therepath.pop(0) if not herepath or not therepath: break if len(herepath) < len(therepath): res = '/'.join(therepath) #print res else: gotolist= ['..']*(len(herepath)-1) gotolist.extend(therepath) res = '/'.join(gotolist) if res and therelist[4]: res = '%s#%s' % (res,therelist[4]) elif not res and therelist[4]: res = '#%s' % therelist[4] elif not res: res = '#' return res def relPathFrom(Here,There): return relPathTo(There,here) if __name__=='__main__': # Simple Test Cases. For a more rigorous test see relpathtest.py hpath = "http://www.s.com/~english/index.html" tpath = "http://www.s.com/~josh/index.html" ipath = "https://www.s.com/~english/index.html" ypath = "http://www.s.com/~english/index.html#blog" epath = "http://www.s.com/~english/education.html" mpath = "http://www.s.com/~english/math/index.html" zpath = "http://www.s.com/~english/math/zach.html" cpath = "http://www.s.com/~english/code.html" fpath = "http://www.s.com/~english/code/fraction.html" wpath = "http://www.s.com/~english/code/webmenu/webmenu.html" spath = "http://www.s.com/~english/code/wcms/wcms.html" npath = "http://www.c.com/index.html" opath = "http://www.c.com/java/magic.html" print '-'*24 _debug = 0 print relPathTo(hpath,tpath)