]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/magicpath.py
Magic Folder.
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / magicpath.py
1
2 import re
3 import os.path
4
5 from allmydata.util.assertutil import precondition, _assert
6
7 def path2magic(path):
8     return re.sub(ur'[/@]',  lambda m: {u'/': u'@_', u'@': u'@@'}[m.group(0)], path)
9
10 def magic2path(path):
11     return re.sub(ur'@[_@]', lambda m: {u'@_': u'/', u'@@': u'@'}[m.group(0)], path)
12
13
14 IGNORE_SUFFIXES = [u'.backup', u'.tmp', u'.conflicted']
15 IGNORE_PREFIXES = [u'.']
16
17 def should_ignore_file(path_u):
18     precondition(isinstance(path_u, unicode), path_u=path_u)
19
20     for suffix in IGNORE_SUFFIXES:
21         if path_u.endswith(suffix):
22             return True
23
24     while path_u != u"":
25         oldpath_u = path_u
26         path_u, tail_u = os.path.split(path_u)
27         if tail_u.startswith(u"."):
28             return True
29         if path_u == oldpath_u:
30             return True  # the path was absolute
31         _assert(len(path_u) < len(oldpath_u), path_u=path_u, oldpath_u=oldpath_u)
32
33     return False