fosKeyMan
Loading...
Searching...
No Matches
configmanager.py
Go to the documentation of this file.
1import json
2import os
3from PySide6.QtCore import QObject
4from PySide6.QtWidgets import QFileDialog, QMessageBox
5
6
7class ConfigManager(QObject):
8 r"""
9 Manage the loading, saving, and validation of configuration settings for directories.
10 Language setting for the user interface (e.g., 'english' or 'german').
11 Handle user interaction for selecting directories, and ensures that the configuration is
12 correctly loaded from and saved to a JSON file.
13 """
14 DEFAULT_COLUMNS = ["Project", "Operator", "Specimen", "DFOS_Type", "Installation", "Note"]
15
16 def __init__(self, config_path):
17 r"""
18 Initialize the ConfigManager with the path to the configuration file.
19
20 \param config_path (str): The file path where the configuration is stored.
21 """
22 super(ConfigManager, self).__init__()
23 self.config_path = config_path
24 self.directory1 = None
25 self.directory2 = None
26 self.directory3 = None
27 self.language = None
28 self.custom_columns = None
29 self.config = self.load_config()
30
32 r"""
33 Create a default configuration file. Default language is English.
34
35 \return (dict): The default configuration.
36 """
37 default_config = {
38 'directory1': '',
39 'directory2': '',
40 'directory3': '',
41 'language': 'english',
42 'custom_columns': self.DEFAULT_COLUMNS
43 }
44 with open(self.config_path, 'w') as f:
45 json.dump(default_config, f)
46 return default_config
47
48 def load_config(self):
49 r"""
50 Load the configuration file. If it doesn't exist or has errors, return an empty configuration.
51
52 \return (dict): The loaded configuration as a dictionary, or the default dictionary if the file contains errors.
53 """
54 if os.path.exists(self.config_path):
55 try:
56 with open(self.config_path, 'r') as f:
57 return json.load(f)
58 except json.JSONDecodeError:
59 return self.create_default_config()
60 else:
61 return self.create_default_config()
62
63 def save_config(self):
64 r"""
65 Save the current directory paths to the configuration file.
66 """
67 config_data = {
68 'directory1': self.directory1,
69 'directory2': self.directory2,
70 'directory3': self.directory3,
71 'language': self.language,
72 'custom_columns': self.custom_columns
73 }
74 with open(self.config_path, 'w') as f:
75 json.dump(config_data, f)
76
78 r"""
79 Load and check if previously saved directories are valid.
80
81 \return (tuple): directory1, directory2, language
82 """
83 dir1 = self.config.get('directory1', None)
84 dir2 = self.config.get('directory2', None)
85 dir3 = self.config.get('directory3', None)
86 language = self.config.get('language', 'english')
87 columns = self.config.get('custom_columns', self.DEFAULT_COLUMNS)
88
89 if dir1 and os.path.exists(dir1):
90 self.directory1 = dir1
91
92 if dir2 and os.path.exists(dir2):
93 self.directory2 = dir2
94
95 if dir3 and os.path.exists(dir3):
96 self.directory3 = dir3
97
98 if language in ('english', 'german'):
99 self.language = language
100 else:
101 self.language = 'english'
102
103 if isinstance(columns, list) and all(isinstance(col, str) for col in columns):
104 self.custom_columns = columns
105 else:
107
108 return self.directory1, self.directory2, self.directory3, self.language, self.custom_columns
109
110 def select_directory1(self, dialog, open_ui):
111 r"""
112 Open a file dialog for the user to select the first (activated keyfile) directory.
113
114 \param dialog (QDialog): The dialog window that allows the user to select directories.
115 \param open_ui (QWidget): The UI that contains the directory input fields.
116 """
117 dir1 = QFileDialog.getExistingDirectory(dialog, self.tr("Please select the activation key file directory"))
118 if dir1:
119 self.directory1 = dir1
120 open_ui.acLineEdit.setText(dir1)
121
122 def select_directory2(self, dialog, open_ui):
123 r"""
124 Open a file dialog for the user to select the second (deactivated keyfile) directory.
125
126 \param dialog (QDialog): The dialog window that allows the user to select directories.
127 \param open_ui (QWidget): The UI that contains the directory input fields.
128 """
129 dir2 = QFileDialog.getExistingDirectory(dialog, self.tr("Please select the deactivation key file directory"))
130 if dir2:
131 self.directory2 = dir2
132 open_ui.deacLineEdit.setText(dir2)
133
134 def select_directory3(self, dialog, open_ui):
135 r"""
136 Open a file dialog for the user to select the trash directory.
137
138 \param dialog (QDialog): The dialog window that allows the user to select directories.
139 \param open_ui (QWidget): The UI that contains the directory input fields.
140 """
141 dir3 = QFileDialog.getExistingDirectory(dialog, self.tr("Please select the trash key file directory"))
142 if dir3:
143 self.directory3 = dir3
144 open_ui.trashLineEdit.setText(dir3)
145
146 def confirm_directory_selection(self, dialog, open_ui):
147 r"""
148 Confirm the directory selection made by the user.
149 Validate that both directories exist, and close the dialog if valid.
150
151 \param dialog (QDialog): The dialog window that allows the user to select directories.
152 \param open_ui (QWidget): The UI that contains the directory input fields.
153 """
154 dir1 = open_ui.acLineEdit.text().strip()
155 dir2 = open_ui.deacLineEdit.text().strip()
156 dir3 = open_ui.trashLineEdit.text().strip()
157
158 if not dir1 or not os.path.exists(dir1):
159 QMessageBox.warning(dialog, self.tr("Error"),
160 self.tr("Please select a valid directory for activated keyfiles."))
161 return
162 if not dir2 or not os.path.exists(dir2):
163 QMessageBox.warning(dialog, self.tr("Error"),
164 self.tr("Please select a valid directory for deactivated keyfiles."))
165 return
166 if not dir3 or not os.path.exists(dir3):
167 QMessageBox.warning(dialog, self.tr("Error"),
168 self.tr("Please select a valid directory for deactivated keyfiles."))
169 return
170
171 dialog.accept()
Manage the loading, saving, and validation of configuration settings for directories.
load_config(self)
Load the configuration file.
select_directory2(self, dialog, open_ui)
Open a file dialog for the user to select the second (deactivated keyfile) directory.
select_directory1(self, dialog, open_ui)
Open a file dialog for the user to select the first (activated keyfile) directory.
save_config(self)
Save the current directory paths to the configuration file.
select_directory3(self, dialog, open_ui)
Open a file dialog for the user to select the trash directory.
check_and_load_previous_config(self)
Load and check if previously saved directories are valid.
__init__(self, config_path)
Initialize the ConfigManager with the path to the configuration file.
confirm_directory_selection(self, dialog, open_ui)
Confirm the directory selection made by the user.
create_default_config(self)
Create a default configuration file.