From: David Stainton Date: Tue, 2 Jun 2015 00:11:57 +0000 (-0700) Subject: Add Magic Folder path encoding. refs ticket:2442 X-Git-Url: https://git.rkrishnan.org/%5B/frontends/flags/index.php?a=commitdiff_plain;h=4fce26bc35e92e72ac6d8a1ee159b1a58d15c314;p=tahoe-lafs%2Ftahoe-lafs.git Add Magic Folder path encoding. refs ticket:2442 --- diff --git a/src/allmydata/magicpath.py b/src/allmydata/magicpath.py new file mode 100644 index 00000000..e99e3417 --- /dev/null +++ b/src/allmydata/magicpath.py @@ -0,0 +1,9 @@ + +import re + + +def path2magic(path): + return re.sub(ur'[/@]', lambda m: {u'/': u'@_', u'@': u'@@'}[m.group(0)], path) + +def magic2path(path): + return re.sub(ur'@[_@]', lambda m: {u'@_': u'/', u'@@': u'@'}[m.group(0)], path) diff --git a/src/allmydata/test/test_magicpath.py b/src/allmydata/test/test_magicpath.py new file mode 100644 index 00000000..8ba5e40f --- /dev/null +++ b/src/allmydata/test/test_magicpath.py @@ -0,0 +1,20 @@ + +from twisted.trial import unittest + +from allmydata import magicpath + + +class MagicPath(unittest.TestCase): + tests = { + u"Documents/work/critical-project/qed.txt": u"Documents@_work@_critical-project@_qed.txt", + u"Documents/emails/bunnyfufu@hoppingforest.net": u"Documents@_emails@_bunnyfufu@@hoppingforest.net", + u"foo/@/bar": u"foo@_@@@_bar", + } + + def test_path2magic(self): + for test, expected in self.tests.items(): + self.failUnlessEqual(magicpath.path2magic(test), expected) + + def test_magic2path(self): + for expected, test in self.tests.items(): + self.failUnlessEqual(magicpath.magic2path(test), expected)