Skip to content

Commit 81cf3de

Browse files
authored
Small optimisations (#159)
2 parents 422cd42 + 353b1d7 commit 81cf3de

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/em_keyboard/__init__.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@
1616
from __future__ import annotations
1717

1818
import argparse
19-
import json
2019
import os
21-
import random
22-
import re
2320
import sys
2421

2522
from em_keyboard import _version
@@ -55,6 +52,8 @@ def try_copy_to_clipboard(text: str) -> bool:
5552

5653

5754
def parse_emojis(filename: str | os.PathLike[str] = EMOJI_PATH) -> EmojiDict:
55+
import json
56+
5857
return json.load(open(filename, encoding="utf-8"))
5958

6059

@@ -80,8 +79,7 @@ def do_find(lookup: EmojiDict, terms: tuple[str, ...]) -> list[tuple[str, str]]:
8079

8180
def clean_name(name: str) -> str:
8281
"""Clean emoji name replacing specials chars by underscore"""
83-
special_chars = "[-. ]" # square brackets are part of the regex
84-
return re.sub(special_chars, "_", name).lower()
82+
return name.replace("-", "_").replace(".", "_").replace(" ", "_").lower()
8583

8684

8785
def cli() -> None:
@@ -101,13 +99,18 @@ def cli() -> None:
10199
args = parser.parse_args()
102100
no_copy = args.no_copy
103101

102+
if not args.name and not args.random:
103+
sys.exit("Error: the 'name' argument is required")
104+
104105
# Grab the lookup dictionary.
105106
lookup = parse_emojis()
106107

107108
if os.path.isfile(CUSTOM_EMOJI_PATH):
108109
lookup.update(parse_emojis(CUSTOM_EMOJI_PATH))
109110

110111
if args.random:
112+
import random
113+
111114
emoji, keywords = random.choice(list(lookup.items()))
112115
name = keywords[0]
113116
if not no_copy:
@@ -117,9 +120,6 @@ def cli() -> None:
117120
print(f"Copied! {emoji} {name}" if copied else f"{emoji} {name}")
118121
sys.exit(0)
119122

120-
if not args.name:
121-
sys.exit("Error: the 'name' argument is required")
122-
123123
names = tuple(map(clean_name, args.name))
124124

125125
# Marker for if the given emoji isn't found.

0 commit comments

Comments
 (0)