utils.py: add findInPaths
authorrgbecker
Wed, 09 Sep 2009 16:36:49 +0000
changeset 3213 b6da4a7ffec2
parent 3212 d23f2325df9a
child 3214 f7e2da62a5a0
utils.py: add findInPaths
src/reportlab/lib/utils.py
--- a/src/reportlab/lib/utils.py	Wed Sep 09 12:55:01 2009 +0000
+++ b/src/reportlab/lib/utils.py	Wed Sep 09 16:36:49 2009 +0000
@@ -1094,3 +1094,16 @@
     ['a', ',b', 'c']    
     '''
     return ','.join([ ' ' + i.replace(',', ',,') + ' ' for i in l ])
+
+def findInPaths(fn,paths,isfile=True,fail=False):
+    '''search for relative files in likely places'''
+    exists = isfile and os.path.isfile or os.path.isdir
+    if exists(fn): return fn
+    pjoin = os.path.join
+    if not os.path.isabs(fn):
+        for p in paths:
+            pfn = pjoin(p,fn)
+            if exists(pfn):
+                return pfn
+    if fail: raise ValueError('cannot locate %r with paths=%r' % (fn,paths))
+    return fn