3
# Copyright (C) 2014 Tim Marston <tim@edm.am>
5
# This file is part of stdhome (hereafter referred to as "this program").
6
# See http://ed.am/dev/stdhome for more information.
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.
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.
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/>.
30
def __init__( self, file, section = None ):
32
self.section = section
39
file = os.path.expanduser( self.file )
40
with open( file ) as f:
44
capture = False if self.section else True
47
# clean up line and skip empty lines and comments
49
if not len( line ): continue
50
if line[ :1 ] == '#': continue
52
# if section is set, capture lines after the section heading and
53
# only until the next heading
55
if self.section and re.match( '\[.*\]', line ): break
56
self.patterns.append( self.convert_line_to_regex( line ) )
57
elif self.section and line == '[' + self.section + ']':
61
def convert_line_to_regex( self, line ):
63
# detect (and remove) quotes
65
# if line[ :1 ] == line[ -1: ] and line[ :1 ] in list( "'", '"' ):
69
# detect escaped chars
73
line = re.sub( r'\?', '.', line )
74
line = re.sub( r'\*', '.*', line )
79
def match( self, rel_file ):
80
for pattern in self.patterns:
81
if re.match( pattern, rel_file ):