Files
sub-provider/app/services/rules.py
2026-04-20 11:47:10 +08:00

70 lines
2.1 KiB
Python

from __future__ import annotations
from pathlib import Path
from app.models import AppConfig, ClientConfig
from app.services.profile_resolver import resolve_profile
from app.services.rule_resolver import (
build_rule_provider_entries as build_rule_provider_entries_for_profile,
iter_resolved_rule_lines,
load_rule_payload,
load_rule_text,
resolve_policy,
)
def build_rule_provider_entries(app_config: AppConfig, client: ClientConfig, base_url: str, public_path: str):
resolved_profile = resolve_profile(
app_config=app_config,
client_type=_find_client_type(app_config, client),
selected_source_names=list(app_config.sources.keys()),
)
return build_rule_provider_entries_for_profile(
resolved_profile=resolved_profile,
base_url=base_url,
public_path=public_path,
)
def build_rule_set_references(app_config: AppConfig, client: ClientConfig) -> list[str]:
resolved_profile = resolve_profile(
app_config=app_config,
client_type=_find_client_type(app_config, client),
selected_source_names=list(app_config.sources.keys()),
)
return iter_resolved_rule_lines(
resolved_profile=resolved_profile,
include_rule_set_references=True,
inline_file_payloads=False,
)
def build_inline_rules(app_config: AppConfig, client: ClientConfig) -> list[str]:
resolved_profile = resolve_profile(
app_config=app_config,
client_type=_find_client_type(app_config, client),
selected_source_names=list(app_config.sources.keys()),
)
return iter_resolved_rule_lines(
resolved_profile=resolved_profile,
include_rule_set_references=False,
inline_file_payloads=True,
)
def _find_client_type(app_config: AppConfig, client: ClientConfig) -> str:
for client_type, candidate in app_config.clients.items():
if candidate == client:
return client_type
raise KeyError("client config not found in app config")
__all__ = [
"build_inline_rules",
"build_rule_provider_entries",
"build_rule_set_references",
"load_rule_payload",
"load_rule_text",
"resolve_policy",
]