-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdesktop_config.py
More file actions
130 lines (107 loc) · 4.37 KB
/
desktop_config.py
File metadata and controls
130 lines (107 loc) · 4.37 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
#!/usr/bin/env python3
"""
Configuration manager for desktop application
"""
import os
import configparser
from pathlib import Path
class ConfigManager:
"""Manage application configuration"""
def __init__(self):
self.config_file = "desktop_config.ini"
self.config = configparser.ConfigParser()
self.load_config()
def load_config(self):
"""Load configuration from file or create default"""
if os.path.exists(self.config_file):
try:
self.config.read(self.config_file)
except Exception as e:
print(f"Warning: Could not load config file: {e}")
self.create_default_config()
else:
self.create_default_config()
def create_default_config(self):
"""Create default configuration"""
# Try to read from existing config.env
api_key = ""
if os.path.exists("config.env"):
try:
with open("config.env", "r") as f:
for line in f:
line = line.strip()
if line.startswith("TRANSCRIBE_API_KEY="):
api_key = self._clean_key(line.split("=", 1)[1])
break
except Exception:
pass
self.config["API"] = {
"transcribe_api_key": api_key,
"translate_api_key": api_key,
"transcribe_base_url": "https://aihubmix.com/v1",
"translate_base_url": "https://aihubmix.com/v1",
"transcribe_model": "whisper-1",
"translate_model": "gemini-2.5-flash-lite"
}
self.config["APP"] = {
"max_file_size": "500",
"output_directory": "",
"auto_open_results": "true"
}
self.save_config()
def save_config(self):
"""Save configuration to file"""
try:
with open(self.config_file, "w") as f:
self.config.write(f)
except Exception as e:
print(f"Warning: Could not save config file: {e}")
def get_transcribe_api_key(self):
"""Get transcription API key"""
raw = self.config.get("API", "transcribe_api_key", fallback="")
return self._clean_key(raw)
def get_translate_api_key(self):
"""Get translation API key"""
raw = self.config.get("API", "translate_api_key", fallback="")
return self._clean_key(raw)
def get_transcribe_base_url(self):
"""Get transcription API base URL"""
return self.config.get("API", "transcribe_base_url", fallback="https://aihubmix.com/v1")
def get_translate_base_url(self):
"""Get translation API base URL"""
return self.config.get("API", "translate_base_url", fallback="https://aihubmix.com/v1")
def get_transcribe_model(self):
"""Get transcription model"""
return self.config.get("API", "transcribe_model", fallback="whisper-1")
def get_translate_model(self):
"""Get translation model"""
return self.config.get("API", "translate_model", fallback="gemini-2.5-flash-lite")
def get_output_directory(self):
"""Get output directory"""
return self.config.get("APP", "output_directory", fallback="")
def set_output_directory(self, directory):
"""Set output directory"""
self.config.set("APP", "output_directory", directory)
self.save_config()
def should_auto_open_results(self):
"""Check if results should be auto-opened"""
return self.config.getboolean("APP", "auto_open_results", fallback=True)
def set_api_key(self, key):
"""Set API key for both transcription and translation"""
self.config.set("API", "transcribe_api_key", key)
self.config.set("API", "translate_api_key", key)
self.save_config()
def _clean_key(self, raw):
"""Strip inline comments/whitespace/quotes from API keys"""
if not raw:
return ""
cleaned = raw.strip()
for sep in ("#", ";"):
if sep in cleaned:
cleaned = cleaned.split(sep, 1)[0].strip()
# Remove surrounding quotes if present
if (cleaned.startswith('"') and cleaned.endswith('"')) or (cleaned.startswith("'") and cleaned.endswith("'")):
cleaned = cleaned[1:-1].strip()
return cleaned
# Global config instance
config = ConfigManager()