Skip to content

Commit c1adf36

Browse files
committed
Bring back config file
- Remember previously used .mar in config and use on startup - Store config file in correct folder independent of OS
1 parent 123fd7d commit c1adf36

File tree

4 files changed

+44
-29
lines changed

4 files changed

+44
-29
lines changed

mal_gui/app.py

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import os
12
import sys
23

4+
from appdirs import user_config_dir
5+
36
if __name__ == "__main__" and __package__ is None:
47
print(
58
"Warning: You are running 'app.py' directly.\n"
@@ -34,22 +37,27 @@ def __init__(self, parent=None):
3437
verticalLayout = QVBoxLayout()
3538

3639
# Label to instruct the user
37-
self.label = QLabel("Select MAL Language mar file to load:")
40+
self.label = QLabel("Select MAL Language .mar file to load:")
3841
verticalLayout.addWidget(self.label)
3942

4043
horizontalLayout = QHBoxLayout()
44+
self.malLangFilePathText = QLineEdit(self)
45+
46+
# Load the config file containing latest lang file path
47+
config_file_dir = user_config_dir("mal-gui", "mal-lang")
48+
self.config_file_path = config_file_dir + '/config.ini'
4149

42-
self.malLanguageMarFilePathText = QLineEdit(self)
50+
# Make sure config file exists
51+
os.makedirs(os.path.dirname(self.config_file_path), exist_ok=True)
4352

44-
# Load the config file
4553
self.config = configparser.ConfigParser()
46-
# self.config.read('config.ini')
47-
self.config.read('config.ini')
48-
self.marFilePath = self.config.get('Settings', 'marFilePath', fallback=None)
49-
print(f"Initial marFilePath path: {self.marFilePath}")
50-
self.malLanguageMarFilePathText.setText(self.marFilePath)
54+
self.config.read(self.config_file_path)
55+
self.selectedLangFile = self.config.get(
56+
'Settings', 'langFilePath', fallback=None)
57+
print(f"Initial langFilePath path: {self.selectedLangFile}")
58+
self.malLangFilePathText.setText(self.selectedLangFile)
5159

52-
horizontalLayout.addWidget(self.malLanguageMarFilePathText)
60+
horizontalLayout.addWidget(self.malLangFilePathText)
5361

5462
browseButton = QPushButton("Browse")
5563
horizontalLayout.addWidget(browseButton)
@@ -67,7 +75,7 @@ def __init__(self, parent=None):
6775
self.setLayout(verticalLayout)
6876

6977
browseButton.clicked.connect(self.openFileDialog)
70-
loadButton.clicked.connect(self.loadFile)
78+
loadButton.clicked.connect(self.saveLangFilePath)
7179
quitButton.clicked.connect(self.reject)
7280

7381
def openFileDialog(self):
@@ -80,36 +88,42 @@ def openFileDialog(self):
8088
fileDialog.setWindowTitle("Select a MAR File")
8189

8290
if fileDialog.exec() == QFileDialog.Accepted:
83-
selectedFilePath = fileDialog.selectedFiles()[0]
84-
self.malLanguageMarFilePathText.setText(selectedFilePath)
91+
selectedLangFilePath = fileDialog.selectedFiles()[0]
92+
self.malLangFilePathText.setText(selectedLangFilePath)
93+
94+
def saveLangFilePath(self):
95+
"""
96+
Set current language MAR archive file and store
97+
latest chosen language in user config file
98+
"""
8599

86-
def loadFile(self):
87-
selectedFile = self.malLanguageMarFilePathText.text()
100+
selectedLangFile = self.malLangFilePathText.text()
88101

89-
# Check if the path ends with .mar or .jar --> Need to confirm with Andrei
90-
# if selectedFile.endswith(('.jar','.mar')):
102+
if selectedLangFile.endswith('.mar'):
103+
self.selectedLangFile = selectedLangFile
104+
105+
# Remember language choice in user settings
106+
self.config.set('Settings', 'langFilePath', self.selectedLangFile)
107+
with open(self.config_file_path, 'w') as configfile:
108+
self.config.write(configfile)
91109

92-
if selectedFile.endswith('.mar'):
93-
self.selectedFile = selectedFile
94110
self.accept() # Close the dialog and return accepted
95111
else:
96112
QMessageBox.warning(self, "Invalid File", "Please select a valid .mar file.")
97113

98114
def getSelectedFile(self):
99-
return self.selectedFile
115+
return self.selectedLangFile
100116

101117

102118
def main():
103119
app = QApplication(sys.argv)
104120

105121
dialog = FileSelectionDialog()
106122
if dialog.exec() == QDialog.Accepted:
107-
selectedFilePath = dialog.getSelectedFile()
108-
109-
window = MainWindow(app,selectedFilePath)
123+
selectedLangFilePath = dialog.getSelectedFile()
124+
window = MainWindow(app, selectedLangFilePath)
110125
window.show()
111-
112-
print(f"Selected MAR file Path: {selectedFilePath}")
126+
print(f"Selected MAR file Path: {selectedLangFilePath}")
113127

114128
app.exec()
115129
else:

mal_gui/config.ini

Lines changed: 0 additions & 3 deletions
This file was deleted.

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ dependencies = [
1717
"PySide6_Essentials==6.7.0",
1818
"shiboken6==6.7.0",
1919
"mal-toolbox>=0.1.8",
20-
"qt-material==2.14"
20+
"qt-material==2.14",
21+
"appdirs==1.4.4"
2122
]
2223

2324
[project.scripts]

requirements.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@ PySide6==6.7.0
44
PySide6_Addons==6.7.0
55
PySide6_Essentials==6.7.0
66
shiboken6==6.7.0
7-
mal-toolbox>=0.1.8
87
qt-material==2.14
8+
mal-toolbox>=0.1.8
9+
10+
# For config file path
11+
appdirs==1.4.4

0 commit comments

Comments
 (0)