Skip to content

route_rules.container

Modules:

Classes:

Rule

Bases: BaseModel

Methods:

Attributes:

Source code in src/route_rules/container/_rule.py
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
class Rule(BaseModel):
    model_config = ConfigDict(extra="forbid")
    domain: Set = set()
    domain_suffix: Set = set()
    domain_keyword: Set = set()
    domain_regex: Set = set()
    ip_cidr: Set = set()

    @classmethod
    def from_file(cls, path: StrPath) -> "Rule":
        rule_set: rr.RuleSet = rr.RuleSet.from_file(path)
        return Rule().union(*rule_set.rules)

    def __getitem__(self, key: str) -> set[str]:
        return getattr(self, key)

    def __len__(self) -> int:
        return sum(len(v) for k, v in self)

    def __or__(self, other: "Rule") -> "Rule":
        return self.op(operator.or_, other)

    def __sub__(self, other: "Rule") -> "Rule":
        return self.op(operator.sub, other)

    def union(self, *others: "Rule") -> "Rule":
        return Rule(**{k: v.union(*(r[k] for r in others)) for k, v in self})

    def difference(self, *others: "Rule") -> "Rule":
        return Rule(**{k: v.difference(*(r[k] for r in others)) for k, v in self})

    def geoip(self) -> "Rule":
        return Rule(ip_cidr=self.ip_cidr)

    def geosite(self) -> "Rule":
        return Rule(
            domain=self.domain,
            domain_suffix=self.domain_suffix,
            domain_keyword=self.domain_keyword,
            domain_regex=self.domain_regex,
        )

    def op(self, op: Callable[[Any, Any], Any], other: "Rule") -> "Rule":
        return Rule(**{k: op(self[k], other[k]) for k, v in self})

    def optimize(self) -> None:
        self.domain = optim.remove_unresolvable(self.domain)
        self.domain, self.domain_suffix = optim.merge_domain_with_suffix(
            self.domain, self.domain_suffix
        )
        self.domain_suffix = optim.merge_between_suffix(self.domain_suffix)
        self.domain, self.domain_keyword = optim.merge_domain_with_keyword(
            self.domain, self.domain_keyword
        )
        self.domain_suffix, self.domain_keyword = optim.merge_suffix_with_keyword(
            self.domain_suffix, self.domain_keyword
        )
        self.ip_cidr = optim.merge_ip_cidr(self.ip_cidr)

    def save(self, path: StrPath) -> None:
        rr.RuleSet(version=1, rules=[self]).save(path)

    def summary(self) -> str:
        res: str = ""
        for k, v in self:
            if v:
                name: str = k.upper().replace("_", "-")
                res += f"{name}: {len(v)}\n"
        res += f"TOTAL: {len(self)}"
        return res

domain class-attribute instance-attribute

domain: Set = set()

domain_keyword class-attribute instance-attribute

domain_keyword: Set = set()

domain_regex class-attribute instance-attribute

domain_regex: Set = set()

domain_suffix class-attribute instance-attribute

domain_suffix: Set = set()

ip_cidr class-attribute instance-attribute

ip_cidr: Set = set()

model_config class-attribute instance-attribute

model_config = ConfigDict(extra='forbid')

__getitem__

__getitem__(key: str) -> set[str]
Source code in src/route_rules/container/_rule.py
28
29
def __getitem__(self, key: str) -> set[str]:
    return getattr(self, key)

__len__

__len__() -> int
Source code in src/route_rules/container/_rule.py
31
32
def __len__(self) -> int:
    return sum(len(v) for k, v in self)

__or__

__or__(other: Rule) -> Rule
Source code in src/route_rules/container/_rule.py
34
35
def __or__(self, other: "Rule") -> "Rule":
    return self.op(operator.or_, other)

__sub__

__sub__(other: Rule) -> Rule
Source code in src/route_rules/container/_rule.py
37
38
def __sub__(self, other: "Rule") -> "Rule":
    return self.op(operator.sub, other)

difference

difference(*others: Rule) -> Rule
Source code in src/route_rules/container/_rule.py
43
44
def difference(self, *others: "Rule") -> "Rule":
    return Rule(**{k: v.difference(*(r[k] for r in others)) for k, v in self})

