-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmain.py
162 lines (139 loc) Β· 6.24 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import os
import logging
import sqlite3
from pprint import pprint
from ulauncher.api.client.Extension import Extension
from ulauncher.api.client.EventListener import EventListener
from ulauncher.api.shared.event import KeywordQueryEvent
from ulauncher.api.shared.item.ExtensionResultItem import ExtensionResultItem
from ulauncher.api.shared.action.RenderResultListAction import RenderResultListAction
from ulauncher.api.shared.action.CopyToClipboardAction import CopyToClipboardAction
from ulauncher.api.shared.action.DoNothingAction import DoNothingAction
logger = logging.getLogger(__name__)
extension_icon = 'images/icon.png'
db_path = os.path.join(os.path.dirname(__file__), 'emoji.sqlite')
conn = sqlite3.connect(db_path, check_same_thread=False)
conn.row_factory = sqlite3.Row
SEARCH_LIMIT_MIN = 2
SEARCH_LIMIT_DEFAULT = 8
SEARCH_LIMIT_MAX = 100
def normalize_skin_tone(tone):
"""
Converts from the more visual skin tone preferences string to a more
machine-readable format.
"""
if tone == "π default":
return ''
elif tone == "ππ» light":
return 'light'
elif tone == "ππΌ medium-light":
return 'medium-light'
elif tone == "ππ½ medium":
return 'medium'
elif tone == "ππΎ medium-dark":
return 'medium-dark'
elif tone == "ππΏ dark":
return 'dark'
else:
return None
class EmojiExtension(Extension):
def __init__(self):
super(EmojiExtension, self).__init__()
self.subscribe(KeywordQueryEvent, KeywordQueryEventListener())
self.allowed_skin_tones = ["", "dark", "light",
"medium", "medium-dark", "medium-light"]
class KeywordQueryEventListener(EventListener):
def on_event(self, event, extension):
search_limit = extension.preferences['search_limit']
try:
search_limit = search_limit.strip()
search_limit = int(search_limit)
if search_limit < SEARCH_LIMIT_MIN:
search_limit = SEARCH_LIMIT_MIN
elif search_limit > SEARCH_LIMIT_MAX:
search_limit = SEARCH_LIMIT_MAX
except Exception as e:
search_limit = SEARCH_LIMIT_DEFAULT
icon_style = extension.preferences['emoji_style']
fallback_icon_style = extension.preferences['fallback_emoji_style']
search_term = event.get_argument().replace(
'%', '') if event.get_argument() else None
search_with_shortcodes = search_term and search_term.startswith(':')
# Add %'s to search term (since LIKE %?% doesn't work)
skin_tone = normalize_skin_tone(extension.preferences['skin_tone'])
if skin_tone not in extension.allowed_skin_tones:
logger.warning('Unknown skin tone "%s"' % skin_tone)
skin_tone = ''
search_term_orig = search_term
if search_term and search_with_shortcodes:
search_term = ''.join([search_term, '%'])
elif search_term:
search_term = ''.join(['%', search_term, '%'])
if search_with_shortcodes:
query = '''
SELECT em.name, em.code, em.keywords,
em.icon_apple, em.icon_twemoji, em.icon_noto, em.icon_blobmoji,
skt.icon_apple AS skt_icon_apple, skt.icon_twemoji AS skt_icon_twemoji,
skt.icon_noto AS skt_icon_noto, skt.icon_blobmoji AS skt_icon_blobmoji,
skt.code AS skt_code, sc.code as "shortcode"
FROM emoji AS em
LEFT JOIN skin_tone AS skt
ON skt.name = em.name AND tone = ?
LEFT JOIN shortcode AS sc
ON sc.name = em.name
WHERE sc.code LIKE ?
GROUP BY em.name
ORDER BY length(replace(sc.code, ?, ''))
LIMIT ?;
'''
sql_args = [skin_tone, search_term, search_term_orig, search_limit]
else:
query = '''
SELECT em.name, em.code,
em.icon_apple, em.icon_twemoji, em.icon_noto, em.icon_blobmoji,
skt.icon_apple AS skt_icon_apple, skt.icon_twemoji AS skt_icon_twemoji,
skt.icon_noto AS skt_icon_noto, skt.icon_blobmoji AS skt_icon_blobmoji,
skt.code AS skt_code
FROM emoji AS em
LEFT JOIN skin_tone AS skt
ON skt.name = em.name AND tone = ?
WHERE em.name LIKE ?
OR em.name_search LIKE ?
ORDER BY
CASE
WHEN em.name LIKE ? THEN 0
WHEN em.name_search LIKE ? THEN 1
END
LIMIT ?;
'''
sql_args = [skin_tone, search_term, search_term, search_term, search_term, search_limit]
# Display blank prompt if user hasn't typed anything
if not search_term:
search_icon = 'images/%s/icon.png' % icon_style
return RenderResultListAction([
ExtensionResultItem(icon=search_icon,
name='Type in emoji name...',
on_enter=DoNothingAction())
])
# Get list of results from sqlite DB
items = []
display_char = extension.preferences['display_char'] != 'no'
for row in conn.execute(query, sql_args):
if row['skt_code']:
icon = row['skt_icon_%s' % icon_style]
icon = row['skt_icon_%s' %
fallback_icon_style] if not icon else icon
code = row['skt_code']
else:
icon = row['icon_%s' % icon_style]
icon = row['icon_%s' %
fallback_icon_style] if not icon else icon
code = row['code']
name = row['shortcode'] if search_with_shortcodes else row['name'].capitalize()
if display_char:
name += ' | %s' % code
items.append(ExtensionResultItem(icon=icon, name=name,
on_enter=CopyToClipboardAction(code)))
return RenderResultListAction(items)
if __name__ == '__main__':
EmojiExtension().run()