-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
189 lines (173 loc) · 8.12 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
from ulauncher.api.client.Extension import Extension
from ulauncher.api.client.EventListener import EventListener
from ulauncher.api.shared.event import KeywordQueryEvent, ItemEnterEvent
from ulauncher.api.shared.item.ExtensionResultItem import ExtensionResultItem
from ulauncher.api.shared.action.RenderResultListAction import RenderResultListAction
from ulauncher.api.shared.action.HideWindowAction import HideWindowAction
from ulauncher.api.shared.action.CopyToClipboardAction import CopyToClipboardAction
from ulauncher.api.shared.action.DoNothingAction import DoNothingAction
from ulauncher.api.shared.action.ExtensionCustomAction import ExtensionCustomAction
from src.thesaurus import cnrtl, larousse
import random
STRING_resulticonfile = "images/iconsyn.png"
STRING_ulauncherquery = None
STRING_currentsrc = None
LIST_suggestions = {}
STRING_currentcat = None
STRING_notcurrentcat = None
LIST_currentresults = []
INT_currentposition = 0
INT_entereventmode = None
def no_input_item():
global STRING_resulticonfile, STRING_currentsrc
return [
ExtensionResultItem(
icon=STRING_resulticonfile, name="Pas de réponse sur " + STRING_currentsrc + ".fr", on_enter=DoNothingAction()
)
]
def show_source_item():
global STRING_currentsrc, STRING_notcurrentcat
if STRING_currentsrc == "larousse":
STRING_notcurrentsrc = "cnrtl"
else:
STRING_notcurrentsrc = "larousse"
return [
ExtensionResultItem(
icon="images/iconswitch.png",
name="",
description="Cliquer/Entrer ici pour basculer vers les résultats " + STRING_notcurrentsrc + ".fr\nAlt+Entrer pour basculer vers les " + STRING_notcurrentcat,
on_enter=ExtensionCustomAction(2, keep_app_open=True),
on_alt_enter=ExtensionCustomAction(1, keep_app_open=True)
)
]
def show_header_item():
global STRING_resulticonfile, LIST_currentresults, STRING_notcurrentcat
if len(LIST_currentresults) > 10:
return [
ExtensionResultItem(
icon="images/icon.png",
name=str(len(LIST_currentresults)) + " résultats sur " + STRING_currentsrc + " .fr : " + "\U0001F500",
description="Cliquer/Entrer ici pour les résultats suivants\nAlt+Entrer pour basculer vers les " + STRING_notcurrentcat,
on_enter=ExtensionCustomAction(0, keep_app_open=True),
on_alt_enter=ExtensionCustomAction(1, keep_app_open=True)
)
]
else:
return [
ExtensionResultItem(
icon="images/icon.png",
name=str(len(LIST_currentresults)) + " résultats sur " + STRING_currentsrc + " .fr :",
description="Alt+Entrer pour basculer vers les " + STRING_notcurrentcat,
on_enter=DoNothingAction(),
on_alt_enter=ExtensionCustomAction(1, keep_app_open=True)
)
]
def show_suggestion_items():
global STRING_resulticonfile, LIST_currentresults, INT_currentposition
if not LIST_currentresults:
return [
ExtensionResultItem(
icon=STRING_resulticonfile,
name="Pas de réponse",
on_enter=DoNothingAction(),
description="Alt+Entrer pour basculer vers les Antonymes",
on_alt_enter=ExtensionCustomAction(1, keep_app_open=True),
)
]
elif len(LIST_currentresults) < 11:
return [
ExtensionResultItem(
icon=STRING_resulticonfile,
name=result,
on_enter=CopyToClipboardAction(result),
description="\n\nCliquer/Entrer ici pour copier ce résultat" if ("\n" in result) else "Cliquer/Entrer ici pour copier ce résultat",
on_alt_enter=ExtensionCustomAction(1, keep_app_open=True),
)
for result in LIST_currentresults
]
else:
while INT_entereventmode == 0:
INT_currentposition += 10
if INT_currentposition > len(LIST_currentresults):
INT_currentposition = 0
SYN_END_POSITION = INT_currentposition + 10
return [
ExtensionResultItem(
icon=STRING_resulticonfile,
name=result,
on_enter=CopyToClipboardAction(result),
description="\n\nCliquer/Entrer ici pour copier ce résultat" if ("\n" in result) else "Cliquer/Entrer ici pour copier ce résultat",
on_alt_enter=ExtensionCustomAction(1, keep_app_open=True),
)
for result in LIST_currentresults[INT_currentposition:SYN_END_POSITION]
]
return [
ExtensionResultItem(
icon=STRING_resulticonfile,
name=result,
on_enter=CopyToClipboardAction(result),
description="\n\nCliquer/Entrer ici pour copier ce résultat" if ("\n" in result) else "Cliquer/Entrer ici pour copier ce résultat",
on_alt_enter=ExtensionCustomAction(1, keep_app_open=True),
)
for result in LIST_currentresults[0:9]
]
class SynFrExtension(Extension):
def __init__(self):
super(SynFrExtension, self).__init__()
self.subscribe(KeywordQueryEvent, KeywordQueryEventListener())
self.subscribe(ItemEnterEvent, ItemEnterEventListener())
class KeywordQueryEventListener(EventListener):
def on_event(self, event, extension):
query = event.get_argument() or str()
if len(query.strip()) == 0:
return RenderResultListAction(no_input_item())
global STRING_ulauncherquery, STRING_resulticonfile, STRING_currentsrc, LIST_suggestions, STRING_currentcat, LIST_currentresults, INT_currentposition, STRING_notcurrentcat
STRING_resulticonfile = "images/iconsyn.png"
STRING_currentsrc = extension.preferences["thesaurus_src"]
STRING_ulauncherquery = query
LIST_suggestions = dict(eval(STRING_currentsrc)(STRING_ulauncherquery))
STRING_currentcat = "syn"
STRING_notcurrentcat = "Antonymes"
LIST_currentresults = [
(value)
for key, value in LIST_suggestions.items()
if key.startswith(STRING_currentcat)
]
INT_currentposition = 0
return RenderResultListAction(show_header_item() + show_suggestion_items() + show_source_item())
class ItemEnterEventListener(EventListener):
def on_event(self, event, extension):
global STRING_ulauncherquery, STRING_resulticonfile, STRING_currentsrc, LIST_suggestions, STRING_currentcat, LIST_currentresults, INT_entereventmode, STRING_notcurrentcat
INT_entereventmode = event.get_data()
if INT_entereventmode == 0:
return RenderResultListAction(show_header_item() + show_suggestion_items() + show_source_item())
elif INT_entereventmode == 2:
if STRING_currentsrc == "larousse":
STRING_currentsrc = "cnrtl"
else:
STRING_currentsrc = "larousse"
LIST_suggestions = dict(eval(STRING_currentsrc)(STRING_ulauncherquery))
LIST_currentresults = [
(value)
for key, value in LIST_suggestions.items()
if key.startswith(STRING_currentcat)
]
INT_currentposition = 0
return RenderResultListAction(show_header_item() + show_suggestion_items() + show_source_item())
else:
if STRING_currentcat == "syn":
STRING_currentcat = "ant"
STRING_resulticonfile = "images/iconant.png"
STRING_notcurrentcat = "Synonymes"
else:
STRING_currentcat = "syn"
STRING_resulticonfile = "images/iconsyn.png"
STRING_notcurrentcat = "Antonymes"
LIST_currentresults = [
(value)
for key, value in LIST_suggestions.items()
if key.startswith(STRING_currentcat)
]
return RenderResultListAction(show_header_item() + show_suggestion_items() + show_source_item())
if __name__ == "__main__":
SynFrExtension().run()