Skip to content

Commit d3a3da2

Browse files
authored
Update utils.py
1 parent 7cda870 commit d3a3da2

File tree

1 file changed

+94
-80
lines changed

1 file changed

+94
-80
lines changed

utils.py

Lines changed: 94 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,60 @@
1-
import os
21
import logging
2+
import os
3+
import getpass
4+
5+
# Get the current user's home directory
6+
user_home = os.path.expanduser('~')
7+
8+
# Set up logging to a user-writable directory
9+
log_dir = os.path.join(user_home, '.hackeros')
10+
log_file = os.path.join(log_dir, 'hacker-mode.log')
11+
12+
# Ensure the log directory exists and is writable
13+
try:
14+
os.makedirs(log_dir, exist_ok=True)
15+
# Verify that the directory is writable
16+
if not os.access(log_dir, os.W_OK):
17+
raise PermissionError(f"No write permission for directory: {log_dir}")
18+
except PermissionError as e:
19+
# Fallback to a temporary directory if the primary log directory is not writable
20+
log_dir = os.path.join(os.path.expanduser('~'), '.cache', 'hackeros')
21+
os.makedirs(log_dir, exist_ok=True)
22+
log_file = os.path.join(log_dir, 'hacker-mode.log')
23+
logging.warning(f"Failed to use primary log directory due to: {e}. Using fallback: {log_file}")
324

