filter.py 文件源码

python
阅读 31 收藏 0 点赞 0 评论 0

项目:Ellis 作者: Frzk 项目源码 文件源码
def build_regex_list(cls, filter_str, rule_limit):
        """
        Creates a list of :class:`re.RegexObject`s from the given string.

        *filter_str* is a string containing the regular expressions used to
        build the Filter.

        If *filter_str* contains newlines chars, it is split in different
        regular expressions (one per line).

        If one of these strings can not be compiled into a
        :class:`re.RegexObject`, a warning is issued and the pattern is
        ignored.

        *rule_limit* is the Rule's limit above which the Action is executed.

        If *rule_limit* is > 1 and *filter_str* doesn't have at least one named
        capturing group, a warning is issued and the pattern is ignored.

        Returns a list of :class:`re.RegexObject`s built upon the given string.
        """
        regexes = []

        for f in filter_str.splitlines():
            try:
                regex = re.compile(f, re.MULTILINE)
            except sre_constants.error:
                warnings.warn("Unable to compile this pattern: \"{0}\". "
                              "It will be ignored"
                              .format(f))
            else:
                # If the Rule limit is > 1, the pattern MUST have a capturing
                # group.
                # (this capturing group will be used later as an index to
                # count the matches.)
                # If the pattern doesn't respect this, it will be ignored.
                if rule_limit > 1 and not regex.groupindex:
                    warnings.warn("The pattern \"{0}\" doesn't have a "
                                  "capturing group but needs one."
                                  "It will be ignored"
                                  .format(f))
                else:
                    regexes.append(regex)

        return regexes
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号