def new_exception(exception: Exception, message: Optional[str] = None) -> 'FailureDetails': if message is None: return FailureDetails(None, exception) else: return FailureDetails( text_docs.single_pre_formatted_line_object(message), exception)
def new_maybe_svh_validation_error__str(failure_message: Optional[str]) -> SuccessOrValidationErrorOrHardError: if failure_message is None: return new_svh_success() else: return SuccessOrValidationErrorOrHardError(False, text_docs.single_pre_formatted_line_object(failure_message), )
def _range_expr_must_not_be_empty(self): if self._range_expr == '' or self._range_expr.isspace(): msg = text_docs.single_pre_formatted_line_object( str_constructor.FormatPositional( 'Empty {}: {}', names.RANGE_EXPR_SED_NAME, repr(self._range_expr)) ) raise ValidationErrorException(msg)
def _no_satisfied_restriction( self, symbol_name: str, container: SymbolContainer) -> FailureOfDirectReference: if self._sym_name_and_container_2_err_msg_if_no_matching_part is not None: msg = self._sym_name_and_container_2_err_msg_if_no_matching_part( symbol_name, container) else: msg = text_docs.single_pre_formatted_line_object( self._default_error_message(symbol_name, container)) return FailureOfDirectReference(ErrorMessageWithFixTip(msg))
def transform_to_file(self, src_path: pathlib.Path, dst_path: pathlib.Path, transformer: StringTransformer, ) -> Optional[TextRenderer]: error_message = create_file(dst_path, _do_nothing_with_file) if error_message is not None: return text_docs.single_pre_formatted_line_object(error_message) return self._transform(src_path, dst_path, transformer)
def test_WHEN_parser_raises_exception_THEN_parse_SHOULD_raise_this_exception( self): # ARRANGE # parser_error = text_docs.single_pre_formatted_line_object('msg') actor = sut.ActorFromParts(ParserThatRaisesException(parser_error), ValidatorConstructorThatRaises(), ExecutorConstructorThatRaises()) act_phase_instructions = [] # ACT # with self.assertRaises(ParseException) as ex: executor = actor.parse(act_phase_instructions) # ASSERT # self.assertIs(parser_error, ex)
def evaluate(py_expr: str) -> int: """ :raises ValidationErrorException :return: Evaluated value """ try: return python_evaluate(py_expr) except NotAnIntegerException as ex: py_ex_str = ('' if ex.python_exception_message is None else '\n\nPython evaluation error:\n' + ex.python_exception_message) msg = text_docs.single_pre_formatted_line_object( str_constructor.FormatPositional( 'Value must be an integer: `{}\'{}', ex.value_string, py_ex_str)) raise ValidationErrorException(msg)
def _split_into_valid_number_of_parts(self) -> List[str]: self._range_expr_must_not_be_empty() limit_parts = self._range_expr.strip().split(names.LINE_NUMBERS_FILTER__LIMIT_SEPARATOR) if len(limit_parts) > 2: msg = text_docs.single_pre_formatted_line_object( str_constructor.FormatPositional( 'Invalid {} (found more than one "{}"): {}', names.RANGE_EXPR_SED_NAME, names.LINE_NUMBERS_FILTER__LIMIT_SEPARATOR, repr(self._range_expr)) ) raise ValidationErrorException(msg) return limit_parts
def is_satisfied_by(self, symbol_table: SymbolTable, symbol_name: str, container: SymbolContainer) -> Optional[ErrorMessageWithFixTip]: sdv = container.sdv if not isinstance(sdv, PathSdv): return err_msg_for_any_type.invalid_type_msg([ValueType.PATH], symbol_name, container) path = sdv.resolve(symbol_table) actual_relativity = path.relativity() satisfaction = is_satisfied_by(actual_relativity, self._accepted) if satisfaction: return None else: msg = error_messages.unsatisfied_path_relativity(symbol_name, container, self._accepted, actual_relativity) return ErrorMessageWithFixTip(text_docs.single_pre_formatted_line_object(msg))
def validate_pre_sds(self, environment: PathResolvingEnvironmentPreSds): try: resolved_value = self._int_sdv.resolve(environment) except NotAnIntegerException as ex: py_ex_str = ( '' if ex.python_exception_message is None else '\n\nPython evaluation error:\n' + ex.python_exception_message ) msg = text_docs.single_pre_formatted_line_object( str_constructor.FormatPositional( 'Argument must be an integer: `{}\'{}', ex.value_string, py_ex_str) ) raise svh_exception.SvhValidationException(msg) self._validate_custom(resolved_value)
def new_pre_formatted_str_for_test(x: Any, is_line_ended: bool = False ) -> TextRenderer: return text_docs.single_pre_formatted_line_object(x, is_line_ended)
def new_single_string_text_for_test__optional( text: Optional[str]) -> Optional[TextRenderer]: return (None if text is None else text_docs.single_pre_formatted_line_object(text))
def new_single_string_text_for_test(text: str) -> TextRenderer: return text_docs.single_pre_formatted_line_object(text)
AccumulatedComponents.empty(), ) class _ParseAsCommand(ParserFromTokenParserBase[CommandSdv]): def __init__(self): super().__init__(consume_last_line_if_is_at_eol_after_parse=False) self._arguments_parser = parse_arguments.parser() def parse_from_token_parser(self, parser: TokenParser) -> CommandSdv: program_name = parse_string.parse_string_from_token_parser( parser, _PARSE_NAME_CONF) arguments = self._arguments_parser.parse_from_token_parser(parser) return CommandSdv(CommandDriverSdvForSystemProgram(program_name), arguments) _PROGRAM_NAME_STRING_REFERENCES_RESTRICTION = is_string__all_indirect_refs_are_strings( text_docs.single_pre_formatted_line_object( str_constructor.FormatMap( 'A program name must be defined in terms of {string_type}.', { 'string_type': define_symbol.TYPE_W_STR_RENDERING_INFO_DICT[ WithStrRenderingType.STRING].identifier }, ))) _PARSE_NAME_CONF = parse_string.Configuration( 'NAME', _PROGRAM_NAME_STRING_REFERENCES_RESTRICTION)
def new_sh_hard_error__str(failure_message: str) -> SuccessOrHardError: if failure_message is None: raise ValueError( 'A HARD ERROR must have a failure message (that is not None)') return SuccessOrHardError( text_docs.single_pre_formatted_line_object(failure_message), )
def new_pfh_hard_error__str(failure_message: str) -> PassOrFailOrHardError: return new_pfh_hard_error( text_docs.single_pre_formatted_line_object(failure_message))
from exactly_lib.type_val_prims.files_source.files_source import FilesSource from exactly_lib.util.description_tree import details from exactly_lib.util.description_tree.renderer import DetailsRenderer from exactly_lib.util.description_tree.tree import Detail from exactly_lib.util.render import combinators as rend_comb from exactly_lib.util.str_ import str_constructor from exactly_lib.util.symbol_table import SymbolTable from .. import syntax from ..file_maker import FileMakerAdv, FileMakerDdv, FileMakerSdv, FileMaker FILE_NAME_STRING_REFERENCES_RESTRICTION = reference_restrictions.is_string__all_indirect_refs_are_strings( text_docs.single_pre_formatted_line_object( str_constructor.FormatMap( 'A {file_name} must be defined in terms of {string_type}.', { 'file_name': syntax.FILE_NAME.name, 'string_type': syntax_elements.STRING_SYNTAX_ELEMENT.singular_name }, ) ) ) class FileSpecification(WithDetailsDescription): def __init__(self, file_name: str, file_maker: FileMaker, ): self.name = file_name self.maker = file_maker
def new_constant_message( message: str, exception: Optional[Exception] = None) -> 'FailureDetails': return FailureDetails( text_docs.single_pre_formatted_line_object(message), exception)
def new_svh_validation_error__str(failure_message: str) -> SuccessOrValidationErrorOrHardError: if failure_message is None: raise ValueError('A VALIDATION ERROR must have a failure message (that is not None)') return SuccessOrValidationErrorOrHardError(False, text_docs.single_pre_formatted_line_object(failure_message), )
def validator_that_reports_error() -> Optional[TextRenderer]: return text_docs.single_pre_formatted_line_object(failure_message)
_ARGUMENTS__LINE_NUMS = [ a.Single(a.Multiplicity.MANDATORY, names.LINE_NUMBERS_FILTER_OPTION), a.Single(a.Multiplicity.ONE_OR_MORE, a.Named(names.RANGE_EXPR_SED_NAME)) ] _MATCHER_ARGUMENT = a.Named('MATCHER') _RANGE_LIMIT_SEPARATOR = ':' _RANGE_EXPR_REFERENCE_RESTRICTIONS = is_string__all_indirect_refs_are_strings( text_docs.single_pre_formatted_line_object( str_constructor.FormatMap( 'A {RANGE} must be made up of just {string_type} values.', { 'RANGE': names.RANGE_EXPR_SED_NAME, 'string_type': help_texts.ANY_TYPE_INFO_DICT[ValueType.STRING].identifier }))) class SyntaxDescription( grammar.PrimitiveDescriptionWithNameAsInitialSyntaxToken): def __init__(self): self._tp = TextParser({ 'MATCHER': _MATCHER_ARGUMENT.name, '_LINE_MATCHER_': syntax_elements.LINE_MATCHER_SYNTAX_ELEMENT.singular_name, 'FIRST_LINE_NUMBER': line_matcher_type.FIRST_LINE_NUMBER,
def of_str(cause: str) -> 'ParseException': return ParseException(text_docs.single_pre_formatted_line_object(cause))
def parse(self, token_parser: TokenParser) -> IntegerSdv: string_sdv = self._string_parser.parse(token_parser) return integer_sdv.IntegerSdv(string_sdv, self._custom_integer_restriction) def validator_for_non_negative(actual: int) -> Optional[TextRenderer]: if actual < 0: return expected_found.unexpected_lines( _NON_NEGATIVE_INTEGER_ARGUMENT_DESCRIPTION, str(actual)) return None _NON_NEGATIVE_INTEGER_ARGUMENT_DESCRIPTION = 'An integer >= 0' _REFERENCE_RESTRICTIONS = is_string__all_indirect_refs_are_strings( text_docs.single_pre_formatted_line_object( str_constructor.FormatMap( 'The {INTEGER} argument must be made up of just {string_type} values.', { 'INTEGER': syntax_elements.INTEGER_SYNTAX_ELEMENT.argument.name, 'string_type': help_texts.ANY_TYPE_INFO_DICT[ValueType.STRING].identifier }))) _STRING_PARSER_CONFIGURATION = parse_string.Configuration( syntax_elements.INTEGER_SYNTAX_ELEMENT.singular_name, _REFERENCE_RESTRICTIONS, )