4-
logging.basicConfig(filename='/tmp/hacker-mode.log', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
25+
# Configure logging
26+
try:
27+
logging.basicConfig(
28+
filename=log_file,
29+
level=logging.INFO,
30+
format='%(asctime)s - %(levelname)s - %(message)s',
31+
filemode='a' # Append mode to avoid overwriting
32+
)
33+
except PermissionError as e:
34+
logging.basicConfig(
35+
level=logging.INFO,
36+
format='%(asctime)s - %(levelname)s - %(message)s',
37+
handlers=[logging.StreamHandler()] # Fallback to console logging
38+
)
39+
logging.error(f"Failed to open log file {log_file}: {e}. Logging to console instead.")
540

41+
# Language setting
42+
lang = 'en' # Default language
43+
44+
# Translation dictionary
645
translations = {
746
'en': {
8-
'title': 'Hacker Mode',
947
'settings': 'Settings',
10-
'hacker_menu': 'Hacker Menu',
11-
'steam': 'Steam',
12-
'heroic': 'Heroic',
13-
'hyperplay': 'HyperPlay',
14-
'lutris': 'Lutris',
48+
'back': 'Back',
49+
'close': 'Close',
1550
'audio': 'Audio',
1651
'increase_volume': 'Increase Volume',
1752
'decrease_volume': 'Decrease Volume',
1853
'toggle_mute': 'Toggle Mute',
1954
'display': 'Display',
2055
'increase_brightness': 'Increase Brightness',
2156
'decrease_brightness': 'Decrease Brightness',
22-
'toggle_theme': 'Toggle Dark/Light Mode',
57+
'toggle_theme': 'Toggle Theme',
2358
'network': 'Network',
2459
'wifi_settings': 'Wi-Fi Settings',
2560
'toggle_wifi': 'Toggle Wi-Fi',
@@ -33,45 +68,42 @@
3368
'enable_gamescope': 'Enable Gamescope',
3469
'enable_mangohud': 'Enable MangoHUD',
3570
'enable_vkbasalt': 'Enable vkBasalt',
36-
'app_not_installed': 'To install missing applications, use the package manager.',
37-
'launch_cooldown': 'Please wait {seconds} seconds before launching {app} again.',
38-
'no_internet': 'No internet connection. Please enable Wi-Fi.',
71+
'connect': 'Connect',
72+
'scan': 'Scan',
73+
'pair': 'Pair',
3974
'no_networks': 'No networks found',
40-
'connection_failed': 'Connection failed: {error}',
41-
'connecting': 'Connecting to {ssid}...',
42-
'wifi_toggle_success': 'Wi-Fi turned {state}',
75+
'no_selection': 'No network selected',
76+
'connection_failed': 'Failed to connect: {error}',
77+
'connecting': 'Connecting to {ssid}',
4378
'wifi_toggle_failed': 'Failed to toggle Wi-Fi: {error}',
44-
'no_selection': 'Please select an item',
45-
'pairing': 'Pairing {device}...',
46-
'pairing_failed': 'Pairing failed: {error}',
79+
'wifi_toggle_success': 'Wi-Fi turned {state}',
80+
'pairing_failed': 'Failed to pair: {error}',
81+
'pairing': 'Pairing with {device}',
82+
'error_returning_to_main': 'Error returning to main window: {error}',
83+
'app_not_installed': '{app} is not installed',
84+
'no_internet': 'No internet connection available',
85+
'launch_cooldown': 'Please wait {seconds} seconds before launching {app} again',
86+
'title': 'HackerOS',
87+
'hacker_menu': 'Hacker Menu',
4788
'switch_to_plasma': 'Switch to Plasma',
4889
'shutdown': 'Shutdown',
4990
'restart': 'Restart',
5091
'sleep': 'Sleep',
5192
'restart_apps': 'Restart Apps',
52-
'restart_sway': 'Restart Sway',
53-
'close': 'Close',
54-
'back': 'Back',
55-
'connect': 'Connect',
56-
'scan': 'Scan',
57-
'pair': 'Pair'
93+
'restart_sway': 'Restart Wayfire'
5894
},
5995
'pl': {
60-
'title': 'Tryb Hakera',
6196
'settings': 'Ustawienia',
62-
'hacker_menu': 'Menu Hakera',
63-
'steam': 'Steam',
64-
'heroic': 'Heroic',
65-
'hyperplay': 'HyperPlay',
66-
'lutris': 'Lutris',
97+
'back': 'Wróć',
98+
'close': 'Zamknij',
6799
'audio': 'Dźwięk',
68100
'increase_volume': 'Zwiększ głośność',
69101
'decrease_volume': 'Zmniejsz głośność',
70102
'toggle_mute': 'Wycisz/Włącz dźwięk',
71103
'display': 'Wyświetlacz',
72104
'increase_brightness': 'Zwiększ jasność',
73105
'decrease_brightness': 'Zmniejsz jasność',
74-
'toggle_theme': 'Przełącz tryb ciemny/jasny',
106+
'toggle_theme': 'Zmień motyw',
75107
'network': 'Sieć',
76108
'wifi_settings': 'Ustawienia Wi-Fi',
77109
'toggle_wifi': 'Włącz/Wyłącz Wi-Fi',
@@ -81,71 +113,53 @@
81113
'balanced': 'Zrównoważony',
82114
'performance': 'Wydajność',
83115
'general': 'Ogólne',
84-
'gaming_tools': 'Narzędzia do gier',
116+
'gaming_tools': 'Narzędzia dla graczy',
85117
'enable_gamescope': 'Włącz Gamescope',
86118
'enable_mangohud': 'Włącz MangoHUD',
87119
'enable_vkbasalt': 'Włącz vkBasalt',
88-
'app_not_installed': 'Aby zainstalować brakujące aplikacje, użyj menedżera pakietów.',
89-
'launch_cooldown': 'Proszę czekać {seconds} sekund przed ponownym uruchomieniem {app}.',
90-
'no_internet': 'Brak połączenia z internetem. Proszę włączyć Wi-Fi.',
120+
'connect': 'Połącz',
121+
'scan': 'Skanuj',
122+
'pair': 'Paruj',
91123
'no_networks': 'Nie znaleziono sieci',
92-
'connection_failed': 'Połączenie nieudane: {error}',
93-
'connecting': 'Łączenie z {ssid}...',
94-
'wifi_toggle_success': 'Wi-Fi przełączone na {state}',
124+
'no_selection': 'Nie wybrano sieci',
125+
'connection_failed': 'Nie udało się połączyć: {error}',
126+
'connecting': 'Łączenie z {ssid}',
95127
'wifi_toggle_failed': 'Nie udało się przełączyć Wi-Fi: {error}',
96-
'no_selection': 'Proszę wybrać element',
97-
'pairing': 'Parowanie {device}...',
98-
'pairing_failed': 'Parowanie nieudane: {error}',
128+
'wifi_toggle_success': 'Wi-Fi przełączone na {state}',
129+
'pairing_failed': 'Nie udało się sparować: {error}',
130+
'pairing': 'Parowanie z {device}',
131+
'error_returning_to_main': 'Błąd podczas powrotu do głównego okna: {error}',
132+
'app_not_installed': '{app} nie jest zainstalowany',
133+
'no_internet': 'Brak połączenia z internetem',
134+
'launch_cooldown': 'Proszę czekać {seconds} sekund przed ponownym uruchomieniem {app}',
135+
'title': 'HackerOS',
136+
'hacker_menu': 'Menu Hakera',
99137
'switch_to_plasma': 'Przełącz na Plasma',
100138
'shutdown': 'Wyłącz',
101139
'restart': 'Uruchom ponownie',
102140
'sleep': 'Uśpij',
103-
'restart_apps': 'Restartuj aplikacje',
104-
'restart_sway': 'Restartuj sesję Sway',
105-
'close': 'Zamknij',
106-
'back': 'Wróć',
107-
'connect': 'Połącz',
108-
'scan': 'Skanuj',
109-
'pair': 'Paruj'
141+
'restart_apps': 'Uruchom ponownie aplikacje',
142+
'restart_sway': 'Uruchom ponownie Wayfire'
110143
}
111144
}
112145

113-
lang = 'en'
114-
enable_gamescope = False
115-
enable_mangohud = False
116-
enable_vkbasalt = False
146+
def get_text(key, params=None):
147+
text = translations[lang].get(key, key)
148+
if params:
149+
return text.format(**params)
150+
return text
117151

118-
def setup_language():
119-
global lang
120-
try:
121-
locale = os.environ.get('LANG', 'en_US').split('.')[0].split('_')[0]
122-
lang = locale if locale in translations else 'en'
123-
logging.info(f'Language set to: {lang}')
124-
except Exception as e:
125-
logging.error(f'Error setting language: {e}')
126-
lang = 'en'
127-
return lang
152+
def set_gaming_tool(tool, enabled):
153+
logging.info(f'Setting {tool} to {"enabled" if enabled else "disabled"}')
128154

129155
def set_language(new_lang):
130156
global lang
131157
if new_lang in translations:
132158
lang = new_lang
133-
logging.info(f'Language changed to: {new_lang}')
159+
logging.info(f'Language set to {lang}')
160+
else:
161+
logging.warning(f'Invalid language: {new_lang}')
134162

135-
def get_text(key, params=None):
136-
if params is None:
137-
params = {}
138-
text = translations.get(lang, {}).get(key, key)
139-
for k, v in params.items():
140-
text = text.replace(f'{{{k}}}', str(v))
141-
return text
142-
143-
def set_gaming_tool(tool, enabled):
144-
global enable_gamescope, enable_mangohud, enable_vkbasalt
145-
if tool == 'gamescope':
146-
enable_gamescope = enabled
147-
elif tool == 'mangohud':
148-
enable_mangohud = enabled
149-
elif tool == 'vkbasalt':
150-
enable_vkbasalt = enabled
151-
logging.info(f'Toggled {tool} to {enabled}')
163+
def setup_language():
164+
# Placeholder for language initialization logic if needed
165+
pass

0 commit comments

Comments
 (0)