#!python
#
# ScanFiles.py
#
"Funtion to scan all files with names matching a given pattern in a directory tree"

import os
import regex

# Scan files matching pattern in a directory tree
#
# srcdir    directory to search, including subdirectories
# pattern   a compiled regex pattern, for filename selection
# FileFunc  a function to be called for each selected filename
#           as FileFunc( dir, name ).  (NOTE:  this can be an
#           object method with access to the instance data of
#           the object to which it belongs.)
def ScanFiles(srcdir, pattern, FileFunc):
    names = os.listdir(srcdir)
    for name in names:
        srcname = os.path.join(srcdir, name)
        try:
            if os.path.isdir(name):
                ScanFiles(srcname, pattern, filefunc)
            elif pattern.match(name):
                FileFunc(srcdir, name)
        except (IOError, os.error), why:
            print "Can't scan file %s: %s" % (`srcname`, str(why))

