From 5d0f37f96c88ea1718aadf4dc893d200d7689d74 Mon Sep 17 00:00:00 2001 From: Moul <moul@moul.re> Date: Wed, 6 Apr 2022 20:06:49 +0200 Subject: [PATCH] [ref] #426: Create G1MonetaryLicense class out of f() --- silkaj/license.py | 79 ++++++++++++++++++++++--------------------- tests/test_license.py | 13 +++---- 2 files changed, 48 insertions(+), 44 deletions(-) diff --git a/silkaj/license.py b/silkaj/license.py index 918bca71..4110a617 100644 --- a/silkaj/license.py +++ b/silkaj/license.py @@ -26,53 +26,56 @@ def license_approval(currency: str) -> None: if click.confirm( "You will be asked to approve Ğ1 license. Would you like to display it?" ): - display_license() + g1ml = G1MonetaryLicense() + g1ml.display_license() click.confirm("Do you approve Ğ1 license?", abort=True) @click.command("license", help="Display Ğ1 monetary license") def license_command() -> None: - display_license() + g1ml = G1MonetaryLicense() + g1ml.display_license() -def display_license() -> None: - """ - Determine available languages - Ask to select a language code - Display license in the terminal - """ - languages_codes = available_languages() - selected_language_code = language_prompt(languages_codes) - path = license_path(selected_language_code) - with open(path) as license: - click.echo_via_pager(license.read()) +class G1MonetaryLicense: + def __init__(self): + self.licenses_dir_path = gml.__path__.__dict__["_path"][0] # type: ignore # mypy issue #1422 + self._available_languages() + def display_license(self) -> None: + """ + Determine available languages + Ask to select a language code + Display license in the terminal + """ + selected_language_code = self.language_prompt() + license_path = self.get_license_path(selected_language_code) + with open(license_path) as license: + click.echo_via_pager(license.read()) -def language_prompt(languages_choices: List) -> str: - return click.prompt( - f"In which language would you like to display the Ğ1 monetary license?", - type=click.Choice(languages_choices), - show_choices=True, - show_default=True, - default="en", - ) + def language_prompt(self) -> str: + return click.prompt( + f"In which language would you like to display Ğ1 monetary license?", + type=click.Choice(self.languages_codes), + show_choices=True, + show_default=True, + default="en", + ) + def _available_languages(self) -> None: + """ + Handle long language codes ie: 'fr-FR' + """ + self.languages_codes = [] + licenses_path = sorted(Path(self.licenses_dir_path).glob(self.file_name("*"))) + for license_path in licenses_path: + language_code = license_path.stem[-2:] + if language_code.isupper(): + language_code = license_path.stem[-5:] + self.languages_codes.append(language_code) -def available_languages() -> List: - """ - Handle long language codes ie: 'fr-FR' - """ - languages_codes = [] - licenses_dir_path = gml.__path__.__dict__["_path"][0] # type: ignore # mypy issue #1422 - licenses_path = sorted(Path(licenses_dir_path).glob("g1_monetary_license_*.rst")) - for license_path in licenses_path: - language_code = license_path.stem[-2:] - if language_code.isupper(): - language_code = license_path.stem[-5:] - languages_codes.append(language_code) - return languages_codes + def get_license_path(self, language_code: str) -> Path: + return Path(self.licenses_dir_path, self.file_name(language_code)) - -def license_path(lang: str) -> Path: - path = gml.__path__.__dict__["_path"][0] # type: ignore # mypy issue #1422 - return Path(path, f"g1_monetary_license_{lang}.rst") + def file_name(self, language_code: str) -> str: + return f"g1_monetary_license_{language_code}.rst" diff --git a/tests/test_license.py b/tests/test_license.py index a4c7a4d9..e7637e8c 100644 --- a/tests/test_license.py +++ b/tests/test_license.py @@ -38,7 +38,7 @@ def test_license_approval_g1_test(capsys): ], ) @patch("click.confirm") -@patch.object(license, "display_license") +@patch.object(license.G1MonetaryLicense, "display_license") def test_license_approval_g1(mock_display_license, mock_confirm, display, approve): # https://stackoverflow.com/a/62939130 mock_confirm.return_value = display @@ -64,16 +64,17 @@ def test_language_prompt(language, license_sample): assert result.exit_code == SUCCESS_EXIT_STATUS -def test_available_languages_and_license_path(): - languages_codes = license.available_languages() - for language_code in languages_codes: - assert license.license_path(language_code).is_file() +def test_available_languages_and_get_license_path(): + g1ml = license.G1MonetaryLicense() + for language_code in g1ml.languages_codes: + assert g1ml.get_license_path(language_code).is_file() def test_long_language_code_handling(): language_code = "fr-FR" content = "Licence monétaire Ğ1" - license_path = license.license_path(language_code) + g1ml = license.G1MonetaryLicense() + license_path = g1ml.get_license_path(language_code) runner = CliRunner() with runner.isolated_filesystem(): with open(license_path, "w") as f: -- GitLab