GEPS008: Create new module for keyword translation

svn: r19905
This commit is contained in:
Nick Hall 2012-06-23 19:43:18 +00:00
parent 640e203693
commit 3ec4c0f80c
6 changed files with 120 additions and 94 deletions

View File

@ -323,6 +323,7 @@ src/gen/simple/_simpletable.py
# gen.utils # gen.utils
src/gen/utils/alive.py src/gen/utils/alive.py
src/gen/utils/keyword.py
src/gen/utils/lds.py src/gen/utils/lds.py
src/gen/utils/place.py src/gen/utils/place.py
src/gen/utils/trans.py src/gen/utils/trans.py

View File

@ -656,88 +656,6 @@ def profile(func, *args):
stats.print_stats(100) stats.print_stats(100)
stats.print_callers(100) stats.print_callers(100)
#-------------------------------------------------------------------------
#
# Keyword translation interface
#
#-------------------------------------------------------------------------
# keyword, code, translated standard, translated upper
# in gen.display.name.py we find:
# 't' : title = title
# 'f' : given = given (first names)
# 'l' : surname = full surname (lastname)
# 'c' : call = callname
# 'x' : common = nick name if existing, otherwise first first name (common name)
# 'i' : initials = initials of the first names
# 'm' : primary = primary surname (main)
# '0m': primary[pre]= prefix primary surname (main)
# '1m': primary[sur]= surname primary surname (main)
# '2m': primary[con]= connector primary surname (main)
# 'y' : patronymic = pa/matronymic surname (father/mother) - assumed unique
# '0y': patronymic[pre] = prefix "
# '1y': patronymic[sur] = surname "
# '2y': patronymic[con] = connector "
# 'o' : notpatronymic = surnames without pa/matronymic and primary
# 'r' : rest = non primary surnames
# 'p' : prefix = list of all prefixes
# 'q' : rawsurnames = surnames without prefixes and connectors
# 's' : suffix = suffix
# 'n' : nickname = nick name
# 'g' : familynick = family nick name
KEYWORDS = [("title", "t", _("Person|Title"), _("Person|TITLE")),
("given", "f", _("Given"), _("GIVEN")),
("surname", "l", _("Surname"), _("SURNAME")),
("call", "c", _("Name|Call"), _("Name|CALL")),
("common", "x", _("Name|Common"), _("Name|COMMON")),
("initials", "i", _("Initials"), _("INITIALS")),
("suffix", "s", _("Suffix"), _("SUFFIX")),
("primary", "m", _("Name|Primary"), _("PRIMARY")),
("primary[pre]", "0m", _("Primary[pre]"), _("PRIMARY[PRE]")),
("primary[sur]", "1m", _("Primary[sur]"), _("PRIMARY[SUR]")),
("primary[con]", "2m", _("Primary[con]"), _("PRIMARY[CON]")),
("patronymic", "y", _("Patronymic"), _("PATRONYMIC")),
("patronymic[pre]", "0y", _("Patronymic[pre]"), _("PATRONYMIC[PRE]")),
("patronymic[sur]", "1y", _("Patronymic[sur]"), _("PATRONYMIC[SUR]")),
("patronymic[con]", "2y", _("Patronymic[con]"), _("PATRONYMIC[CON]")),
("rawsurnames", "q", _("Rawsurnames"), _("RAWSURNAMES")),
("notpatronymic", "o", _("Notpatronymic"),_("NOTPATRONYMIC")),
("prefix", "p", _("Prefix"), _("PREFIX")),
("nickname", "n", _("Nickname"), _("NICKNAME")),
("familynick", "g", _("Familynick"), _("FAMILYNICK")),
]
KEY_TO_TRANS = {}
TRANS_TO_KEY = {}
for (key, code, standard, upper) in KEYWORDS:
KEY_TO_TRANS[key] = standard
KEY_TO_TRANS[key.upper()] = upper
KEY_TO_TRANS["%" + ("%s" % code)] = standard
KEY_TO_TRANS["%" + ("%s" % code.upper())] = upper
TRANS_TO_KEY[standard.lower()] = key
TRANS_TO_KEY[standard] = key
TRANS_TO_KEY[upper] = key.upper()
def get_translation_from_keyword(keyword):
""" Return the translation of keyword """
return KEY_TO_TRANS.get(keyword, keyword)
def get_keyword_from_translation(word):
""" Return the keyword of translation """
return TRANS_TO_KEY.get(word, word)
def get_keywords():
""" Get all keywords, longest to shortest """
keys = KEY_TO_TRANS.keys()
keys.sort(lambda a,b: -cmp(len(a), len(b)))
return keys
def get_translations():
""" Get all translations, longest to shortest """
trans = TRANS_TO_KEY.keys()
trans.sort(lambda a,b: -cmp(len(a), len(b)))
return trans
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #
# Config-based functions # Config-based functions

View File

