Skip to content

Commit 73dd68d

Browse files
authored
use pathlib not os where possible
1 parent 13857c5 commit 73dd68d

9 files changed

+45
-36
lines changed

src/gui.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def is_nvidia_gpu(self):
4444
return False
4545

4646
def load_config(self):
47-
script_dir = os.path.dirname(os.path.realpath(__file__))
47+
script_dir = Path(__file__).resolve().parent
4848
config_path = os.path.join(script_dir, 'config.yaml')
4949
with open(config_path, 'r') as file:
5050
config = yaml.safe_load(file)
@@ -53,7 +53,7 @@ def load_config(self):
5353
def init_ui(self):
5454
main_splitter = QSplitter(Qt.Horizontal)
5555
self.setWindowTitle('LM Studio ChromaDB Plugin - www.chintellalaw.com')
56-
self.setGeometry(300, 300, 1060, 1060)
56+
self.setGeometry(300, 300, 1077, 1077)
5757
self.setMinimumSize(450, 510)
5858

5959
# LEFT FRAME

src/gui_tabs.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from PySide6.QtWebEngineWidgets import QWebEngineView
33
from PySide6.QtWidgets import QTextEdit, QTabWidget, QVBoxLayout, QWidget, QGroupBox, QPushButton, QHBoxLayout
44
from PySide6.QtCore import QUrl
5-
import os
5+
from pathlib import Path
66
from gui_tabs_settings import GuiSettingsTab
77
from gui_tabs_tools import GuiSettingsTab as ToolsSettingsTab
88

@@ -17,7 +17,7 @@ def create_tabs():
1717
tab_font.setPointSize(13)
1818
tab_widget.setFont(tab_font)
1919

20-
user_manual_folder = os.path.join(os.path.dirname(__file__), 'User_Manual')
20+
user_manual_folder = Path(__file__).parent / 'User_Manual'
2121

2222
# SETTINGS TAB
2323
settings_tab = GuiSettingsTab()
@@ -43,8 +43,8 @@ def create_tabs():
4343

4444
for button_name, html_file in buttons_dict.items():
4545
button = QPushButton(button_name)
46-
button_url = os.path.join(user_manual_folder, html_file)
47-
button.clicked.connect(partial(load_url, user_guide_view, button_url))
46+
button_url = user_manual_folder / html_file
47+
button.clicked.connect(partial(load_url, user_guide_view, str(button_url)))
4848
menu_layout.addWidget(button)
4949

5050
menu_group.setLayout(menu_layout)

src/gui_tabs_settings_bark.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from PySide6.QtWidgets import QLabel, QComboBox, QWidget, QHBoxLayout, QVBoxLayout, QApplication, QCheckBox
22
import yaml
3-
import os
3+
from pathlib import Path
44

55
class BarkModelSettingsTab(QWidget):
66

@@ -55,9 +55,10 @@ def initialize_layout(self):
5555
self.setLayout(main_layout)
5656

5757
def load_config_and_set_values(self):
58-
if os.path.exists('config.yaml'):
58+
config_file_path = Path('config.yaml')
59+
if config_file_path.exists():
5960
try:
60-
with open('config.yaml', 'r') as f:
61+
with open(config_file_path, 'r') as f:
6162
config = yaml.safe_load(f)
6263
except Exception as e:
6364
config = None
@@ -80,8 +81,8 @@ def connect_signals(self):
8081
self.cpu_offload_checkbox.stateChanged.connect(self.update_config)
8182

8283
def update_config(self):
83-
config_file_path = 'config.yaml'
84-
if os.path.exists(config_file_path):
84+
config_file_path = Path('config.yaml')
85+
if config_file_path.exists():
8586
try:
8687
with open(config_file_path, 'r') as f:
8788
config = yaml.safe_load(f)

src/gui_tabs_settings_database.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import yaml
55
from pathlib import Path
66
import shutil
7-
import os
87

