1
+ import os
1
2
import sys
2
3
4
+ from appdirs import user_config_dir
5
+
3
6
if __name__ == "__main__" and __package__ is None :
4
7
print (
5
8
"Warning: You are running 'app.py' directly.\n "
@@ -34,22 +37,27 @@ def __init__(self, parent=None):
34
37
verticalLayout = QVBoxLayout ()
35
38
36
39
# 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:" )
38
41
verticalLayout .addWidget (self .label )
39
42
40
43
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'
41
49
42
- self .malLanguageMarFilePathText = QLineEdit (self )
50
+ # Make sure config file exists
51
+ os .makedirs (os .path .dirname (self .config_file_path ), exist_ok = True )
43
52
44
- # Load the config file
45
53
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 )
51
59
52
- horizontalLayout .addWidget (self .malLanguageMarFilePathText )
60
+ horizontalLayout .addWidget (self .malLangFilePathText )
53
61
54
62
browseButton = QPushButton ("Browse" )
55
63
horizontalLayout .addWidget (browseButton )
@@ -67,7 +75,7 @@ def __init__(self, parent=None):
67
75
self .setLayout (verticalLayout )
68
76
69
77
browseButton .clicked .connect (self .openFileDialog )
70
- loadButton .clicked .connect (self .loadFile )
78
+ loadButton .clicked .connect (self .saveLangFilePath )
71
79
quitButton .clicked .connect (self .reject )
72
80
73
81
def openFileDialog (self ):
@@ -80,36 +88,42 @@ def openFileDialog(self):
80
88
fileDialog .setWindowTitle ("Select a MAR File" )
81
89
82
90
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
+ """
85
99
86
- def loadFile (self ):
87
- selectedFile = self .malLanguageMarFilePathText .text ()
100
+ selectedLangFile = self .malLangFilePathText .text ()
88
101
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 )
91
109
92
- if selectedFile .endswith ('.mar' ):
93
- self .selectedFile = selectedFile
94
110
self .accept () # Close the dialog and return accepted
95
111
else :
96
112
QMessageBox .warning (self , "Invalid File" , "Please select a valid .mar file." )
97
113
98
114
def getSelectedFile (self ):
99
- return self .selectedFile
115
+ return self .selectedLangFile
100
116
101
117
102
118
def main ():
103
119
app = QApplication (sys .argv )
104
120
105
121
dialog = FileSelectionDialog ()
106
122
if dialog .exec () == QDialog .Accepted :
107
- selectedFilePath = dialog .getSelectedFile ()
108
-
109
- window = MainWindow (app ,selectedFilePath )
123
+ selectedLangFilePath = dialog .getSelectedFile ()
124
+ window = MainWindow (app , selectedLangFilePath )
110
125
window .show ()
111
-
112
- print (f"Selected MAR file Path: { selectedFilePath } " )
126
+ print (f"Selected MAR file Path: { selectedLangFilePath } " )
113
127
114
128
app .exec ()
115
129
else :
0 commit comments