• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# -- coding: utf-8 --
3#
4# Copyright (c) 2024-2025 Huawei Device Co., Ltd.
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18from typing import Optional
19
20from runner.common_exceptions import InvalidConfiguration
21from runner.logger import Log
22
23_LOGGER = Log.get_logger(__file__)
24
25
26class LocalEnv:
27    __instance: Optional['LocalEnv'] = None
28
29    def __init__(self) -> None:
30        self.__env: dict[str, str] = {}
31
32    @staticmethod
33    def get() -> 'LocalEnv':
34        if LocalEnv.__instance is None:
35            LocalEnv.__instance = LocalEnv()
36        return LocalEnv.__instance
37
38    def add(self, key: str, env_value: str) -> None:
39        if key not in self.__env:
40            self.__env[key] = env_value
41            return
42        if key in self.__env and self.__env[key] != env_value:
43            raise InvalidConfiguration(
44                "Attention: environment has been changed during test execution. "
45                f"For key `{key}` the env_value `{self.__env[key]}` changed to `{env_value}`"
46            )
47
48    def is_present(self, key: str) -> bool:
49        return key in self.__env
50
51    def get_value(self, key: str) -> str:
52        return str(self.__env[key])
53
54    def list(self) -> str:
55        return str(self.__env)
56