-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenter.py
More file actions
147 lines (139 loc) · 5.51 KB
/
enter.py
File metadata and controls
147 lines (139 loc) · 5.51 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
147
from const import get_logger
from logging import Logger
from typing import Any
log: Logger = get_logger(__name__)
def execute_action(
action: str, preferences: dict[str, Any], execute: bool = True
) -> None:
"""
Executes the given action from the Steam extension.
Args:
action (str): The action to execute.
preferences (dict[str, Any]): The preferences dictionary.
execute (bool, optional): Whether to execute the action. If this is False, the action is only logged in the cache as having been launched. Defaults to True.
"""
from cache import (
build_cache,
clear_cache,
clear_images,
datetime_to_timestamp,
ensure_dict_key_is_dict,
load_cache,
save_cache,
)
from const import DIR_SEP
from datetime import datetime
from os import name as os_name
from subprocess import Popen as SubprocessPopen
from typing import Literal
from query import get_launches
command: str = "steam"
if os_name == "nt":
if not preferences["STEAM_FOLDER"].endswith(DIR_SEP):
preferences["STEAM_FOLDER"] = f"{preferences['STEAM_FOLDER']}{DIR_SEP}"
command = f'"{preferences["STEAM_FOLDER"]}steam.exe"'
cache: dict[str, Any] = load_cache()
cache_item: dict[str, Any]
force_cache: bool | Literal["skip"] = False
if action.startswith("APP"):
app_id: int = int(action.split("/")[-1])
if "apps" in cache.keys() and str(app_id) in cache["apps"].keys():
cache_item = cache["apps"][str(app_id)]
elif "nonSteam" in cache.keys() and str(app_id) in cache["nonSteam"].keys():
cache_item = cache["nonSteam"][str(app_id)]
else:
log.error(f"Cannot execute '{action}', app ID {app_id} not found in cache")
return
if execute:
app_action: str = f"{command} {action[3:]}"
log.info(f"Launching app ID {app_id} via '{app_action}'")
SubprocessPopen(app_action, shell=True)
elif action.startswith("FRIEND"):
friend_id: int = int(action[6:])
if "friends" in cache.keys() and str(friend_id) in cache["friends"].keys():
cache_item = cache["friends"][str(friend_id)]
else:
log.error(
f"Cannot execute action, friend ID {friend_id} not found in cache"
)
return
if execute:
friend_action: str
if preferences["FRIEND_ACTION"] == "chat":
friend_action = f"{command} steam://friends/message/{friend_id}"
elif preferences["FRIEND_ACTION"] == "profile":
friend_action = f"{command} steam://url/SteamIDPage/{friend_id}"
else:
log.error(
f"Unknown default friend action '{preferences['FRIEND_ACTION']}'"
)
return
log.info(f"Launching friend ID {friend_id} via '{friend_action}'")
SubprocessPopen(friend_action, shell=True)
elif action.startswith("s:") or action.startswith("w:"):
to_run: str
if action.startswith("s:"):
to_run = f"{command} steam://{action[2:]}"
elif os_name == "nt": # "w:"
to_run = f"xdg-open https://{action[2:]}"
else:
# TODO (low priority): Add support for opening URLs in Windows
log.error("Opening URLs is not supported on this platform")
return
ensure_dict_key_is_dict(cache, "navs")
ensure_dict_key_is_dict(cache["navs"], action)
cache_item = cache["navs"][action]
if execute:
log.info(f"Launching navigation '{action}' via '{to_run}'")
SubprocessPopen(to_run, shell=True)
elif action == "update_cache":
log.info("Updating cache")
ensure_dict_key_is_dict(cache, "navs")
ensure_dict_key_is_dict(cache["navs"], action)
cache_item = cache["navs"][action]
if execute:
force_cache = True
elif action == "clear_cache":
log.info("Clearing cache")
if execute:
clear_cache()
return
ensure_dict_key_is_dict(cache, "navs")
ensure_dict_key_is_dict(cache["navs"], action)
cache_item = cache["navs"][action]
elif action == "clear_images":
log.info("Clearing images")
ensure_dict_key_is_dict(cache, "navs")
ensure_dict_key_is_dict(cache["navs"], action)
cache_item = cache["navs"][action]
if execute:
clear_images()
force_cache = "skip"
elif action == "rebuild_cache":
log.info("Rebuilding cache")
if execute:
clear_cache()
clear_images()
build_cache(preferences)
return
ensure_dict_key_is_dict(cache, "navs")
ensure_dict_key_is_dict(cache["navs"], action)
cache_item = cache["navs"][action]
elif action in ("no_results", "error"):
return
else:
log.error(f"Invalid action '{action}'")
return
launched: datetime | None
times: int
launched, times = get_launches(cache_item)
times += 1
cache_item["launched"] = f"{datetime_to_timestamp(launched)}x{times}"
save_cache(cache, preferences)
if execute and not isinstance(force_cache, str): # != "skip"
build_cache(preferences, force=force_cache)
if __name__ == "__main__":
from const import get_preferences_from_env
import sys
preferences: dict[str, Any] = get_preferences_from_env()
execute_action(" ".join(sys.argv[1:]), preferences)