98
class DatabaseSettingsTab(QWidget):
109
def __init__(self):

src/gui_tabs_settings_server.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from PySide6.QtWidgets import QWidget, QLabel, QLineEdit, QGridLayout, QMessageBox, QSizePolicy, QCheckBox, QComboBox
22
from PySide6.QtGui import QIntValidator, QDoubleValidator
33
import yaml
4-
import os
4+
from pathlib import Path
55

66
class ServerSettingsTab(QWidget):
77
def __init__(self):
@@ -81,6 +81,10 @@ def create_edit(self, setting, settings_dict):
8181
self.widgets[setting]['edit'] = edit
8282
return edit
8383

84+
def refresh_labels(self):
85+
self.widgets['prefix']['label'].setText(f"Prefix: {self.config_data.get('server', {}).get('prefix', '')}")
86+
self.widgets['suffix']['label'].setText(f"Suffix: {self.config_data.get('server', {}).get('suffix', '')}")
87+
8488
def update_prefix_suffix(self, index):
8589
option = self.prompt_format_combobox.currentText()
8690

@@ -100,10 +104,10 @@ def update_prefix_suffix(self, index):
100104
self.widgets['suffix']['edit'].setText(self.config_data.get('server', {}).get(suffix_key, ''))
101105

102106
def update_config(self):
103-
config_file_path = 'config.yaml'
104-
if os.path.exists(config_file_path):
107+
config_file_path = Path('config.yaml')
108+
if config_file_path.exists():
105109
try:
106-
with open(config_file_path, 'r') as file:
110+
with config_file_path.open('r') as file:
107111
config_data = yaml.safe_load(file)
108112
except Exception as e:
109113
config_data = {}
@@ -127,7 +131,10 @@ def update_config(self):
127131
config_data['server']['prompt_format_disabled'] = current_disable_state
128132

129133
if updated:
130-
with open(config_file_path, 'w') as file:
134+
with config_file_path.open('w') as file:
131135
yaml.safe_dump(config_data, file)
132136

137+
self.config_data = config_data
138+
self.refresh_labels()
139+
133140
return updated

src/gui_tabs_settings_vision.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from PySide6.QtWidgets import QLabel, QGridLayout, QVBoxLayout, QComboBox, QCheckBox, QSpinBox, QWidget
22
from PySide6.QtCore import Qt
33
import yaml
4-
import os
4+
from pathlib import Path
55

66
class VisionSettingsTab(QWidget):
77
def __init__(self):
@@ -94,8 +94,8 @@ def updateChosenQuant(self):
9494
self.updateConfigFile()
9595

9696
def updateConfigFile(self):
97-
config_file_path = 'config.yaml'
98-
if os.path.exists(config_file_path):
97+
config_file_path = Path('config.yaml')
98+
if config_file_path.exists():
9999
try:
100100
with open(config_file_path, 'r') as file:
101101
current_config = yaml.safe_load(file)

src/gui_tabs_settings_whisper.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import ctranslate2
44
import yaml
55
from voice_recorder_module import VoiceRecorder
6-
import os
6+
from pathlib import Path
77

88
class TranscriberSettingsTab(QWidget):
99

@@ -86,8 +86,8 @@ def create_layout(self):
8686
self.setLayout(layout)
8787

8888
def update_config(self):
89-
config_file_path = 'config.yaml'
90-
if os.path.exists(config_file_path):
89+
config_file_path = Path('config.yaml')
90+
if config_file_path.exists():
9191
try:
9292
with open(config_file_path, 'r') as f:
9393
config_data = yaml.safe_load(f)

src/gui_tabs_tools_transcribe.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
from functools import partial
12
from PySide6.QtWidgets import (
23
QLabel, QComboBox, QWidget, QHBoxLayout, QVBoxLayout, QPushButton, QApplication, QCheckBox, QFileDialog
34
)
45
from PySide6.QtCore import Qt
56
import yaml
6-
import os
7+
from pathlib import Path
78
from constants import WHISPER_MODEL_NAMES
89
from transcribe_module import TranscribeFile
910
import threading
@@ -129,10 +130,11 @@ def update_model_in_config(self, new_model):
129130
self.update_config_file()
130131

