-
Notifications
You must be signed in to change notification settings - Fork 1
/
args.py
266 lines (193 loc) · 6.45 KB
/
args.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/python3
# -*- coding: utf8 -*-
import os, time, shutil
import configparser
import wx
import io
###
### Configuration
###
###
### This class holds config vars of the program + keeps them synced on disk.
### (config written back when program exits)
###
class Args :
args = None
def __init__( self, fname, game='kw' ) :
# Yeah global variable!
# Cos one program can have only one configuration duh...
# I'm tired of copying args to everywhere.
Args.args = self
self.icon = 'kw.ico' # default
self.game = game
#self.dirty = False # Has non-saved + changed options?
self.cfg_fname = fname # for saving to disk on exit.
self.last_replay = None
self.add_vs_info = False
self.add_username = True
self.add_faction = False
self.custom_date_format = None
self.mcmap = dict() # map crc to 1.02+R dict.
self.cfg = self.load_from_file( fname )
def __str__( self ) :
s = io.StringIO()
print( "last_replay:", self.last_replay, file=s )
print( "add_vs_info:", self.add_vs_info, file=s )
print( "add_username:", self.add_username, file=s )
print( "add_faction:", self.add_faction, file=s )
print( "custom_date_format:", self.custom_date_format, file=s )
print( "mcmap:", self.mcmap, file=s )
return s.getvalue()
def set_var( self, key, val, section='options' ) :
self.__dict__[ key ] = val # set self var.
if key == 'custom_date_format' :
val = val.replace( "%", "%%" )
# The % symbols all mess up setting this variable. :(
self.cfg[ section ][ key ] = str( val ) # set it in cfg.
self.save()
def remove_var( self, key, section='options' ) :
if not self.cfg.has_section( section ) :
return
if not key in self.cfg[ section ] :
return
self.cfg.remove_option( section, key )
# get variable.
# returns default if not in cfg.
def get_var( self, key, default=None, section='options' ) :
if not self.cfg.has_section( section ) :
self.cfg.add_section( section )
if not key in self.cfg[ section ] :
return default
return self.cfg[ section ][ key ]
# Same as get var (actually uses get_var) but converts them to bool
def get_bool( self, key, default=None, section='options' ) :
# awwwwwwww this is so confusing.
# So, if I invoke get_var with no default value, then I should get
# 'None' when section or key doesn't exist.
# Then I shall return default val.
# Otherwise it means some bool value exists to parse.
# I may use getboolean function safely,
# which will do 'on/off', 'true/false', 'yes/no' parsing burden.
val = self.get_var( key )
if val == None :
# Write the default values to the config
if default == True :
self.set_var( key, 'true', section )
else :
self.set_var( key, 'false', section )
return default
else :
return self.cfg.getboolean( section, key )
# similar to get_bool, but for int.
def get_int( self, key, default=0, section='options' ) :
val = self.get_var( key )
if val == None :
self.set_var( key, str( default ), section )
return default
else :
return self.cfg.getint( section, key )
def get_aka( self, uid ) :
return self.get_var( uid, section='akas' )
def remove_aka( self, uid ) :
return self.remove_var( uid, section='akas' )
def set_aka( self, uid, aka ) :
return self.set_var( uid, aka, section='akas' )
def akaed_name( self, p ) :
name = p.name
aka = self.get_aka( p.ip )
if aka :
return aka + " (" + name + ")"
else :
return name
def aka_xor_name( self, p ) :
aka = self.get_aka( p.ip )
if aka :
return aka
else :
return p.name
# Make user to choose the last replay file.
def set_last_replay( self ) :
# Well, I'll turn to wxPython for dialogs.
# It is asserted that some kind of wx.App() instance is initialized by
# the user of this class.
diag = wx.FileDialog( None, "Open the last replay file", "", "",
"Kane's Wrath Replays (*.KWReplay)|*.KWReplay" +
"|Tiberium Wars Replays (*.CNC3Replay)|*.CNC3Replay" +
"|RA3 Replays (*.RA3Replay)|*.RA3Replay", wx.FD_OPEN | wx.FD_FILE_MUST_EXIST )
args = Args.args
if args.game == "kw" :
diag.SetFilterIndex( 0 )
elif args.game == "cnc3" :
diag.SetFilterIndex( 1 )
elif args.game == "ra3" :
diag.SetFilterIndex( 2 )
if diag.ShowModal() == wx.ID_OK :
# if dialog set properly, set it.
self.set_var( 'last_replay', diag.GetPath() )
else :
# if not set properly and if we have a proper value of last_replay
# somehow, we may continue.
if not self.last_replay :
# if we may not continue... we quit the app.
wx.MessageBox( "You must select the last replay file!, exiting." )
exit( 1 )
diag.Destroy()
def load_from_file( self, fname ) :
self.cfg = configparser.ConfigParser()
self.cfg.optionxform = str # make it case sensitive.
# List default options here.
# Currently, it only specifies map CRC settings.
# Note that, read_dict() must come before read()
defaults = {
'102mc': {
'15':'R10c',
'14':'R10b',
'13':'R10',
'11':'R9',
'F':'R8',
'D':'R7',
'B':'R6'
}
}
self.cfg.read_dict( defaults ) # please call this before cfg.read()!!
if os.path.isfile( fname ) :
self.cfg.read( fname )
self.last_replay = self.get_var( 'last_replay' )
if not self.last_replay :
wx.MessageBox( "Please select the last replay file!" )
self.set_last_replay() # ask the user for it. it is a critical var!
self.add_username = self.get_bool( 'add_username', True )
self.add_faction = self.get_bool( 'add_faction', False )
self.add_vs_info = self.get_bool( 'add_vs_info', False )
self.custom_date_format = self.get_var( 'custom_date_format' )
if self.custom_date_format == None :
self.custom_date_format = "[%Y-%m-%dT%H%M]"
# Loads CRC values for 1.02+ maps
self.cfg = self.load_section( self.cfg, '102mc' )
# player name also known as...
self.cfg = self.load_section( self.cfg, 'akas' )
return self.cfg
def load_section( self, cfg, section_name ) :
if not cfg.has_section( section_name ) :
# just quit.
return cfg
section = cfg[ section_name ]
for option in section :
self.mcmap[ option ] = section[ option ]
#print( option, section[ option ] )
return cfg
def save( self ) :
self.save_to_file( self.cfg_fname )
def save_to_file( self, fname ) :
with open( fname, 'w' ) as f :
self.cfg.write( f )
def main() :
# Open config
app = wx.App( None ) # needed for event processing for the MsgBox.
args = Args( "config.ini" )
print( args )
###
### main
###
if __name__ == "__main__" :
main()