/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: 2013-12-08 19:28:11 UTC
  • Revision ID: tim@ed.am-20131208192811-r20qj7cgmn4duw11
initial commit; basic app startup and initial command-line processing

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