EnvConfig.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http:#www.apache.org/licenses/LICENSE-2.0
  8. #
  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. The test env could change when we running test from different computers.
  16. Test env config provide ``get_variable`` method to allow user get test environment related variables.
  17. It will first try to get variable from config file.
  18. If failed, then it will try to auto detect (Not supported yet).
  19. Config file format is yaml. it's a set of key-value pair. The following is an example of config file::
  20. Example_WIFI:
  21. ap_ssid: "myssid"
  22. ap_password: "mypassword"
  23. Example_ShieldBox:
  24. attenuator_port: "/dev/ttyUSB2"
  25. ap_ssid: "myssid"
  26. ap_password: "mypassword"
  27. It will first define the env tag for each environment, then add its key-value pairs.
  28. This will prevent test cases from getting configs from other env when there're configs for multiple env in one file.
  29. """
  30. import logging
  31. import yaml
  32. try:
  33. from yaml import CLoader as Loader
  34. except ImportError:
  35. from yaml import Loader as Loader
  36. class Config(object):
  37. """ Test Env Config """
  38. def __init__(self, config_file, env_tag):
  39. self.configs = self.load_config_file(config_file, env_tag)
  40. @staticmethod
  41. def load_config_file(config_file, env_name):
  42. """
  43. load configs from config file.
  44. :param config_file: config file path
  45. :param env_name: env tag name
  46. :return: configs for the test env
  47. """
  48. try:
  49. with open(config_file) as f:
  50. configs = yaml.load(f, Loader=Loader)[env_name]
  51. except (OSError, TypeError, IOError):
  52. configs = dict()
  53. except KeyError:
  54. logging.error('No config env "{}" in config file "{}"'.format(env_name, config_file))
  55. raise
  56. return configs
  57. def get_variable(self, variable_name):
  58. """
  59. first try to get from config file. if not found, try to auto detect the variable.
  60. :param variable_name: name of variable
  61. :return: value or None
  62. """
  63. try:
  64. value = self.configs[variable_name]
  65. except KeyError:
  66. # TODO: to support auto get variable here
  67. value = None
  68. if value is None:
  69. raise ValueError('Failed to get variable')
  70. return value