15f9996aaSopenharmony_ci#!/usr/bin/env python3 25f9996aaSopenharmony_ci# -*- coding: utf-8 -*- 35f9996aaSopenharmony_ci 45f9996aaSopenharmony_ci# 55f9996aaSopenharmony_ci# Copyright (c) 2023 Huawei Device Co., Ltd. 65f9996aaSopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License"); 75f9996aaSopenharmony_ci# you may not use this file except in compliance with the License. 85f9996aaSopenharmony_ci# You may obtain a copy of the License at 95f9996aaSopenharmony_ci# 105f9996aaSopenharmony_ci# http://www.apache.org/licenses/LICENSE-2.0 115f9996aaSopenharmony_ci# 125f9996aaSopenharmony_ci# Unless required by applicable law or agreed to in writing, software 135f9996aaSopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS, 145f9996aaSopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 155f9996aaSopenharmony_ci# See the License for the specific language governing permissions and 165f9996aaSopenharmony_ci# limitations under the License. 175f9996aaSopenharmony_ci# 185f9996aaSopenharmony_ci 195f9996aaSopenharmony_cifrom abc import ABCMeta 205f9996aaSopenharmony_cifrom containers.arg import Arg 215f9996aaSopenharmony_cifrom exceptions.ohos_exception import OHOSException 225f9996aaSopenharmony_cifrom containers.status import throw_exception 235f9996aaSopenharmony_ci 245f9996aaSopenharmony_ci 255f9996aaSopenharmony_ciclass ArgsResolverInterface(metaclass=ABCMeta): 265f9996aaSopenharmony_ci 275f9996aaSopenharmony_ci def __init__(self, args_dict: dict): 285f9996aaSopenharmony_ci self._args_to_function = dict() 295f9996aaSopenharmony_ci self._map_args_to_function(args_dict) 305f9996aaSopenharmony_ci 315f9996aaSopenharmony_ci @throw_exception 325f9996aaSopenharmony_ci def resolve_arg(self, target_arg: Arg, module): 335f9996aaSopenharmony_ci if target_arg.arg_name not in self._args_to_function.keys(): 345f9996aaSopenharmony_ci raise OHOSException( 355f9996aaSopenharmony_ci 'You are tring to call {} resolve function, but it has not been defined yet', '0000') 365f9996aaSopenharmony_ci if not hasattr(self._args_to_function[target_arg.arg_name], '__call__'): 375f9996aaSopenharmony_ci raise OHOSException() 385f9996aaSopenharmony_ci 395f9996aaSopenharmony_ci resolve_function = self._args_to_function[target_arg.arg_name] 405f9996aaSopenharmony_ci return resolve_function(target_arg, module) 415f9996aaSopenharmony_ci 425f9996aaSopenharmony_ci @throw_exception 435f9996aaSopenharmony_ci def _map_args_to_function(self, args_dict: dict): 445f9996aaSopenharmony_ci for entity in args_dict.values(): 455f9996aaSopenharmony_ci if isinstance(entity, Arg) and entity.arg_name != 'sshkey': 465f9996aaSopenharmony_ci args_name = entity.arg_name 475f9996aaSopenharmony_ci function_name = entity.resolve_function 485f9996aaSopenharmony_ci if not hasattr(self, function_name) or \ 495f9996aaSopenharmony_ci not hasattr(self.__getattribute__(function_name), '__call__'): 505f9996aaSopenharmony_ci raise OHOSException( 515f9996aaSopenharmony_ci 'There is no resolution function for arg: {}'.format( 525f9996aaSopenharmony_ci args_name), 535f9996aaSopenharmony_ci "0004") 545f9996aaSopenharmony_ci entity.resolve_function = self.__getattribute__(function_name) 555f9996aaSopenharmony_ci self._args_to_function[args_name] = self.__getattribute__( 565f9996aaSopenharmony_ci function_name) 57