Skip to content

Commit c9b506c

Browse files
committed
more robust editing of AI profile name
make sure that the new name can be used as a section name in the ini file and that it is unique
1 parent 43a43d2 commit c9b506c

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

src/qualcoder/settings.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import sys
2525
from PyQt6 import QtGui, QtWidgets, QtCore
2626
import copy
27+
import re
2728

2829
from .GUI.ui_dialog_settings import Ui_Dialog_settings
2930
from .helpers import Message
@@ -282,6 +283,23 @@ def ai_profile_name_edit(self):
282283
curr_name # text
283284
)
284285
if ok and new_name != '':
286+
# clean up new name for use in ini file
287+
new_name = new_name.replace('[', '').replace(']', '') # Remove square brackets
288+
new_name = re.sub(r'[\r\n]+', ' ', new_name) # Replace line breaks with a space
289+
new_name = re.sub(r'\s+', ' ', new_name) # Remove repeated spaces
290+
new_name = new_name.strip() # Remove leading/trailing whitespace
291+
# if name not altered, return
292+
if new_name == curr_name:
293+
return
294+
# make the new name unique
295+
existing_names = {model['name'] for model in self.ai_models}
296+
i = 1
297+
candidate = new_name
298+
while candidate in existing_names: # Find next available unique name: new_name_1, new_name_2, etc.
299+
candidate = f"{new_name}_{i}"
300+
i += 1
301+
new_name = candidate
302+
285303
self.ai_models[int(self.settings['ai_model_index'])]['name'] = new_name
286304
with QtCore.QSignalBlocker(self.ui.comboBox_ai_profile):
287305
self.ui.comboBox_ai_profile.setItemText(int(self.settings['ai_model_index']), new_name)

0 commit comments

Comments
 (0)