/stdhome

To get this branch, use:
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
38 by Tim Marston
fixed bug in FileMatcher causing it to blow up when ~/.stdhomerc wasn't present
22
import os, re, errno
19 by Tim Marston
added file matcher (for symlink accept and/or ignore lists)
23
24
25
class FileMatcher:
26
27
28
	def __init__( self, file, section = None ):
29
		self.file = file
25 by Tim Marston
fixed file_matcher
30
		self.section = section
38 by Tim Marston
fixed bug in FileMatcher causing it to blow up when ~/.stdhomerc wasn't present
31
		self.patterns = list()
19 by Tim Marston
added file matcher (for symlink accept and/or ignore lists)
32
		self.read()
33
34
35
	def read( self ):
36
37
		# read file
38
		file = os.path.expanduser( self.file )
38 by Tim Marston
fixed bug in FileMatcher causing it to blow up when ~/.stdhomerc wasn't present
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()
19 by Tim Marston
added file matcher (for symlink accept and/or ignore lists)
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:
61 by Tim Marston
added home directory change reporting to CopyOutWalker; added --quiet option to
58
				if self.section and re.match( '\[', line ): break
19 by Tim Marston
added file matcher (for symlink accept and/or ignore lists)
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
61 by Tim Marston
added home directory change reporting to CopyOutWalker; added --quiet option to
73
#		if quotes ==
19 by Tim Marston
added file matcher (for symlink accept and/or ignore lists)
74
75
		# convert * and ?
76
		line = re.sub( r'\?', '.', line )
77
		line = re.sub( r'\*', '.*', line )
61 by Tim Marston
added home directory change reporting to CopyOutWalker; added --quiet option to
78
		line = "^%s$" % ( line )
19 by Tim Marston
added file matcher (for symlink accept and/or ignore lists)
79
80
		return line
81
82
38 by Tim Marston
fixed bug in FileMatcher causing it to blow up when ~/.stdhomerc wasn't present
83
	def matches( self, rel_file ):
19 by Tim Marston
added file matcher (for symlink accept and/or ignore lists)
84
		for pattern in self.patterns:
61 by Tim Marston
added home directory change reporting to CopyOutWalker; added --quiet option to
85
			if re.search( pattern, rel_file ):
19 by Tim Marston
added file matcher (for symlink accept and/or ignore lists)
86
				return True
87
		return False
38 by Tim Marston
fixed bug in FileMatcher causing it to blow up when ~/.stdhomerc wasn't present
88
89
90
	def __str__( self ):
91
		content = "', '".join( self.patterns )
92
		if content: content = " '" + content + "' "
93
		return "FileMatcher(" + content + ")"