Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
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.

legacy.py 2.0KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. from typing import Any, Dict, Optional, Union
  2. from warnings import warn
  3. from .api import from_bytes
  4. from .constant import CHARDET_CORRESPONDENCE
  5. def detect(
  6. byte_str: bytes, should_rename_legacy: bool = False, **kwargs: Any
  7. ) -> Dict[str, Optional[Union[str, float]]]:
  8. """
  9. chardet legacy method
  10. Detect the encoding of the given byte string. It should be mostly backward-compatible.
  11. Encoding name will match Chardet own writing whenever possible. (Not on encoding name unsupported by it)
  12. This function is deprecated and should be used to migrate your project easily, consult the documentation for
  13. further information. Not planned for removal.
  14. :param byte_str: The byte sequence to examine.
  15. :param should_rename_legacy: Should we rename legacy encodings
  16. to their more modern equivalents?
  17. """
  18. if len(kwargs):
  19. warn(
  20. f"charset-normalizer disregard arguments '{','.join(list(kwargs.keys()))}' in legacy function detect()"
  21. )
  22. if not isinstance(byte_str, (bytearray, bytes)):
  23. raise TypeError( # pragma: nocover
  24. "Expected object of type bytes or bytearray, got: "
  25. "{0}".format(type(byte_str))
  26. )
  27. if isinstance(byte_str, bytearray):
  28. byte_str = bytes(byte_str)
  29. r = from_bytes(byte_str).best()
  30. encoding = r.encoding if r is not None else None
  31. language = r.language if r is not None and r.language != "Unknown" else ""
  32. confidence = 1.0 - r.chaos if r is not None else None
  33. # Note: CharsetNormalizer does not return 'UTF-8-SIG' as the sig get stripped in the detection/normalization process
  34. # but chardet does return 'utf-8-sig' and it is a valid codec name.
  35. if r is not None and encoding == "utf_8" and r.bom:
  36. encoding += "_sig"
  37. if should_rename_legacy is False and encoding in CHARDET_CORRESPONDENCE:
  38. encoding = CHARDET_CORRESPONDENCE[encoding]
  39. return {
  40. "encoding": encoding,
  41. "language": language,
  42. "confidence": confidence,
  43. }