esp-idf/tools/ci/python_packages/tiny_test_fw/EnvConfig.py

84 lines
2.8 KiB
Python
Raw Normal View History

2017-10-09 22:44:55 -04:00
# Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http:#www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
The test env could change when we running test from different computers.
Test env config provide ``get_variable`` method to allow user get test environment related variables.
It will first try to get variable from config file.
If failed, then it will try to auto detect (Not supported yet).
Config file format is yaml. it's a set of key-value pair. The following is an example of config file::
Example_WIFI:
ap_ssid: "myssid"
ap_password: "mypassword"
Example_ShieldBox:
attenuator_port: "/dev/ttyUSB2"
ap_ssid: "myssid"
ap_password: "mypassword"
It will first define the env tag for each environment, then add its key-value pairs.
This will prevent test cases from getting configs from other env when there're configs for multiple env in one file.
"""
2020-07-13 00:35:50 -04:00
import logging
2017-10-09 22:44:55 -04:00
import yaml
try:
from yaml import CLoader as Loader
except ImportError:
from yaml import Loader as Loader
2017-10-09 22:44:55 -04:00
class Config(object):
""" Test Env Config """
def __init__(self, config_file, env_tag):
self.configs = self.load_config_file(config_file, env_tag)
@staticmethod
def load_config_file(config_file, env_name):
"""
load configs from config file.
:param config_file: config file path
:param env_name: env tag name
:return: configs for the test env
"""
try:
with open(config_file) as f:
configs = yaml.load(f, Loader=Loader)[env_name]
except (OSError, TypeError, IOError):
2017-10-09 22:44:55 -04:00
configs = dict()
2020-07-13 00:35:50 -04:00
except KeyError:
logging.error('No config env "{}" in config file "{}"'.format(env_name, config_file))
raise
2017-10-09 22:44:55 -04:00
return configs
def get_variable(self, variable_name):
"""
first try to get from config file. if not found, try to auto detect the variable.
:param variable_name: name of variable
:return: value or None
"""
try:
value = self.configs[variable_name]
except KeyError:
2018-12-04 07:46:48 -05:00
# TODO: to support auto get variable here
2017-10-09 22:44:55 -04:00
value = None
if value is None:
raise ValueError('Failed to get variable')
2017-10-09 22:44:55 -04:00
return value