mirror of
https://github.com/shlomif/PySolFC.git
synced 2025-04-05 00:02:29 -04:00
Compare commits
8 commits
2bec58be2d
...
c12be2eaed
Author | SHA1 | Date | |
---|---|---|---|
|
c12be2eaed | ||
|
b6da50d20f | ||
|
75ffea5005 | ||
|
a073d20af7 | ||
|
9acc97be8a | ||
|
26e5aeb045 | ||
|
32411ab0ec | ||
|
efddc917b2 |
4 changed files with 296 additions and 303 deletions
|
@ -330,7 +330,7 @@ class PysolMenubar(PysolMenubarTk):
|
|||
elif type == 'not played' and won+lost == 0:
|
||||
games.append(gi.id)
|
||||
if games:
|
||||
game_id = self.app.getRandomGameId(games)
|
||||
game_id = self.app.chooseRandomOutOfGames(games)
|
||||
if game_id and game_id != self.game.id:
|
||||
self.game.endGame()
|
||||
self.game.quitGame(game_id)
|
||||
|
@ -518,15 +518,12 @@ class PysolMenubar(PysolMenubarTk):
|
|||
text += os.linesep
|
||||
enc = locale.getpreferredencoding()
|
||||
try:
|
||||
fd = open(fn, 'a')
|
||||
fd.write(text.encode(enc, 'replace'))
|
||||
open(fn, 'a').write(text.encode(enc, 'replace'))
|
||||
except Exception as err:
|
||||
d = MfxExceptionDialog(
|
||||
self.top, err,
|
||||
text=_("Error while writing to file"))
|
||||
else:
|
||||
if fd:
|
||||
fd.close()
|
||||
d = MfxMessageDialog(
|
||||
self.top, title=TITLE+_(" Info"), bitmap="info",
|
||||
text=_("Comments were appended to\n\n") + fn)
|
||||
|
@ -537,7 +534,6 @@ class PysolMenubar(PysolMenubarTk):
|
|||
#
|
||||
|
||||
def _mStatsSave(self, player, filename, write_method):
|
||||
file = None
|
||||
if player is None:
|
||||
text = _("Demo statistics")
|
||||
filename = filename + "_demo"
|
||||
|
@ -546,17 +542,12 @@ class PysolMenubar(PysolMenubarTk):
|
|||
filename = os.path.join(self.app.dn.config, filename + ".txt")
|
||||
filename = os.path.normpath(filename)
|
||||
try:
|
||||
file = open(filename, "a")
|
||||
a = FileStatsFormatter(self.app, file)
|
||||
a = FileStatsFormatter(self.app, open(filename, "a"))
|
||||
write_method(a, player)
|
||||
except EnvironmentError as ex:
|
||||
if file:
|
||||
file.close()
|
||||
MfxExceptionDialog(self.top, ex,
|
||||
text=_("Error while writing to file"))
|
||||
else:
|
||||
if file:
|
||||
file.close()
|
||||
MfxMessageDialog(
|
||||
self.top, title=TITLE+_(" Info"), bitmap="info",
|
||||
text=text + _(" were appended to\n\n") + filename)
|
||||
|
|
344
pysollib/app.py
344
pysollib/app.py
|
@ -79,6 +79,11 @@ GameStat = pysollib.app_stat.GameStat
|
|||
# * Also handles all global resources.
|
||||
# ************************************************************************
|
||||
|
||||
image_ext_re_str = "(" + "|".join(
|
||||
["(?:\\" + e + ")" for e in IMAGE_EXTENSIONS]) + ")$"
|
||||
image_ext_re = re.compile(image_ext_re_str, re.I | re.U)
|
||||
|
||||
|
||||
class Application:
|
||||
def __init__(self):
|
||||
self.gdb = GAME_DB
|
||||
|
@ -184,25 +189,12 @@ class Application:
|
|||
def gameproc(self):
|
||||
while True:
|
||||
logging.info('App: gameproc waiting for game to start')
|
||||
(id, random) = yield
|
||||
logging.info('App: game started %s,%s' % (str(id), str(random)))
|
||||
self.runGame(id, random)
|
||||
(id_, random) = yield
|
||||
logging.info('App: game started {},{}'.format(id_, random))
|
||||
self.runGame(id_, random)
|
||||
|
||||
def mainproc(self):
|
||||
# copy startup options
|
||||
self.startup_opt = self.opt.copy()
|
||||
# try to load statistics
|
||||
try:
|
||||
self.loadStatistics()
|
||||
except Exception:
|
||||
traceback.print_exc()
|
||||
pass
|
||||
# startup information
|
||||
if self.getGameClass(self.opt.last_gameid):
|
||||
self.nextgame.id = self.opt.last_gameid
|
||||
# load a holded or saved game
|
||||
id = self.gdb.getGamesIdSortedByName()[0]
|
||||
tmpgame = self.constructGame(id)
|
||||
def _load_held_or_saved_game(self, tmpgame):
|
||||
"""docstring for _load_held_or_saved_game"""
|
||||
if self.opt.game_holded > 0 and not self.nextgame.loadedgame:
|
||||
game = None
|
||||
try:
|
||||
|
@ -219,76 +211,9 @@ class Application:
|
|||
# not a holded game
|
||||
game.destruct()
|
||||
destruct(game)
|
||||
game = None
|
||||
if not self.nextgame.loadedgame:
|
||||
if self.commandline.loadgame:
|
||||
try:
|
||||
self.nextgame.loadedgame = tmpgame._loadGame(
|
||||
self.commandline.loadgame, self)
|
||||
self.nextgame.loadedgame.gstats.holded = 0
|
||||
except Exception:
|
||||
traceback.print_exc()
|
||||
self.nextgame.loadedgame = None
|
||||
elif self.commandline.game is not None:
|
||||
gameid = self.gdb.getGameByName(self.commandline.game)
|
||||
if gameid is None:
|
||||
print_err(_("can't find game: ") + self.commandline.game)
|
||||
sys.exit(-1)
|
||||
else:
|
||||
self.nextgame.id = gameid
|
||||
deal = self.commandline.deal
|
||||
self.nextgame.random = \
|
||||
None if deal is None else constructRandom(deal)
|
||||
elif self.commandline.gameid is not None:
|
||||
self.nextgame.id, self.nextgame.random = \
|
||||
self.commandline.gameid, None
|
||||
self.opt.game_holded = 0
|
||||
tmpgame.destruct()
|
||||
destruct(tmpgame)
|
||||
tmpgame = None
|
||||
#
|
||||
# widgets
|
||||
#
|
||||
# create the menubar
|
||||
if self.intro.progress:
|
||||
self.intro.progress.update(step=1)
|
||||
self.menubar = PysolMenubar(self, self.top,
|
||||
progress=self.intro.progress)
|
||||
# create the statusbar(s)
|
||||
self.statusbar = PysolStatusbar(self.top)
|
||||
self.statusbar.show(self.opt.statusbar)
|
||||
self.statusbar.config('gamenumber', self.opt.statusbar_game_number)
|
||||
self.statusbar.config('stuck', self.opt.statusbar_stuck)
|
||||
self.helpbar = HelpStatusbar(self.top)
|
||||
self.helpbar.show(self.opt.helpbar)
|
||||
# create the canvas
|
||||
self.scrolled_canvas = MfxScrolledCanvas(self.top, propagate=True)
|
||||
self.canvas = self.scrolled_canvas.canvas
|
||||
padx, pady = TkSettings.canvas_padding
|
||||
self.scrolled_canvas.grid(row=1, column=1, sticky='nsew',
|
||||
padx=padx, pady=pady)
|
||||
self.top.grid_columnconfigure(1, weight=1)
|
||||
self.top.grid_rowconfigure(1, weight=1)
|
||||
self.setTile(self.tabletile_index, force=True)
|
||||
# create the toolbar
|
||||
dir = self.getToolbarImagesDir()
|
||||
self.toolbar = PysolToolbar(self.top, self.menubar, dir=dir,
|
||||
size=self.opt.toolbar_size,
|
||||
relief=self.opt.toolbar_relief,
|
||||
compound=self.opt.toolbar_compound)
|
||||
self.toolbar.show(self.opt.toolbar)
|
||||
if TOOLKIT == 'tk':
|
||||
for w, v in self.opt.toolbar_vars.items():
|
||||
self.toolbar.config(w, v)
|
||||
#
|
||||
if self.intro.progress:
|
||||
self.intro.progress.update(step=1)
|
||||
#
|
||||
|
||||
if TOOLKIT == 'kivy':
|
||||
self.gproc = self.gameproc()
|
||||
self.gproc.send(None)
|
||||
|
||||
def _main_loop(self):
|
||||
"""docstring for _main_loop"""
|
||||
try:
|
||||
# this is the mainloop
|
||||
while 1:
|
||||
|
@ -377,6 +302,91 @@ class Application:
|
|||
logging.info('App: mainloop end position')
|
||||
yield
|
||||
|
||||
def mainproc(self):
|
||||
# copy startup options
|
||||
self.startup_opt = self.opt.copy()
|
||||
# try to load statistics
|
||||
try:
|
||||
self.loadStatistics()
|
||||
except Exception:
|
||||
traceback.print_exc()
|
||||
pass
|
||||
# startup information
|
||||
if self.getGameClass(self.opt.last_gameid):
|
||||
self.nextgame.id = self.opt.last_gameid
|
||||
# load a holded or saved game
|
||||
tmpgame = self.constructGame(self.gdb.getGamesIdSortedByName()[0])
|
||||
self._load_held_or_saved_game(tmpgame)
|
||||
if not self.nextgame.loadedgame:
|
||||
if self.commandline.loadgame:
|
||||
try:
|
||||
self.nextgame.loadedgame = tmpgame._loadGame(
|
||||
self.commandline.loadgame, self)
|
||||
self.nextgame.loadedgame.gstats.holded = 0
|
||||
except Exception:
|
||||
traceback.print_exc()
|
||||
self.nextgame.loadedgame = None
|
||||
elif self.commandline.game is not None:
|
||||
gameid = self.gdb.getGameByName(self.commandline.game)
|
||||
if gameid is None:
|
||||
print_err(_("can't find game: ") + self.commandline.game)
|
||||
sys.exit(-1)
|
||||
else:
|
||||
self.nextgame.id = gameid
|
||||
deal = self.commandline.deal
|
||||
self.nextgame.random = \
|
||||
None if deal is None else constructRandom(deal)
|
||||
elif self.commandline.gameid is not None:
|
||||
self.nextgame.id, self.nextgame.random = \
|
||||
self.commandline.gameid, None
|
||||
self.opt.game_holded = 0
|
||||
tmpgame.destruct()
|
||||
destruct(tmpgame)
|
||||
tmpgame = None
|
||||
#
|
||||
# widgets
|
||||
#
|
||||
# create the menubar
|
||||
if self.intro.progress:
|
||||
self.intro.progress.update(step=1)
|
||||
self.menubar = PysolMenubar(self, self.top,
|
||||
progress=self.intro.progress)
|
||||
# create the statusbar(s)
|
||||
self.statusbar = PysolStatusbar(self.top)
|
||||
self.statusbar.show(self.opt.statusbar)
|
||||
self.statusbar.config('gamenumber', self.opt.statusbar_game_number)
|
||||
self.statusbar.config('stuck', self.opt.statusbar_stuck)
|
||||
self.helpbar = HelpStatusbar(self.top)
|
||||
self.helpbar.show(self.opt.helpbar)
|
||||
# create the canvas
|
||||
self.scrolled_canvas = MfxScrolledCanvas(self.top, propagate=True)
|
||||
self.canvas = self.scrolled_canvas.canvas
|
||||
padx, pady = TkSettings.canvas_padding
|
||||
self.scrolled_canvas.grid(row=1, column=1, sticky='nsew',
|
||||
padx=padx, pady=pady)
|
||||
self.top.grid_columnconfigure(1, weight=1)
|
||||
self.top.grid_rowconfigure(1, weight=1)
|
||||
self.setTile(self.tabletile_index, force=True)
|
||||
# create the toolbar
|
||||
dirname = self.getToolbarImagesDir()
|
||||
self.toolbar = PysolToolbar(self.top, self.menubar, dir=dirname,
|
||||
size=self.opt.toolbar_size,
|
||||
relief=self.opt.toolbar_relief,
|
||||
compound=self.opt.toolbar_compound)
|
||||
self.toolbar.show(self.opt.toolbar)
|
||||
if TOOLKIT == 'tk':
|
||||
for w, v in self.opt.toolbar_vars.items():
|
||||
self.toolbar.config(w, v)
|
||||
#
|
||||
if self.intro.progress:
|
||||
self.intro.progress.update(step=1)
|
||||
#
|
||||
|
||||
if TOOLKIT == 'kivy':
|
||||
self.gproc = self.gameproc()
|
||||
self.gproc.send(None)
|
||||
return self._main_loop()
|
||||
|
||||
def runGame(self, id_, random=None):
|
||||
self.top.connectApp(self)
|
||||
# create game instance
|
||||
|
@ -489,63 +499,63 @@ class Application:
|
|||
|
||||
def loadImages1(self):
|
||||
# load dialog images
|
||||
dir = os.path.join("images", "logos")
|
||||
dirname = os.path.join("images", "logos")
|
||||
for f in ("joker07_40_774",
|
||||
"joker08_40_774",
|
||||
"joker07_50_774",
|
||||
"joker08_50_774",
|
||||
"joker11_100_774",
|
||||
"joker10_100",):
|
||||
self.gimages.logos.append(self.dataloader.findImage(f, dir))
|
||||
self.gimages.logos.append(self.dataloader.findImage(f, dirname))
|
||||
if WIN_SYSTEM == 'win32':
|
||||
dir = os.path.join('images', 'dialog', 'default')
|
||||
dirname = os.path.join('images', 'dialog', 'default')
|
||||
else:
|
||||
dir = os.path.join('images', 'dialog', 'bluecurve')
|
||||
dirname = os.path.join('images', 'dialog', 'bluecurve')
|
||||
for f in ('error', 'info', 'question', 'warning'):
|
||||
fn = self.dataloader.findImage(f, dir)
|
||||
fn = self.dataloader.findImage(f, dirname)
|
||||
im = loadImage(fn)
|
||||
MfxMessageDialog.img[f] = im
|
||||
|
||||
# load button images
|
||||
if 0 and TOOLKIT == 'tk':
|
||||
dir = os.path.join('images', 'buttons', 'bluecurve')
|
||||
dirname = os.path.join('images', 'buttons', 'bluecurve')
|
||||
for n, f in (
|
||||
(_('&OK'), 'ok'),
|
||||
(_('&Cancel'), 'cancel'),
|
||||
(_('&New game'), 'new'),
|
||||
):
|
||||
fn = self.dataloader.findImage(f, dir)
|
||||
fn = self.dataloader.findImage(f, dirname)
|
||||
im = loadImage(fn)
|
||||
MfxDialog.button_img[n] = im
|
||||
|
||||
def loadImages2(self):
|
||||
# load canvas images
|
||||
dir = "images"
|
||||
dirname = "images"
|
||||
# for f in ("noredeal", "redeal",):
|
||||
for f in ("stopsign", "redeal",):
|
||||
self.gimages.redeal.append(self.dataloader.findImage(f, dir))
|
||||
dir = os.path.join("images", "demo")
|
||||
self.gimages.redeal.append(self.dataloader.findImage(f, dirname))
|
||||
dirname = os.path.join("images", "demo")
|
||||
for f in ("demo01", "demo02", "demo03", "demo04", "demo05",):
|
||||
self.gimages.demo.append(self.dataloader.findImage(f, dir))
|
||||
dir = os.path.join("images", "pause")
|
||||
self.gimages.demo.append(self.dataloader.findImage(f, dirname))
|
||||
dirname = os.path.join("images", "pause")
|
||||
for f in ("pause01", "pause02", "pause03",):
|
||||
self.gimages.pause.append(self.dataloader.findImage(f, dir))
|
||||
# dir = os.path.join("images", "stats")
|
||||
self.gimages.pause.append(self.dataloader.findImage(f, dirname))
|
||||
# dirname = os.path.join("images", "stats")
|
||||
# for f in ("barchart",):
|
||||
# self.gimages.stats.append(self.dataloader.findImage(f, dir))
|
||||
# self.gimages.stats.append(self.dataloader.findImage(f, dirname))
|
||||
|
||||
def loadImages3(self):
|
||||
# load treeview images
|
||||
SelectDialogTreeData.img = []
|
||||
dir = os.path.join('images', 'tree')
|
||||
dirname = os.path.join('images', 'tree')
|
||||
for f in ('folder', 'openfolder', 'node', 'emptynode'):
|
||||
fn = self.dataloader.findImage(f, dir)
|
||||
fn = self.dataloader.findImage(f, dirname)
|
||||
im = loadImage(fn)
|
||||
SelectDialogTreeData.img.append(im)
|
||||
|
||||
# load htmlviewer images
|
||||
dir = os.path.join('images', 'htmlviewer')
|
||||
fn = self.dataloader.findImage('disk', dir)
|
||||
dirname = os.path.join('images', 'htmlviewer')
|
||||
fn = self.dataloader.findImage('disk', dirname)
|
||||
HTMLViewer.symbols_fn['disk'] = fn
|
||||
|
||||
def loadImages4(self):
|
||||
|
@ -1005,17 +1015,21 @@ Please select a %s type %s.
|
|||
return n
|
||||
|
||||
def getGameSaveName(self, id):
|
||||
if os.path.supports_unicode_filenames: # new in python 2.3
|
||||
if os.path.supports_unicode_filenames:
|
||||
return self.getGameTitleName(id)
|
||||
n = self.gdb.get(id).en_name # english name
|
||||
if not n:
|
||||
return None
|
||||
return re.sub(r"[\s]", "_", latin1_normalize(n))
|
||||
|
||||
def getRandomGameId(self, games=None):
|
||||
if games is None:
|
||||
return self.miscrandom.choice(self.gdb.getGamesIdSortedById())
|
||||
return self.miscrandom.choice(games)
|
||||
def _choice(self, lst):
|
||||
return self.miscrandom.choice(lst)
|
||||
|
||||
def chooseRandomOutOfGames(self, games):
|
||||
return self._choice(games)
|
||||
|
||||
def getRandomGameId(self):
|
||||
return self._choice(self.gdb.getGamesIdSortedById())
|
||||
|
||||
def getAllUserNames(self):
|
||||
names = []
|
||||
|
@ -1036,10 +1050,10 @@ Please select a %s type %s.
|
|||
# plugins
|
||||
#
|
||||
|
||||
def loadPlugins(self, dir):
|
||||
for name in self._my_list_dir(dir):
|
||||
def loadPlugins(self, dirname):
|
||||
for name in self._my_list_dir(dirname):
|
||||
m = re.search(r"^(.+)\.py$", name)
|
||||
n = os.path.join(dir, name)
|
||||
n = os.path.join(dirname, name)
|
||||
if m and os.path.isfile(n):
|
||||
try:
|
||||
loadGame(m.group(1), n)
|
||||
|
@ -1053,14 +1067,9 @@ Please select a %s type %s.
|
|||
#
|
||||
|
||||
# read & parse a cardset config.txt file - see class Cardset in resource.py
|
||||
def _readCardsetConfig(self, dir, filename):
|
||||
f = None
|
||||
try:
|
||||
f = open(filename, "r")
|
||||
def _readCardsetConfig(self, dirname, filename):
|
||||
with open(filename, "r") as f:
|
||||
lines = f.readlines()
|
||||
finally:
|
||||
if f:
|
||||
f.close()
|
||||
lines = [l.strip() for l in lines]
|
||||
if not lines[0].startswith("PySol"):
|
||||
return None
|
||||
|
@ -1071,7 +1080,7 @@ Please select a %s type %s.
|
|||
if config.CARDD > self.top.winfo_screendepth():
|
||||
return None
|
||||
cs = Cardset()
|
||||
cs.dir = dir
|
||||
cs.dir = dirname
|
||||
cs.update(config.__dict__)
|
||||
return cs
|
||||
|
||||
|
@ -1188,18 +1197,18 @@ Please select a %s type %s.
|
|||
# print dirs
|
||||
found, t = [], {}
|
||||
fnames = {} # (to check for duplicates)
|
||||
for dir in dirs:
|
||||
dir = dir.strip()
|
||||
for dirname in dirs:
|
||||
dirname = dirname.strip()
|
||||
try:
|
||||
names = []
|
||||
if dir and os.path.isdir(dir) and dir not in t:
|
||||
t[dir] = 1
|
||||
names = os.listdir(dir)
|
||||
if dirname and os.path.isdir(dirname) and dirname not in t:
|
||||
t[dirname] = 1
|
||||
names = os.listdir(dirname)
|
||||
names.sort()
|
||||
for name in names:
|
||||
if not name.startswith('cardset-'):
|
||||
continue
|
||||
d = os.path.join(dir, name)
|
||||
d = os.path.join(dirname, name)
|
||||
if not os.path.isdir(d):
|
||||
continue
|
||||
f1 = os.path.join(d, "config.txt")
|
||||
|
@ -1239,6 +1248,32 @@ Please select a %s type %s.
|
|||
#
|
||||
# init tiles
|
||||
#
|
||||
def _init_tiles_process_dir(self, dirname, found, t):
|
||||
"""docstring for _init_tiles_process_die"""
|
||||
names = []
|
||||
if dirname and os.path.isdir(dirname):
|
||||
names = os.listdir(dirname)
|
||||
for name in names:
|
||||
if not name or not image_ext_re.search(name):
|
||||
continue
|
||||
f = os.path.join(dirname, name)
|
||||
if not os.path.isfile(f):
|
||||
continue
|
||||
tile = Tile()
|
||||
tile.filename = f
|
||||
n = image_ext_re.sub("", name)
|
||||
if os.path.split(dirname)[-1] == 'stretch':
|
||||
tile.stretch = 1
|
||||
if os.path.split(dirname)[-1] == 'save-aspect':
|
||||
tile.stretch = 1
|
||||
tile.save_aspect = 1
|
||||
# n = re.sub("[-_]", " ", n)
|
||||
n = n.replace('_', ' ')
|
||||
tile.name = n
|
||||
key = n.lower()
|
||||
if key not in t:
|
||||
t.add(key)
|
||||
found.append((n, tile))
|
||||
|
||||
def initTiles(self):
|
||||
manager = self.tabletile_manager
|
||||
|
@ -1250,35 +1285,10 @@ Please select a %s type %s.
|
|||
os.path.join("tiles", "save-aspect")),
|
||||
"PYSOL_TILES")
|
||||
# print dirs
|
||||
s = "((\\" + ")|(\\".join(IMAGE_EXTENSIONS) + "))$"
|
||||
ext_re = re.compile(s, re.I | re.U)
|
||||
found, t = [], {}
|
||||
for dir in dirs:
|
||||
found, t = [], set()
|
||||
for dirname in dirs:
|
||||
try:
|
||||
names = []
|
||||
if dir and os.path.isdir(dir):
|
||||
names = os.listdir(dir)
|
||||
for name in names:
|
||||
if not name or not ext_re.search(name):
|
||||
continue
|
||||
f = os.path.join(dir, name)
|
||||
if not os.path.isfile(f):
|
||||
continue
|
||||
tile = Tile()
|
||||
tile.filename = f
|
||||
n = ext_re.sub("", name)
|
||||
if os.path.split(dir)[-1] == 'stretch':
|
||||
tile.stretch = 1
|
||||
if os.path.split(dir)[-1] == 'save-aspect':
|
||||
tile.stretch = 1
|
||||
tile.save_aspect = 1
|
||||
# n = re.sub("[-_]", " ", n)
|
||||
n = n.replace('_', ' ')
|
||||
tile.name = n
|
||||
key = n.lower()
|
||||
if key not in t:
|
||||
t[key] = 1
|
||||
found.append((n, tile))
|
||||
self._init_tiles_process_dir(dirname, found, t)
|
||||
except EnvironmentError:
|
||||
pass
|
||||
# register tiles
|
||||
|
@ -1288,10 +1298,10 @@ Please select a %s type %s.
|
|||
if not manager.getByName(obj.name):
|
||||
manager.register(obj)
|
||||
|
||||
def _my_list_dir(self, dir):
|
||||
def _my_list_dir(self, dirname):
|
||||
"""docstring for _my_list_dir"""
|
||||
if dir and os.path.isdir(dir):
|
||||
names = os.listdir(dir)
|
||||
if dirname and os.path.isdir(dirname):
|
||||
names = os.listdir(dirname)
|
||||
names = list(map(os.path.normcase, names))
|
||||
names.sort()
|
||||
return names
|
||||
|
@ -1303,16 +1313,16 @@ Please select a %s type %s.
|
|||
#
|
||||
|
||||
def initResource(self, manager, dirs, ext_re, Resource_Class):
|
||||
found, t = [], {}
|
||||
for dir in dirs:
|
||||
dir = dir.strip()
|
||||
if dir:
|
||||
dir = os.path.normpath(dir)
|
||||
found, t = [], set()
|
||||
for dirname in dirs:
|
||||
dirname = dirname.strip()
|
||||
if dirname:
|
||||
dirname = os.path.normpath(dirname)
|
||||
try:
|
||||
for name in self._my_list_dir(dir):
|
||||
for name in self._my_list_dir(dirname):
|
||||
if not name or not ext_re.search(name):
|
||||
continue
|
||||
f = os.path.join(dir, name)
|
||||
f = os.path.join(dirname, name)
|
||||
f = os.path.normpath(f)
|
||||
if not os.path.isfile(f):
|
||||
continue
|
||||
|
@ -1322,7 +1332,7 @@ Please select a %s type %s.
|
|||
obj.name = n
|
||||
key = n.lower()
|
||||
if key not in t:
|
||||
t[key] = 1
|
||||
t.add(key)
|
||||
found.append((n, obj))
|
||||
except EnvironmentError:
|
||||
pass
|
||||
|
|
185
pysollib/game.py
185
pysollib/game.py
|
@ -84,6 +84,89 @@ PLAY_TIME_TIMEOUT = 200
|
|||
# ************************************************************************
|
||||
|
||||
|
||||
def _updateStatus_process_key_val(tb, sb, k, v):
|
||||
if k == "gamenumber":
|
||||
if v is None:
|
||||
if sb:
|
||||
sb.updateText(gamenumber="")
|
||||
# self.top.wm_title("%s - %s"
|
||||
# % (TITLE, self.getTitleName()))
|
||||
return
|
||||
if isinstance(v, six.string_types):
|
||||
if sb:
|
||||
sb.updateText(gamenumber=v)
|
||||
# self.top.wm_title("%s - %s %s" % (TITLE,
|
||||
# self.getTitleName(), v))
|
||||
return
|
||||
if k == "info":
|
||||
# print 'updateStatus info:', v
|
||||
if v is None:
|
||||
if sb:
|
||||
sb.updateText(info="")
|
||||
return
|
||||
if isinstance(v, str):
|
||||
if sb:
|
||||
sb.updateText(info=v)
|
||||
return
|
||||
if k == "moves":
|
||||
if v is None:
|
||||
# if tb: tb.updateText(moves="Moves\n")
|
||||
if sb:
|
||||
sb.updateText(moves="")
|
||||
return
|
||||
if isinstance(v, tuple):
|
||||
# if tb: tb.updateText(moves="Moves\n%d/%d" % v)
|
||||
if sb:
|
||||
sb.updateText(moves="%d/%d" % v)
|
||||
return
|
||||
if isinstance(v, int):
|
||||
# if tb: tb.updateText(moves="Moves\n%d" % v)
|
||||
if sb:
|
||||
sb.updateText(moves="%d" % v)
|
||||
return
|
||||
if isinstance(v, str):
|
||||
# if tb: tb.updateText(moves=v)
|
||||
if sb:
|
||||
sb.updateText(moves=v)
|
||||
return
|
||||
if k == "player":
|
||||
if v is None:
|
||||
if tb:
|
||||
tb.updateText(player=_("Player\n"))
|
||||
return
|
||||
if isinstance(v, six.string_types):
|
||||
if tb:
|
||||
# if self.app.opt.toolbar_size:
|
||||
if tb.getSize():
|
||||
tb.updateText(player=_("Player\n") + v)
|
||||
else:
|
||||
tb.updateText(player=v)
|
||||
return
|
||||
if k == "stats":
|
||||
if v is None:
|
||||
if sb:
|
||||
sb.updateText(stats="")
|
||||
return
|
||||
if isinstance(v, tuple):
|
||||
t = "%d: %d/%d" % (v[0]+v[1], v[0], v[1])
|
||||
if sb:
|
||||
sb.updateText(stats=t)
|
||||
return
|
||||
if k == "time":
|
||||
if v is None:
|
||||
if sb:
|
||||
sb.updateText(time='')
|
||||
if isinstance(v, six.string_types):
|
||||
if sb:
|
||||
sb.updateText(time=v)
|
||||
return
|
||||
if k == 'stuck':
|
||||
if sb:
|
||||
sb.updateText(stuck=v)
|
||||
return
|
||||
raise AttributeError(k)
|
||||
|
||||
|
||||
class Game(object):
|
||||
# for self.gstats.updated
|
||||
U_PLAY = 0
|
||||
|
@ -1035,87 +1118,8 @@ class Game(object):
|
|||
if self.preview:
|
||||
return
|
||||
tb, sb = self.app.toolbar, self.app.statusbar
|
||||
for k, v in kw.items():
|
||||
if k == "gamenumber":
|
||||
if v is None:
|
||||
if sb:
|
||||
sb.updateText(gamenumber="")
|
||||
# self.top.wm_title("%s - %s"
|
||||
# % (TITLE, self.getTitleName()))
|
||||
continue
|
||||
if isinstance(v, six.string_types):
|
||||
if sb:
|
||||
sb.updateText(gamenumber=v)
|
||||
# self.top.wm_title("%s - %s %s" % (TITLE,
|
||||
# self.getTitleName(), v))
|
||||
continue
|
||||
if k == "info":
|
||||
# print 'updateStatus info:', v
|
||||
if v is None:
|
||||
if sb:
|
||||
sb.updateText(info="")
|
||||
continue
|
||||
if isinstance(v, str):
|
||||
if sb:
|
||||
sb.updateText(info=v)
|
||||
continue
|
||||
if k == "moves":
|
||||
if v is None:
|
||||
# if tb: tb.updateText(moves="Moves\n")
|
||||
if sb:
|
||||
sb.updateText(moves="")
|
||||
continue
|
||||
if isinstance(v, tuple):
|
||||
# if tb: tb.updateText(moves="Moves\n%d/%d" % v)
|
||||
if sb:
|
||||
sb.updateText(moves="%d/%d" % v)
|
||||
continue
|
||||
if isinstance(v, int):
|
||||
# if tb: tb.updateText(moves="Moves\n%d" % v)
|
||||
if sb:
|
||||
sb.updateText(moves="%d" % v)
|
||||
continue
|
||||
if isinstance(v, str):
|
||||
# if tb: tb.updateText(moves=v)
|
||||
if sb:
|
||||
sb.updateText(moves=v)
|
||||
continue
|
||||
if k == "player":
|
||||
if v is None:
|
||||
if tb:
|
||||
tb.updateText(player=_("Player\n"))
|
||||
continue
|
||||
if isinstance(v, six.string_types):
|
||||
if tb:
|
||||
# if self.app.opt.toolbar_size:
|
||||
if self.app.toolbar.getSize():
|
||||
tb.updateText(player=_("Player\n") + v)
|
||||
else:
|
||||
tb.updateText(player=v)
|
||||
continue
|
||||
if k == "stats":
|
||||
if v is None:
|
||||
if sb:
|
||||
sb.updateText(stats="")
|
||||
continue
|
||||
if isinstance(v, tuple):
|
||||
t = "%d: %d/%d" % (v[0]+v[1], v[0], v[1])
|
||||
if sb:
|
||||
sb.updateText(stats=t)
|
||||
continue
|
||||
if k == "time":
|
||||
if v is None:
|
||||
if sb:
|
||||
sb.updateText(time='')
|
||||
if isinstance(v, six.string_types):
|
||||
if sb:
|
||||
sb.updateText(time=v)
|
||||
continue
|
||||
if k == 'stuck':
|
||||
if sb:
|
||||
sb.updateText(stuck=v)
|
||||
continue
|
||||
raise AttributeError(k)
|
||||
for k, v in six.iteritems(kw):
|
||||
_updateStatus_process_key_val(tb, sb, k, v)
|
||||
|
||||
def _unmapHandler(self, event):
|
||||
# pause game if root window has been iconified
|
||||
|
@ -3165,14 +3169,9 @@ Please report this bug."""))
|
|||
|
||||
def _loadGame(self, filename, app):
|
||||
game = None
|
||||
f = None
|
||||
try:
|
||||
f = open(filename, "rb")
|
||||
with open(filename, "rb") as f:
|
||||
game = self._undumpGame(Unpickler(f), app)
|
||||
game.gstats.loaded = game.gstats.loaded + 1
|
||||
finally:
|
||||
if f:
|
||||
f.close()
|
||||
return game
|
||||
|
||||
def _undumpGame(self, p, app):
|
||||
|
@ -3269,15 +3268,9 @@ in the current implementation.''') % version)
|
|||
return game
|
||||
|
||||
def _saveGame(self, filename, protocol=-1):
|
||||
f = None
|
||||
try:
|
||||
if not self.canSaveGame():
|
||||
raise Exception("Cannot save this game.")
|
||||
f = open(filename, "wb")
|
||||
self._dumpGame(Pickler(f, protocol))
|
||||
finally:
|
||||
if f:
|
||||
f.close()
|
||||
if self.canSaveGame():
|
||||
with open(filename, "wb") as f:
|
||||
self._dumpGame(Pickler(f, protocol))
|
||||
|
||||
def _dumpGame(self, p, bookmark=0):
|
||||
self.updateTime()
|
||||
|
|
|
@ -399,11 +399,11 @@ def write_game(app, game=None):
|
|||
check_game = False
|
||||
|
||||
# print '===>', fn
|
||||
fd = open(fn, 'w')
|
||||
with open(fn, 'w') as fd:
|
||||
|
||||
fd.write('''\
|
||||
fd.write('''\
|
||||
## -*- coding: utf-8 -*-
|
||||
## THIS FILE WAS GENERATED AUTOMATICALLY BY SOLITAIRE WIZARD
|
||||
## THIS FILE WAS GENERATED AUTOMATICALLY BY THE SOLITAIRE WIZARD
|
||||
## DO NOT EDIT
|
||||
|
||||
from pysollib.customgame import CustomGame, registerCustomGame
|
||||
|
@ -413,35 +413,34 @@ class MyCustomGame(CustomGame):
|
|||
SETTINGS = {
|
||||
''')
|
||||
|
||||
for w in WizardWidgets:
|
||||
if isinstance(w, six.string_types):
|
||||
continue
|
||||
v = w.variable.get()
|
||||
if w.widget in ('menu', 'preset'):
|
||||
v = w.translation_map[v]
|
||||
if v == w.default:
|
||||
# save only unique values
|
||||
continue
|
||||
if isinstance(v, int):
|
||||
fd.write(" '%s': %i,\n" % (w.var_name, v))
|
||||
else:
|
||||
if w.var_name == 'name':
|
||||
# escape
|
||||
v = v.replace('\\', '\\\\')
|
||||
v = v.replace("'", "\\'")
|
||||
if isinstance(v, six.text_type):
|
||||
v = v.encode('utf-8')
|
||||
if not v:
|
||||
v = 'Invalid Game Name'
|
||||
fd.write(" '%s': '%s',\n" % (w.var_name, v))
|
||||
fd.write(" 'gameid': %i,\n" % gameid)
|
||||
for w in WizardWidgets:
|
||||
if isinstance(w, six.string_types):
|
||||
continue
|
||||
v = w.variable.get()
|
||||
if w.widget in ('menu', 'preset'):
|
||||
v = w.translation_map[v]
|
||||
if v == w.default:
|
||||
# save only unique values
|
||||
continue
|
||||
if isinstance(v, int):
|
||||
fd.write(" '%s': %i,\n" % (w.var_name, v))
|
||||
else:
|
||||
if w.var_name == 'name':
|
||||
# escape
|
||||
v = v.replace('\\', '\\\\')
|
||||
v = v.replace("'", "\\'")
|
||||
if isinstance(v, six.text_type):
|
||||
v = v.encode('utf-8')
|
||||
if not v:
|
||||
v = 'Invalid Game Name'
|
||||
fd.write(" '%s': '%s',\n" % (w.var_name, v))
|
||||
fd.write(" 'gameid': %i,\n" % gameid)
|
||||
|
||||
fd.write('''\
|
||||
}
|
||||
fd.write('''\
|
||||
}
|
||||
|
||||
registerCustomGame(MyCustomGame)
|
||||
''')
|
||||
fd.close()
|
||||
|
||||
loadGame(mn, fn, check_game=check_game)
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue