Coverage for lice2/config.py: 100%
19 statements
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-13 11:13 +0000
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-13 11:13 +0000
1"""Setup configuration for lice2."""
3from rich.console import Console
4from rich.panel import Panel
5from simple_toml_settings import TOMLSettings
7from lice2.constants import LICENSES
10class Settings(TOMLSettings):
11 """Settings for lice2."""
13 default_license: str = "bsd3"
14 organization: str = ""
15 legacy: bool = False
16 clipboard: bool = False
19def check_default_license() -> str:
20 """Check the default license is in the list of available licenses.
22 Return the default license if it is in the list, otherwise return "bsd3".
23 This is only used to ensure that the configuration file does not have an
24 invalid default license hence crashing the application, and will be called
25 automatically by 'Typer'
26 """
27 if settings.default_license not in LICENSES:
28 console = Console(width=80)
29 error_text = (
30 f"[red]Invalid default license '[b]{settings.default_license}"
31 "'[/b] in the configuration file, falling back to '[b]bsd3[/b]', "
32 "unless specified otherwise on the command line.\n\nCheck that [b]"
33 f"{settings.get_settings_folder()/settings.settings_file_name}[/b]'"
34 " has a valid value for [b]'default_license'[/b]."
35 )
36 panel = Panel(
37 error_text,
38 title="[b]Error[/b]",
39 title_align="left",
40 expand=False,
41 style="red",
42 )
44 console.print()
45 console.print(panel)
46 settings.default_license = "bsd3"
47 return settings.default_license
50settings = Settings.get_instance(
51 "lice",
52 xdg_config=True,
53 auto_create=False,
54 allow_missing_file=True,
55 schema_version="1",
56)