others.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # !/usr/bin/env python3
  2. # -*- coding: UTF-8 -*-
  3. ################################################################################
  4. #
  5. # Copyright (c) 2024 Baidu.com, Inc. All Rights Reserved
  6. #
  7. ################################################################################
  8. """
  9. Author: PaddlePaddle Authors
  10. """
  11. import json
  12. import signal
  13. __all__ = [
  14. 'UnsupportedAPIError', 'UnsupportedParamError', 'CalledProcessError',
  15. 'raise_unsupported_api_error', 'raise_key_not_found_error',
  16. 'raise_class_not_found_error', 'raise_unsupported_device_error',
  17. 'DuplicateRegistrationError'
  18. ]
  19. class UnsupportedAPIError(Exception):
  20. """ UnsupportedAPIError """
  21. pass
  22. class UnsupportedParamError(Exception):
  23. """ UnsupportedParamError """
  24. pass
  25. class KeyNotFoundError(Exception):
  26. """ KeyNotFoundError """
  27. pass
  28. class ClassNotFoundException(Exception):
  29. """ ClassNotFoundException """
  30. pass
  31. class UnsupportedDeviceError(Exception):
  32. """ UnsupportedDeviceError """
  33. pass
  34. class CalledProcessError(Exception):
  35. """ CalledProcessError """
  36. def __init__(self, returncode, cmd, output=None, stderr=None):
  37. super().__init__()
  38. self.returncode = returncode
  39. self.cmd = cmd
  40. self.output = output
  41. self.stderr = stderr
  42. def __str__(self):
  43. if self.returncode and self.returncode < 0:
  44. try:
  45. return f"Command {repr(self.cmd)} died with {repr(signal.Signals(-self.returncode))}."
  46. except ValueError:
  47. return f"Command {repr(self.cmd)} died with unknown signal {-self.returncode}."
  48. else:
  49. return f"Command {repr(self.cmd)} returned non-zero exit status {self.returncode}."
  50. class DuplicateRegistrationError(Exception):
  51. """ DuplicateRegistrationError """
  52. pass
  53. def raise_unsupported_api_error(api_name, cls=None):
  54. """ raise unsupported api error """
  55. # TODO: Automatically extract `api_name` and `cls` from stack frame
  56. if cls is not None:
  57. name = f"{cls.__name__}.{api_name}"
  58. else:
  59. name = api_name
  60. raise UnsupportedAPIError(f"The API `{name}` is not supported.")
  61. def raise_key_not_found_error(key, config=None):
  62. """ raise key not found error """
  63. msg = f"`{key}` not found in config."
  64. if config:
  65. config_str = json.dumps(config, indent=4, ensure_ascii=False)
  66. msg += f"\nThe content of config:\n{config_str}"
  67. raise KeyNotFoundError(msg)
  68. def raise_class_not_found_error(cls_name, base_cls, all_entities=None):
  69. """ raise class not found error """
  70. base_cls_name = base_cls.__name__
  71. msg = f"`{cls_name}` not registry on {base_cls_name}."
  72. if all_entities is not None:
  73. all_entities_str = ",".join(all_entities)
  74. msg += f"\nThe registied entities:`[{all_entities_str}]`"
  75. raise ClassNotFoundException(msg)
  76. def raise_unsupported_device_error(device, supported_device=None):
  77. """ raise_unsupported_device_error """
  78. msg = f"The device `{device}` is not supported! "
  79. if supported_device is not None:
  80. supported_device_str = ", ".join(supported_device)
  81. msg += f"The supported device types are: [{supported_device_str}]."
  82. raise UnsupportedDeviceError(msg)