diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 792cd3b..4ec6c2f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -13,7 +13,7 @@ jobs: strategy: matrix: - python-version: [3.7] + python-version: [3.6] poetry-version: [1.1.4] steps: diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 9736283..51e878a 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -8,13 +8,12 @@ repos: - id: trailing-whitespace - repo: https://github.com/ambv/black - rev: stable + rev: 19.10b0 hooks: - id: black - language_version: python3.7 - repo: https://gitlab.com/pycqa/flake8 - rev: 3.8.2 + rev: 3.9.0 hooks: - id: flake8 additional_dependencies: @@ -25,6 +24,6 @@ repos: - 'pydocstyle' - repo: https://github.com/terrencepreilly/darglint - rev: master + rev: v1.4.1 hooks: - id: darglint diff --git a/.python-version b/.python-version new file mode 100644 index 0000000..9575d51 --- /dev/null +++ b/.python-version @@ -0,0 +1 @@ +3.6.1 diff --git a/src/cmdict/run_script.py b/src/cmdict/run_script.py index d75cb67..139d9d6 100644 --- a/src/cmdict/run_script.py +++ b/src/cmdict/run_script.py @@ -80,19 +80,27 @@ def download(): @cli.command() @click.argument("words", nargs=-1) -def search(words): +@click.option( + "--german", "-de", is_flag=True, help="Whether it is German word." +) +def search(words, german): """Type in one English word and echo its Chinese translation. Args: words (str): one English word to be searched. For example, "a lot" or "mirror". + german (bool): whether it is German word. """ - if _valid_db_exists(): - db_engine = ECDICTConnector() - for i, word in enumerate(words): - _echo_item(word, db_engine.query(word)) + if german: + for _, word in enumerate(words): + _echo_item_de(word, None) else: - _echo_warn_download() + if _valid_db_exists(): + db_engine = ECDICTConnector() + for _, word in enumerate(words): + _echo_item(word, db_engine.query(word)) + else: + _echo_warn_download() @cli.command() @@ -184,6 +192,26 @@ def _echo_item(word, res): ) +def _echo_item_de(word, res): + """Echo a German word search result to cli. + + Args: + word (str): The word. + res (dict): The word search result. + """ + _echo_divider() + if res: + click.echo(Fore.CYAN + Style.BRIGHT + res + "\n") + else: + click.echo( + Fore.RED + + Style.BRIGHT + + word + + Style.RESET_ALL + + " can not be returned successfully!" + ) + + def _valid_db_exists(): """Return if a valid database is found. diff --git a/tests/cmdict/test_run_script.py b/tests/cmdict/test_run_script.py index ccec61a..000de65 100644 --- a/tests/cmdict/test_run_script.py +++ b/tests/cmdict/test_run_script.py @@ -22,6 +22,10 @@ def test_cli_search(): """Test cli word search.""" res = CliRunner().invoke(search, "play") assert "theatrical performance of a drama" in res.output + res = CliRunner().invoke(search, ["play", "-de"]) + assert "play can not be returned successfully" in res.output + res = CliRunner().invoke(search, ["play", "--german"]) + assert "play can not be returned successfully" in res.output def test_cli_search_non_exist_word():