__init__.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. ######################## BEGIN LICENSE BLOCK ########################
  2. # This library is free software; you can redistribute it and/or
  3. # modify it under the terms of the GNU Lesser General Public
  4. # License as published by the Free Software Foundation; either
  5. # version 2.1 of the License, or (at your option) any later version.
  6. #
  7. # This library is distributed in the hope that it will be useful,
  8. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. # Lesser General Public License for more details.
  11. #
  12. # You should have received a copy of the GNU Lesser General Public
  13. # License along with this library; if not, write to the Free Software
  14. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  15. # 02110-1301 USA
  16. ######################### END LICENSE BLOCK #########################
  17. from .universaldetector import UniversalDetector
  18. from .enums import InputState
  19. from .version import __version__, VERSION
  20. __all__ = ['UniversalDetector', 'detect', 'detect_all', '__version__', 'VERSION']
  21. def detect(byte_str):
  22. """
  23. Detect the encoding of the given byte string.
  24. :param byte_str: The byte sequence to examine.
  25. :type byte_str: ``bytes`` or ``bytearray``
  26. """
  27. if not isinstance(byte_str, bytearray):
  28. if not isinstance(byte_str, bytes):
  29. raise TypeError('Expected object of type bytes or bytearray, got: '
  30. '{}'.format(type(byte_str)))
  31. else:
  32. byte_str = bytearray(byte_str)
  33. detector = UniversalDetector()
  34. detector.feed(byte_str)
  35. return detector.close()
  36. def detect_all(byte_str):
  37. """
  38. Detect all the possible encodings of the given byte string.
  39. :param byte_str: The byte sequence to examine.
  40. :type byte_str: ``bytes`` or ``bytearray``
  41. """
  42. if not isinstance(byte_str, bytearray):
  43. if not isinstance(byte_str, bytes):
  44. raise TypeError('Expected object of type bytes or bytearray, got: '
  45. '{}'.format(type(byte_str)))
  46. else:
  47. byte_str = bytearray(byte_str)
  48. detector = UniversalDetector()
  49. detector.feed(byte_str)
  50. detector.close()
  51. if detector._input_state == InputState.HIGH_BYTE:
  52. results = []
  53. for prober in detector._charset_probers:
  54. if prober.get_confidence() > detector.MINIMUM_THRESHOLD:
  55. charset_name = prober.charset_name
  56. lower_charset_name = prober.charset_name.lower()
  57. # Use Windows encoding name instead of ISO-8859 if we saw any
  58. # extra Windows-specific bytes
  59. if lower_charset_name.startswith('iso-8859'):
  60. if detector._has_win_bytes:
  61. charset_name = detector.ISO_WIN_MAP.get(lower_charset_name,
  62. charset_name)
  63. results.append({
  64. 'encoding': charset_name,
  65. 'confidence': prober.get_confidence(),
  66. 'language': prober.language,
  67. })
  68. if len(results) > 0:
  69. return sorted(results, key=lambda result: -result['confidence'])
  70. return [detector.result]