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 18"""This module provides structures, to save some info while parsing headers.""" 19 20import os 21from typing import Any, Dict 22from file_tools import print_to_yaml 23 24statistics: Dict[str, Dict[str, Any]] = {} 25custom_yamls: Dict[str, Dict[str, Any]] = {} 26LIB_GEN_FOLDER = "" 27 28 29def init_collections(lib_gen_folder: str) -> None: # pylint: disable=C 30 global statistics, custom_yamls, LIB_GEN_FOLDER # pylint: disable=W 31 LIB_GEN_FOLDER = lib_gen_folder 32 33 statistics = { 34 "unreachable": { 35 "log_file": os.path.join(LIB_GEN_FOLDER, "./gen/logs/unreachable.txt"), 36 "collection": set(), 37 }, 38 "skip": { 39 "log_file": os.path.join(LIB_GEN_FOLDER, "./gen/logs/skip.txt"), 40 "collection": set() 41 }, 42 "generated_yamls": { 43 "log_file": os.path.join(LIB_GEN_FOLDER, "./gen/logs/generated_yamls.txt"), 44 "collection": set(), 45 }, 46 } 47 48 custom_yamls = { 49 "allEnums": { 50 "yaml_file": os.path.join(LIB_GEN_FOLDER, "./gen/headers/allEnums.yaml"), 51 "collection": {"enums": []}, 52 }, 53 "pathsToHeaders": { 54 "yaml_file": os.path.join(LIB_GEN_FOLDER, "./gen/headers/pathsToHeaders.yaml"), 55 "collection": {"paths": []}, 56 }, 57 } 58 59 60def add_to_statistics(key: str, data: Any) -> None: 61 if isinstance(statistics[key]["collection"], set): # CC-OFF(G.TYP.07) dict key exist 62 statistics[key]["collection"].add(data) # CC-OFF(G.TYP.07) dict key exist 63 elif isinstance(statistics[key]["collection"], list): # CC-OFF(G.TYP.07) dict key exist 64 statistics[key]["collection"].append(data) # CC-OFF(G.TYP.07) dict key exist 65 else: 66 raise RuntimeError("Unreachable") 67 68 69def add_to_custom_yamls(yaml_name: str, key: str, data: Any) -> None: 70 custom_yamls[yaml_name]["collection"][key].append(data) # CC-OFF(G.TYP.07) dict key exist 71 72 73def save_custom_yamls() -> None: 74 headers_path = os.path.join(LIB_GEN_FOLDER, "./gen/headers") 75 if not os.path.exists(headers_path): 76 os.makedirs(headers_path) 77 78 for _, value in custom_yamls.items(): 79 yaml_file = value.get("yaml_file") 80 print_to_yaml(yaml_file, value.get("collection")) 81 82 statistics["generated_yamls"]["collection"].add( # CC-OFF(G.TYP.07) dict key exist 83 os.path.basename(yaml_file) 84 ) 85 86 87def save_statistics() -> None: 88 logs_path = os.path.join(LIB_GEN_FOLDER, "./gen/logs") 89 if not os.path.exists(logs_path): 90 os.makedirs(logs_path) 91 92 for _, value in statistics.items(): 93 with os.fdopen(os.open(value.get("log_file"), os.O_WRONLY | os.O_CREAT, mode=511), "w", encoding="utf-8") as f: 94 for item in value.get("collection"): 95 f.write(f"{item}\n") 96