
import os,string

from common import *

class FS_file_system:
  def FS_walk_and_load_config(s,fun,path):
#    if DEBUG: print 'loadcgf:walking',path
    dir=os.listdir(path)
    dir.sort()
    for fn in dir:
      fp=os.path.join(path,fn)
      if   os.path.islink(fp):
        if DEBUG: print 'ThingFactory: skipping link',fp
        pass
      elif os.path.isdir(fp):
        if fn!='CVS':
          s.FS_walk_and_load_config(fun,fp)
      elif os.path.isfile(fp):
        ext=string.lower( string.split(fn,'.')[-1] )
        if ext in ['conf','config','cfg' ]:
          apply( fun, ( path, fp ) )
  def FS_walk_and_find_file(s,name,path):
#    if DEBUG: print 'find:walking',path
    dir=os.listdir(path)
    dir.sort()
    rv=[]
    for fn in dir:
      fp=os.path.join(path,fn)
      if fn==name:
        rv.append(fp)
      else:
        if   os.path.islink(fp):
          pass
        elif os.path.isdir(fp):
          if fn!='CVS':
            rv=rv+s.FS_walk_and_find_file(name,fp)
    if len(rv)==0: return None
    if len(rv)>1:  raise 'FS.find_file: found more than one',(name,rv)
    return rv[0]