131132
def select_audio_file(self):
132-
current_dir = os.path.dirname(os.path.realpath(__file__))
133-
file_name, _ = QFileDialog.getOpenFileName(self, "Select Audio File", current_dir)
133+
current_dir = Path(__file__).resolve().parent
134+
file_name, _ = QFileDialog.getOpenFileName(self, "Select Audio File", str(current_dir))
134135
if file_name:
135-
short_path = "..." + os.path.join(os.path.basename(os.path.dirname(file_name)), os.path.basename(file_name))
136+
file_path = Path(file_name)
137+
short_path = "..." + str(Path(file_path.parent.name) / file_path.name)
136138
self.file_path_label.setText(short_path)
137139
self.selected_audio_file = file_name
138140

@@ -146,7 +148,7 @@ def transcription_thread():
146148
transcriber.start_transcription()
147149

148150
threading.Thread(target=transcription_thread, daemon=True).start()
149-
print(f"Transcription process for {os.path.basename(self.selected_audio_file)} started.")
151+
print(f"Transcription process for {Path(self.selected_audio_file).name} started.")
150152

151153
if __name__ == "__main__":
152154
app = QApplication([])

src/gui_tabs_tools_vision.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from PySide6.QtWebEngineWidgets import QWebEngineView
33
from PySide6.QtCore import Qt, QThread, QUrl
44
import yaml
5-
import os
65
import multiprocessing
76
import vision_llava_module
87
import vision_cogvlm_module
@@ -37,8 +36,9 @@ def __init__(self):
3736

3837
# Initialize the WebEngineView
3938
self.webView = QWebEngineView()
40-
html_file_path = os.path.join(os.path.dirname(__file__), 'vision_model_table.html')
41-
self.webView.setUrl(QUrl.fromLocalFile(html_file_path))
39+
script_dir = Path(__file__).resolve().parent
40+
html_file_path = script_dir / 'vision_model_table.html'
41+
self.webView.setUrl(QUrl.fromLocalFile(str(html_file_path)))
4242
mainVLayout.addWidget(self.webView)
4343

4444
def confirmationBeforeProcessing(self):
@@ -52,15 +52,15 @@ def confirmationBeforeProcessing(self):
5252
self.startProcessing()
5353

5454
def startProcessing(self):
55-
script_dir = os.path.dirname(os.path.realpath(__file__))
56-
images_dir = Path(script_dir) / "Images_for_DB"
55+
script_dir = Path(__file__).resolve().parent
56+
images_dir = script_dir / "Images_for_DB"
5757

5858
with open('config.yaml', 'r') as file:
5959
updated_config = yaml.safe_load(file)
6060

6161
if platform.system() == "Darwin" and any(images_dir.iterdir()):
6262
QMessageBox.warning(self, "Error",
63-
"Image processing has been disabled for MacOS for the time being until a fix can be implemented. Please remove all files from the 'Images_for_DB' folder and try again.")
63+
"Image processing has been disabled for MacOS for the time being until a fix can be implemented. Please remove all files from the 'Images_for_DB' folder and try again.")
6464
return
6565

6666
chosen_model = updated_config['vision']['chosen_model']
@@ -86,8 +86,8 @@ def openFolderDialog(self):
8686
if file_path:
8787
self.folderPathLabel.setText(file_path)
8888

89-
config_file_path = 'config.yaml'
90-
if os.path.exists(config_file_path):
89+
config_file_path = Path('config.yaml')
90+
if config_file_path.exists():
9191
try:
9292
with open(config_file_path, 'r') as file:
9393
config = yaml.safe_load(file)

0 commit comments

Comments
 (0)