No Description
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 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #
  2. # This file is part of Lodel 2 (https://github.com/OpenEdition)
  3. #
  4. # Copyright (C) 2015-2017 Cléo UMS-3287
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU Affero General Public License as published
  8. # by the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU Affero General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Affero General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. ## @package lodel.settings.utils Lodel 2 settings utility
  20. #
  21. # For now defines exception classes
  22. ##@brief Error class for settings errors
  23. class SettingsError(Exception):
  24. ##@brief Instanciate a new SettingsError
  25. # @param msg str : Error message
  26. # @param key_id str : The key concerned by the error
  27. # @param filename str
  28. def __init__(self, msg = "Unknown error", key_id = None, filename = None):
  29. self.__msg = msg
  30. self.__key_id = key_id
  31. self.__filename = filename
  32. def __repr__(self): return str(self)
  33. def __str__(self):
  34. res = "Error "
  35. if self.__filename is not None:
  36. res += "in file '%s' " % self.__filename
  37. if self.__key_id is not None:
  38. res += "for key '%s'" % self.__key_id
  39. res += ": %s" % (self.__msg)
  40. return res
  41. ##@brief Designed to handles multiple SettingsError
  42. class SettingsErrors(Exception):
  43. ##@brief Instanciate a SettingsErrors
  44. # @param exception list : list of SettingsError instance
  45. def __init__(self, exceptions):
  46. for expt in exceptions:
  47. if not isinstance(expt, SettingsError):
  48. raise ValueError("The 'exceptions' argument has to be an array of <class SettingsError>, but a %s was found in the list" % type(expt))
  49. self.__exceptions = exceptions
  50. def __repr__(self): return str(self)
  51. ## @brief Return a string representation of a list of SettingError
  52. # This representation is the concatenation of all SettingError string representations
  53. def __str__(self):
  54. res = "Errors :\n"
  55. for expt in self.__exceptions:
  56. res += "\t%s\n" % str(expt)
  57. return res