Exemple #1
0
    def try_parse_directive(
            self, line: str, doc_file_ext: str,
            directive_config: DirectiveConfigList) -> Tuple[str, Any]:
        """
        :param line: next line to try parse a directive from.
        :param doc_file_ext: file extention.
        :param directive_config: a list used to output parsed directive configuration.
        :return: parse result.
        """

        if doc_file_ext not in self.ext_to_patterns:
            return failure()

        matches = self.ext_to_patterns[doc_file_ext].findall(line)
        if len(matches) > 1:
            raise ValueError("more than one directives in a line")

        match = matches[0] if len(matches) else None
        if match:
            for parser in self.config_parsers:
                if succeeded(parser(match, directive_config)):
                    return success()

            raise ValueError("Failed to parse configuration.")
        else:
            return failure()
Exemple #2
0
    def try_parse_directive(
            self, ctx: DocCheckerCtx,
            directive_config: DirectiveConfigList) -> Tuple[str, Any]:
        """
        :param ctx: parser context.
        :param directive_config: a list used to output parsed directive configuration.
        :return: parse result.
        """
        try:
            line = ctx.doc_file.next_non_empty_line()
        except RuntimeError as e:
            # Do not raise exception when next non-empty line
            # does not exist. Instead, return failure.
            if str(e) != "Enf of file.":
                raise
            return failure()

        matches = self.ext_to_patterns[ctx.doc_file_ext()].findall(line)
        if len(matches) > 1:
            raise ValueError("more than one directives in a line")

        match = matches[0] if len(matches) else None
        if match:
            for parser in self.config_parsers:
                if succeeded(parser(match, directive_config)):
                    return success()

            raise ValueError("Failed to parse configuration.")
        else:
            return failure()
Exemple #3
0
def generic_config_parser(
        match: str, directive_config: DirectiveConfigList) -> Tuple[str, Any]:
    """
    Generic configuration parser.
    Will return success if and only if configuration is specified as a python dictionary literal.

    @param match: the content from which to parse the directive configuration.
    @param directive_config: a list to output the parsed directive_config.
    @return: parsing result.
    """
    try:
        directive_config.append(ast.literal_eval(match))
        return success()
    except (SyntaxError, ValueError):
        # If literal_eval failed, return parsing failure.
        return failure()