/stdhome

To get this branch, use:
bzr branch http://bzr.ed.am/stdhome

« back to all changes in this revision

Viewing changes to lib/stdhome/file_matcher.py

  • Committer: Tim Marston
  • Date: 2014-07-17 18:19:47 UTC
  • Revision ID: tim@ed.am-20140717181947-pe060idibfu0kfsk
fixed diff command out-of-order output issue

Show diffs side-by-side

added added

removed removed

19
19
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
20
 
21
21
 
22
 
import os
 
22
import os, re, errno
23
23
 
24
24
 
25
25
class FileMatcher:
27
27
 
28
28
        def __init__( self, file, section = None ):
29
29
                self.file = file
 
30
                self.section = section
30
31
                self.patterns = list()
31
32
                self.read()
32
33
 
35
36
 
36
37
                # read file
37
38
                file = os.path.expanduser( self.file )
38
 
                with open( file ) as f:
39
 
                        lines = f.readlines()
 
39
                try:
 
40
                        with open( file ) as f:
 
41
                                lines = f.readlines()
 
42
                except IOError as e:
 
43
                        if e.errno != errno.ENOENT: raise
 
44
                        lines = list()
40
45
 
41
46
                # parse file
42
47
                capture = False if self.section else True
74
79
                return line
75
80
 
76
81
 
77
 
        def match( self, rel_file ):
 
82
        def matches( self, rel_file ):
78
83
                for pattern in self.patterns:
79
84
                        if re.match( pattern, rel_file ):
80
85
                                return True
81
86
                return False
 
87
 
 
88
 
 
89
        def __str__( self ):
 
90
                content = "', '".join( self.patterns )
 
91
                if content: content = " '" + content + "' "
 
92
                return "FileMatcher(" + content + ")"