/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-05 19:21:27 UTC
  • Revision ID: tim@ed.am-20140405192127-yvt08c7qnnp2nr8i
fixed some bugs in Bzr.revert()

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, errno
 
22
import os
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
31
30
                self.patterns = list()
32
31
                self.read()
33
32
 
36
35
 
37
36
                # read file
38
37
                file = os.path.expanduser( self.file )
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()
 
38
                with open( file ) as f:
 
39
                        lines = f.readlines()
45
40
 
46
41
                # parse file
47
42
                capture = False if self.section else True
55
50
                        # if section is set, capture lines after the section heading and
56
51
                        # only until the next heading
57
52
                        if capture:
58
 
                                if self.section and re.match( '\[', line ): break
 
53
                                if self.section and re.match( '\[.*\]', line ): break
59
54
                                self.patterns.append( self.convert_line_to_regex( line ) )
60
55
                        elif self.section and line == '[' + self.section + ']':
61
56
                                capture = True
70
65
#                       line = line[ 1:-1 ]
71
66
 
72
67
                # detect escaped chars
73
 
#               if quotes ==
 
68
#               if quotes == 
74
69
 
75
70
                # convert * and ?
76
71
                line = re.sub( r'\?', '.', line )
77
72
                line = re.sub( r'\*', '.*', line )
78
 
                line = "^%s$" % ( line )
79
73
 
80
74
                return line
81
75
 
82
76
 
83
 
        def matches( self, rel_file ):
 
77
        def match( self, rel_file ):
84
78
                for pattern in self.patterns:
85
 
                        if re.search( pattern, rel_file ):
 
79
                        if re.match( pattern, rel_file ):
86
80
                                return True
87
81
                return False
88
 
 
89
 
 
90
 
        def __str__( self ):
91
 
                content = "', '".join( self.patterns )
92
 
                if content: content = " '" + content + "' "
93
 
                return "FileMatcher(" + content + ")"