Coverage for src/python/ensembl/vep/config/report.py: 100%
24 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"""The formats a report can be rendered in, and which of them a request can combine.
16One vocabulary for every reporting command, so ``--output_format`` means the same thing to ``explain``,
17``catalogue``, ``corpus`` and ``bench``. A report is a table unless ``--graph`` asks for a diagram,
18and a format belongs to one kind or the other: resolution refuses the combinations that cannot be
19drawn rather than quietly rendering something else.
20"""
22from __future__ import annotations
24__all__ = [
25 "GRAPH_FORMATS",
26 "REPORT_FORMATS",
27 "TABLE_FORMATS",
28 "resolve_report_format",
29]
31TABLE_FORMATS = ("text", "markdown", "confluence", "html")
32"""Formats that render rows."""
34GRAPH_FORMATS = ("svg", "dot", "mermaid")
35"""Formats that render a diagram. ``svg`` is wrapped in a navigable HTML document."""
37REPORT_FORMATS = TABLE_FORMATS + GRAPH_FORMATS
38"""Every format ``--format`` accepts, whichever kind the command can render."""
41def resolve_report_format(
42 format_name: str | None,
43 *,
44 graph: bool,
45 table_default: str,
46 graph_default: str = "svg",
47 graph_formats: tuple[str, ...] = GRAPH_FORMATS,
48) -> str:
49 """Return the format to render in, or say why the request cannot be met.
51 Args:
52 format_name (str | None): The requested format, or None to take the default for the kind
53 of report asked for.
54 graph (bool): Whether a diagram was asked for.
55 table_default (str): The format a table takes when none was named.
56 graph_default (str): The format a diagram takes when none was named.
57 graph_formats (tuple[str, ...]): The diagram formats this report can draw; empty for a
58 report that has no diagram form.
60 Returns:
61 str: One of ``REPORT_FORMATS``.
63 Raises:
64 ValueError: If a diagram was asked for in a table format or of a report that has none, or
65 if a diagram format was named without asking for a diagram.
66 """
68 if graph and not graph_formats:
69 raise ValueError("this report has no diagram form; drop --graph")
70 if format_name is None:
71 return graph_default if graph else table_default
73 if graph:
74 if format_name not in graph_formats:
75 drawable = ", ".join(graph_formats)
76 raise ValueError(f"--graph draws {drawable}, not {format_name!r}")
77 return format_name
79 if format_name in GRAPH_FORMATS:
80 raise ValueError(f"{format_name!r} draws a diagram; pass --graph to ask for one")
81 if format_name not in TABLE_FORMATS:
82 known = ", ".join(REPORT_FORMATS)
83 raise ValueError(f"unknown format {format_name!r}; expected one of {known}")
84 return format_name