Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:

strategy:
matrix:
python-version: [3.7]
python-version: [3.6]
poetry-version: [1.1.4]

steps:
Expand Down
7 changes: 3 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -25,6 +24,6 @@ repos:
- 'pydocstyle'

- repo: https://github.com/terrencepreilly/darglint
rev: master
rev: v1.4.1
hooks:
- id: darglint
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.6.1
40 changes: 34 additions & 6 deletions src/cmdict/run_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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.

Expand Down
4 changes: 4 additions & 0 deletions tests/cmdict/test_run_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down