mirror of
https://github.com/shlomif/PySolFC.git
synced 2025-04-05 00:02:29 -04:00
* changed to use `configobj' module for save/load options
git-svn-id: file:///home/shlomif/Backup/svn-dumps/PySolFC/svnsync-repos/pysolfc/PySolFC/trunk@181 efabe8c0-fbe8-4139-b769-b5e6d273206e
This commit is contained in:
parent
adcf65dca4
commit
fb1141d96c
4 changed files with 3822 additions and 71 deletions
139
pysollib/app.py
139
pysollib/app.py
|
@ -78,6 +78,7 @@ from help import help_about, destroy_help_html
|
||||||
# // Options
|
# // Options
|
||||||
# ************************************************************************/
|
# ************************************************************************/
|
||||||
|
|
||||||
|
from configobj import configobj
|
||||||
|
|
||||||
class Options:
|
class Options:
|
||||||
GENERAL_OPTIONS = [
|
GENERAL_OPTIONS = [
|
||||||
|
@ -142,17 +143,8 @@ class Options:
|
||||||
|
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
config = self._config = ConfigParser.ConfigParser()
|
self._config = None # configobj.ConfigObj instance
|
||||||
for section in (
|
self._config_encoding = 'utf-8'
|
||||||
'general',
|
|
||||||
'sound_samples',
|
|
||||||
'fonts',
|
|
||||||
'colors',
|
|
||||||
'timeouts',
|
|
||||||
'cardsets',
|
|
||||||
'games_geometry',
|
|
||||||
):
|
|
||||||
config.add_section(section)
|
|
||||||
|
|
||||||
self.version_tuple = VERSION_TUPLE # XXX
|
self.version_tuple = VERSION_TUPLE # XXX
|
||||||
self.saved = 0 # XXX
|
self.saved = 0 # XXX
|
||||||
|
@ -335,8 +327,12 @@ class Options:
|
||||||
|
|
||||||
# not changeable options
|
# not changeable options
|
||||||
def setConstants(self):
|
def setConstants(self):
|
||||||
## self.dragcursor = True
|
if 'shuffle' not in self.toolbar_vars:
|
||||||
## self.randomize_place = False
|
# new in v.1.1
|
||||||
|
self.toolbar_vars['shuffle'] = True
|
||||||
|
if isinstance(self.mahjongg_create_solvable, bool):
|
||||||
|
# changed in v.1.1
|
||||||
|
self.mahjongg_create_solvable = 2
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def copy(self):
|
def copy(self):
|
||||||
|
@ -351,81 +347,90 @@ class Options:
|
||||||
# general
|
# general
|
||||||
for key, t in self.GENERAL_OPTIONS:
|
for key, t in self.GENERAL_OPTIONS:
|
||||||
val = getattr(self, key)
|
val = getattr(self, key)
|
||||||
if t == 'str' and isinstance(val, unicode):
|
config['general'][key] = val
|
||||||
val = val.encode('utf-8')
|
|
||||||
config.set('general', key, val)
|
|
||||||
|
|
||||||
recent_gameid = ' '.join([str(i) for i in self.recent_gameid])
|
|
||||||
config.set('general', 'recent_gameid', recent_gameid)
|
|
||||||
|
|
||||||
favorite_gameid = ' '.join([str(i) for i in self.favorite_gameid])
|
|
||||||
config.set('general', 'favorite_gameid', favorite_gameid)
|
|
||||||
|
|
||||||
|
config['general']['recent_gameid'] = self.recent_gameid
|
||||||
|
config['general']['favorite_gameid'] = self.favorite_gameid
|
||||||
visible_buttons = [b for b in self.toolbar_vars
|
visible_buttons = [b for b in self.toolbar_vars
|
||||||
if self.toolbar_vars[b]]
|
if self.toolbar_vars[b]]
|
||||||
visible_buttons = ' '.join(visible_buttons)
|
config['general']['visible_buttons'] = visible_buttons
|
||||||
config.set('general', 'visible_buttons', visible_buttons)
|
|
||||||
|
|
||||||
# sound_samples
|
# sound_samples
|
||||||
for key, val in self.sound_samples.items():
|
config['sound_samples'] = self.sound_samples
|
||||||
config.set('sound_samples', key, val)
|
|
||||||
|
|
||||||
# fonts
|
# fonts
|
||||||
for key, val in self.fonts.items():
|
for key, val in self.fonts.items():
|
||||||
if val is None:
|
if val is None:
|
||||||
continue
|
continue
|
||||||
val = list(val)
|
config['fonts'][key] = val
|
||||||
val[0] = val[0].replace(' ', '_') # XXX: hack
|
|
||||||
val = ' '.join([str(i) for i in val])
|
|
||||||
config.set('fonts', key, val)
|
|
||||||
|
|
||||||
# colors
|
# colors
|
||||||
for key, val in self.colors.items():
|
config['colors'] = self.colors
|
||||||
config.set('colors', key, val)
|
|
||||||
|
|
||||||
# timeouts
|
# timeouts
|
||||||
for key, val in self.timeouts.items():
|
config['timeouts'] = self.timeouts
|
||||||
config.set('timeouts', key, val)
|
|
||||||
|
|
||||||
# cardsets
|
# cardsets
|
||||||
for key, val in self.cardset.items():
|
for key, val in self.cardset.items():
|
||||||
val = list(val)
|
config['cardsets'][str(key)] = val
|
||||||
if val[1] == '':
|
|
||||||
val[1] = 'none'
|
|
||||||
val = ' '.join(val)
|
|
||||||
config.set('cardsets', str(key), val)
|
|
||||||
|
|
||||||
# games_geometry
|
# games_geometry
|
||||||
|
config['games_geometry'].clear()
|
||||||
for key, val in self.games_geometry.items():
|
for key, val in self.games_geometry.items():
|
||||||
val = ' '.join([str(i) for i in val])
|
config['games_geometry'][str(key)] = val
|
||||||
config.set('games_geometry', str(key), val)
|
|
||||||
|
config.write()
|
||||||
|
##config.write(sys.stdout); print
|
||||||
|
|
||||||
config.write(file(filename, 'w'))
|
|
||||||
#config.write(sys.stdout)
|
|
||||||
|
|
||||||
def _getOption(self, section, key, t):
|
def _getOption(self, section, key, t):
|
||||||
config = self._config
|
config = self._config
|
||||||
try:
|
try:
|
||||||
if t == 'bool':
|
if t == 'bool':
|
||||||
val = config.getboolean(section, key)
|
val = config[section].as_bool(key)
|
||||||
elif t == 'int':
|
elif t == 'int':
|
||||||
val = config.getint(section, key)
|
val = config[section].as_int(key)
|
||||||
elif t == 'float':
|
elif t == 'float':
|
||||||
val = config.getfloat(section, key)
|
val = config[section].as_float(key)
|
||||||
|
elif t == 'list':
|
||||||
|
val = config[section][key]
|
||||||
|
assert isinstance(val, (list, tuple))
|
||||||
else: # str
|
else: # str
|
||||||
val = config.get(section, key)
|
val = config[section][key]
|
||||||
val = unicode(val, 'utf-8')
|
except KeyError:
|
||||||
except ConfigParser.NoOptionError:
|
|
||||||
val = None
|
val = None
|
||||||
except:
|
except:
|
||||||
print_err('load option error: '+key)
|
print_err('load option error: %s: %s' % (section, key))
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
val = None
|
val = None
|
||||||
return val
|
return val
|
||||||
|
|
||||||
def load(self, filename):
|
def load(self, filename):
|
||||||
config = self._config
|
|
||||||
config.read(filename)
|
try:
|
||||||
|
config = configobj.ConfigObj(filename,
|
||||||
|
encoding=self._config_encoding)
|
||||||
|
except configobj.ParseError:
|
||||||
|
traceback.print_exc()
|
||||||
|
config = configobj.ConfigObj(encoding=self._config_encoding)
|
||||||
|
self._config = config
|
||||||
|
|
||||||
|
for section in (
|
||||||
|
'general',
|
||||||
|
'sound_samples',
|
||||||
|
'fonts',
|
||||||
|
'colors',
|
||||||
|
'timeouts',
|
||||||
|
'cardsets',
|
||||||
|
'games_geometry',
|
||||||
|
):
|
||||||
|
if section not in config:
|
||||||
|
config[section] = {}
|
||||||
|
|
||||||
|
if not os.path.exists(filename):
|
||||||
|
config.initial_comment = ['-*- coding: %s -*-' %
|
||||||
|
self._config_encoding]
|
||||||
|
return
|
||||||
|
|
||||||
# general
|
# general
|
||||||
for key, t in self.GENERAL_OPTIONS:
|
for key, t in self.GENERAL_OPTIONS:
|
||||||
|
@ -433,24 +438,23 @@ class Options:
|
||||||
if val is not None:
|
if val is not None:
|
||||||
setattr(self, key, val)
|
setattr(self, key, val)
|
||||||
|
|
||||||
recent_gameid = self._getOption('general', 'recent_gameid', 'str')
|
recent_gameid = self._getOption('general', 'recent_gameid', 'list')
|
||||||
if recent_gameid is not None:
|
if recent_gameid is not None:
|
||||||
try:
|
try:
|
||||||
self.recent_gameid = [int(i) for i in recent_gameid.split()]
|
self.recent_gameid = [int(i) for i in recent_gameid]
|
||||||
except:
|
except:
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
|
|
||||||
favorite_gameid = self._getOption('general', 'favorite_gameid', 'str')
|
favorite_gameid = self._getOption('general', 'favorite_gameid', 'list')
|
||||||
if favorite_gameid is not None:
|
if favorite_gameid is not None:
|
||||||
try:
|
try:
|
||||||
self.favorite_gameid = [int(i) for i in favorite_gameid.split()]
|
self.favorite_gameid = [int(i) for i in favorite_gameid]
|
||||||
except:
|
except:
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
|
|
||||||
visible_buttons = self._getOption('general', 'visible_buttons', 'str')
|
visible_buttons = self._getOption('general', 'visible_buttons', 'list')
|
||||||
if visible_buttons is not None:
|
if visible_buttons is not None:
|
||||||
visible_buttons = visible_buttons.split()
|
for key in TOOLBAR_BUTTONS:
|
||||||
for key in self.toolbar_vars:
|
|
||||||
self.toolbar_vars[key] = (key in visible_buttons)
|
self.toolbar_vars[key] = (key in visible_buttons)
|
||||||
|
|
||||||
# sound_samples
|
# sound_samples
|
||||||
|
@ -463,9 +467,7 @@ class Options:
|
||||||
for key in self.fonts:
|
for key in self.fonts:
|
||||||
val = self._getOption('fonts', key, 'str')
|
val = self._getOption('fonts', key, 'str')
|
||||||
if val is not None:
|
if val is not None:
|
||||||
val = val.split()
|
|
||||||
try:
|
try:
|
||||||
val[0] = val[0].replace('_', ' ')
|
|
||||||
val[1] = int(val[1])
|
val[1] = int(val[1])
|
||||||
except:
|
except:
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
|
@ -487,28 +489,24 @@ class Options:
|
||||||
|
|
||||||
# cardsets
|
# cardsets
|
||||||
for key in self.cardset:
|
for key in self.cardset:
|
||||||
val = self._getOption('cardsets', str(key), 'str')
|
val = self._getOption('cardsets', str(key), 'list')
|
||||||
if val is not None:
|
if val is not None:
|
||||||
try:
|
try:
|
||||||
i = val.rindex(' ')
|
|
||||||
if val[i+1:] == 'none':
|
|
||||||
val = (val[:i], '')
|
|
||||||
else:
|
|
||||||
val = [val[:i], val[i+1:]]
|
|
||||||
self.cardset[int(key)] = val
|
self.cardset[int(key)] = val
|
||||||
except:
|
except:
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
|
|
||||||
# games_geometry
|
# games_geometry
|
||||||
for key, val in config.items('games_geometry'):
|
for key, val in config['games_geometry'].items():
|
||||||
try:
|
try:
|
||||||
val = [int(i) for i in val.split()]
|
val = [int(i) for i in val]
|
||||||
assert len(val) == 2
|
assert len(val) == 2
|
||||||
self.games_geometry[int(key)] = val
|
self.games_geometry[int(key)] = val
|
||||||
except:
|
except:
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# /***********************************************************************
|
# /***********************************************************************
|
||||||
# // Statistics
|
# // Statistics
|
||||||
# ************************************************************************/
|
# ************************************************************************/
|
||||||
|
@ -1488,8 +1486,7 @@ Please select a %s type %s.
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if os.path.exists(self.fn.opt_cfg):
|
self.opt.load(self.fn.opt_cfg)
|
||||||
self.opt.load(self.fn.opt_cfg)
|
|
||||||
self.opt.setConstants()
|
self.opt.setConstants()
|
||||||
|
|
||||||
def loadStatistics(self):
|
def loadStatistics(self):
|
||||||
|
|
0
pysollib/configobj/__init__.py
Normal file
0
pysollib/configobj/__init__.py
Normal file
2281
pysollib/configobj/configobj.py
Normal file
2281
pysollib/configobj/configobj.py
Normal file
File diff suppressed because it is too large
Load diff
1473
pysollib/configobj/validate.py
Normal file
1473
pysollib/configobj/validate.py
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Add table
Reference in a new issue