/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-04-18 14:43:42 UTC
  • Revision ID: tim@ed.am-20140418144342-9bba5zb0o6gqko98
fixed bug in FileMatcher causing it to blow up when ~/.stdhomerc wasn't present

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, re
 
22
import os, re, errno
23
23
 
24
24
 
25
25
class FileMatcher:
26
26
 
27
 
        patterns = list()
28
 
 
29
27
 
30
28
        def __init__( self, file, section = None ):
31
29
                self.file = file
32
30
                self.section = section
 
31
                self.patterns = list()
33
32
                self.read()
34
33
 
35
34
 
37
36
 
38
37
                # read file
39
38
                file = os.path.expanduser( self.file )
40
 
                with open( file ) as f:
41
 
                        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()
42
45
 
43
46
                # parse file
44
47
                capture = False if self.section else True
76
79
                return line
77
80
 
78
81
 
79
 
        def match( self, rel_file ):
 
82
        def matches( self, rel_file ):
80
83
                for pattern in self.patterns:
81
84
                        if re.match( pattern, rel_file ):
82
85
                                return True
83
86
                return False
 
87
 
 
88
 
 
89
        def __str__( self ):
 
90
                content = "', '".join( self.patterns )
 
91
                if content: content = " '" + content + "' "
 
92
                return "FileMatcher(" + content + ")"