Coverage for src/python/ensembl/vep/runner/catalogue/common.py: 86%
37 statements
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-18 14:41 +0000
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-18 14:41 +0000
1# See the NOTICE file distributed with this work for additional information
2# regarding copyright ownership.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14"""Shared helpers for the catalogue runners."""
16from __future__ import annotations
18import copy
19import difflib
20import logging
21from pathlib import Path
22from typing import Any
24from ensembl.vep.annotators.consequence.rules.catalogue import catalogue_path, dump_catalogue, load_catalogue
25from ensembl.vep.annotators.consequence.rules.validation import ValidationReport
26from ensembl.vep.config import Config
28__all__ = ["destination_for", "log_diff", "log_validation_report", "read_payload", "write_payload"]
30logger = logging.getLogger(__name__)
33def read_payload(config: Config) -> tuple[dict[str, Any], dict[str, Any]]:
34 """Return the catalogue payload and an untouched copy of it.
36 The copy is the baseline a change is validated against, so a pre-existing finding is not
37 reported as one this change introduced.
38 """
39 payload = load_catalogue(config.catalogue.catalogue_file)
40 return copy.deepcopy(payload), payload
43def destination_for(config: Config) -> Path:
44 """Return where a change should be written.
46 Raises:
47 ValueError: If neither a path nor permission to overwrite the packaged catalogue was given.
48 An installed ``vep`` must not silently rewrite its own wheel.
49 """
50 if config.catalogue.catalogue_file is not None:
51 return config.catalogue.catalogue_file
52 if config.catalogue_write.catalogue_overwrite: 52 ↛ 53line 52 didn't jump to line 53 because the condition on line 52 was never true
53 return catalogue_path()
54 raise ValueError(
55 "refusing to write: give --catalogue PATH, or --catalogue_overwrite to replace the "
56 "packaged catalogue in place"
57 )
60def log_validation_report(report: ValidationReport) -> None:
61 """Log a validation report, errors first."""
62 for error in report.errors: 62 ↛ 63line 62 didn't jump to line 63 because the loop on line 62 never started
63 logger.error("ERROR: %s", error)
64 for finding in report.unreachable_rules: 64 ↛ 65line 64 didn't jump to line 65 because the loop on line 64 never started
65 logger.error("ERROR: unreachable rule introduced by this change: %s", finding.term)
66 for warning in report.warnings:
67 logger.warning("warning: %s", warning)
68 if not report.has_errors: 68 ↛ exitline 68 didn't return from function 'log_validation_report' because the condition on line 68 was always true
69 logger.info("validation passed")
72def log_diff(original: dict[str, Any], updated: dict[str, Any], destination: Path) -> list[str]:
73 """Log a unified diff of a catalogue change, and return its lines."""
74 lines = list(
75 difflib.unified_diff(
76 dump_catalogue(original).splitlines(keepends=True),
77 dump_catalogue(updated).splitlines(keepends=True),
78 fromfile=f"a/{destination.name}",
79 tofile=f"b/{destination.name}",
80 )
81 )
82 for line in lines:
83 logger.info("%s", line.rstrip())
84 return lines
87def write_payload(payload: dict[str, Any], destination: Path) -> None:
88 """Write a catalogue payload, in the checked-in formatting."""
89 destination.write_text(dump_catalogue(payload), encoding="utf-8")
90 logger.info("wrote %s", destination)