From 4fce26bc35e92e72ac6d8a1ee159b1a58d15c314 Mon Sep 17 00:00:00 2001 From: David Stainton Date: Mon, 1 Jun 2015 17:11:57 -0700 Subject: [PATCH] Add Magic Folder path encoding. refs ticket:2442 --- src/allmydata/magicpath.py | 9 +++++++++ src/allmydata/test/test_magicpath.py | 20 ++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 src/allmydata/magicpath.py create mode 100644 src/allmydata/test/test_magicpath.py 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) -- 2.45.2