Welcome, guest | Sign In | My Account | Store | Cart

This is a simple Object Oriented way use regular expressions to with a simple include/exclude metaphore to select files on the filesystem.

Python, 76 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
class Action:
    """
    Actions are things that can be executed.
    """
        
    def execute(self):
        pass

class PatternSet:
    """
    Pattern set provides the interface and abstract functionality to provide the include and exclude
    semantics for any classes that want to filter their results based on include and exclude criteria
    """

    def __init__(self):
        self.includeList = []
        self.excludeList = []
        
    def include(self, pattern):
        """
        Patterns should only defined on a single target to match so that we can use the fast fail
        functionality when matching. If you need multiple patterns, call .include() multiple
        times with each expression
        """
        self.includeList.append(re.compile(pattern))
    
    def __isIncluded(self, name):
        result = False
        if len(self.includeList) > 0:
            for pattern in self.includeList:
                if pattern.match(name) != None:
                    result = True
                    break
        return result 

    def exclude(self, pattern):
        """
        Patterns should only defined on single target to match so that we can use the fast fail
        functionality when matching. If you need multiple patterns, call .include() multiple
        times with each expression
        """
        self.excludeList.append(re.compile(pattern))
    
    def __isNotExcluded(self,name):
        result = True
        if len(self.excludeList) > 0:
            for pattern in self.excludeList:
                if pattern.match(name) != None:
                    result = False
                    break
        return result

class FileSet(PatternSet, Action):
    """
    This class allows the user to define a set of files to work on using simple include/exclude semantics
    coupled with the full power of regular expressions
    """    
    def __init__(self, rootDir):
        PatternSet.__init__(self)
        self.rootDir = rootDir

    def execute(self):
        """
        This implementation walks the filesystem from the rootDir and creates a list of
        fully qualified files and returns them.
        """
        results = []
        # walk the dirs from the rootDir and build the results
        for root, dirs, files in os.walk(self.rootDir):
            # if the name is not on the include list or is on the exclude
            # list remove it from the results
            for name in files:
                fqname = join(root,name)
                if self._PatternSet__isIncluded(fqname) and self._PatternSet__isNotExcluded(fqname):
                    results.append(fqname)
        return results

This is just a small part of a project I am working on and thought that this particular piece might be useful to someone else, an Object Oriented simple way to select files from a directory tree and return the list.

Created by Jarrod Roberson on Tue, 12 Jul 2005 (PSF)
Python recipes (4591)
Jarrod Roberson's recipes (1)

Required Modules

  • (none specified)

Other Information and Tasks