config_exceptions.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import pathlib
  2. class config_exception(Exception):
  3. """
  4. This exception is base class for other exceptions in configuration.
  5. """
  6. pass
  7. class key_not_implemented(config_exception):
  8. """
  9. This exception is raised when key is not in defaults config, but found
  10. in the config file.
  11. """
  12. def __init__(self, key: str) -> None:
  13. """
  14. Parameters
  15. ----------
  16. key : str
  17. Name of the key, which is not implemented.
  18. """
  19. super().__init__("Key \"" + key + "\" is not implemented.")
  20. class bad_type_loaded(config_exception):
  21. """
  22. This exception is raised when key loaded from config file is in invalid
  23. type. Valid type is same as type as type of value that key from defaults
  24. config.
  25. """
  26. def __init__(self, key: str, required: type, own: type) -> None:
  27. """
  28. Parameters
  29. ----------
  30. key : str
  31. Name of the key.
  32. required : type
  33. Required type for that key.
  34. own : type
  35. Type of that key from config file.
  36. """
  37. required = required.__name__
  38. own = own.__name__
  39. error = "Value for key \"" + key + "\" must be an " + required
  40. error = error + " but is " + own + "."
  41. super().__init__(error)
  42. class invalid_config_processor(config_exception):
  43. """
  44. That exception is raised when config preprocessor class, given in
  45. config_loader builder is not valid config_loader child class.
  46. """
  47. def __init__(self, which: any) -> None:
  48. """
  49. Parameters
  50. ----------
  51. which : any
  52. Class given as config preprocessor.
  53. """
  54. super().__init__("Invalid config processor: " + str(which) + ".")
  55. class bad_key_type(config_exception):
  56. """
  57. That exception is raised when name of the key is not an string.
  58. """
  59. def __init__(self, key: any) -> None:
  60. """
  61. Parameters
  62. ----------
  63. key : any
  64. Key, which is not an string.
  65. """
  66. super().__init__("Key must be string, but is \"" + repr(key) + "\".")
  67. class bad_value_type(config_exception):
  68. """
  69. This exception is raised when key in defaults config is not in valid.
  70. Valid type is string, number (int or float) and boolean. Value can not
  71. being object, list or other complex types.
  72. """
  73. def __init__(self, key: str, value: any) -> None:
  74. """
  75. Parameters
  76. ----------
  77. key : str
  78. Name of the invalid key.
  79. value : any
  80. Value in invalid type.
  81. """
  82. error = "Key \"" + key + "\" has invalid type of value. Int, "
  83. error = error + "string, float or bool is required, but \""
  84. error = error + repr(value) + "\" found."
  85. super().__init__(error)
  86. class config_file_not_exists(config_exception):
  87. """
  88. That exception is raised when config file, which config_loader tried
  89. to load, does not exists.
  90. """
  91. def __init__(self, file: pathlib.Path) -> None:
  92. """
  93. Parameters
  94. ----------
  95. file : pathlib.Path
  96. Config file which not exists.
  97. """
  98. super().__init__("Config file " + str(file) + "not exists.")
  99. class config_file_not_readable(config_exception):
  100. """
  101. That exception is raised when config file is not readable, because could
  102. not being opened or have syntax error.
  103. """
  104. def __init__(self, error: Exception) -> None:
  105. """
  106. Parameters
  107. ----------
  108. error : Exception
  109. Exception which was raised by json loader or file handler.
  110. """
  111. super().__init__("Config file is not readable: " + str(error) + ".")