Skip to content

Commit

Permalink
Fix yaml format for combined ruleset.
Browse files Browse the repository at this point in the history
  • Loading branch information
xkww3n committed Dec 6, 2023
1 parent be5a64a commit 931f9fb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
9 changes: 8 additions & 1 deletion Tests/test_2_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ def test_dump_combined(self):

assert not (test_dist/"text"/"combined.txt").exists()
assert not (test_dist/"text-plus"/"combined.txt").exists()
assert not (test_dist/"yaml"/"combined.yaml").exists()
assert not (test_dist/"geosite"/"combined").exists()

assert (test_dist/"clash-compatible"/"combined.txt").exists()
Expand All @@ -172,6 +171,14 @@ def test_dump_combined(self):
"IP-CIDR,11.4.5.14\n"
"IP-CIDR6,fc00:114::514\n")

assert (test_dist/"yaml"/"combined.yaml").exists()
with open(test_dist/"yaml"/"combined.yaml", mode="r") as f:
assert f.read() == ("payload:\n"
" - 'DOMAIN,example.com'\n"
" - 'DOMAIN-SUFFIX,example.com'\n"
" - 'IP-CIDR,11.4.5.14'\n"
" - 'IP-CIDR6,fc00:114::514'\n")

assert (test_dist/"sing-ruleset"/"combined.json").exists()
with open(test_dist/"sing-ruleset"/"combined.json", mode="r") as f:
assert f.read() == ('{\n'
Expand Down
42 changes: 23 additions & 19 deletions Utils/ruleset.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,26 +162,30 @@ def dump(src: RuleSet, target: str, dst: Path, filename: str) -> None:
and rule.Type == "DomainSuffix"
) else f"{to_write}\n"
dist.writelines(to_write)
elif target in ("surge-compatible", "clash-compatible"):
elif target in ("surge-compatible", "clash-compatible", "yaml"):
if target == "yaml":
dist.writelines("payload:\n")
for rule in src:
match rule.Type:
case "DomainSuffix":
to_write = f"DOMAIN-SUFFIX,{rule.Payload}"
case "DomainFull":
to_write = f"DOMAIN,{rule.Payload}"
case "IPCIDR":
to_write = f"IP-CIDR,{rule.Payload}"
case "IPCIDR6":
to_write = f"IP-CIDR6,{rule.Payload}"
to_write = f"{to_write},Policy\n" if target == "clash-compatible" else f"{to_write}\n"
dist.writelines(to_write)
elif target == "yaml":
dist.writelines("payload:\n")
for rule in src:
if rule.Type == "DomainSuffix":
dist.writelines(f" - '+.{rule.Payload}'\n")
elif rule.Type == "DomainFull" or "IPCIDR" or "IPCIDR6":
dist.writelines(f" - '{rule.Payload}'\n")
if target == "yaml" and src.Type != "Combined":
if rule.Type == "DomainSuffix":
to_write = f"+.{rule.Payload}"
else:
to_write = rule.Payload
else:
match rule.Type:
case "DomainSuffix":
to_write = f"DOMAIN-SUFFIX,{rule.Payload}"
case "DomainFull":
to_write = f"DOMAIN,{rule.Payload}"
case "IPCIDR":
to_write = f"IP-CIDR,{rule.Payload}"
case "IPCIDR6":
to_write = f"IP-CIDR6,{rule.Payload}"
if target == "clash-compatible":
to_write += ",Policy"
elif target == "yaml":
to_write = f" - '{to_write}'"
dist.writelines(f"{to_write}\n")
elif target == "geosite":
for rule in src:
match rule.Type:
Expand Down

0 comments on commit 931f9fb

Please sign in to comment.