/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: 2016-02-23 19:35:21 UTC
  • Revision ID: tim@ed.am-20160223193521-2vgtxbfos50rrpku
renamed version -> VERSION

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
52
55
                        # if section is set, capture lines after the section heading and
53
56
                        # only until the next heading
54
57
                        if capture:
55
 
                                if self.section and re.match( '\[.*\]', line ): break
 
58
                                if self.section and re.match( '\[', line ): break
56
59
                                self.patterns.append( self.convert_line_to_regex( line ) )
57
60
                        elif self.section and line == '[' + self.section + ']':
58
61
                                capture = True
67
70
#                       line = line[ 1:-1 ]
68
71
 
69
72
                # detect escaped chars
70
 
#               if quotes == 
 
73
#               if quotes ==
71
74
 
72
75
                # convert * and ?
73
76
                line = re.sub( r'\?', '.', line )
74
77
                line = re.sub( r'\*', '.*', line )
 
78
                line = "^%s$" % ( line )
75
79
 
76
80
                return line
77
81
 
78
82
 
79
 
        def match( self, rel_file ):
 
83
        def matches( self, rel_file ):
80
84
                for pattern in self.patterns:
81
 
                        if re.match( pattern, rel_file ):
 
85
                        if re.search( pattern, rel_file ):
82
86
                                return True
83
87
                return False
 
88
 
 
89
 
 
90
        def __str__( self ):
 
91
                content = "', '".join( self.patterns )
 
92
                if content: content = " '" + content + "' "
 
93
                return "FileMatcher(" + content + ")"