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.

39 lines
1.5 KiB

4 years ago
  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 .compat import PY2, PY3
  18. from .universaldetector import UniversalDetector
  19. from .version import __version__, VERSION
  20. def detect(byte_str):
  21. """
  22. Detect the encoding of the given byte string.
  23. :param byte_str: The byte sequence to examine.
  24. :type byte_str: ``bytes`` or ``bytearray``
  25. """
  26. if not isinstance(byte_str, bytearray):
  27. if not isinstance(byte_str, bytes):
  28. raise TypeError('Expected object of type bytes or bytearray, got: '
  29. '{0}'.format(type(byte_str)))
  30. else:
  31. byte_str = bytearray(byte_str)
  32. detector = UniversalDetector()
  33. detector.feed(byte_str)
  34. return detector.close()