Skip to content

Commit

Permalink
fix: ignore the previous release's rule validity (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
Snoopy1866 authored Dec 25, 2024
1 parent 697e20a commit 8525ac2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
27 changes: 27 additions & 0 deletions scripts/check.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
from collections import Counter
import re
from jsonschema import validate

from const import BILIBILI_BLOCK_SCHEMA, BILIBILI_BLOCK
Expand Down Expand Up @@ -58,6 +59,30 @@ def check_rule_of_user(rules: list[Rule]) -> None:
)


def check_regex(rules: list[Rule]) -> None:
"""检查正则表达式是否匹配
Args:
rules (list[Rule]): 规则列表
Raises:
ValueError: 正则表达式未能匹配需屏蔽的字符串
ValueError: 正则表达式错误匹配需排除的字符串
"""
for rule in rules:
if rule.type == RuleType.REGEX:
try:
pattern = re.compile(rule.filter)
for example in rule.examples:
if not pattern.search(example):
raise ValueError(f"正则表达式未能匹配需屏蔽的字符串: '{example}'")
for example in rule.exclude_examples:
if pattern.search(example):
raise ValueError(f"正则表达式错误匹配需排除的字符串: '{example}'")
except ValueError as e:
raise ValueError(f"规则 {rule} 错误: {e}") from e


def main() -> None:
try:
with open(BILIBILI_BLOCK_SCHEMA, "r", encoding="utf-8") as f:
Expand All @@ -74,6 +99,8 @@ def main() -> None:
check_duplicate_ids(rules)

check_rule_of_user(rules)

check_regex(rules)
except Exception as e:
print(e)
exit(1)
Expand Down
17 changes: 0 additions & 17 deletions scripts/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

import re
from enum import Enum
from time import time_ns

Expand Down Expand Up @@ -46,25 +45,9 @@ def __init__(
self.examples.sort(key=lambda example: lazy_pinyin(example))
self.exclude_examples.sort(key=lambda example: lazy_pinyin(example))

# 检查正则表达式是否匹配
self.check_regex_match()

def __str__(self) -> str:
return f"Rule(id={self.id}, filter={self.filter})"

def check_regex_match(self) -> None:
if self.type == RuleType.REGEX:
try:
pattern = re.compile(self.filter)
for example in self.examples:
if not pattern.search(example):
raise ValueError(f"正则表达式未能匹配需屏蔽的字符串: '{example}'")
for example in self.exclude_examples:
if pattern.search(example):
raise ValueError(f"正则表达式错误匹配需排除的字符串: '{example}'")
except ValueError as e:
raise ValueError(f"规则 {self} 错误: {e}") from e

@classmethod
def from_dict(cls, dict: dict) -> Rule:
try:
Expand Down

0 comments on commit 8525ac2

Please sign in to comment.