dabo Commit
Revision 2311
Date: 2006-09-30 06:41:36 -0700 (Sat, 30 Sep 2006)
Author: ed

Changed:
U   trunk/dabo/lib/utils.py

Log:
Added two methods: 'relativePathList()', which returns a list containing the 
elements of the relative path between two paths (or a single path and the 
default of os.getcwd()), and 'relativePath()', which does the same thing, but 
returns a string containing the relativePathList() value joined with 
os.path.sep. I'm planning on using these to improve pathing throughout Dabo by 
making every path relative instead of absolute.


Diff:
Modified: trunk/dabo/lib/utils.py
===================================================================
--- trunk/dabo/lib/utils.py     2006-09-30 13:38:10 UTC (rev 2310)
+++ trunk/dabo/lib/utils.py     2006-09-30 13:41:36 UTC (rev 2311)
@@ -128,4 +128,31 @@
                        ret[kk] = vv
        return ret
                
+
+def relativePathList(toLoc, fromLoc=None):
+       """Given two paths, returns a list that, when joined with 
+       os.path.sep, gives the relative path from 'fromLoc' to
+       "toLoc'. If 'fromLoc' is not specified, the current directory
+       is assumed.
+       """
+       if fromLoc is None:
+               fromLoc = os.getcwd()
+       fromList = fromLoc.split(os.path.sep)
+       toList = toLoc.split(os.path.sep)
        
+       lev = 0
+       
+       while (len(fromList) > lev) and (len(toList) > lev) and \
+                       (fromList[lev] == toList[lev]):
+               lev += 1
+               
+       # 'lev' now contains the first level where they differ
+       fromDiff = fromList[lev:]
+       toDiff = toList[lev:]
+       return [".."] * len(fromDiff) + toDiff
+
+
+def relativePath(toLoc, fromLoc=None):
+       """Given two paths, returns a relative path from fromLoc to toLoc."""
+       return os.path.sep.join(relativePathList(toLoc, fromLoc))
+       




_______________________________________________
Post Messages to: [email protected]
Subscription Maintenance: http://leafe.com/mailman/listinfo/dabo-dev

Reply via email to