1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3 4# 5# Copyright (c) 2023 Huawei Device Co., Ltd. 6# Licensed under the Apache License, Version 2.0 (the "License"); 7# you may not use this file except in compliance with the License. 8# You may obtain a copy of the License at 9# 10# http://www.apache.org/licenses/LICENSE-2.0 11# 12# Unless required by applicable law or agreed to in writing, software 13# distributed under the License is distributed on an "AS IS" BASIS, 14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15# See the License for the specific language governing permissions and 16# limitations under the License. 17# 18 19from abc import ABCMeta 20from containers.arg import Arg 21from exceptions.ohos_exception import OHOSException 22from containers.status import throw_exception 23 24 25class ArgsResolverInterface(metaclass=ABCMeta): 26 27 def __init__(self, args_dict: dict): 28 self._args_to_function = dict() 29 self._map_args_to_function(args_dict) 30 31 @throw_exception 32 def resolve_arg(self, target_arg: Arg, module): 33 if target_arg.arg_name not in self._args_to_function.keys(): 34 raise OHOSException( 35 'You are tring to call {} resolve function, but it has not been defined yet', '0000') 36 if not hasattr(self._args_to_function[target_arg.arg_name], '__call__'): 37 raise OHOSException() 38 39 resolve_function = self._args_to_function[target_arg.arg_name] 40 return resolve_function(target_arg, module) 41 42 @throw_exception 43 def _map_args_to_function(self, args_dict: dict): 44 for entity in args_dict.values(): 45 if isinstance(entity, Arg) and entity.arg_name != 'sshkey': 46 args_name = entity.arg_name 47 function_name = entity.resolve_function 48 if not hasattr(self, function_name) or \ 49 not hasattr(self.__getattribute__(function_name), '__call__'): 50 raise OHOSException( 51 'There is no resolution function for arg: {}'.format( 52 args_name), 53 "0004") 54 entity.resolve_function = self.__getattribute__(function_name) 55 self._args_to_function[args_name] = self.__getattribute__( 56 function_name) 57