Coverage for silkaj/g1_monetary_license.py: 100%

36 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-04-17 17:03 +0000

1# Copyright 2016-2024 Maël Azimi <m.a@moul.re> 

2# 

3# Silkaj is free software: you can redistribute it and/or modify 

4# it under the terms of the GNU Affero General Public License as published by 

5# the Free Software Foundation, either version 3 of the License, or 

6# (at your option) any later version. 

7# 

8# Silkaj is distributed in the hope that it will be useful, 

9# but WITHOUT ANY WARRANTY; without even the implied warranty of 

10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

11# GNU Affero General Public License for more details. 

12# 

13# You should have received a copy of the GNU Affero General Public License 

14# along with Silkaj. If not, see <https://www.gnu.org/licenses/>. 

15 

16from pathlib import Path 

17 

18import rich_click as click 

19 

20import g1_monetary_license as gml 

21 

22 

23def license_approval(currency: str) -> None: 

24 if currency != "g1": 

25 return 

26 if click.confirm( 

27 "You will be asked to approve Ğ1 license. Would you like to display it?", 

28 ): 

29 g1ml = G1MonetaryLicense() 

30 g1ml.display_license() 

31 click.confirm("Do you approve Ğ1 license?", abort=True) 

32 

33 

34@click.command("license", help="Display Ğ1 monetary license") 

35def license_command() -> None: 

36 g1ml = G1MonetaryLicense() 

37 g1ml.display_license() 

38 

39 

40class G1MonetaryLicense: 

41 def __init__(self): 

42 self.licenses_dir_path = gml.__path__.__dict__["_path"][0] 

43 self._available_languages() 

44 

45 def display_license(self) -> None: 

46 """ 

47 Determine available languages 

48 Ask to select a language code 

49 Display license in the terminal 

50 """ 

51 selected_language_code = self.language_prompt() 

52 license_path = self.get_license_path(selected_language_code) 

53 click.echo_via_pager(license_path.read_text(encoding="utf-8")) 

54 

55 def language_prompt(self) -> str: 

56 return click.prompt( 

57 "In which language would you like to display Ğ1 monetary license?", 

58 type=click.Choice(self.languages_codes), 

59 show_choices=True, 

60 show_default=True, 

61 default="en", 

62 ) 

63 

64 def _available_languages(self) -> None: 

65 """ 

66 Handle long language codes ie: 'fr-FR' 

67 """ 

68 self.languages_codes = [] 

69 licenses_path = sorted(Path(self.licenses_dir_path).glob(file_name("*"))) 

70 for license_path in licenses_path: 

71 language_code = license_path.stem[-2:] 

72 if language_code.isupper(): 

73 language_code = license_path.stem[-5:] 

74 self.languages_codes.append(language_code) 

75 

76 def get_license_path(self, language_code: str) -> Path: 

77 return Path(self.licenses_dir_path, file_name(language_code)) 

78 

79 

80def file_name(language_code: str) -> str: 

81 return f"g1_monetary_license_{language_code}.rst"