1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4# Copyright (c) 2020-2021 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 os
12import hdf_utils
13
14from hdf_tool_exception import HdfToolException
15from .hdf_command_error_code import CommandErrorCode
16from .hdf_tool_argument_parser import HdfToolArgumentParser
17
18
19class HdfCommandHandlerBase(object):
20    def __init__(self):
21        self.cmd = 'base'
22        self.action_type = 'base_action_type'
23        self.handlers = {}
24        self.args = {}
25        self.parser = HdfToolArgumentParser()
26
27    def run(self):
28        self.action_type = self._get_action_type()
29        if self.action_type in self.handlers:
30            return self.handlers.get(self.action_type)()
31        else:
32            raise HdfToolException(
33                'unknown action_type: "%s" for "%s" cmd' %
34                (self.action_type, self.cmd),
35                CommandErrorCode.INTERFACE_ERROR)
36
37    def _get_action_type(self):
38        try:
39            return getattr(self.args, 'action_type')
40        except AttributeError:
41            return ''
42        finally:
43            pass
44
45    def check_arg_raise_if_not_exist(self, arg):
46        try:
47            value = getattr(self.args, arg)
48            if not value:
49                raise AttributeError()
50        except AttributeError:
51            raise HdfToolException(
52                'argument "--%s" is required for "%s" cmd' %
53                (arg, self.cmd), CommandErrorCode.INTERFACE_ERROR)
54        finally:
55            pass
56
57    def _get_arg(self, arg):
58        try:
59            value = getattr(self.args, arg)
60            return value
61        except AttributeError:
62            return ''
63        finally:
64            pass
65
66    def get_args(self):
67        args = ['vendor_name', 'module_name',
68                'driver_name', 'board_name',
69                'kernel_name', "runmode",
70                'device_name']
71        ret = [self._get_arg('root_dir')]
72        for arg in args:
73            if arg == "runmode":
74                value = self._get_arg(arg)
75                if value:
76                    board_index = args.index('board_name') + 1
77                    ret[board_index] = '_'.join([ret[board_index], value])
78            else:
79                res_value = self.board_args_format_operation(arg)
80                ret.append(res_value)
81        return tuple(ret)
82
83    def board_args_format_operation(self, arg):
84        if arg == "board_name":
85            value = self._get_arg(arg)
86        else:
87            value = self._get_arg(arg)
88            if value:
89                value = hdf_utils.WordsConverter(value).lower_case()
90        return value
91
92    @staticmethod
93    def check_path_raise_if_not_exist(full_path):
94        if not os.path.exists(full_path):
95            raise HdfToolException(
96                '%s not exist' %
97                full_path, CommandErrorCode.TARGET_NOT_EXIST)
98