def validate(self, type: str, value): if not type in self.__entity_to_func.keys(): raise Validator.InvalidObjectTypeException( f"Wrong validation type: `{type}`.") try: self.__entity_to_func[type](value) except KeyError: DataProvider.get_service_command(type).get_validation_func()(value) except CommonValidator.ValidationError as exception: raise Validator.ValidationError(str(exception))
def __validate_command(value: (str, list)): command, params = value try: command = DataProvider.get_service_command(command) except KeyError: raise Validator.ValidationError(f"Wrong command name: `{command}`") if command.get_name() != Semantic.get_symbol( 'var') and command.get_attr_count() != len(params): raise Validator.ValidationError( f"Wrong params count: found {len(params)}" f" instead of {command.get_attr_count()}") if command.get_name() == Semantic.get_symbol('import'): Validator.__validate_import(params[0]) Validator.__import_module(params[0]) return param_types = command.get_param_types() for idx, param in enumerate(params): try: if ModuleManager.module_was_loaded('CORE'): if CommonValidator.looks_like_variable(param): raise ValueError param_types[idx](param) except ValueError: if ModuleManager.module_was_loaded('CORE'): if CommonValidator.looks_like_variable(param): CommonValidator.check_if_variable_exists(param) continue raise Validator.ValidationError( f"Wrong param type. Param #{idx + 1}: `{param}` " f"cannot be casted to {param_types[idx]}. ") except TypeError: new_params = params[idx + 1:] Validator.__validate_command((param, new_params)) break previous_command = DataProvider.peek_command_stack() if DataProvider.is_closing_command(command.get_name()) and \ not DataProvider.is_pair(previous_command, command.get_name()): raise Validator.ValidationError( f"Wrong command pair: [{previous_command}, {command.get_name()}]." ) try: command.get_validation_func()(params) except TypeError as ignored: # some commands may have no validation function pass
def __run_command(self, command, params): command = DataProvider.get_service_command(command) if ModuleManager.module_was_loaded('CORE'): for idx, param in enumerate(params): try: CommonValidator.validate_name_variable(param) CommonValidator.check_if_variable_exists(param) variable = param[1:] params[idx] = DataProvider.get_variable_value(variable) except Exception: pass try: self.__validator.validate('command', (command.get_name(), params)) except (Validator.ValidationError, CommonValidator.ValidationError) as exception: raise Interpreter.RunTimeError( f'Command: `{command.get_name()}`. ' + str(exception)) param_types = command.get_param_types() for idx, param in enumerate(params): if inspect.isclass(param_types[idx]): params[idx] = param_types[idx](params[idx]) else: break try: self.__validator.validate('command', (command.get_name(), params)) except (Validator.ValidationError, CommonValidator.ValidationError) as exception: raise Interpreter.RunTimeError( f'Command: `{command.get_name()}`. ' + str(exception)) if command.get_name() == Semantic.get_symbol('var'): new_command, *new_params = params[2:] self.__run_command(new_command, new_params) params = params[:2] params.append(DataProvider.get_returned_value()) DataProvider.return_value(None) if command.get_name() == Semantic.get_symbol('import'): return try: command.get_exec_func()(*params) except CommonLogic.RunTimeError as exception: raise Interpreter.RunTimeError(f'Command: {command.get_name()}. ' f'{str(exception)}')