changeset 1903:f83b66646d75

implement StringIO as contextmanager, use it for stores
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Mon, 07 Jan 2013 00:58:56 +0100
parents 70c5a243d19b
children 9b9332497ff8
files MoinMoin/storage/stores/__init__.py MoinMoin/storage/stores/kc.py MoinMoin/storage/stores/kt.py MoinMoin/storage/stores/memory.py MoinMoin/storage/stores/sqla.py MoinMoin/storage/stores/sqlite.py MoinMoin/util/StringIOClosing.py
diffstat 7 files changed, 21 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/storage/stores/__init__.py	Mon Jan 07 00:51:58 2013 +0100
+++ b/MoinMoin/storage/stores/__init__.py	Mon Jan 07 00:58:56 2013 +0100
@@ -16,6 +16,7 @@
 from abc import abstractmethod
 from collections import Mapping, MutableMapping
 
+from MoinMoin.util.StringIOClosing import StringIO
 
 class StoreBase(Mapping):
     """
--- a/MoinMoin/storage/stores/kc.py	Mon Jan 07 00:51:58 2013 +0100
+++ b/MoinMoin/storage/stores/kc.py	Mon Jan 07 00:58:56 2013 +0100
@@ -18,11 +18,10 @@
 from __future__ import absolute_import, division
 
 import os, errno
-from StringIO import StringIO
 
 from kyotocabinet import *
 
-from . import MutableStoreBase, BytesMutableStoreBase, FileMutableStoreBase
+from . import MutableStoreBase, BytesMutableStoreBase, FileMutableStoreBase, StringIO
 
 
 class _Store(MutableStoreBase):
--- a/MoinMoin/storage/stores/kt.py	Mon Jan 07 00:51:58 2013 +0100
+++ b/MoinMoin/storage/stores/kt.py	Mon Jan 07 00:58:56 2013 +0100
@@ -15,9 +15,7 @@
 import urllib
 from httplib import HTTPConnection
 
-from StringIO import StringIO
-
-from . import MutableStoreBase, BytesMutableStoreBase, FileMutableStoreBase
+from . import MutableStoreBase, BytesMutableStoreBase, FileMutableStoreBase, StringIO
 
 
 class _Store(MutableStoreBase):
--- a/MoinMoin/storage/stores/memory.py	Mon Jan 07 00:51:58 2013 +0100
+++ b/MoinMoin/storage/stores/memory.py	Mon Jan 07 00:58:56 2013 +0100
@@ -12,9 +12,7 @@
 
 from __future__ import absolute_import, division
 
-from StringIO import StringIO
-
-from . import MutableStoreBase, BytesMutableStoreBase, FileMutableStoreBase
+from . import MutableStoreBase, BytesMutableStoreBase, FileMutableStoreBase, StringIO
 
 
 class _Store(MutableStoreBase):
--- a/MoinMoin/storage/stores/sqla.py	Mon Jan 07 00:51:58 2013 +0100
+++ b/MoinMoin/storage/stores/sqla.py	Mon Jan 07 00:58:56 2013 +0100
@@ -10,12 +10,10 @@
 
 from __future__ import absolute_import, division
 
-from StringIO import StringIO
-
 from sqlalchemy import create_engine, select, MetaData, Table, Column, String, Binary
 from sqlalchemy.pool import StaticPool
 
-from . import MutableStoreBase, BytesMutableStoreBase, FileMutableStoreBase
+from . import MutableStoreBase, BytesMutableStoreBase, FileMutableStoreBase, StringIO
 
 KEY_LEN = 128
 VALUE_LEN = 1024 * 1024 # 1MB binary data
--- a/MoinMoin/storage/stores/sqlite.py	Mon Jan 07 00:51:58 2013 +0100
+++ b/MoinMoin/storage/stores/sqlite.py	Mon Jan 07 00:58:56 2013 +0100
@@ -14,11 +14,10 @@
 
 from __future__ import absolute_import, division
 
-from StringIO import StringIO
 import zlib
 from sqlite3 import *
 
-from . import MutableStoreBase, BytesMutableStoreBase, FileMutableStoreBase
+from . import MutableStoreBase, BytesMutableStoreBase, FileMutableStoreBase, StringIO
 
 
 class _Store(MutableStoreBase):
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MoinMoin/util/StringIOClosing.py	Mon Jan 07 00:58:56 2013 +0100
@@ -0,0 +1,15 @@
+from StringIO import StringIO as StringIOBase
+
+
+class StringIO(StringIOBase):
+    """
+    same as StringIO from stdlib, but enhanced with a context manager, so it
+    can be used within a "with" statement and gets automatically closed when
+    the with-block is left. The standard "file" object behaves that way, so
+    a StringIO "file emulation" should behave the same.
+    """
+    def __enter__(self):
+        return self
+
+    def __exit__(self, exc_type, exc_value, exc_tb):
+        self.close()