-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain-application.py
450 lines (378 loc) · 15.3 KB
/
main-application.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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
import sys
import os
import json
import webbrowser
from typing import Dict, Any
from dataclasses import dataclass
from PyQt6.QtWidgets import (
QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout,
QLabel, QPushButton, QFileDialog, QMessageBox, QTabWidget,
QMenuBar, QMenu, QStatusBar, QDialog, QDialogButtonBox,
QLineEdit, QTextEdit, QComboBox
)
from PyQt6.QtCore import Qt, QSize
from PyQt6.QtGui import QAction, QIcon
class ConfigManager:
"""Manages configuration file operations"""
@staticmethod
def load_config(filepath: str) -> dict:
"""Load configuration from file"""
config = {}
current_section = None
try:
with open(filepath, 'r') as file:
for line in file:
line = line.strip()
if line.startswith('#'):
if 'Section' in line:
current_section = line.split(':')[-1].strip()
config[current_section] = {}
elif '=' in line:
if current_section:
key, value = line.split('=', 1)
config[current_section][key.strip()] = value.strip()
return config
except Exception as e:
raise Exception(f"Failed to load configuration: {str(e)}")
@staticmethod
def save_config(config: dict, filepath: str):
"""Save configuration to file"""
try:
with open(filepath, 'w') as file:
for section, values in config.items():
file.write(f"############################################################################################\n")
file.write(f"# Section: {section}\n")
file.write(f"############################################################################################\n\n")
for key, value in values.items():
file.write(f"{key}={value}\n")
file.write("\n")
except Exception as e:
raise Exception(f"Failed to save configuration: {str(e)}")
class ConfigurationApp(QMainWindow):
"""Main application window"""
def __init__(self):
super().__init__()
self.current_file = None
self.config_manager = ConfigManager()
self.setup_ui()
def setup_ui(self):
"""Initialize the user interface"""
self.setWindowTitle("Environment Configuration Manager")
self.setMinimumSize(1200, 800)
# Create central widget and layout
central_widget = QWidget()
self.setCentralWidget(central_widget)
layout = QVBoxLayout(central_widget)
# Create menu bar
self.create_menu_bar()
# Create tab widget
self.tab_widget = QTabWidget()
layout.addWidget(self.tab_widget)
# Add configuration tabs
self.add_configuration_tabs()
# Create status bar
self.status_bar = QStatusBar()
self.setStatusBar(self.status_bar)
# Set initial status
self.update_status("Ready")
def create_menu_bar(self):
"""Create the application menu bar"""
menubar = self.menuBar()
# File menu
file_menu = menubar.addMenu("File")
new_action = QAction("New", self)
new_action.setShortcut("Ctrl+N")
new_action.triggered.connect(self.new_config)
file_menu.addAction(new_action)
open_action = QAction("Open", self)
open_action.setShortcut("Ctrl+O")
open_action.triggered.connect(self.open_config)
file_menu.addAction(open_action)
save_action = QAction("Save", self)
save_action.setShortcut("Ctrl+S")
save_action.triggered.connect(self.save_config)
file_menu.addAction(save_action)
save_as_action = QAction("Save As...", self)
save_as_action.setShortcut("Ctrl+Shift+S")
save_as_action.triggered.connect(self.save_config_as)
file_menu.addAction(save_as_action)
file_menu.addSeparator()
exit_action = QAction("Exit", self)
exit_action.setShortcut("Ctrl+Q")
exit_action.triggered.connect(self.close)
file_menu.addAction(exit_action)
# Tools menu
tools_menu = menubar.addMenu("Tools")
validate_action = QAction("Validate All", self)
validate_action.triggered.connect(self.validate_all)
tools_menu.addAction(validate_action)
export_action = QAction("Export Configuration", self)
export_action.triggered.connect(self.export_config)
tools_menu.addAction(export_action)
# Help menu
help_menu = menubar.addMenu("Help")
about_action = QAction("About", self)
about_action.triggered.connect(self.show_about)
help_menu.addAction(about_action)
docs_action = QAction("Documentation", self)
docs_action.triggered.connect(self.show_documentation)
help_menu.addAction(docs_action)
def add_configuration_tabs(self):
"""Add all configuration section tabs"""
# Import all section widgets
from host_config import BroadseaHostSection
from atlas_config import AtlasGUISection
from webapi_config import WebAPISection
from security_config import SecuritySection
from datasource_config import DataSourceSection
from build_config import BuildSection
from monitoring_config import MonitoringSection
# Create and add tabs
self.sections = {
"Host": BroadseaHostSection(),
"Atlas": AtlasGUISection(),
"WebAPI": WebAPISection(),
"Security": SecuritySection(),
"DataSource": DataSourceSection(),
"Build": BuildSection(),
"Monitoring": MonitoringSection()
}
for name, section in self.sections.items():
self.tab_widget.addTab(section, name)
def new_config(self):
"""Create new configuration"""
if self.check_unsaved_changes():
self.current_file = None
for section in self.sections.values():
section.reset_to_defaults()
self.update_status("New configuration created")
def open_config(self):
"""Open configuration file"""
if not self.check_unsaved_changes():
return
filename, _ = QFileDialog.getOpenFileName(
self,
"Open Configuration",
"",
"Environment Files (*.env);;All Files (*.*)"
)
if filename:
try:
config = self.config_manager.load_config(filename)
self.current_file = filename
# Update each section with loaded configuration
for section in self.sections.values():
section.load_config(config.get(section.section_name, {}))
self.update_status(f"Loaded configuration from {filename}")
except Exception as e:
QMessageBox.critical(
self,
"Error",
f"Failed to load configuration: {str(e)}"
)
def save_config(self):
"""Save current configuration"""
if not self.current_file:
self.save_config_as()
return
try:
config = {}
for section in self.sections.values():
config[section.section_name] = section.get_config()
self.config_manager.save_config(config, self.current_file)
self.update_status(f"Saved configuration to {self.current_file}")
except Exception as e:
QMessageBox.critical(
self,
"Error",
f"Failed to save configuration: {str(e)}"
)
def save_config_as(self):
"""Save configuration to new file"""
filename, _ = QFileDialog.getSaveFileName(
self,
"Save Configuration As",
"",
"Environment Files (*.env);;All Files (*.*)"
)
if filename:
self.current_file = filename
self.save_config()
def validate_all(self):
"""Validate all configuration sections"""
issues = []
for name, section in self.sections.items():
section_issues = section.validate()
if section_issues:
issues.extend([f"{name}: {issue}" for issue in section_issues])
if issues:
QMessageBox.warning(
self,
"Validation Issues",
"The following issues were found:\n\n" + "\n".join(issues)
)
else:
QMessageBox.information(
self,
"Validation Success",
"All configuration sections are valid!"
)
def export_config(self):
"""Export configuration to different formats"""
formats = {
"JSON": (".json", self.export_json),
"YAML": (".yaml", self.export_yaml),
"Docker Compose": (".yml", self.export_docker_compose)
}
dialog = ExportDialog(list(formats.keys()), self)
if dialog.exec():
format_name = dialog.get_selected_format()
if format_name in formats:
extension, export_func = formats[format_name]
filename, _ = QFileDialog.getSaveFileName(
self,
f"Export as {format_name}",
"",
f"{format_name} Files (*{extension});;All Files (*.*)"
)
if filename:
try:
export_func(filename)
self.update_status(f"Exported configuration as {format_name}")
except Exception as e:
QMessageBox.critical(
self,
"Export Error",
f"Failed to export configuration: {str(e)}"
)
def export_json(self, filename):
"""Export configuration as JSON"""
config = {}
for section in self.sections.values():
config[section.section_name] = section.get_config()
with open(filename, 'w') as f:
json.dump(config, f, indent=2)
def export_yaml(self, filename):
"""Export configuration as YAML"""
try:
import yaml
config = {}
for section in self.sections.values():
config[section.section_name] = section.get_config()
with open(filename, 'w') as f:
yaml.dump(config, f, default_flow_style=False)
except ImportError:
raise Exception("PyYAML is required for YAML export")
def export_docker_compose(self, filename):
"""Export configuration as Docker Compose file"""
services = {
"atlas": {
"image": f"ohdsi/atlas:{self.sections['Atlas'].get_config()['ATLAS_VERSION']}",
"ports": [f"{self.sections['Atlas'].get_config()['ATLAS_PORT']}:8080"],
"environment": self.sections['Atlas'].get_config(),
"depends_on": ["postgres", "webapi"]
},
"webapi": {
"image": f"ohdsi/webapi:{self.sections['WebAPI'].get_config()['WEBAPI_VERSION']}",
"ports": [f"{self.sections['WebAPI'].get_config()['WEBAPI_PORT']}:8080"],
"environment": self.sections['WebAPI'].get_config(),
"depends_on": ["postgres"]
},
"postgres": {
"image": "postgres:13",
"ports": ["5432:5432"],
"environment": {
"POSTGRES_USER": self.sections['DataSource'].get_config()['DATASOURCE_DB_USER'],
"POSTGRES_PASSWORD": self.sections['DataSource'].get_config()['DATASOURCE_DB_PASS'],
"POSTGRES_DB": self.sections['DataSource'].get_config()['DATASOURCE_DB_NAME']
},
"volumes": ["postgres_data:/var/lib/postgresql/data"]
}
}
compose_config = {
"version": "3.8",
"services": services,
"volumes": {
"postgres_data": None
}
}
with open(filename, 'w') as f:
yaml.dump(compose_config, f, default_flow_style=False)
def show_about(self):
"""Show about dialog"""
QMessageBox.about(
self,
"About Environment Configuration Manager",
"Environment Configuration Manager v1.0\n\n"
"A tool for managing OHDSI environment configurations."
)
def show_documentation(self):
"""Show documentation"""
docs_url = "https://github.com/OHDSI/Broadsea/wiki"
try:
webbrowser.open(docs_url)
except Exception as e:
QMessageBox.warning(
self,
"Documentation Error",
f"Failed to open documentation: {str(e)}\n\n"
f"Please visit {docs_url} manually."
)
def check_unsaved_changes(self) -> bool:
"""Check for unsaved changes"""
if not self.current_file:
return True
try:
current_config = {}
for section in self.sections.values():
current_config[section.section_name] = section.get_config()
saved_config = self.config_manager.load_config(self.current_file)
if current_config != saved_config:
reply = QMessageBox.question(
self,
"Unsaved Changes",
"You have unsaved changes. Do you want to continue?",
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No,
QMessageBox.StandardButton.No
)
return reply == QMessageBox.StandardButton.Yes
except Exception:
# If there's any error reading the saved file, assume it's safe to proceed
pass
return True
def update_status(self, message: str):
"""Update status bar message"""
self.status_bar.showMessage(message)
class ExportDialog(QDialog):
"""Dialog for selecting export format"""
def __init__(self, formats, parent=None):
super().__init__(parent)
self.formats = formats
self.setup_ui()
def setup_ui(self):
"""Setup the dialog UI"""
self.setWindowTitle("Export Configuration")
layout = QVBoxLayout(self)
# Format selection
layout.addWidget(QLabel("Select Export Format:"))
self.format_combo = QComboBox()
self.format_combo.addItems(self.formats)
layout.addWidget(self.format_combo)
# Buttons
button_box = QDialogButtonBox(
QDialogButtonBox.StandardButton.Ok |
QDialogButtonBox.StandardButton.Cancel
)
button_box.accepted.connect(self.accept)
button_box.rejected.connect(self.reject)
layout.addWidget(button_box)
def get_selected_format(self) -> str:
"""Get selected format"""
return self.format_combo.currentText()
def main():
app = QApplication(sys.argv)
window = ConfigurationApp()
window.show()
sys.exit(app.exec())
if __name__ == "__main__":
main()