/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 17:30:19 UTC
  • Revision ID: tim@ed.am-20140405173019-5eoi82r8i3etgn4j
added file matcher (for symlink accept and/or ignore lists)

Show diffs side-by-side

added added

removed removed

 
1
# file_matcher.py
 
2
#
 
3
# Copyright (C) 2014 Tim Marston <tim@edm.am>
 
4
#
 
5
# This file is part of stdhome (hereafter referred to as "this program").
 
6
# See http://ed.am/dev/stdhome for more information.
 
7
#
 
8
# This program is free software: you can redistribute it and/or modify
 
9
# it under the terms of the GNU General Public License as published by
 
10
# the Free Software Foundation, either version 3 of the License, or
 
11
# (at your option) any later version.
 
12
#
 
13
# This program is distributed in the hope that it will be useful,
 
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
# GNU General Public License for more details.
 
17
#
 
18
# You should have received a copy of the GNU General Public License
 
19
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
 
 
21
 
 
22
import os
 
23
 
 
24
 
 
25
class FileMatcher:
 
26
 
 
27
 
 
28
        def __init__( self, file, section = None ):
 
29
                self.file = file
 
30
                self.patterns = list()
 
31
                self.read()
 
32
 
 
33
 
 
34
        def read( self ):
 
35
 
 
36
                # read file
 
37
                file = os.path.expanduser( self.file )
 
38
                with open( file ) as f:
 
39
                        lines = f.readlines()
 
40
 
 
41
                # parse file
 
42
                capture = False if self.section else True
 
43
                for line in lines:
 
44
 
 
45
                        # clean up line and skip empty lines and comments
 
46
                        line = line.strip();
 
47
                        if not len( line ): continue
 
48
                        if line[ :1 ] == '#': continue
 
49
 
 
50
                        # if section is set, capture lines after the section heading and
 
51
                        # only until the next heading
 
52
                        if capture:
 
53
                                if self.section and re.match( '\[.*\]', line ): break
 
54
                                self.patterns.append( self.convert_line_to_regex( line ) )
 
55
                        elif self.section and line == '[' + self.section + ']':
 
56
                                capture = True
 
57
 
 
58
 
 
59
        def convert_line_to_regex( self, line ):
 
60
 
 
61
                # detect (and remove) quotes
 
62
#               quotes = None
 
63
#               if line[ :1 ] == line[ -1: ] and line[ :1 ] in list( "'", '"' ):
 
64
#                       quotes = line[ :1 ]
 
65
#                       line = line[ 1:-1 ]
 
66
 
 
67
                # detect escaped chars
 
68
#               if quotes == 
 
69
 
 
70
                # convert * and ?
 
71
                line = re.sub( r'\?', '.', line )
 
72
                line = re.sub( r'\*', '.*', line )
 
73
 
 
74
                return line
 
75
 
 
76
 
 
77
        def match( self, rel_file ):
 
78
                for pattern in self.patterns:
 
79
                        if re.match( pattern, rel_file ):
 
80
                                return True
 
81
                return False