@ -14,6 +14,7 @@ pkgpython_PYTHON = \
configmanager.py \ configmanager.py \
fallback.py \ fallback.py \
image.py \ image.py \
keyword.py \
lds.py \ lds.py \
mactrans.py \ mactrans.py \
place.py \ place.py \

103
src/gen/utils/keyword.py Normal file
View File

@ -0,0 +1,103 @@
#
# Gramps - a GTK+/GNOME based genealogy program
#
# Copyright (C) 2000-2007 Donald N. Allingham
# Copyright (C) 2009 Gary Burton
# Copyright (C) 2011 Tim G L Lyons
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# $Id$
"""
Keyword translation interface
"""
# keyword, code, translated standard, translated upper
# in gen.display.name.py we find:
# 't' : title = title
# 'f' : given = given (first names)
# 'l' : surname = full surname (lastname)
# 'c' : call = callname
# 'x' : common = nick name if existing, otherwise first first name (common name)
# 'i' : initials = initials of the first names
# 'm' : primary = primary surname (main)
# '0m': primary[pre]= prefix primary surname (main)
# '1m': primary[sur]= surname primary surname (main)
# '2m': primary[con]= connector primary surname (main)
# 'y' : patronymic = pa/matronymic surname (father/mother) - assumed unique
# '0y': patronymic[pre] = prefix "
# '1y': patronymic[sur] = surname "
# '2y': patronymic[con] = connector "
# 'o' : notpatronymic = surnames without pa/matronymic and primary
# 'r' : rest = non primary surnames
# 'p' : prefix = list of all prefixes
# 'q' : rawsurnames = surnames without prefixes and connectors
# 's' : suffix = suffix
# 'n' : nickname = nick name
# 'g' : familynick = family nick name
KEYWORDS = [("title", "t", _("Person|Title"), _("Person|TITLE")),
("given", "f", _("Given"), _("GIVEN")),
("surname", "l", _("Surname"), _("SURNAME")),
("call", "c", _("Name|Call"), _("Name|CALL")),
("common", "x", _("Name|Common"), _("Name|COMMON")),
("initials", "i", _("Initials"), _("INITIALS")),
("suffix", "s", _("Suffix"), _("SUFFIX")),
("primary", "m", _("Name|Primary"), _("PRIMARY")),
("primary[pre]", "0m", _("Primary[pre]"), _("PRIMARY[PRE]")),
("primary[sur]", "1m", _("Primary[sur]"), _("PRIMARY[SUR]")),
("primary[con]", "2m", _("Primary[con]"), _("PRIMARY[CON]")),
("patronymic", "y", _("Patronymic"), _("PATRONYMIC")),
("patronymic[pre]", "0y", _("Patronymic[pre]"), _("PATRONYMIC[PRE]")),
("patronymic[sur]", "1y", _("Patronymic[sur]"), _("PATRONYMIC[SUR]")),
("patronymic[con]", "2y", _("Patronymic[con]"), _("PATRONYMIC[CON]")),
("rawsurnames", "q", _("Rawsurnames"), _("RAWSURNAMES")),
("notpatronymic", "o", _("Notpatronymic"),_("NOTPATRONYMIC")),
("prefix", "p", _("Prefix"), _("PREFIX")),
("nickname", "n", _("Nickname"), _("NICKNAME")),
("familynick", "g", _("Familynick"), _("FAMILYNICK")),
]
KEY_TO_TRANS = {}
TRANS_TO_KEY = {}
for (key, code, standard, upper) in KEYWORDS:
KEY_TO_TRANS[key] = standard
KEY_TO_TRANS[key.upper()] = upper
KEY_TO_TRANS["%" + ("%s" % code)] = standard
KEY_TO_TRANS["%" + ("%s" % code.upper())] = upper
TRANS_TO_KEY[standard.lower()] = key
TRANS_TO_KEY[standard] = key
TRANS_TO_KEY[upper] = key.upper()
def get_translation_from_keyword(keyword):
""" Return the translation of keyword """
return KEY_TO_TRANS.get(keyword, keyword)
def get_keyword_from_translation(word):
""" Return the keyword of translation """
return TRANS_TO_KEY.get(word, word)
def get_keywords():
""" Get all keywords, longest to shortest """
keys = KEY_TO_TRANS.keys()
keys.sort(lambda a,b: -cmp(len(a), len(b)))
return keys
def get_translations():
""" Get all translations, longest to shortest """
trans = TRANS_TO_KEY.keys()
trans.sort(lambda a,b: -cmp(len(a), len(b)))
return trans

View File

