Skip to content

route_rules.gen ¤

Classes:

ArtifactMeta pydantic-model ¤

Bases: BaseModel

Parameters:

Show JSON schema:
{
  "$defs": {
    "Behavior": {
      "enum": [
        "domain",
        "ipcidr",
        "classical"
      ],
      "title": "Behavior",
      "type": "string"
    },
    "Format": {
      "enum": [
        "yaml",
        "text",
        "mrs"
      ],
      "title": "Format",
      "type": "string"
    }
  },
  "properties": {
    "behavior": {
      "$ref": "#/$defs/Behavior"
    },
    "format": {
      "$ref": "#/$defs/Format"
    },
    "path": {
      "format": "path",
      "title": "Path",
      "type": "string"
    },
    "size": {
      "title": "Size",
      "type": "integer"
    }
  },
  "required": [
    "behavior",
    "format",
    "path",
    "size"
  ],
  "title": "ArtifactMeta",
  "type": "object"
}

Fields:

behavior pydantic-field ¤

behavior: Behavior

format pydantic-field ¤

format: Format

path pydantic-field ¤

path: Path

size pydantic-field ¤

size: int

Builder ¤

Parameters:

  • dist_dir (Path, default: PosixPath('dist') ) –
  • exporters (list[ExporterMihomo], default: [ExporterMihomo(behavior=<Behavior.DOMAIN: 'domain'>, format=<Format.MRS: 'mrs'>), ExporterMihomo(behavior=<Behavior.DOMAIN: 'domain'>, format=<Format.TEXT: 'text'>), ExporterMihomo(behavior=<Behavior.IPCIDR: 'ipcidr'>, format=<Format.MRS: 'mrs'>), ExporterMihomo(behavior=<Behavior.IPCIDR: 'ipcidr'>, format=<Format.TEXT: 'text'>), ExporterMihomo(behavior=<Behavior.CLASSICAL: 'classical'>, format=<Format.TEXT: 'text'>)] ) –
  • recipes (list[Recipe], default: <dynamic> ) –

    Built-in mutable sequence.

    If no argument is given, the constructor creates a new empty list. The argument must be an iterable if specified.

Methods:

Attributes:

dist_dir class-attribute instance-attribute ¤

dist_dir: Path = Path('dist')

exporters class-attribute instance-attribute ¤

exporters: list[ExporterMihomo] = field(
    factory=_default_exporters
)

recipes class-attribute instance-attribute ¤

recipes: list[Recipe] = field(factory=list)

build async ¤

build() -> None
Source code in src/route_rules/gen/_builder.py
42
43
44
45
46
47
48
async def build(self) -> None:
    meta = Meta(build_time=datetime.now().astimezone())
    for recipe in self.recipes:
        meta.recipes.append(await self.build_recipe(recipe))
    meta_file: Path = self.dist_dir / "meta.json"
    meta_file.parent.mkdir(parents=True, exist_ok=True)
    meta_file.write_text(meta.json_encode())

build_recipe async ¤

build_recipe(recipe: Recipe) -> RecipeMeta
Source code in src/route_rules/gen/_builder.py
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
async def build_recipe(self, recipe: Recipe) -> RecipeMeta:
    ruleset: RuleSet = await recipe.build()
    meta = RecipeMeta(
        name=recipe.name,
        slug=recipe.slug,
        statistics=await self._build_statistics(recipe, ruleset),
    )
    for provider in recipe.providers:
        meta.providers.append(
            ProviderMeta(
                name=provider,
                download_url=recipe.registry.download_url(provider),
                preview_url=recipe.registry.preview_url(provider),
            )
        )
    for exporter in self.exporters:
        file: Path | None = exporter.export(self.dist_dir, recipe.slug, ruleset)
        if file is None:
            continue
        meta.artifacts.append(
            ArtifactMeta(
                behavior=exporter.behavior,
                format=exporter.format,
                path=file,
                size=file.stat().st_size,
            )
        )
    return meta

load classmethod ¤

load(file: str | PathLike[str]) -> Self
Source code in src/route_rules/gen/_builder.py
34
35
36
37
38
39
40
@classmethod
def load(cls, file: str | os.PathLike[str]) -> Self:
    config: Config = Config.load(file)
    self: Self = cls()
    for recipe_config in config.recipes:
        self.recipes.append(Recipe.from_config(recipe_config))
    return self

Config pydantic-model ¤

Bases: BaseModel

Parameters:

