detect_utils.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #
  2. # Copyright 2019 The FATE Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import typing
  17. def check_config(config: typing.Dict, required_arguments: typing.List):
  18. if not config or not isinstance(config, dict):
  19. raise TypeError('no parameters')
  20. no_arguments = []
  21. error_arguments = []
  22. for require_argument in required_arguments:
  23. if isinstance(require_argument, tuple):
  24. config_value = config.get(require_argument[0], None)
  25. if isinstance(require_argument[1], (tuple, list)):
  26. if config_value not in require_argument[1]:
  27. error_arguments.append(require_argument)
  28. elif config_value != require_argument[1]:
  29. error_arguments.append(require_argument)
  30. elif require_argument not in config:
  31. no_arguments.append(require_argument)
  32. if no_arguments or error_arguments:
  33. error_string = ""
  34. if no_arguments:
  35. error_string += "required parameters are missing: {}; ".format(",".join(no_arguments))
  36. if error_arguments:
  37. error_string += "required parameter values: {}".format(",".join(["{}={}".format(a[0], a[1]) for a in error_arguments]))
  38. raise KeyError(error_string)