• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

stfc / janus-core / 9305151760

30 May 2024 03:26PM UTC coverage: 93.607% (+0.3%) from 93.312%
9305151760

Pull #168

github

web-flow
Update janus_core/calculations/single_point.py

Co-authored-by: Alin Marin Elena <alin@elena.re>
Pull Request #168: Adjust file handling to be a mixin

53 of 54 new or added lines in 4 files covered. (98.15%)

17 existing lines in 8 files now uncovered.

1142 of 1220 relevant lines covered (93.61%)

3.74 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.97
/janus_core/cli/utils.py
1
"""Utility functions for CLI."""
2

3
import datetime
4✔
4
import logging
4✔
5
from pathlib import Path
4✔
6
from typing import Any
4✔
7

8
from typer import Context
4✔
9
from typer_config import conf_callback_factory, yaml_loader
4✔
10
import yaml
4✔
11

12
from janus_core.cli.types import TyperDict
4✔
13
from janus_core.helpers.utils import dict_remove_hyphens
4✔
14

15

16
def parse_typer_dicts(typer_dicts: list[TyperDict]) -> list[dict]:
4✔
17
    """
18
    Convert list of TyperDict objects to list of dictionaries.
19

20
    Parameters
21
    ----------
22
    typer_dicts : list[TyperDict]
23
        List of TyperDict objects to convert.
24

25
    Returns
26
    -------
27
    list[dict]
28
        List of converted dictionaries.
29

30
    Raises
31
    ------
32
    ValueError
33
        If items in list are not converted to dicts.
34
    """
35
    for i, typer_dict in enumerate(typer_dicts):
4✔
36
        typer_dicts[i] = typer_dict.value if typer_dict else {}
4✔
37
        if not isinstance(typer_dicts[i], dict):
4✔
UNCOV
38
            raise ValueError(
×
39
                f"""{typer_dicts[i]} must be passed as a dictionary wrapped in quotes.\
40
 For example, "{{'key' : value}}" """
41
            )
42
    return typer_dicts
4✔
43

44

45
def yaml_converter_loader(config_file: str) -> dict[str, Any]:
4✔
46
    """
47
    Load yaml configuration and replace hyphens with underscores.
48

49
    Parameters
50
    ----------
51
    config_file : str
52
        Yaml configuration file to read.
53

54
    Returns
55
    -------
56
    dict[str, Any]
57
        Dictionary with loaded configuration.
58
    """
59
    if not config_file:
4✔
60
        return {}
4✔
61

62
    config = yaml_loader(config_file)
4✔
63
    # Replace all "-"" with "_" in conf
64
    return dict_remove_hyphens(config)
4✔
65

66

67
yaml_converter_callback = conf_callback_factory(yaml_converter_loader)
4✔
68

69

70
def start_summary(*, command: str, summary: Path, inputs: dict) -> None:
4✔
71
    """
72
    Write initial summary contents.
73

74
    Parameters
75
    ----------
76
    command : str
77
        Name of CLI command being used.
78
    summary : Path
79
        Path to summary file being saved.
80
    inputs : dict
81
        Inputs to CLI command to save.
82
    """
83
    save_info = {
4✔
84
        "command": f"janus {command}",
85
        "start_time": datetime.datetime.now().strftime("%d/%m/%Y, %H:%M:%S"),
86
        "inputs": inputs,
87
    }
88
    with open(summary, "w", encoding="utf8") as outfile:
4✔
89
        yaml.dump(save_info, outfile, default_flow_style=False)
4✔
90

91

92
def end_summary(summary: Path) -> None:
4✔
93
    """
94
    Write final time to summary and close.
95

96
    Parameters
97
    ----------
98
    summary : Path
99
        Path to summary file being saved.
100
    """
101
    with open(summary, "a", encoding="utf8") as outfile:
4✔
102
        yaml.dump(
4✔
103
            {"end_time": datetime.datetime.now().strftime("%d/%m/%Y, %H:%M:%S")},
104
            outfile,
105
            default_flow_style=False,
106
        )
107
    logging.shutdown()
4✔
108

109

110
def check_config(ctx: Context) -> None:
4✔
111
    """
112
    Check options in configuration file are valid options for CLI command.
113

114
    Parameters
115
    ----------
116
    ctx : Context
117
        Typer (Click) Context within command.
118
    """
119
    # Compare options from config file (default_map) to function definition (params)
120
    for option in ctx.default_map:
4✔
121
        # Check options individually so can inform user of specific issue
122
        if option not in ctx.params:
4✔
123
            raise ValueError(f"'{option}' in configuration file is not a valid option")
4✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc