Add a fsimport context manage to localy enable fsimporting. stable
authorPierre-Yves David <pierre-yves.david@logilab.fr>
Fri, 01 Oct 2010 17:04:09 +0200
branchstable
changeset 6383 19ebe0b994d6
parent 6382 4efd0f07fd53
child 6385 9f91d09ee5fa
Add a fsimport context manage to localy enable fsimporting.
server/sources/storages.py
server/test/unittest_storage.py
--- a/server/sources/storages.py	Fri Oct 01 17:03:50 2010 +0200
+++ b/server/sources/storages.py	Fri Oct 01 17:04:09 2010 +0200
@@ -18,6 +18,7 @@
 """custom storages for the system source"""
 
 from os import unlink, path as osp
+from contextlib import contextmanager
 
 from yams.schema import role_name
 
@@ -93,6 +94,17 @@
             return path
     return None
 
+@contextmanager
+def fsimport(session):
+    present = 'fs_importing' in session.transaction_data
+    old_value = session.transaction_data.get('fs_importing')
+    session.transaction_data['fs_importing'] = True
+    yield
+    if present:
+        session.transaction_data['fs_importing'] = old_value
+    else:
+        del session.transaction_data['fs_importing']
+
 
 class BytesFileSystemStorage(Storage):
     """store Bytes attribute value on the file system"""
--- a/server/test/unittest_storage.py	Fri Oct 01 17:03:50 2010 +0200
+++ b/server/test/unittest_storage.py	Fri Oct 01 17:04:09 2010 +0200
@@ -263,6 +263,35 @@
         self.assertEqual(self.fspath(f1), new_fspath)
         self.failIf(osp.isfile(old_fspath))
 
+    @tag('fsimport')
+    def test_clean(self):
+        fsimport = storages.fsimport
+        td = self.session.transaction_data
+        self.assertNotIn('fs_importing', td)
+        with fsimport(self.session):
+            self.assertIn('fs_importing', td)
+            self.assertTrue(td['fs_importing'])
+        self.assertNotIn('fs_importing', td)
+
+    @tag('fsimport')
+    def test_true(self):
+        fsimport = storages.fsimport
+        td = self.session.transaction_data
+        td['fs_importing'] = True
+        with fsimport(self.session):
+            self.assertIn('fs_importing', td)
+            self.assertTrue(td['fs_importing'])
+        self.assertTrue(td['fs_importing'])
+
+    @tag('fsimport')
+    def test_False(self):
+        fsimport = storages.fsimport
+        td = self.session.transaction_data
+        td['fs_importing'] = False
+        with fsimport(self.session):
+            self.assertIn('fs_importing', td)
+            self.assertTrue(td['fs_importing'])
+        self.assertFalse(td['fs_importing'])
 
 if __name__ == '__main__':
     unittest_main()