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.

_utils.py 3.6KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. """
  2. _url.py
  3. websocket - WebSocket client library for Python
  4. Copyright 2023 engn33r
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. """
  15. __all__ = ["NoLock", "validate_utf8", "extract_err_message", "extract_error_code"]
  16. class NoLock:
  17. def __enter__(self) -> None:
  18. pass
  19. def __exit__(self, exc_type, exc_value, traceback) -> None:
  20. pass
  21. try:
  22. # If wsaccel is available we use compiled routines to validate UTF-8
  23. # strings.
  24. from wsaccel.utf8validator import Utf8Validator
  25. def _validate_utf8(utfbytes: bytes) -> bool:
  26. return Utf8Validator().validate(utfbytes)[0]
  27. except ImportError:
  28. # UTF-8 validator
  29. # python implementation of http://bjoern.hoehrmann.de/utf-8/decoder/dfa/
  30. _UTF8_ACCEPT = 0
  31. _UTF8_REJECT = 12
  32. _UTF8D = [
  33. # The first part of the table maps bytes to character classes that
  34. # to reduce the size of the transition table and create bitmasks.
  35. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  36. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  37. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  38. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  39. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
  40. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  41. 8,8,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
  42. 10,3,3,3,3,3,3,3,3,3,3,3,3,4,3,3, 11,6,6,6,5,8,8,8,8,8,8,8,8,8,8,8,
  43. # The second part is a transition table that maps a combination
  44. # of a state of the automaton and a character class to a state.
  45. 0,12,24,36,60,96,84,12,12,12,48,72, 12,12,12,12,12,12,12,12,12,12,12,12,
  46. 12, 0,12,12,12,12,12, 0,12, 0,12,12, 12,24,12,12,12,12,12,24,12,24,12,12,
  47. 12,12,12,12,12,12,12,24,12,12,12,12, 12,24,12,12,12,12,12,12,12,24,12,12,
  48. 12,12,12,12,12,12,12,36,12,36,12,12, 12,36,12,12,12,12,12,36,12,36,12,12,
  49. 12,36,12,12,12,12,12,12,12,12,12,12, ]
  50. def _decode(state: int, codep: int, ch: int) -> tuple:
  51. tp = _UTF8D[ch]
  52. codep = (ch & 0x3f) | (codep << 6) if (
  53. state != _UTF8_ACCEPT) else (0xff >> tp) & ch
  54. state = _UTF8D[256 + state + tp]
  55. return state, codep
  56. def _validate_utf8(utfbytes: str or bytes) -> bool:
  57. state = _UTF8_ACCEPT
  58. codep = 0
  59. for i in utfbytes:
  60. state, codep = _decode(state, codep, i)
  61. if state == _UTF8_REJECT:
  62. return False
  63. return True
  64. def validate_utf8(utfbytes: str or bytes) -> bool:
  65. """
  66. validate utf8 byte string.
  67. utfbytes: utf byte string to check.
  68. return value: if valid utf8 string, return true. Otherwise, return false.
  69. """
  70. return _validate_utf8(utfbytes)
  71. def extract_err_message(exception: Exception) -> str or None:
  72. if exception.args:
  73. return exception.args[0]
  74. else:
  75. return None
  76. def extract_error_code(exception: Exception) -> int or None:
  77. if exception.args and len(exception.args) > 1:
  78. return exception.args[0] if isinstance(exception.args[0], int) else None