Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Depend on pyperclip on all platforms #155

Merged
merged 4 commits into from
Jan 19, 2025
Merged
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
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -78,7 +78,15 @@ At this time, **em** requires Python and pip:
python3 -m pip install em-keyboard
```

That's it!
On Linux, an additional dependency is required for automatic copying to clipboard. This
would be either [`xclip`](https://github.com/astrand/xclip) in an X11 session or
[`wl-clipboard`](https://github.com/bugaevc/wl-clipboard) in a Wayland session. On a
Debian-based distribution these are installable with:

```sh
sudo apt install xclip
sudo apt install wl-clipboard
```

## Tests

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -37,7 +37,7 @@ classifiers = [
]
dynamic = [ "version" ]
dependencies = [
"pyperclip; platform_system=='Windows'",
"pyperclip",
]
optional-dependencies.tests = [
"pytest",
52 changes: 30 additions & 22 deletions src/em_keyboard/__init__.py
Original file line number Diff line number Diff line change
@@ -23,14 +23,6 @@
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

from em_keyboard import _version

__version__ = _version.__version__
@@ -45,6 +37,24 @@
EmojiDict = dict[str, list[str]]


def try_copy_to_clipboard(text: str) -> bool:
try:
import pyperclip # type: ignore[import]
except ModuleNotFoundError:
pyperclip = None
try:
import xerox # type: ignore[import]
except ModuleNotFoundError:
return False

Check warning on line 48 in src/em_keyboard/__init__.py

Codecov / codecov/patch

src/em_keyboard/__init__.py#L43-L48

Added lines #L43 - L48 were not covered by tests
copier = pyperclip if pyperclip else xerox
copier_error = pyperclip.PyperclipException if pyperclip else xerox.ToolNotFound
try:
copier.copy(text)
except copier_error:
return False
return True


def parse_emojis(filename: str | os.PathLike[str] = EMOJI_PATH) -> EmojiDict:
return json.load(open(filename, encoding="utf-8"))

@@ -106,11 +116,11 @@
if args.random:
emoji, keywords = random.choice(list(lookup.items()))
name = keywords[0]
if copier and not no_copy:
copier.copy(emoji)
print(f"Copied! {emoji} {name}")
if not no_copy:
copied = try_copy_to_clipboard(emoji)
else:
print(f"{emoji} {name}")
copied = False
print(f"Copied! {emoji} {name}" if copied else f"{emoji} {name}")
sys.exit(0)

if not args.name:
@@ -131,11 +141,11 @@
# 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)
print(f"Copied! {emoji} {name}")
if not no_copy and len(found) == 1:
copied = try_copy_to_clipboard(emoji)
else:
print(f"{emoji} {name}")
copied = False
print(f"Copied! {emoji} {name}" if copied else f"{emoji} {name}")

# Sometimes, an emoji will have no value.
except TypeError:
@@ -160,13 +170,11 @@
results = "".join(results)

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

# Script-kiddies.
if not no_copy and not missing:
copied = try_copy_to_clipboard(results)
else:
print(print_results)
copied = False
print(f"Copied! {print_results}" if copied else print_results)

sys.exit(int(missing))

12 changes: 7 additions & 5 deletions tests/test_em.py
Original file line number Diff line number Diff line change
@@ -6,7 +6,9 @@

import pytest

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

copier_deps_installed = try_copy_to_clipboard("checking if copy works")


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

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

# Assert
if copier:
if copier_deps_installed:
mock_print.assert_called_once_with("Copied! 🇺🇦 flag_ukraine")
else:
mock_print.assert_called_once_with("🇺🇦 flag_ukraine")
@@ -152,7 +154,7 @@ def test_search_multi_word(mock_print: MagicMock, mock_argparse: MagicMock) -> N
cli()

# Assert
if copier:
if copier_deps_installed:
mock_print.assert_called_once_with("Copied! 🎪 circus_tent")
else:
mock_print.assert_called_once_with("🎪 circus_tent")
@@ -174,7 +176,7 @@ def test_random(mock_print: MagicMock, mock_argparse: MagicMock) -> None:
cli()

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