/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 22:29:08 UTC
  • Revision ID: tim@ed.am-20140405222908-3onggkfp5akpz21t
got symlink accept lists working; fixed some walker bugs

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