Commits

Andriy Kornatskyy  committed 860b832

Introduced module gzip with one-shot functions for compression and decompression.

  • Participants
  • Parent commits 474802c

Comments (0)

Files changed (5)

File doc/modules.rst

 .. automodule:: wheezy.core.feistel
    :members:
 
+wheezy.core.gzip
+----------------
+
+.. automodule:: wheezy.core.gzip
+   :members:
+
 wheezy.core.httpclient
 ----------------------
 

File doc/userguide.rst

     >>> total_seconds(timedelta(hours=2))
     7200
 
+gzip
+----
+
+One-shot compression and decompression is provided through the
+:py:meth:`~wheezy.core.gzip.compress` and
+:py:meth:`~wheezy.core.gzip.decompress` functions.
+
 httpclient
 ----------
 

File src/wheezy/core/comp.py

 
 
 if PY3:  # pragma: nocover
+    from io import BytesIO
     from queue import Queue
     from queue import LifoQueue
     xrange = range
     u = lambda s: s
 
 else:  # pragma: nocover
+    from cStringIO import StringIO as BytesIO  # noqa
     from Queue import Queue  # noqa
     try:
         from Queue import LifoQueue  # noqa
     from urllib.parse import urlsplit
     from urllib.parse import urlunsplit
 else:  # pragma: nocover
-    from httplib import HTTPConnection
+    from httplib import HTTPConnection  # noqa
     from urllib import urlencode  # noqa
     from urlparse import urljoin  # noqa
     from urlparse import urlparse  # noqa

File src/wheezy/core/gzip.py

+""" One-shot compression and decompression.
+"""
+
+from wheezy.core.comp import BytesIO
+from wheezy.core.introspection import import_name
+
+
+GzipFile = import_name('gzip.GzipFile')
+
+
+def compress(data, compresslevel=9):
+    """ Compress data in one shot.
+    """
+    s = BytesIO()
+    f = GzipFile(fileobj=s, mode='wb', mtime=0)
+    f.write(data)
+    f.close()
+    return s.getvalue()
+
+
+def decompress(data):
+    """ Decompress data in one shot.
+    """
+    return GzipFile(fileobj=BytesIO(data), mode='rb').read()

File src/wheezy/core/tests/test_gzip.py

+
+"""
+"""
+
+import unittest
+
+
+class GzipTestCase(unittest.TestCase):
+
+    def test_compress_decompress(self):
+        """ Ensure decompress is a reverse function of compress.
+        """
+        from wheezy.core.gzip import compress
+        from wheezy.core.gzip import decompress
+        assert 'test' == decompress(compress('test'))