Skip to content
Snippets Groups Projects
Commit e04d6bed authored by Moul's avatar Moul
Browse files

[ref] #197, #221, #308, #328, #392: Refactor license cmd

Check if it is running on an headless system
Define new `--g1-license-web/-w` general option

Define language prompt f() based on the available languages
(keys of the dicts), and use click.prompt()

Write tests around `license` cmd
parent 29ff5657
No related branches found
No related tags found
No related merge requests found
...@@ -97,6 +97,13 @@ from silkaj.constants import ( ...@@ -97,6 +97,13 @@ from silkaj.constants import (
help="By-pass licence, confirmation. \ help="By-pass licence, confirmation. \
Do not send the document, but display it instead", Do not send the document, but display it instead",
) )
@option(
"--g1-license-web",
"-w",
is_flag=True,
help="Display Ğ1 monetary license in a web browser if not running on a headless system. \
Defaults to terminal display.",
)
@pass_context @pass_context
def cli( def cli(
ctx, ctx,
...@@ -110,6 +117,7 @@ def cli( ...@@ -110,6 +117,7 @@ def cli(
auth_wif, auth_wif,
display, display,
dry_run, dry_run,
g1_license_web,
): ):
if display and dry_run: if display and dry_run:
sys.exit("ERROR: display and dry-run options can not be used together") sys.exit("ERROR: display and dry-run options can not be used together")
...@@ -126,6 +134,7 @@ def cli( ...@@ -126,6 +134,7 @@ def cli(
ctx.obj["AUTH_WIF"] = auth_wif ctx.obj["AUTH_WIF"] = auth_wif
ctx.obj["DISPLAY_DOCUMENT"] = display ctx.obj["DISPLAY_DOCUMENT"] = display
ctx.obj["DRY_RUN"] = dry_run ctx.obj["DRY_RUN"] = dry_run
ctx.obj["G1_LICENSE_WEB"] = g1_license_web
cli.add_command(argos_info) cli.add_command(argos_info)
......
...@@ -17,9 +17,20 @@ along with Silkaj. If not, see <https://www.gnu.org/licenses/>. ...@@ -17,9 +17,20 @@ along with Silkaj. If not, see <https://www.gnu.org/licenses/>.
import click import click
import webbrowser import webbrowser
from pathlib import Path
import g1_monetary_license as gml
def license_approval(currency):
languages = ["es", "en", "eo", "fr", "pt"]
licenses_urls = {
"en": "https://duniter.org/en/wiki/g1-license/",
"fr": "https://duniter.org/fr/wiki/licence-g1/",
}
def license_approval(currency: str) -> None:
if currency != "g1": if currency != "g1":
return return
if click.confirm( if click.confirm(
...@@ -29,16 +40,46 @@ def license_approval(currency): ...@@ -29,16 +40,46 @@ def license_approval(currency):
click.confirm("Do you approve Ğ1 license?", abort=True) click.confirm("Do you approve Ğ1 license?", abort=True)
@click.command("license", help="Display Ğ1 license") @click.command("license", help="Display Ğ1 monetary license")
def license_command(): def license_command() -> None:
display_license() display_license()
def display_license(): @click.pass_context
language = input("In which language would you like to display Ğ1 license [en/fr]? ") def display_license(ctx) -> None:
if language == "en": """
if not webbrowser.open("https://duniter.org/en/wiki/g1-license/"): Display in web browser if flag set and not headless system
click.echo_via_pager(open("licence-G1/license/license_g1-en.rst").read()) Otherwise, display in the terminal
"""
if ctx.obj["G1_LICENSE_WEB"] and has_web_browser():
languages_choices = list(licenses_urls.keys())
language = language_prompt(languages_choices)
webbrowser.open(licenses_urls[language])
else: else:
if not webbrowser.open("https://duniter.org/fr/wiki/licence-g1/"): language = language_prompt(languages)
click.echo_via_pager(open("licence-G1/license/license_g1-fr-FR.rst").read()) path = license_path(language)
with open(path) as license:
click.echo_via_pager(license.read())
def has_web_browser() -> bool:
try:
webbrowser.get()
return True
except webbrowser.Error:
return False
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 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")
"""
Copyright 2016-2021 Maël Azimi <m.a@moul.re>
Silkaj is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Silkaj is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with Silkaj. If not, see <https://www.gnu.org/licenses/>.
"""
import pytest
from unittest.mock import patch
from click.testing import CliRunner
import webbrowser
from pathlib import Path
from silkaj import cli, license
from silkaj.constants import SUCCESS_EXIT_STATUS
def test_license_approval_g1_test(capsys):
license.license_approval("g1-test")
assert not capsys.readouterr().out
# TODO: approve is not tested
@pytest.mark.parametrize(
"display, approve",
[
(True, True),
(True, False),
(False, True),
(False, False),
],
)
@patch("click.confirm")
@patch.object(license, "display_license")
def test_license_approval_g1(mock_display_license, mock_confirm, display, approve):
# https://stackoverflow.com/a/62939130
mock_confirm.return_value = display
license.license_approval("g1")
if display:
mock_display_license.assert_called_once()
mock_confirm.assert_called()
@pytest.mark.parametrize(
"language, web, license_sample",
[
("en", False, "**Currency licensing"),
("fr", False, "**Licence de la monnaie"),
("en", True, "**Currency licensing"),
("fr", True, "**Licence de la monnaie"),
],
)
@patch("webbrowser.open")
def test_display_license(webbrowser_open, language, web, license_sample):
args = []
if web:
args += ["--g1-license-web"]
args += ["license"]
result = CliRunner().invoke(cli.cli, args=args, input=language)
assert "In which language" in result.output
if web and license.has_web_browser():
webbrowser_open.assert_called_once_with(license.licenses_urls[language])
else:
assert license_sample in result.output
assert result.exit_code == SUCCESS_EXIT_STATUS
def test_has_web_browser():
"""
https://stackoverflow.com/a/50990039
"""
with patch("webbrowser.get", side_effect=webbrowser.Error):
assert not license.has_web_browser()
with patch("webbrowser.get", side_effect=None):
assert license.has_web_browser()
@pytest.mark.parametrize(
"language, license_sample",
[
("en", "**Currency licensing"),
("", "**Currency licensing"),
("fr", "**Licence de la monnaie"),
("blabla", ""),
],
)
def test_language_prompt(language, license_sample):
result = CliRunner().invoke(cli.cli, args=["license"], input=language)
assert "In which language" in result.output
assert license_sample in result.output
assert result.exit_code == SUCCESS_EXIT_STATUS
def test_license_path():
for lang in license.languages:
assert license.license_path(lang).is_file()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment