bzr branch
http://bzr.ed.am/stdhome
19
by Tim Marston
added file matcher (for symlink accept and/or ignore lists) |
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 |
||
25
by Tim Marston
fixed file_matcher |
22 |
import os, re |
19
by Tim Marston
added file matcher (for symlink accept and/or ignore lists) |
23 |
|
24 |
||
25 |
class FileMatcher: |
|
26 |
||
25
by Tim Marston
fixed file_matcher |
27 |
patterns = list() |
28 |
||
19
by Tim Marston
added file matcher (for symlink accept and/or ignore lists) |
29 |
|
30 |
def __init__( self, file, section = None ): |
|
31 |
self.file = file |
|
25
by Tim Marston
fixed file_matcher |
32 |
self.section = section |
19
by Tim Marston
added file matcher (for symlink accept and/or ignore lists) |
33 |
self.read() |
34 |
||
35 |
||
36 |
def read( self ): |
|
37 |
||
38 |
# read file |
|
39 |
file = os.path.expanduser( self.file ) |
|
40 |
with open( file ) as f: |
|
41 |
lines = f.readlines() |
|
42 |
||
43 |
# parse file |
|
44 |
capture = False if self.section else True |
|
45 |
for line in lines: |
|
46 |
||
47 |
# clean up line and skip empty lines and comments |
|
48 |
line = line.strip(); |
|
49 |
if not len( line ): continue |
|
50 |
if line[ :1 ] == '#': continue |
|
51 |
||
52 |
# if section is set, capture lines after the section heading and |
|
53 |
# only until the next heading |
|
54 |
if capture: |
|
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 + ']': |
|
58 |
capture = True |
|
59 |
||
60 |
||
61 |
def convert_line_to_regex( self, line ): |
|
62 |
||
63 |
# detect (and remove) quotes |
|
64 |
# quotes = None |
|
65 |
# if line[ :1 ] == line[ -1: ] and line[ :1 ] in list( "'", '"' ): |
|
66 |
# quotes = line[ :1 ] |
|
67 |
# line = line[ 1:-1 ] |
|
68 |
||
69 |
# detect escaped chars |
|
70 |
# if quotes == |
|
71 |
||
72 |
# convert * and ? |
|
73 |
line = re.sub( r'\?', '.', line ) |
|
74 |
line = re.sub( r'\*', '.*', line ) |
|
75 |
||
76 |
return line |
|
77 |
||
78 |
||
79 |
def match( self, rel_file ): |
|
80 |
for pattern in self.patterns: |
|
81 |
if re.match( pattern, rel_file ): |
|
82 |
return True |
|
83 |
return False |