Show JSON schema:
{
  "$defs": {
    "RecipeConfig": {
      "properties": {
        "name": {
          "title": "Name",
          "type": "string"
        },
        "providers": {
          "items": {
            "type": "string"
          },
          "title": "Providers",
          "type": "array"
        },
        "excludes": {
          "items": {
            "type": "string"
          },
          "title": "Excludes",
          "type": "array"
        }
      },
      "required": [
        "name"
      ],
      "title": "RecipeConfig",
      "type": "object"
    }
  },
  "properties": {
    "recipes": {
      "items": {
        "$ref": "#/$defs/RecipeConfig"
      },
      "title": "Recipes",
      "type": "array"
    }
  },
  "required": [
    "recipes"
  ],
  "title": "Config",
  "type": "object"
}

Fields:

recipes pydantic-field ¤

recipes: list[RecipeConfig]

load classmethod ¤

load(file: str | PathLike[str]) -> Self
Source code in src/route_rules/gen/_config.py
18
19
20
21
22
@classmethod
def load(cls, file: str | os.PathLike[str]) -> Self:
    file = Path(file)
    data: Any = msgspec.yaml.decode(file.read_bytes())
    return cls.model_validate(data)

Meta pydantic-model ¤

Bases: BaseModel

Parameters:

  • build_time (datetime) –
  • recipes (list[RecipeMeta], default: <dynamic> ) –

    Built-in mutable sequence.

    If no argument is given, the constructor creates a new empty list. The argument must be an iterable if specified.

Show JSON schema:
{
  "$defs": {
    "ArtifactMeta": {
      "properties": {
        "behavior": {
          "$ref": "#/$defs/Behavior"
        },
        "format": {
          "$ref": "#/$defs/Format"
        },
        "path": {
          "format": "path",
          "title": "Path",
          "type": "string"
        },
        "size": {
          "title": "Size",
          "type": "integer"
        }
      },
      "required": [
        "behavior",
        "format",
        "path",
        "size"
      ],
      "title": "ArtifactMeta",
      "type": "object"
    },
    "Behavior": {
      "enum": [
        "domain",
        "ipcidr",
        "classical"
      ],
      "title": "Behavior",
      "type": "string"
    },
    "Format": {
      "enum": [
        "yaml",
        "text",
        "mrs"
      ],
      "title": "Format",
      "type": "string"
    },
    "ProviderMeta": {
      "properties": {
        "name": {
          "title": "Name",
          "type": "string"
        },
        "download_url": {
          "title": "Download Url",
          "type": "string"
        },
        "preview_url": {
          "title": "Preview Url",
          "type": "string"
        }
      },
      "required": [
        "name",
        "download_url",
        "preview_url"
      ],
      "title": "ProviderMeta",
      "type": "object"
    },
    "RecipeMeta": {
      "properties": {
        "name": {
          "title": "Name",
          "type": "string"
        },
        "slug": {
          "title": "Slug",
          "type": "string"
        },
        "artifacts": {
          "items": {
            "$ref": "#/$defs/ArtifactMeta"
          },
          "title": "Artifacts",
          "type": "array"
        },
        "providers": {
          "items": {
            "$ref": "#/$defs/ProviderMeta"
          },
          "title": "Providers",
          "type": "array"
        },
        "statistics": {
          "$ref": "#/$defs/RecipeStatistics"
        }
      },
      "required": [
        "name",
        "slug"
      ],
      "title": "RecipeMeta",
      "type": "object"
    },
    "RecipeStatistics": {
      "properties": {
        "inputs": {
          "additionalProperties": {
            "type": "integer"
          },
          "title": "Inputs",
          "type": "object"
        },
        "outputs": {
          "additionalProperties": {
            "type": "integer"
          },
          "title": "Outputs",
          "type": "object"
        }
      },
      "title": "RecipeStatistics",
      "type": "object"
    }
  },
  "properties": {
    "build_time": {
      "format": "date-time",
      "title": "Build Time",
      "type": "string"
    },
    "recipes": {
      "items": {
        "$ref": "#/$defs/RecipeMeta"
      },
      "title": "Recipes",
      "type": "array"
    }
  },
  "required": [
    "build_time"
  ],
  "title": "Meta",
  "type": "object"
}

Fields:

build_time pydantic-field ¤

build_time: datetime

recipes pydantic-field ¤

recipes: list[RecipeMeta]

json_decode classmethod ¤

json_decode(data: str | bytes | bytearray) -> Self
Source code in src/route_rules/gen/_meta.py
40
41
42
@classmethod
def json_decode(cls, data: str | bytes | bytearray) -> Self:
    return cls.model_validate_json(data)

json_encode ¤

