-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
88 lines (66 loc) · 2.36 KB
/
run.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
import keyring
import os
import signal
import subprocess
import sys
src_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "src"))
sys.path.append(src_path)
from src.gui import PriceTrackerGUI
from src.config import get_config, Config
APP_NAME = "CryptoPriceTracker"
SECURE_KEYS = ["API_KEY", "API_SECRET", "EMAIL_PASSWORD"]
def save_secure_config(config: Config):
for key in SECURE_KEYS:
value = getattr(config, key)
if value:
keyring.set_password(APP_NAME, key, value)
setattr(config, key, None)
def load_secure_config(config: Config):
for key in SECURE_KEYS:
value = keyring.get_password(APP_NAME, key)
if value:
setattr(config, key, value)
def run_main_script(main_script):
expected_path = os.path.join(src_path, "main.py")
if os.path.normpath(main_script) != os.path.normpath(expected_path):
raise ValueError("Unexpected main script path")
process = subprocess.Popen([sys.executable, main_script])
def signal_handler(sig, frame):
if sys.platform.startswith("win"):
process.send_signal(signal.CTRL_C_EVENT)
else:
process.send_signal(signal.SIGINT)
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
try:
process.wait()
except KeyboardInterrupt:
pass
finally:
if process.poll() is None:
process.terminate()
process.wait()
def main():
# Run the GUI
gui = PriceTrackerGUI()
config_updated = gui.run_and_return()
# Get the configuration
config = get_config()
if config_updated:
print("Configuration updated. Starting the Crypto Price Tracker...")
# Save sensitive information securely
save_secure_config(config)
# Save the non-sensitive parts of the configuration
config.save_to_env_file()
elif config.is_valid():
print("Using existing configuration. Starting the Crypto Price Tracker...")
else:
print("Invalid configuration. Please update the configuration and try again.")
return
# Load secure configuration before running the main script
load_secure_config(config)
# Run main.py as a separate process
main_script = os.path.join(src_path, "main.py")
run_main_script(main_script)
if __name__ == "__main__":
main()