Python Library Stringtools
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

__init__.py 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. """
  5. stringtools (Stringtools)
  6. =========================
  7. **Author:**
  8. * Dirk Alders <sudo-dirk@mount-mockery.de>
  9. **Description:**
  10. This Module supports functionality around string operations.
  11. **Submodules:**
  12. * :mod:`stringtools.csp`
  13. * :mod:`stringtools.stp`
  14. * :func:`gzip_compress`
  15. * :func:`gzip_extract`
  16. * :func:`hexlify`
  17. **Unittest:**
  18. See also the :download:`unittest <stringtools/_testresults_/unittest.pdf>` documentation.
  19. **Module Documentation:**
  20. """
  21. from stringtools import stp
  22. from stringtools import csp
  23. __DEPENDENCIES__ = []
  24. import gzip
  25. import logging
  26. import time
  27. import sys
  28. if sys.version_info < (3, 0):
  29. from cStringIO import StringIO
  30. logger_name = 'STRINGTOOLS'
  31. logger = logging.getLogger(logger_name)
  32. __DESCRIPTION__ = """The Module {\\tt %s} is designed to support functionality for strings (e.g. transfer strings via a bytestream, compressing, extracting, ...).
  33. For more Information read the sphinx documentation.""" % __name__.replace('_', '\_')
  34. """The Module Description"""
  35. __INTERPRETER__ = (2, 3)
  36. """The Tested Interpreter-Versions"""
  37. __all__ = ['gzip_compress',
  38. 'gzip_extract',
  39. 'hexlify',
  40. 'csp',
  41. 'stp']
  42. def gzip_compress(s, compresslevel=9):
  43. """
  44. Method to compress a stream of bytes.
  45. :param str s: The bytestream (string) to be compressed
  46. :param int compresslevel: An optional compressionn level (default is 9)
  47. :return: The compressed bytestream
  48. :rtype: str
  49. **Example:**
  50. .. literalinclude:: ../examples/gzip_compress.py
  51. Will result to the following output:
  52. .. literalinclude:: ../examples/gzip_compress.log
  53. """
  54. rv = None
  55. t = time.time()
  56. if sys.version_info >= (3, 0):
  57. rv = gzip.compress(s, compresslevel)
  58. else:
  59. buf = StringIO()
  60. f = gzip.GzipFile(mode='wb', compresslevel=compresslevel, fileobj=buf)
  61. try:
  62. f.write(s)
  63. finally:
  64. f.close()
  65. rv = buf.getvalue()
  66. buf.close()
  67. if rv is not None:
  68. logger.debug('GZIP: Finished to compress a string (compression_rate=%.3f, consumed_time=%.1fs).', len(rv) / float(len(s)), time.time() - t)
  69. return rv
  70. def gzip_extract(s):
  71. """
  72. Method to extract data from a compress stream of bytes.
  73. :param str s: The compressed bytestream (string) to be extracted
  74. :return: The extracted data
  75. :rtype: str
  76. **Example:**
  77. .. literalinclude:: ../examples/gzip_extract.py
  78. Will result to the following output:
  79. .. literalinclude:: ../examples/gzip_extract.log
  80. """
  81. t = time.time()
  82. rv = None
  83. if sys.version_info >= (3, 0):
  84. rv = gzip.decompress(s)
  85. else:
  86. inbuffer = StringIO(s)
  87. f = gzip.GzipFile(mode='rb', fileobj=inbuffer)
  88. try:
  89. rv = f.read()
  90. finally:
  91. f.close()
  92. inbuffer.close()
  93. if rv is not None:
  94. logger.debug('GZIP: Finished to extract a string (compression_rate=%.3f, consumed_time=%.1fs).', len(s) / float(len(rv)), time.time() - t)
  95. return rv
  96. def hexlify(s):
  97. """Method to hexlify a string.
  98. :param str s: A string including the bytes to be hexlified.
  99. :returns: The hexlified string
  100. :rtype: str
  101. **Example:**
  102. .. literalinclude:: ../examples/hexlify.py
  103. Will result to the following output:
  104. .. literalinclude:: ../examples/hexlify.log
  105. """
  106. rv = '(%d):' % len(s)
  107. for byte in s:
  108. if sys.version_info >= (3, 0):
  109. rv += ' %02x' % byte
  110. else:
  111. rv += ' %02x' % ord(byte)
  112. return rv
  113. def linefeed_filter(s):
  114. """Method to change linefeed and carriage return to '\\\\n' and '\\\\r'
  115. :param str s: A string including carriage return and/ or linefeed.
  116. :returns: A string with converted carriage return and/ or linefeed.
  117. :rtype: str
  118. """
  119. if sys.version_info >= (3, 0):
  120. return s.replace(b'\r', b'\\r').replace(b'\n', b'\\n')
  121. else:
  122. return s.replace('\r', '\\r').replace('\n', '\\n')