From f18c394c33b278ccb48b1a6f71cd8948632df097 Mon Sep 17 00:00:00 2001 From: Shlomi Fish Date: Mon, 12 Mar 2018 21:58:06 +0200 Subject: [PATCH] Extract a common function. --- html-src/gen-html.py | 9 ++------- pysollib/app.py | 9 +++------ pysollib/mfxutil.py | 8 ++++++-- pysollib/resource.py | 1 - scripts/all_games.py | 7 +------ 5 files changed, 12 insertions(+), 22 deletions(-) diff --git a/html-src/gen-html.py b/html-src/gen-html.py index 1b7174bd..b567b8c8 100755 --- a/html-src/gen-html.py +++ b/html-src/gen-html.py @@ -2,13 +2,12 @@ import sys import os -import re import builtins from pysollib.mygettext import fix_gettext from pysollib.gamedb import GAME_DB from pysollib.gamedb import GI -from pysollib.mfxutil import latin1_to_ascii +from pysollib.mfxutil import latin1_normalize # outdir = '../html' pysollib_dir = '../' @@ -129,11 +128,7 @@ alink="#FF0000"> def getGameRulesFilename(n): if n.startswith('Mahjongg'): return 'mahjongg.html' - # n = re.sub(r"[\[\(].*$", "", n) - n = latin1_to_ascii(n) - n = re.sub(r"[^\w]", "", n) - n = n.lower() + ".html" - return n + return latin1_normalize(n) + '.html' def gen_main_html(): diff --git a/pysollib/app.py b/pysollib/app.py index ef88cb94..15ca4976 100644 --- a/pysollib/app.py +++ b/pysollib/app.py @@ -31,7 +31,7 @@ import traceback from pysollib.mfxutil import destruct, Struct from pysollib.mfxutil import pickle, unpickle, UnpicklingError from pysollib.mfxutil import getusername, getprefdir -from pysollib.mfxutil import latin1_to_ascii, print_err +from pysollib.mfxutil import latin1_normalize, print_err from pysollib.mfxutil import USE_PIL from pysollib.util import CARDSET, IMAGE_EXTENSIONS from pysollib.settings import PACKAGE, VERSION_TUPLE, WIN_SYSTEM @@ -1257,16 +1257,13 @@ Please select a %s type %s. return None return gi.short_name - def _calcGameFn(self, n): - return re.sub(r"[^\w]", "", latin1_to_ascii(n).lower()) - def getGameRulesFilename(self, id): gi = self.gdb.get(id) if gi is None: return None if gi.rules_filename is not None: return gi.rules_filename - n = self._calcGameFn(gi.en_name) + '.html' # english name + n = latin1_normalize(gi.en_name) + '.html' # english name f = os.path.join(self.dataloader.dir, "html", "rules", n) if not os.path.exists(f): n = '' @@ -1279,7 +1276,7 @@ Please select a %s type %s. n = self.gdb.get(id).en_name # english name if not n: return None - return re.sub(r"[\s]", "_", self._calcGameFn(n)) + return re.sub(r"[\s]", "_", latin1_normalize(n)) def getRandomGameId(self, games=None): if games is None: diff --git a/pysollib/mfxutil.py b/pysollib/mfxutil.py index 41540930..1a4a0e4a 100644 --- a/pysollib/mfxutil.py +++ b/pysollib/mfxutil.py @@ -22,6 +22,7 @@ # ---------------------------------------------------------------------------## # imports +import re import sys import os import time @@ -79,13 +80,16 @@ def latin1_to_ascii(n): # return n n = n.encode('iso8859-1', 'replace') # FIXME: rewrite this for better speed - n = (n.replace("\xc4", "Ae") + return (n.replace("\xc4", "Ae") .replace("\xd6", "Oe") .replace("\xdc", "Ue") .replace("\xe4", "ae") .replace("\xf6", "oe") .replace("\xfc", "ue")) - return n + + +def latin1_normalize(n): + return re.sub(r"[^\w]", "", latin1_to_ascii(n).lower()) def format_time(t): diff --git a/pysollib/resource.py b/pysollib/resource.py index 523c20cf..e139bd1a 100644 --- a/pysollib/resource.py +++ b/pysollib/resource.py @@ -58,7 +58,6 @@ class Resource(Struct): def getSortKey(self): return self.name.lower() - # return latin1_to_ascii(self.name).lower() class ResourceManager: diff --git a/scripts/all_games.py b/scripts/all_games.py index 4f4e5bed..9905261c 100755 --- a/scripts/all_games.py +++ b/scripts/all_games.py @@ -4,7 +4,6 @@ import sys import os -import re import time # from pprint import pprint import __builtin__ @@ -34,11 +33,7 @@ fix_gettext() def getGameRulesFilename(n): if n.startswith('Mahjongg'): return 'mahjongg.html' - # n = re.sub(r"[\[\(].*$", "", n) - n = latin1_to_ascii(n) - n = re.sub(r"[^\w]", "", n) - n = n.lower() + ".html" - return n + return latin1_normalize(n) + '.html' GAME_BY_TYPE = {