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

stfc / janus-core / 8830718073

25 Apr 2024 10:06AM UTC coverage: 95.722% (+0.2%) from 95.506%
8830718073

push

github

web-flow
Refactor code (#120)

* Refactor CLI

* Rename base CLI module

---------

Co-authored-by: ElliottKasoar <ElliottKasoar@users.noreply.github.com>

192 of 197 new or added lines in 13 files covered. (97.46%)

716 of 748 relevant lines covered (95.72%)

3.83 hits per line

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

96.43
/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_config import conf_callback_factory, yaml_loader
4✔
9
import yaml
4✔
10

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

14

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

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

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

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

43

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

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

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

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

65

66
yaml_converter_callback = conf_callback_factory(yaml_converter_loader)
4✔
67

68

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

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

90

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

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