json_encode() -> str
Source code in src/route_rules/gen/_meta.py
44
45
def json_encode(self) -> str:
    return self.model_dump_json()

ProviderMeta pydantic-model ¤

Bases: BaseModel

Parameters:

  • name (str) –
  • download_url (str) –
  • preview_url (str) –
Show JSON schema:
{
  "properties": {
    "name": {
      "title": "Name",
      "type": "string"
    },
    "download_url": {
      "title": "Download Url",
      "type": "string"
    },
    "preview_url": {
      "title": "Preview Url",
      "type": "string"
    }
  },
  "required": [
    "name",
    "download_url",
    "preview_url"
  ],
  "title": "ProviderMeta",
  "type": "object"
}

Fields:

download_url pydantic-field ¤

download_url: str

name pydantic-field ¤

name: str

preview_url pydantic-field ¤

preview_url: str

Recipe ¤

Parameters:

  • name (str) –
  • providers (str, default: <dynamic> ) –

    Built-in mutable sequence.

    If no argument is given, the constructor creates a new empty list. The argument must be an iterable if specified.

  • excludes (str, default: <dynamic> ) –

    Built-in mutable sequence.

    If no argument is given, the constructor creates a new empty list. The argument must be an iterable if specified.

  • registry (str, default: ProviderRegistry(registry={'blackmatrix7': ProviderMihomo(name='blackmatrix7', download_url_template='https://raw.githubusercontent.com/blackmatrix7/ios_rule_script/master/rule/Clash/{name}/{name}.list', preview_url_template='https://github.com/blackmatrix7/ios_rule_script/tree/master/rule/Clash/{name}', behavior=<Behavior.CLASSICAL: 'classical'>, format=<Format.TEXT: 'text'>), 'dler-io': ProviderMihomo(name='dler-io', download_url_template='https://raw.githubusercontent.com/dler-io/Rules/main/Clash/Provider/{name}.yaml', preview_url_template='https://github.com/dler-io/Rules/blob/main/Clash/Provider/{name}.yaml', behavior=<Behavior.CLASSICAL: 'classical'>, format=<Format.YAML: 'yaml'>), 'liblaf/domain': ProviderMihomo(name='liblaf/domain', download_url_template='https://raw.githubusercontent.com/liblaf/route-rules/main/rules/{name}.domain.list', preview_url_template='https://github.com/liblaf/route-rules/blob/main/rules/{name}.domain.list', behavior=<Behavior.DOMAIN: 'domain'>, format=<Format.TEXT: 'text'>), 'MetaCubeX/geosite': ProviderMihomo(name='MetaCubeX/geosite', download_url_template='https://raw.githubusercontent.com/MetaCubeX/meta-rules-dat/meta/geo/geosite/{name}.yaml', preview_url_template='https://github.com/MetaCubeX/meta-rules-dat/blob/meta/geo/geosite/{name}.yaml', behavior=<Behavior.DOMAIN: 'domain'>, format=<Format.YAML: 'yaml'>), 'SukkaW/classical': ProviderMihomo(name='SukkaW/classical', download_url_template='https://ruleset.skk.moe/Clash/{name}.txt', preview_url_template='https://ruleset.skk.moe/Clash/{name}.txt', behavior=<Behavior.CLASSICAL: 'classical'>, format=<Format.TEXT: 'text'>), 'SukkaW/domain': ProviderMihomo(name='SukkaW/domain', download_url_template='https://ruleset.skk.moe/Clash/{name}.txt', preview_url_template='https://ruleset.skk.moe/Clash/{name}.txt', behavior=<Behavior.DOMAIN: 'domain'>, format=<Format.TEXT: 'text'>)}) ) –
  • slug (str, default: <dynamic> ) –

Methods:

Attributes:

excludes class-attribute instance-attribute ¤

excludes: list[str] = field(factory=list)

name instance-attribute ¤

name: str

providers class-attribute instance-attribute ¤

providers: list[str] = field(factory=list)

registry class-attribute instance-attribute ¤

registry: ProviderRegistry = field(
    repr=False, factory=presets, kw_only=True
)

slug class-attribute instance-attribute ¤

slug: str = field(
    default=Factory(_default_slug, takes_self=True),
    kw_only=True,
)

build async ¤

build() -> RuleSet
Source code in src/route_rules/gen/_recipe.py
44
45
46
47
48
49
@cta.cachedmethod(lambda self: self._cache)
async def build(self) -> RuleSet:
    ruleset: RuleSet = await self._load(self.providers)
    ruleset -= await self._load(self.excludes)
    ruleset = ruleset.optimize()
    return ruleset

from_config classmethod ¤

from_config(config: RecipeConfig) -> Self
Source code in src/route_rules/gen/_recipe.py
38
39
40
41
42
@classmethod
def from_config(cls, config: RecipeConfig) -> Self:
    return cls(
        name=config.name, providers=config.providers, excludes=config.excludes
    )

RecipeConfig pydantic-model ¤

Bases: BaseModel

Parameters:

  • name (str) –
  • providers (list[str], default: <dynamic> ) –

    Built-in mutable sequence.

    If no argument is given, the constructor creates a new empty list. The argument must be an iterable if specified.

  • excludes (list[str], default: <dynamic> ) –

    Built-in mutable sequence.

    If no argument is given, the constructor creates a new empty list. The argument must be an iterable if specified.

Show JSON schema:
{
  "properties": {
    "name": {
      "title": "Name",
      "type": "string"
    },
    "providers": {
      "items": {
        "type": "string"
      },
      "title": "Providers",
      "type": "array"
    },
    "excludes": {
      "items": {
        "type": "string"
      },
      "title": "Excludes",
      "type": "array"
    }
  },
  "required": [
    "name"
  ],
  "title": "RecipeConfig",
  "type": "object"
}

Fields:

excludes pydantic-field ¤

excludes: list[str]

name pydantic-field ¤

name: str

providers pydantic-field ¤

providers: list[str]

RecipeMeta pydantic-model ¤

Bases: BaseModel

Parameters:

  • name (str) –
  • slug (str) –
  • artifacts (list[ArtifactMeta], default: <dynamic> ) –

    Built-in mutable sequence.

    If no argument is given, the constructor creates a new empty list. The argument must be an iterable if specified.

  • providers (list[ProviderMeta], default: <dynamic> ) –

    Built-in mutable sequence.

    If no argument is given, the constructor creates a new empty list. The argument must be an iterable if specified.

  • statistics (RecipeStatistics, default: <dynamic> ) –
Show JSON schema:
{
  "$defs": {
    "ArtifactMeta": {
      "properties": {
        "behavior": {
          "$ref": "#/$defs/Behavior"
        },
        "format": {
          "$ref": "#/$defs/Format"
        },
        "path": {
          "format": "path",
          "title": "Path",
          "type": "string"
        },
        "size": {
          "title": "Size",
          "type": "integer"
        }
      },
      "required": [
        "behavior",
        "format",
        "path",
        "size"
      ],
      "title": "ArtifactMeta",
      "type": "object"
    },
    "Behavior": {
      "enum": [
        "domain",
        "ipcidr",
        "classical"
      ],
      "title": "Behavior",
      "type": "string"
    },
    "Format": {
      "enum": [
        "yaml",
        "text",
        "mrs"
      ],
      "title": "Format",
      "type": "string"
    },
    "ProviderMeta": {
      "properties": {
        "name": {
          "title": "Name",
          "type": "string"
        },
        "download_url": {
          "title": "Download Url",
          "type": "string"
        },
        "preview_url": {
          "title": "Preview Url",
          "type": "string"
        }
      },
      "required": [
        "name",
        "download_url",
        "preview_url"
      ],
      "title": "ProviderMeta",
      "type": "object"
    },
    "RecipeStatistics": {
      "properties": {
        "inputs": {
          "additionalProperties": {
            "type": "integer"
          },
          "title": "Inputs",
          "type": "object"
        },
        "outputs": {
          "additionalProperties": {
            "type": "integer"
          },
          "title": "Outputs",
          "type": "object"
        }
      },
      "title": "RecipeStatistics",
      "type": "object"
    }
  },
  "properties": {
    "name": {
      "title": "Name",
      "type": "string"
    },
    "slug": {
      "title": "Slug",
      "type": "string"
    },
    "artifacts": {
      "items": {
        "$ref": "#/$defs/ArtifactMeta"
      },
      "title": "Artifacts",
      "type": "array"
    },
    "providers": {
      "items": {
        "$ref": "#/$defs/ProviderMeta"
      },
      "title": "Providers",
      "type": "array"
    },
    "statistics": {
      "$ref": "#/$defs/RecipeStatistics"
    }
  },
  "required": [
    "name",
    "slug"
  ],
  "title": "RecipeMeta",
  "type": "object"
}

Fields:

artifacts pydantic-field ¤

artifacts: list[ArtifactMeta]

name pydantic-field ¤

name: str

providers pydantic-field ¤

providers: list[ProviderMeta]

slug pydantic-field ¤

slug: str

statistics pydantic-field ¤

statistics: RecipeStatistics