from_file classmethod

from_file(path: StrPath) -> Rule
Source code in src/route_rules/container/_rule.py
23
24
25
26
@classmethod
def from_file(cls, path: StrPath) -> "Rule":
    rule_set: rr.RuleSet = rr.RuleSet.from_file(path)
    return Rule().union(*rule_set.rules)

geoip

geoip() -> Rule
Source code in src/route_rules/container/_rule.py
46
47
def geoip(self) -> "Rule":
    return Rule(ip_cidr=self.ip_cidr)

geosite

geosite() -> Rule
Source code in src/route_rules/container/_rule.py
49
50
51
52
53
54
55
def geosite(self) -> "Rule":
    return Rule(
        domain=self.domain,
        domain_suffix=self.domain_suffix,
        domain_keyword=self.domain_keyword,
        domain_regex=self.domain_regex,
    )

op

op(op: Callable[[Any, Any], Any], other: Rule) -> Rule
Source code in src/route_rules/container/_rule.py
57
58
def op(self, op: Callable[[Any, Any], Any], other: "Rule") -> "Rule":
    return Rule(**{k: op(self[k], other[k]) for k, v in self})

optimize

optimize() -> None
Source code in src/route_rules/container/_rule.py
60
61
62
63
64
65
66
67
68
69
70
71
72
def optimize(self) -> None:
    self.domain = optim.remove_unresolvable(self.domain)
    self.domain, self.domain_suffix = optim.merge_domain_with_suffix(
        self.domain, self.domain_suffix
    )
    self.domain_suffix = optim.merge_between_suffix(self.domain_suffix)
    self.domain, self.domain_keyword = optim.merge_domain_with_keyword(
        self.domain, self.domain_keyword
    )
    self.domain_suffix, self.domain_keyword = optim.merge_suffix_with_keyword(
        self.domain_suffix, self.domain_keyword
    )
    self.ip_cidr = optim.merge_ip_cidr(self.ip_cidr)

save

save(path: StrPath) -> None
Source code in src/route_rules/container/_rule.py
74
75
def save(self, path: StrPath) -> None:
    rr.RuleSet(version=1, rules=[self]).save(path)

summary

summary() -> str
Source code in src/route_rules/container/_rule.py
77
78
79
80
81
82
83
84
def summary(self) -> str:
    res: str = ""
    for k, v in self:
        if v:
            name: str = k.upper().replace("_", "-")
            res += f"{name}: {len(v)}\n"
    res += f"TOTAL: {len(self)}"
    return res

union

union(*others: Rule) -> Rule
Source code in src/route_rules/container/_rule.py
40
41
def union(self, *others: "Rule") -> "Rule":
    return Rule(**{k: v.union(*(r[k] for r in others)) for k, v in self})

RuleSet

Bases: BaseModel

Methods:

Attributes:

Source code in src/route_rules/container/_rule_set.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class RuleSet(BaseModel):
    version: Literal[1, 2]
    rules: list[rr.Rule]

    @classmethod
    def from_file(cls, path: StrPath) -> "RuleSet":
        fpath: Path = Path(path)
        return RuleSet.model_validate_json(fpath.read_text())

    def save(self, path: StrPath) -> None:
        json: str = self.model_dump_json(exclude_defaults=True)
        fpath: Path = Path(path)
        fpath.parent.mkdir(parents=True, exist_ok=True)
        fpath.write_text(json)

rules instance-attribute

rules: list[Rule]

version instance-attribute

version: Literal[1, 2]

from_file classmethod

from_file(path: StrPath) -> RuleSet
Source code in src/route_rules/container/_rule_set.py
14
15
16
17
@classmethod
def from_file(cls, path: StrPath) -> "RuleSet":
    fpath: Path = Path(path)
    return RuleSet.model_validate_json(fpath.read_text())

save

save(path: StrPath) -> None
Source code in src/route_rules/container/_rule_set.py
19
20
21
22
23
def save(self, path: StrPath) -> None:
    json: str = self.model_dump_json(exclude_defaults=True)
    fpath: Path = Path(path)
    fpath.parent.mkdir(parents=True, exist_ok=True)
    fpath.write_text(json)