/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-04-10 20:30:10 UTC
  • Revision ID: tim@ed.am-20160410203010-xt9rvf6mf74xh5g1
updated todo

Show diffs side-by-side

added added

removed removed

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