1# Copyright 2024 The Pigweed Authors 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); you may not 4# use this file except in compliance with the License. You may obtain a copy of 5# the License at 6# 7# https://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, WITHOUT 11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12# License for the specific language governing permissions and limitations under 13# the License. 14"""JSON config file loader mixin.""" 15 16from typing import Any 17 18import json 19 20from pw_config_loader import yaml_config_loader_mixin 21 22Stage = yaml_config_loader_mixin.Stage 23 24 25class JsonConfigLoaderMixin(yaml_config_loader_mixin.YamlConfigLoaderMixin): 26 """JSON Config file loader mixin. 27 28 Use this mixin to load json file settings and save them into 29 ``self._config``. For example: 30 31 :: 32 33 from pw_cli.json_config_loader_mixin import JsonConfigLoaderMixin 34 35 class PwBloatPrefs(JsonConfigLoaderMixin): 36 def __init__(self) -> None: 37 self.config_init( 38 config_section_title='pw_bloat', 39 project_file=Path('$PW_PROJECT_ROOT/.pw_bloat.json'), 40 project_user_file=Path( 41 '$PW_PROJECT_ROOT/.pw_bloat.user.json'), 42 user_file=Path('~/.pw_bloat.json'), 43 default_config={}, 44 environment_var='PW_BLOAT_CONFIG_FILE', 45 ) 46 47 """ 48 49 def _load_config_from_string( # pylint: disable=no-self-use 50 self, file_contents: str 51 ) -> list[dict[Any, Any]]: 52 return [json.loads(file_contents)] 53