1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4# Copyright (c) 2023 Huawei Device Co., Ltd.
5#
6# HDF is dual licensed: you can use it either under the terms of
7# the GPL, or the BSD license, at your option.
8# See the LICENSE file in the root of this repository for complete details.
9
10
11import json
12import os.path
13
14import hdf_tool_settings
15import hdf_utils
16from command_line.hdf_command_handler_base import HdfCommandHandlerBase
17
18
19class HdiGetHandler(HdfCommandHandlerBase):
20    def __init__(self, args):
21        super(HdiGetHandler, self).__init__()
22        self.cmd = "geti"
23        self.handlers = {
24            'interface': self._get_interface_handler,
25            'peripheral': self._get_peripheral_handler,
26            'unittest': self._get_create_unittest,
27        }
28        self.parser.add_argument("--action_type",
29                                 help=' '.join(self.handlers.keys()),
30                                 required=True)
31        self.parser.add_argument("--root_dir", required=True)
32        self.args = self.parser.parse_args(args)
33        self.set_config = hdf_tool_settings.HdfToolSettings()
34        self.root = None
35        self.type = self.args.action_type
36        self.result_temp = None
37        self.hdi_info_config = None
38
39    def _get_type_comm(self):
40        self.check_arg_raise_if_not_exist("root_dir")
41        self.root = self.args.root_dir
42        hdf_setting = hdf_tool_settings.HdfToolSettings()
43        config_hdi_dir_path = hdf_setting.get_file_path()
44        config_dict = hdf_setting.get_config_setting_info()
45        hdi_file = os.path.join(
46            config_hdi_dir_path,
47            config_dict.get("config_pre_dir"),
48            config_dict.get("create_hdi_file"))
49        self.hdi_info_config = hdf_utils.read_file(hdi_file)
50
51    def _get_interface_handler(self):
52        self._get_type_comm()
53        self.result_temp = json.loads(self.hdi_info_config)[self.type]
54        return self.format_create_result()
55
56    def _get_peripheral_handler(self):
57        self._get_type_comm()
58        self.result_temp = json.loads(self.hdi_info_config)[self.type]
59        return self.format_create_result()
60
61    def _get_create_unittest(self):
62        self._get_type_comm()
63        self.result_temp = json.loads(self.hdi_info_config)[self.type]
64        return self.format_create_result()
65
66    def format_create_result(self):
67        if self.result_temp is None:
68            return ""
69        format_result = {}
70        for type_key, value in self.result_temp.items():
71            temp = {
72                "create_file": list(
73                    map(
74                        lambda val: os.path.join(self.root, val),
75                        value["create_file"])),
76                "config": list(
77                    map(
78                        lambda val: os.path.join(self.root, val),
79                        value["config"]))
80            }
81            format_result[type_key] = temp
82        return json.dumps(format_result, indent=4)
83
84    def get_type_create_info(self, type_name):
85        return self.handlers.get(type_name)
86