Coverage for src/python/ensembl/vep/config/parser/common.py: 100%
32 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"""Helpers shared by the command-line, environment and file readers."""
16from __future__ import annotations
18from ensembl.vep.config.option_spec import OptionSpec, section_options
19from ensembl.vep.config.sections.base import ConfigSection
21__all__ = ["flag_for", "option_spellings", "resolve_option", "resolve_option_key"]
24def flag_for(field_name: str, spec: OptionSpec) -> str:
25 """Return the long flag spelling for one option, without the leading dashes.
27 Flags keep underscores, so ``--input_file`` matches legacy VEP and a legacy config file needs
28 no key translation.
29 """
30 return spec.flag or field_name
33def option_spellings(field_name: str, spec: OptionSpec) -> tuple[str, ...]:
34 """Return every long spelling that sets one option: its field name, flag and synonyms."""
36 spellings = [field_name, flag_for(field_name, spec)]
37 spellings.extend(spec.synonyms)
38 unique: list[str] = []
39 for spelling in spellings:
40 if spelling not in unique:
41 unique.append(spelling)
42 return tuple(unique)
45def _normalised(key: str) -> str:
46 """Return a flat key as a field name: no dashes, underscores throughout."""
48 return key.strip().lstrip("-").replace("-", "_")
51def resolve_option_key(key: str, section_types: tuple[type[ConfigSection], ...]) -> tuple[str, str] | None:
52 """Resolve a flat option key to the section and field that declare it.
54 Args:
55 key (str): The key as written, with or without leading dashes.
56 section_types (tuple[type[ConfigSection], ...]): The sections in play.
58 Returns:
59 tuple[str, str] | None: (section name, field name), or None if no section declares it.
60 """
61 resolved = resolve_option(key, None, section_types)
62 if resolved is None:
63 return None
64 section_name, field_name, _value = resolved
65 return section_name, field_name
68def resolve_option(
69 key: str, value: object, section_types: tuple[type[ConfigSection], ...]
70) -> tuple[str, str, object] | None:
71 """Resolve a flat key and its value to the section, field and value they set.
73 A key naming one of an option's constants (``--vcf`` for ``--output_format vcf``) sets that
74 constant, whatever value came with it: the flag carries the value, so a config file writing
75 ``vcf 1`` means the same as ``vcf``.
77 Args:
78 key (str): The key as written, with or without leading dashes.
79 value (object): The value given with it, ignored for a constant flag.
80 section_types (tuple[type[ConfigSection], ...]): The sections in play.
82 Returns:
83 tuple[str, str, object] | None: (section name, field name, value), or None if no section
84 declares the key.
85 """
86 normalised = _normalised(key)
87 for section_type in section_types:
88 for field_name, (_field, spec) in section_options(section_type).items():
89 if normalised in option_spellings(field_name, spec):
90 return section_type.section_name, field_name, value
91 for constant_flag, constant in spec.constants.items():
92 if normalised == _normalised(constant_flag):
93 return section_type.section_name, field_name, constant
94 return None