Add Magic Folder path encoding. refs ticket:2442
authorDavid Stainton <dstainton415@gmail.com>
Tue, 2 Jun 2015 00:11:57 +0000 (17:11 -0700)
committerDaira Hopwood <daira@jacaranda.org>
Mon, 20 Jul 2015 23:20:55 +0000 (00:20 +0100)
src/allmydata/magicpath.py [new file with mode: 0644]
src/allmydata/test/test_magicpath.py [new file with mode: 0644]

diff --git a/src/allmydata/magicpath.py b/src/allmydata/magicpath.py
new file mode 100644 (file)
index 0000000..e99e341
--- /dev/null
@@ -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 (file)
index 0000000..8ba5e40
--- /dev/null
@@ -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)