Skip to content

Commit

Permalink
Only import copier when copying
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Mar 4, 2024
1 parent 15a996e commit fd48525
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
43 changes: 27 additions & 16 deletions src/em_keyboard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,17 @@
import re
import sys

try:
import pyperclip as copier # type: ignore[import]
except ImportError:
try:
import xerox as copier # type: ignore[import]
except ImportError:
copier = None

__version__: str = importlib.metadata.version("em_keyboard")

if sys.version_info >= (3, 9):
from functools import cache

EmojiDict = dict[str, list[str]]
else:
from functools import lru_cache as cache

EmojiDict = "dict[str, list[str]]"

try:
from importlib.resources import as_file, files

Expand All @@ -46,8 +47,18 @@

CUSTOM_EMOJI_PATH = os.path.join(os.path.expanduser("~/.emojis.json"))

# TODO Remove quotes when dropping Python 3.8
EmojiDict = "dict[str, list[str]]"

@cache
def get_copier():
try:
import pyperclip as copier # type: ignore[import]
except ImportError:
try:
import xerox as copier # type: ignore[import]
except ImportError:
copier = None

return copier


def parse_emojis(filename: str | os.PathLike[str] = EMOJI_PATH) -> EmojiDict:
Expand Down Expand Up @@ -115,8 +126,8 @@ def cli() -> None:
if args.random:
emoji, keywords = random.choice(list(lookup.items()))
name = keywords[0]
if copier and not no_copy:
copier.copy(emoji)
if not no_copy:
get_copier().copy(emoji)
print(f"Copied! {emoji} {name}")
else:
print(f"{emoji} {name}")
Expand All @@ -140,8 +151,8 @@ def cli() -> None:
# Some registered emoji have no value.
try:
# Copy the results (and say so!) to the clipboard.
if copier and not no_copy and len(found) == 1:
copier.copy(emoji)
if not no_copy and len(found) == 1:
get_copier().copy(emoji)
print(f"Copied! {emoji} {name}")
else:
print(f"{emoji} {name}")
Expand Down Expand Up @@ -169,8 +180,8 @@ def cli() -> None:
results = "".join(results)

# Copy the results (and say so!) to the clipboard.
if copier and not no_copy and not missing:
copier.copy(results)
if not no_copy and not missing:
get_copier().copy(results)
print(f"Copied! {print_results}")

# Script-kiddies.
Expand Down
8 changes: 4 additions & 4 deletions tests/test_em.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import pytest

from em_keyboard import cli, copier # type: ignore[import-untyped]
from em_keyboard import cli, get_copier # type: ignore[import-untyped]


@pytest.mark.parametrize(
Expand All @@ -31,7 +31,7 @@ def test_star(mock_print: MagicMock, mock_argparse: MagicMock, test_name: str) -
cli()

# Assert
if copier:
if get_copier():
mock_print.assert_called_once_with("Copied! ⭐")
else:
mock_print.assert_called_once_with("⭐")
Expand Down Expand Up @@ -113,7 +113,7 @@ def test_search_single_result_is_copied(
cli()

# Assert
if copier:
if get_copier():
mock_print.assert_called_once_with("Copied! 🇺🇦 flag_ukraine")
else:
mock_print.assert_called_once_with("🇺🇦 flag_ukraine")
Expand Down Expand Up @@ -153,7 +153,7 @@ def test_random(mock_print: MagicMock, mock_argparse: MagicMock) -> None:
cli()

# Assert
if copier:
if get_copier():
mock_print.assert_called_once_with("Copied! 😽 kissing_cat")
else:
mock_print.assert_called_once_with("😽 kissing_cat")
Expand Down

0 comments on commit fd48525

Please sign in to comment.