Coverage for lice2/config.py: 100%

1"""Setup configuration for lice2."""

2

3 from rich.console import Console

4 from rich.panel import Panel

5 from simple_toml_settings import TOMLSettings

6

7 from lice2.constants import LICENSES

8

9

10 class Settings(TOMLSettings):

11 """Settings for lice2."""

12

13 default_license: str = "bsd3"

14 organization: str = ""

15 legacy: bool = False

16 clipboard: bool = False

17

18

19 def check_default_license() -> str:

20 """Check the default license is in the list of available licenses.

21

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 )

43

44 console.print()

45 console.print(panel)

46 settings.default_license = "bsd3"

47 return settings.default_license

48

49

50 settings = Settings.get_instance(

51 "lice",

52 xdg_config=True,

53 auto_create=False,

54 allow_missing_file=True,

55 schema_version="1",

56)