• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
43    def check_arg_raise_if_not_exist(self, arg):
44        try:
45            value = getattr(self.args, arg)
46            if not value:
47                raise AttributeError()
48        except AttributeError:
49            raise HdfToolException(
50                'argument "--%s" is required for "%s" cmd' %
51                (arg, self.cmd), CommandErrorCode.INTERFACE_ERROR)
52
53    def _get_arg(self, arg):
54        try:
55            value = getattr(self.args, arg)
56            return value
57        except AttributeError:
58            return ''
59
60    def get_args(self):
61        args = ['vendor_name', 'module_name',
62                'driver_name', 'board_name', 'kernel_name']
63        ret = [self._get_arg('root_dir')]
64        for arg in args:
65            value = self._get_arg(arg)
66            if value:
67                value = hdf_utils.WordsConverter(value).lower_case()
68            ret.append(value)
69        return tuple(ret)
70
71    @staticmethod
72    def check_path_raise_if_not_exist(full_path):
73        if not os.path.exists(full_path):
74            raise HdfToolException(
75                '%s not exist' %
76                full_path, CommandErrorCode.TARGET_NOT_EXIST)
77