#!/usr/bin/env python3
"""Строгий реестр публичных источников нормативного корпуса."""
from __future__ import annotations
import argparse
import hashlib
import html
import json
import os
import re
import sys
from copy import deepcopy
from datetime import datetime
from html.parser import HTMLParser
from pathlib import Path, PurePosixPath
from urllib.parse import urlsplit
from update_legal_corpus import KODEKSY, SUDACT_PLENUM_CATALOG, TOC_ENTRY_TMPL, extract_article_text
CONFIG = Path("config/corpus-needs.json")
MAX_CONFIG_BYTES = 256 * 1024
MAX_DOCUMENT_BYTES = 10 * 1024 * 1024
ID_RE = re.compile(r"^[a-z0-9][a-z0-9:._-]{0,159}$")
SLUG_RE = re.compile(r"^[a-z0-9][a-z0-9-]{0,79}$")
CONSULTANT_RE = re.compile(
r"^https://www\.consultant\.ru/document/cons_doc_LAW_(\d+)/$")
PLENUM_RE = re.compile(
r"^https://sudact\.ru/law/postanovlenie-plenuma-[a-z0-9_-]+/$")
VSRF_RE = re.compile(r"^https://(?:www\.)?vsrf\.ru/[^?#]+$")
ALLOWED_PARSERS = frozenset({
"consultant-code", "consultant-act", "sudact-plenum", "sudact-catalog", "vsrf-review",
})
PARSER_KINDS = {
"consultant-code": frozenset({"code"}),
"consultant-act": frozenset({"federal-law", "bylaw"}),
"sudact-plenum": frozenset({"plenum"}),
"sudact-catalog": frozenset({"catalog"}),
"vsrf-review": frozenset({"review"}),
}
OUTPUT_PREFIX = {
"consultant-code": "knowledge/kodeksy/",
"consultant-act": "knowledge/akty/",
"sudact-plenum": "knowledge/plenumy/",
"sudact-catalog": ".agent/maintenance/corpus/",
"vsrf-review": "knowledge/obzory/",
}
VOID_TAGS = frozenset({
"area", "base", "br", "col", "embed", "hr", "img", "input", "link",
"meta", "param", "source", "track", "wbr",
})
BLOCK_TAGS = frozenset({
"address", "article", "aside", "blockquote", "div", "dl", "dt", "dd",
"fieldset", "figcaption", "figure", "footer", "form", "h1", "h2", "h3",
"h4", "h5", "h6", "header", "hr", "li", "main", "nav", "ol", "p",
"pre", "section", "table", "tbody", "td", "tfoot", "th", "thead", "tr",
"ul",
})
DENIED_CLASS_PARTS = ("blocked-document", "snippet", "teaser", "pagination")
# Оглавление страницы акта; после родителя сразу идет вложенный
с детьми.
TOC_CLASS_RE = re.compile(r"""(?\s* str:
if (not isinstance(value, str) or not value or value != value.strip()
or len(value) > maximum or "\x00" in value):
raise ValueError(f"invalid {field}")
return value
def _validate_output(value: object, parser: str) -> str:
output = _plain_string(value, "output", 240)
path = PurePosixPath(output)
if (path.is_absolute() or path.as_posix() != output or "\\" in output or ".." in path.parts
or "." in path.parts or output.startswith("/") or output.endswith("/")):
raise ValueError("invalid output")
if not output.startswith(OUTPUT_PREFIX[parser]):
raise ValueError("output outside parser tree")
suffix = ".json" if parser == "sudact-catalog" else ".md"
if not output.endswith(suffix):
raise ValueError("invalid output suffix")
return output
def _validate_url(value: object, parser: str) -> tuple[str, str | None]:
url = _plain_string(value, "url", 2048)
try:
parsed = urlsplit(url)
port = parsed.port
except ValueError as exc:
raise ValueError("invalid url") from exc
if (parsed.scheme != "https" or parsed.username is not None
or parsed.password is not None or port not in (None, 443)
or parsed.query or parsed.fragment):
raise ValueError("invalid url")
if parser in {"consultant-code", "consultant-act"}:
match = CONSULTANT_RE.fullmatch(url)
if not match:
raise ValueError("invalid Consultant URL")
return url, match.group(1)
if parser == "sudact-plenum":
if not PLENUM_RE.fullmatch(url):
raise ValueError("invalid Sudact Plenum URL")
return url, None
if parser == "vsrf-review":
if not VSRF_RE.fullmatch(url):
raise ValueError("invalid VS RF review URL")
return url, None
if parser == "sudact-catalog":
if url != SUDACT_PLENUM_CATALOG:
raise ValueError("invalid Sudact catalog URL")
return url, None
raise ValueError("unknown parser")
def _validate_entry(value: object, doc_id: str) -> dict:
if not isinstance(value, dict):
raise ValueError("consultant-code entry required")
allowed = {"slug", "title", "doc_ids", "part_labels", "chapter_scope"}
if set(value) - allowed:
raise ValueError("unknown code entry field")
slug = _plain_string(value.get("slug"), "entry.slug", 80)
if not SLUG_RE.fullmatch(slug):
raise ValueError("invalid entry.slug")
title = _plain_string(value.get("title"), "entry.title", 500)
doc_ids = value.get("doc_ids")
if (not isinstance(doc_ids, list) or not doc_ids
or any(type(item) is not int or item <= 0 for item in doc_ids)
or len(set(doc_ids)) != len(doc_ids) or str(doc_ids[0]) != doc_id):
raise ValueError("invalid entry.doc_ids")
result = {"slug": slug, "title": title, "doc_ids": list(doc_ids)}
if "part_labels" in value:
labels = value["part_labels"]
if (not isinstance(labels, list) or len(labels) != len(doc_ids)
or any(not isinstance(item, str) or not item.strip() for item in labels)):
raise ValueError("invalid entry.part_labels")
result["part_labels"] = list(labels)
if "chapter_scope" in value:
scope = _plain_string(value["chapter_scope"], "entry.chapter_scope", 20)
if not re.fullmatch(r"\d+(?:\.\d+)*", scope):
raise ValueError("invalid entry.chapter_scope")
result["chapter_scope"] = scope
return result
def validate_resource(resource: object) -> dict:
"""Возвращает нормализованную копию ресурса либо отклоняет его."""
if not isinstance(resource, dict):
raise ValueError("resource must be an object")
allowed = {
"id", "kind", "parser", "url", "output", "entry", "label",
"title", "date", "number",
}
if set(resource) - allowed:
raise ValueError("unknown resource field")
parser = _plain_string(resource.get("parser"), "parser", 40)
if parser not in ALLOWED_PARSERS:
raise ValueError("unknown parser")
kind = _plain_string(resource.get("kind"), "kind", 40)
if kind not in PARSER_KINDS[parser]:
raise ValueError("parser/kind mismatch")
resource_id = _plain_string(resource.get("id"), "id", 160)
if not ID_RE.fullmatch(resource_id):
raise ValueError("invalid id")
url, doc_id = _validate_url(resource.get("url"), parser)
output = _validate_output(resource.get("output"), parser)
result = {
"id": resource_id, "kind": kind, "parser": parser, "url": url,
"output": output,
}
if parser == "consultant-code":
result["entry"] = _validate_entry(resource.get("entry"), doc_id or "")
if output != f"knowledge/kodeksy/{result['entry']['slug']}.md":
raise ValueError("code output/entry mismatch")
elif "entry" in resource:
raise ValueError("entry allowed only for consultant-code")
if parser in {"consultant-act", "vsrf-review"}:
for field in ("title", "date", "number"):
result[field] = _plain_string(resource.get(field), field, 1000)
if not re.fullmatch(r"\d{2}\.\d{2}\.\d{4}", result["date"]):
raise ValueError("invalid date")
try:
datetime.strptime(result["date"], "%d.%m.%Y")
except ValueError as exc:
raise ValueError("invalid date") from exc
elif any(field in resource for field in ("title", "date", "number")):
raise ValueError("act identity allowed only for consultant-act")
if parser == "sudact-plenum":
result["label"] = _plain_string(resource.get("label"), "label", 1000)
elif "label" in resource:
raise ValueError("label allowed only for sudact-plenum")
return deepcopy(result)
def _ensure_not_symlink(root: Path, relative: str) -> None:
if root.is_symlink():
raise ValueError("registry root is symlink")
current = root
for part in PurePosixPath(relative).parts:
current /= part
if current.is_symlink():
raise ValueError("managed path is symlink")
def _read_json(path: Path) -> dict:
if path.is_symlink():
raise ValueError("config is symlink")
try:
if path.stat().st_size > MAX_CONFIG_BYTES:
raise ValueError("config too large")
raw = path.read_bytes()
value = json.loads(raw)
except FileNotFoundError:
return {"schema": 1, "needs": [], "reviews": []}
except (OSError, UnicodeDecodeError, json.JSONDecodeError) as exc:
raise ValueError("invalid corpus needs config") from exc
if (not isinstance(value, dict) or value.get("schema") != 1
or set(value) - {"schema", "needs", "reviews"}
or not isinstance(value.get("needs", []), list)
or not isinstance(value.get("reviews", []), list)):
raise ValueError("invalid corpus needs config")
value.setdefault("needs", [])
value.setdefault("reviews", [])
return value
def _configured_resources(root: Path) -> list[dict]:
path = root / CONFIG
_ensure_not_symlink(root, CONFIG.as_posix())
value = _read_json(path)
resources = []
for need in value["needs"]:
if not isinstance(need, dict) or set(need) != {
"source", "kind", "title", "date", "number", "slug"}:
raise ValueError("invalid corpus need")
slug = _plain_string(need["slug"], "slug", 80)
if not SLUG_RE.fullmatch(slug):
raise ValueError("invalid slug")
resources.append(validate_resource({
"id": f"legal-corpus:act:{slug}",
"kind": need["kind"],
"parser": "consultant-act",
"url": need["source"],
"output": f"knowledge/akty/{slug}.md",
"title": need["title"],
"date": need["date"],
"number": need["number"],
}))
for review in value["reviews"]:
if not isinstance(review, dict) or set(review) != {
"source", "title", "date", "number", "slug"}:
raise ValueError("invalid corpus review")
slug = _plain_string(review["slug"], "slug", 80)
if not SLUG_RE.fullmatch(slug):
raise ValueError("invalid review slug")
resources.append(validate_resource({
"id": f"legal-corpus:review:{slug}", "kind": "review",
"parser": "vsrf-review", "url": review["source"],
"output": f"knowledge/obzory/{slug}.md", "title": review["title"],
"date": review["date"], "number": review["number"],
}))
return resources
def _plenum_resources(root: Path) -> list[dict]:
base = root / "knowledge" / "plenumy"
if not base.exists():
return []
_ensure_not_symlink(root, "knowledge/plenumy")
if not base.is_dir():
raise ValueError("Plenum corpus is not a directory")
result = []
for path in sorted(base.glob("*.md")):
if path.is_symlink():
raise ValueError("Plenum source is symlink")
try:
if path.stat().st_size > MAX_DOCUMENT_BYTES:
raise ValueError("Plenum source too large")
prefix = path.read_text(encoding="utf-8")[:64 * 1024]
except OSError as exc:
raise ValueError("cannot read Plenum source") from exc
front = re.match(r"^---\s*\n(.*?)\n---\s*\n", prefix, re.S)
if not front:
continue
source_match = re.search(
r'(?m)^источник:\s*(?:\"([^\"]+)\"|\'([^\']+)\'|(\S+))\s*$',
front.group(1),
)
heading = re.search(r"(?m)^#\s+(.+?)\s*$", prefix[front.end():])
if not source_match or not heading:
continue
source = next(item for item in source_match.groups() if item is not None)
stem = path.stem
result.append(validate_resource({
"id": f"legal-corpus:plenum:{stem}",
"kind": "plenum",
"parser": "sudact-plenum",
"url": source,
"output": f"knowledge/plenumy/{path.name}",
"label": heading.group(1),
}))
return result
def registry(root: Path | str) -> list[dict]:
"""Собирает стабильный реестр кодексов, Пленумов, каталога и approved needs."""
root = Path(root)
if not root.is_absolute():
root = root.resolve()
resources = []
for entry in KODEKSY:
first = entry["doc_ids"][0]
resources.append(validate_resource({
"id": f"legal-corpus:code:{entry['slug']}",
"kind": "code",
"parser": "consultant-code",
"url": f"https://www.consultant.ru/document/cons_doc_LAW_{first}/",
"output": f"knowledge/kodeksy/{entry['slug']}.md",
"entry": entry,
}))
resources.extend(_plenum_resources(root))
resources.append(validate_resource({
"id": "legal-corpus:plenum-catalog",
"kind": "catalog",
"parser": "sudact-catalog",
"url": SUDACT_PLENUM_CATALOG,
"output": ".agent/maintenance/corpus/sudact-plenum-catalog.json",
}))
resources.extend(_configured_resources(root))
ids, outputs = set(), set()
for resource in resources:
if resource["id"] in ids or resource["output"] in outputs:
raise ValueError("duplicate resource id or output")
ids.add(resource["id"])
outputs.add(resource["output"])
_ensure_not_symlink(root, resource["output"])
return resources
class _ConsultantDocumentParser(HTMLParser):
def __init__(self, container: str = "document__text"):
super().__init__(convert_charrefs=True)
self.container = container
self.canonical: list[str] = []
self.h1_depth = 0
self.h1_parts: list[str] = []
self.container_stack: list[str] = []
self.container_count = 0
self.body_parts: list[str] = []
self.denied = False
self.malformed = False
self.suppressed_depth = 0
def handle_starttag(self, tag, attrs):
tag = tag.lower()
values = {str(key).lower(): value or "" for key, value in attrs}
classes = set(values.get("class", "").split())
if tag == "link" and "canonical" in values.get("rel", "").lower().split():
self.canonical.append(values.get("href", ""))
if tag == "h1":
self.h1_depth += 1
denied_class = any(
part in token.lower() for token in classes for part in DENIED_CLASS_PARTS)
if self.container_stack:
if self.container in classes:
self.container_count += 1
if denied_class:
self.denied = True
if tag in BLOCK_TAGS:
self.body_parts.append("\n")
if tag not in VOID_TAGS:
self.container_stack.append(tag)
if self.suppressed_depth or tag in {"script", "style"}:
self.suppressed_depth += 1
elif self.container in classes:
self.container_count += 1
self.denied = self.denied or denied_class
if tag in VOID_TAGS:
self.malformed = True
else:
self.container_stack.append(tag)
def handle_startendtag(self, tag, attrs):
self.handle_starttag(tag, attrs)
if tag.lower() not in VOID_TAGS:
self.handle_endtag(tag)
def handle_endtag(self, tag):
tag = tag.lower()
if self.h1_depth:
if tag == "h1":
self.h1_depth -= 1
if self.container_stack:
if tag in VOID_TAGS or self.container_stack[-1] != tag:
self.malformed = True
return
if tag in BLOCK_TAGS:
self.body_parts.append("\n")
self.container_stack.pop()
if self.suppressed_depth:
self.suppressed_depth -= 1
def handle_data(self, data):
if self.h1_depth:
self.h1_parts.append(data)
if self.container_stack and not self.suppressed_depth:
self.body_parts.append(data)
def _normal_text(parts: list[str]) -> str:
text = html.unescape("".join(parts)).replace("\r", "\n")
lines = [re.sub(r"[ \t\f\v]+", " ", line).strip() for line in text.splitlines()]
result: list[str] = []
for line in lines:
if line:
result.append(line)
elif result and result[-1] != "":
result.append("")
return "\n".join(result).strip()
def _identity(value: str) -> str:
value = re.sub(r"\s*\(ред\.\s+от\s+\d{2}\.\d{2}\.\d{4}[^)]*\)", "", value,
flags=re.I)
value = value.translate(str.maketrans({"«": '"', "»": '"', "“": '"', "”": '"'}))
return re.sub(r"\s+", " ", value).strip()
def _yaml(value: str) -> str:
return json.dumps(value, ensure_ascii=False)
def _receipt_page(receipt: object, url: str) -> tuple[str, str]:
"""Страница из квитанции: тело привязано к адресу и своему хешу, это UTF-8. Возвращает (текст, revision)."""
if not isinstance(receipt, dict):
raise ValueError("invalid receipt")
body = receipt.get("body")
if not isinstance(body, bytes) or not body or len(body) > MAX_DOCUMENT_BYTES:
raise ValueError("invalid receipt body")
revision = "sha256:" + hashlib.sha256(body).hexdigest()
if receipt.get("source_url") != url or receipt.get("source_revision") != revision:
raise ValueError("receipt is not bound to resource")
try:
return body.decode("utf-8"), revision
except UnicodeDecodeError as exc:
raise ValueError("Consultant page is not UTF-8") from exc
def _toc_html(page: str) -> str | None:
"""Разметка оглавления: все внутри до его закрывающего
."""
depth, start = 0, None
for tag in re.finditer(r"<(/?)div\b[^>]*>", page, re.I):
if start is None:
if not tag.group(1) and TOC_CLASS_RE.search(tag.group(0)):
depth, start = 1, tag.end()
continue
depth += -1 if tag.group(1) else 1
if depth == 0:
return page[start:tag.start()]
if start is not None:
raise ValueError("act table of contents is not closed")
return None
def _toc_parts(page: str, doc_id: str) -> list[dict]:
"""Части акта по оглавлению: адреса по порядку без повторов (TOC_ENTRY_TMPL, как у кодексов). Ребенок
идет вложенным сразу после родителя. [] - на странице нет оглавления."""
toc = _toc_html(page)
if toc is None:
return []
entries = list(re.finditer(TOC_ENTRY_TMPL.format(doc_id=doc_id), toc))
# Запись, которую регулярное выражение не берет, была бы молча пропущенной частью акта.
if not entries or len(entries) != len(re.findall(r" tuple[dict, str, _ConsultantDocumentParser, list[dict]]:
"""Страница акта: привязка квитанции, canonical и шапка (название, дата, номер). Возвращает (ресурс,
revision, разбор страницы, части по оглавлению); части есть, только если у страницы нет document__text."""
resource = validate_resource(resource)
if resource["parser"] != "consultant-act":
raise ValueError("generic candidate requires consultant-act")
page, revision = _receipt_page(receipt, resource["url"])
parser = _ConsultantDocumentParser()
parser.feed(page)
parser.close()
expected_doc_id = CONSULTANT_RE.fullmatch(resource["url"]).group(1)
canonical_ids = [
match.group(1) for value in parser.canonical
if (match := CONSULTANT_RE.fullmatch(value))
]
if len(parser.canonical) != 1 or canonical_ids != [expected_doc_id]:
raise ValueError("canonical document mismatch")
heading = _normal_text(parser.h1_parts)
if (_identity(resource["title"]) not in _identity(heading)
or resource["date"] not in heading
or not re.search(rf"\bN\s*{re.escape(resource['number'])}\b", heading)):
raise ValueError("act identity mismatch")
toc = _toc_parts(page, expected_doc_id) if parser.container_count == 0 else []
if not toc and (parser.container_count != 1 or parser.denied or parser.malformed
or parser.container_stack):
raise ValueError("full document__text is absent")
return resource, revision, parser, toc
def act_parts(resource: object, receipt: object) -> list[dict]:
"""Части акта по оглавлению его страницы: [{url, label, parent}]. [] - весь текст лежит на странице."""
return _act_page(resource, receipt)[3]
def act_part_section(part: dict, receipt: object) -> str:
"""Раздел акта по странице части: «## метка оглавления» и текст (extract_article_text, как у кодексов).
Canonical равен адресу части, запрещенного класса внутри document-page__content нет. Текст пуст только у
родителя вложенного списка, у которого на странице есть заголовок."""
page, _ = _receipt_page(receipt, part["url"])
parser = _ConsultantDocumentParser("document-page__content")
parser.feed(page)
parser.close()
if parser.canonical != [part["url"]]:
raise ValueError("act part canonical mismatch")
if (parser.container_count != 1 or parser.denied or parser.malformed
or parser.container_stack):
raise ValueError("full document-page__content is absent")
text = extract_article_text(receipt["body"])
if not re.search(r"[0-9A-Za-zА-Яа-я]", text):
if not (part["parent"] and _normal_text(parser.h1_parts)):
raise ValueError("act part text is empty")
text = ""
return f"## {part['label']}" + (f"\n\n{text}" if text else "")
def _toc_text(toc: list[dict], parts: object, act_url: str, act_revision: str) -> tuple[str, str]:
"""Текст акта из частей и общая revision страниц. Части - ровно по оглавлению: без пропусков, повторов,
чужих страниц и перестановок."""
if parts is None:
raise ValueError("act parts are required")
parts = list(parts)
wanted = [part["url"] for part in toc]
got = [item.get("source_url") if isinstance(item, dict) else None for item in parts]
if got != wanted:
if set(got) - set(wanted):
raise ValueError("act part is foreign")
if set(wanted) - set(got):
raise ValueError("act part is missing")
raise ValueError("act parts are out of order or repeated")
text = "\n\n".join(act_part_section(part, item) for part, item in zip(toc, parts))
pages = [(act_url, act_revision), *((part["url"], item["source_revision"])
for part, item in zip(toc, parts))]
digest = hashlib.sha256("\n".join(f"{url} {revision}" for url, revision in pages).encode()).hexdigest()
return text, "sha256:" + digest
def build_generic_candidate(resource: object, receipt: object, now: datetime,
parts: list[dict] | None = None) -> tuple[str, bytes]:
"""Строит полный verified Consultant act, не принимая snippets/чужой акт. Акт без document__text, но с
оглавлением собирается по частям: parts - квитанции частей в порядке оглавления (act_parts)."""
resource, revision, parser, toc = _act_page(resource, receipt)
if not isinstance(now, datetime):
raise ValueError("invalid timestamp")
if toc:
text, revision = _toc_text(toc, parts, resource["url"], revision)
elif parts:
raise ValueError("act part is foreign")
else:
text = _normal_text(parser.body_parts)
if not re.search(r"[0-9A-Za-zА-Яа-я]", text):
raise ValueError("full document__text is empty")
return resource["output"], _markdown(
[("источник", resource["url"]), ("название", resource["title"]), ("дата_акта", resource["date"]),
("номер_акта", resource["number"]),
("canonical_doc_id", CONSULTANT_RE.fullmatch(resource["url"]).group(1)),
("дата_выгрузки", now.strftime("%d.%m.%Y")), ("sha256_источника", revision)],
f"# {resource['title']}\n\n{text}\n")
def _review_pdf_text(body: bytes) -> str:
"""Текст PDF обзора через PDFium - основной путь чтения PDF в доме: pypdf рвет слова пробелами
(«декаб ря», «граж данину»). Номер страницы в начале страницы снимается: иначе граф права принял бы его
за пункт обзора. Строка без пробела или тире в конце - конец абзаца (так PDFium отдает текст из Word)."""
import pypdfium2 as pdfium
pdf = pdfium.PdfDocument(body)
try:
pages = []
for page in pdf:
lines = page.get_textpage().get_text_range().replace("\r\n", "\n").rstrip("\n").split("\n")
if lines and lines[0].strip().isdigit():
lines = lines[1:]
pages.append("\n".join(lines))
finally:
pdf.close()
text = "\n".join(pages).replace("", "-").replace("", "–")
return _normal_text([re.sub(r"(? str:
"""Шапка PDF набрана прописными, к слову приклеена сноска («ФЕДЕРАЦИИ1»)."""
return _identity(re.sub(r"(?<=[^\W\d_])\d{1,2}\b", "", value)).casefold()
def build_review_candidate(resource: object, receipt: object,
now: datetime) -> tuple[str, bytes]:
"""Строит полный обзор ВС РФ из PDF vsrf.ru: шапка с названием, номером и датой утверждения -
этого обзора, текст полный. HTML-страницы (заглушки, капча) не принимаются."""
resource = validate_resource(resource)
if resource["parser"] != "vsrf-review":
raise ValueError("review candidate requires vsrf-review")
if not isinstance(receipt, dict):
raise ValueError("invalid receipt")
body = receipt.get("body")
if not isinstance(body, bytes) or not body or len(body) > MAX_DOCUMENT_BYTES:
raise ValueError("invalid receipt body")
revision = "sha256:" + hashlib.sha256(body).hexdigest()
if receipt.get("source_url") != resource["url"] or receipt.get("source_revision") != revision:
raise ValueError("receipt is not bound to resource")
if not isinstance(now, datetime):
raise ValueError("invalid timestamp")
checked = receipt.get("checked_at")
if not isinstance(checked, str):
raise ValueError("receipt checked_at missing")
if not body.startswith(b"%PDF-"):
raise ValueError("review source is not PDF")
try:
text = _review_pdf_text(body)
except Exception as exc: # noqa: BLE001 - любой сбой PDFium = документ не принят
raise ValueError("review PDF extraction failed") from exc
heading = _review_identity(text[:3000])
day, month, year = resource["date"].split(".")
date_words = f"{int(day)} {MONTHS[int(month) - 1]} {year}"
if (_review_identity(resource["title"]) not in heading
or (resource["date"] not in heading and date_words not in heading)
or resource["number"].casefold() not in heading):
raise ValueError("review identity mismatch")
if len(text) < 200:
raise ValueError("review text is incomplete")
return resource["output"], _markdown(
[("источник", resource["url"]), ("название", resource["title"]), ("дата_акта", resource["date"]),
("номер_акта", resource["number"]), ("дата_выгрузки", now.strftime("%d.%m.%Y")),
("дата_проверки", checked), ("sha256_источника", revision)],
f"# {resource['title']}\n\n{text}\n")
def _markdown(fields: list[tuple[str, str]], body: str) -> bytes:
"""Frontmatter + тело. sha256 - хеш тела после frontmatter: его сверяет граф права
(legal_graph._read_source); хеш скачанного источника - отдельным полем."""
lines = ["---", *(f"{key}: {_yaml(value)}" for key, value in fields),
f"sha256: {_yaml(hashlib.sha256(body.encode('utf-8')).hexdigest())}", "---", ""]
return ("\n".join(lines) + body).encode("utf-8")
def _write_config(root: Path, value: dict) -> None:
relative = CONFIG.as_posix()
_ensure_not_symlink(root, relative)
path = root / CONFIG
path.parent.mkdir(parents=True, exist_ok=True)
temp = path.with_name(f".{path.name}.{os.getpid()}.tmp")
try:
with temp.open("xb") as stream:
stream.write((json.dumps(value, ensure_ascii=False, indent=2) + "\n").encode())
stream.flush()
os.fsync(stream.fileno())
os.replace(temp, path)
finally:
temp.unlink(missing_ok=True)
def add_need(root: Path | str, *, source: str, kind: str, title: str, date: str,
number: str, slug: str) -> dict:
root = Path(root).resolve()
candidate = validate_resource({
"id": f"legal-corpus:act:{slug}",
"kind": kind,
"parser": "consultant-act",
"url": source,
"output": f"knowledge/akty/{slug}.md",
"title": title,
"date": date,
"number": number,
})
path = root / CONFIG
value = _read_json(path)
need = {
"source": candidate["url"], "kind": candidate["kind"],
"title": candidate["title"], "date": candidate["date"],
"number": candidate["number"], "slug": slug,
}
if any(item.get("slug") == slug or item.get("source") == source
for item in value["needs"] if isinstance(item, dict)):
raise ValueError("need already registered")
value["needs"].append(need)
_write_config(root, value)
return candidate
def add_review(root: Path | str, *, source: str, title: str, date: str,
number: str, slug: str) -> dict:
root = Path(root).resolve()
candidate = validate_resource({
"id": f"legal-corpus:review:{slug}", "kind": "review", "parser": "vsrf-review",
"url": source, "output": f"knowledge/obzory/{slug}.md", "title": title,
"date": date, "number": number,
})
path = root / CONFIG
value = _read_json(path)
item = {"source": candidate["url"], "title": candidate["title"],
"date": candidate["date"], "number": candidate["number"], "slug": slug}
if any(isinstance(row, dict) and (row.get("slug") == slug or row.get("source") == source)
for row in value["reviews"]):
raise ValueError("review already registered")
value["reviews"].append(item)
_write_config(root, value)
return candidate
def main(argv: list[str] | None = None) -> int:
parser = argparse.ArgumentParser()
parser.add_argument("--root", type=Path, default=Path(__file__).resolve().parents[1])
parser.add_argument("--add", action="store_true")
parser.add_argument("--source")
parser.add_argument("--kind", choices=("federal-law", "bylaw", "review"))
parser.add_argument("--title")
parser.add_argument("--date")
parser.add_argument("--number")
parser.add_argument("--slug")
args = parser.parse_args(argv)
if not args.add:
parser.error("поддерживается только явный --add")
fields = (args.source, args.kind, args.title, args.date, args.number, args.slug)
if any(value is None for value in fields):
parser.error("--add требует source/kind/title/date/number/slug")
try:
if args.kind == "review": # обзор ВС РФ: PDF vsrf.ru, разборщик vsrf-review
resource = add_review(args.root, source=args.source, title=args.title,
date=args.date, number=args.number, slug=args.slug)
else:
resource = add_need(
args.root, source=args.source, kind=args.kind, title=args.title,
date=args.date, number=args.number, slug=args.slug,
)
except ValueError as exc:
print(str(exc), file=sys.stderr)
return 2
print(json.dumps(resource, ensure_ascii=False, sort_keys=True))
return 0
if __name__ == "__main__":
raise SystemExit(main())