diff --git a/tsearchpath.py b/tsearchpath.py index f15295d..e18f07f 100755 --- a/tsearchpath.py +++ b/tsearchpath.py @@ -5,7 +5,7 @@ # Program Information -RCSID = "$Id: tsearchpath.py,v 1.101 2010/06/24 19:44:52 tundra Exp $" +RCSID = "$Id: tsearchpath.py,v 1.102 2010/06/24 20:15:32 tundra Exp $" #----------------------------------------------------------# @@ -19,7 +19,7 @@ # Constants & Literals # #----------------------------------------------------------# - +DEFAULTDELIM = ":" @@ -28,23 +28,61 @@ ##### -def tsearchpath(filename, searchpath, PATHDELIM=":", PATHSEP=os.path.sep): +def tsearchpath(filename, pathlist, + PATHDELIM=DEFAULTDELIM, + PATHSEP=os.path.sep): - """ Search 'searchpath' looking for 'filename' returning - on first (leftmost) match. + """ Search 'pathlist' looking for 'filename' returning on first + (leftmost in 'pathlist') match. The function is insensitive + to whether 'filename' is actually a file. It merely looks for + some filesystem entity having the given name. If found, return the fully qualified filename. If not found, return None. - 'searchpath' is a list a paths to search separated by + 'pathlist' is a list of paths to search separated by PATHDELIM characters. If the 'filename' passed contains 'PATHSEP' characters, - presume it to be a relative/absolute path specified - by the caller. In this case, ignore 'searchpath' - and just return the fully qualified filename. + presume it to be a relative/absolute path specified by the + caller. In this case, ignore 'pathlist' and just return the + fully qualified filename. + + Caller can override the delimiter used to separate path + entries and the path separator character by passing explicit + instances of PATHDELIM= and PATHSEP= respectively. """ - pass + + # What we'll return if we find nothing + + retval = None + + # Handle explicit/relative filename path specifications + + if filename.find(PATHSEP) > -1: + retval = filename + + + # Find first matching filename on specified path + + else: + + paths = pathlist.split(PATHDELIM) + + for path in paths: + + if path[-1] != PATHSEP: + path += PATHSEP + + path += filename + + if os.path.exists(path): + retval = path + break + if retval: + retval = os.path.abspath(retval) + + return retval # End of 'tsearchpath()'