-
Notifications
You must be signed in to change notification settings - Fork 16
/
pathmap.py
56 lines (48 loc) · 1.53 KB
/
pathmap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#
# Copyright (c) 2010-2012 Liraz Siri <[email protected]>
#
# This file is part of TKLBAM (TurnKey GNU/Linux BAckup and Migration).
#
# TKLBAM is open source software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 3 of
# the License, or (at your option) any later version.
#
import glob
from os.path import *
class PathMap(dict):
@staticmethod
def _expand(path):
def needsglob(path):
for c in ('*?[]'):
if c in path:
return True
return False
path = abspath(path)
if needsglob(path):
return glob.glob(path)
else:
return [ path ]
def __init__(self, paths):
self.default = True
for path in paths:
if path[0] == '-':
path = path[1:]
sign = False
else:
self.default = False
sign = True
for expanded in self._expand(path):
self[expanded] = sign
def includes(self):
return [ path for path in self if self[path] ]
includes = property(includes)
def excludes(self):
return [ path for path in self if not self[path] ]
excludes = property(excludes)
def __contains__(self, path):
while path not in ('', '/'):
if dict.__contains__(self, path):
return self[path]
path = dirname(path)
return self.default