-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
146 lines (127 loc) · 5.86 KB
/
Copy pathmain.py
File metadata and controls
146 lines (127 loc) · 5.86 KB
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
from os import name as os_name
from typing import Any
if os_name != "nt":
from const import get_logger
from logging import Logger
from ulauncher.api.client.EventListener import EventListener # type: ignore
from ulauncher.api.client.Extension import Extension # type: ignore
from ulauncher.api.shared.action.ExtensionCustomAction import ExtensionCustomAction # type: ignore
from ulauncher.api.shared.action.RenderResultListAction import RenderResultListAction # type: ignore
from ulauncher.api.shared.event import ( # type: ignore
ItemEnterEvent,
KeywordQueryEvent,
PreferencesEvent,
)
from ulauncher.api.shared.item.ExtensionResultItem import ExtensionResultItem # type: ignore
log: Logger = get_logger(__name__)
class SteamExtension(Extension):
"""
The uLauncher Steam extension class.
"""
def __init__(self) -> None:
"""
Initialises a new SteamExtension instance.
"""
log.debug("Initialising Steam extension")
super(SteamExtension, self).__init__()
self.subscribe(PreferencesEvent, SteamExtensionStartListener())
self.subscribe(KeywordQueryEvent, SteamExtensionQueryListener())
self.subscribe(ItemEnterEvent, SteamExtensionItemListener())
log.info("Steam extension initialised")
class SteamExtensionStartListener(EventListener):
def on_event(self, event, _) -> None:
"""
Called when the Steam extension is started.
Args:
event (PreferencesEvent): The event that triggered this listener.
_ (Extension): The Steam extension, unused in this context due to it being empty.
"""
from cache import build_cache
log.debug("Steam extension started, building cache")
preferences: dict[str, Any] = event.preferences
build_cache(preferences)
class SteamExtensionQueryListener(EventListener):
def on_event(self, event, extension) -> RenderResultListAction:
"""
Called when the Steam extension is queried.
Args:
event (KeywordQueryEvent): The event that triggered this listener, containing the search argument.
extension (Extension): The Steam extension, containing the preferences dictionary.
Returns:
RenderResultListAction: A call for uLauncher to render the query results.
"""
result_items: list[ExtensionResultItem] = []
try:
from const import DIR_SEP, EXTENSION_PATH
from query import SteamExtensionItem, query_cache
preferences: dict[str, Any] = extension.preferences
items: list[SteamExtensionItem] = query_cache(
event.get_keyword(), preferences, event.get_argument()
)
log.debug("Converting query results to ExtensionResultItems")
for item in items:
result_items.append(
ExtensionResultItem(
icon=item.icon.replace(EXTENSION_PATH, ""),
name=item.get_name(),
description=item.get_description(),
on_enter=ExtensionCustomAction(item.get_action()),
)
)
except Exception as err:
return result_items.insert(
0,
ExtensionResultItem(
icon=f"images{DIR_SEP}icon.png",
name=err.__class__.__name__,
description=str(err),
on_enter=ExtensionCustomAction("error"),
),
)
return RenderResultListAction(result_items)
class SteamExtensionItemListener(EventListener):
def on_event(self, event, extension) -> None:
"""
Called when an item as presented in uLauncher is selected.
Args:
event (ItemEnterEvent): The event that triggered this listener, containing the selected action.
extension (Extension): The Steam extension, containing the preferences dictionary.
"""
from enter import execute_action
action: str = event.get_data()
preferences: dict[str, Any] = extension.preferences
execute_action(action, preferences)
if __name__ == "__main__":
SteamExtension().run()
elif __name__ == "__main__":
from cache import build_cache
from const import get_preferences_from_env
from enter import execute_action
from query import SteamExtensionItem, query_cache
preferences: dict[str, Any] = get_preferences_from_env()
build_cache(preferences)
while True:
user_input_list: list[str] = input(
"Enter keyword and search (or exit): "
).split()
keyword: str = user_input_list[0] if len(user_input_list) >= 1 else ""
if keyword == "exit":
break
search: str | None = None
if len(user_input_list) >= 2:
search = " ".join(user_input_list[1:])
items: list[SteamExtensionItem] = query_cache(keyword, preferences, search)
print("\n".join(f"{index + 1}. {item}" for index, item in enumerate(items)))
if items[0].name == "no_results":
continue
user_input: str = input(
"Enter item number to execute (or anything else to discard): "
)
if user_input.split(" ")[0] == "exit":
break
try:
user_input_int: int = int(user_input)
if user_input_int >= 1 and user_input_int <= len(items):
execute_action(items[user_input_int - 1].get_action(), preferences)
except Exception:
pass