@ -51,8 +51,10 @@ import const
import gen.datehandler import gen.datehandler
from gen.display.name import displayer as _nd from gen.display.name import displayer as _nd
from gen.display.name import NameDisplayError from gen.display.name import NameDisplayError
import Utils from Utils import get_unicode_path_from_file_chooser
from gen.utils.alive import update_constants from gen.utils.alive import update_constants
from gen.utils.keyword import (get_keywords, get_translation_from_keyword,
get_translations, get_keyword_from_translation)
import gen.lib import gen.lib
from gen.lib import Name, Surname, NameOriginType from gen.lib import Name, Surname, NameOriginType
from gui.managedwindow import ManagedWindow from gui.managedwindow import ManagedWindow
@ -528,9 +530,9 @@ class GrampsPreferences(ConfigureDialog):
the_index = 0 the_index = 0
for num, name, fmt_str, act in _nd.get_name_format(): for num, name, fmt_str, act in _nd.get_name_format():
translation = fmt_str translation = fmt_str
for key in Utils.get_keywords(): for key in get_keywords():
if key in translation: if key in translation:
translation = translation.replace(key, Utils.get_translation_from_keyword(key)) translation = translation.replace(key, get_translation_from_keyword(key))
self.examplename.set_display_as(num) self.examplename.set_display_as(num)
name_format_model.append( name_format_model.append(
row=[num, translation, fmt_str, _nd.display_name(self.examplename)]) row=[num, translation, fmt_str, _nd.display_name(self.examplename)])
@ -662,9 +664,9 @@ class GrampsPreferences(ConfigureDialog):
new_text[-1] == '"'): new_text[-1] == '"'):
pass pass
else: else:
for key in Utils.get_translations(): for key in get_translations():
if key in pattern: if key in pattern:
pattern = pattern.replace(key, Utils.get_keyword_from_translation(key)) pattern = pattern.replace(key, get_keyword_from_translation(key))
# now build up a proper translation: # now build up a proper translation:
translation = pattern translation = pattern
if (len(new_text) > 2 and if (len(new_text) > 2 and
@ -672,9 +674,9 @@ class GrampsPreferences(ConfigureDialog):
new_text[-1] == '"'): new_text[-1] == '"'):
pass pass
else: else:
for key in Utils.get_keywords(): for key in get_keywords():
if key in translation: if key in translation:
translation = translation.replace(key, Utils.get_translation_from_keyword(key)) translation = translation.replace(key, get_translation_from_keyword(key))
num, name, fmt = self.selected_fmt[COL_NUM:COL_EXPL] num, name, fmt = self.selected_fmt[COL_NUM:COL_EXPL]
node = self.fmt_model.get_iter(path) node = self.fmt_model.get_iter(path)
oldname = self.fmt_model.get_value(node, COL_NAME) oldname = self.fmt_model.get_value(node, COL_NAME)
@ -1195,7 +1197,7 @@ class GrampsPreferences(ConfigureDialog):
status = f.run() status = f.run()
if status == gtk.RESPONSE_OK: if status == gtk.RESPONSE_OK:
val = Utils.get_unicode_path_from_file_chooser(f.get_filename()) val = get_unicode_path_from_file_chooser(f.get_filename())
if val: if val:
self.path_entry.set_text(val) self.path_entry.set_text(val)
f.destroy() f.destroy()

View File

@ -25,7 +25,8 @@
import unittest import unittest
from test import test_util from test import test_util
test_util.path_append_parent() test_util.path_append_parent()
import Utils from gen.utils.keyword import (KEYWORDS, get_translation_from_keyword,
get_keyword_from_translation)
class TestCase(unittest.TestCase): class TestCase(unittest.TestCase):
count = 1 count = 1
@ -38,11 +39,11 @@ class TestCase(unittest.TestCase):
def helper(self, *args): def helper(self, *args):
method_name, test_type, item1, item2 = args method_name, test_type, item1, item2 = args
if test_type == "keyword": if test_type == "keyword":
result = Utils.get_translation_from_keyword(item1) result = get_translation_from_keyword(item1)
self.assertTrue(result == item2, self.assertTrue(result == item2,
"get_translation_from_keyword('%s') returned '%s' rather than '%s'" % (item1, result, item2)) "get_translation_from_keyword('%s') returned '%s' rather than '%s'" % (item1, result, item2))
elif test_type == "translation": elif test_type == "translation":
result = Utils.get_keyword_from_translation(item1) result = get_keyword_from_translation(item1)
self.assertTrue(result == item2, self.assertTrue(result == item2,
"get_keyword_from_translation('%s') returned '%s' rather than '%s'" % (item1, result, item2)) "get_keyword_from_translation('%s') returned '%s' rather than '%s'" % (item1, result, item2))
else: else:
@ -52,7 +53,7 @@ def suite1():
""" """
""" """
suite = unittest.TestSuite() suite = unittest.TestSuite()
for line in Utils.KEYWORDS: for line in KEYWORDS:
keyword, code, standard, upper = line keyword, code, standard, upper = line
suite.addTest(TestCase('keyword-%04d', 'keyword', keyword, standard)) suite.addTest(TestCase('keyword-%04d', 'keyword', keyword, standard))
suite.addTest(TestCase('translation-%04d', 'translation', standard, keyword)) suite.addTest(TestCase('translation-%04d', 'translation', standard, keyword))