diff --git a/gramps/gen/__init__.py b/gramps/gen/__init__.py index ecc759cb4..999f29a83 100644 --- a/gramps/gen/__init__.py +++ b/gramps/gen/__init__.py @@ -17,12 +17,11 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # -# gen/__init__.py """ The gen module provides packages that are common to all gramps interfaces (gui, cli and web). """ -__all__ = [ "datehandler", "db", "display", "filters", "lib", "locale", "merge", - "mime", "plug", "proxy", "simple", "utils" ] +__all__ = ["datehandler", "db", "display", "filters", "lib", "merge", + "mime", "plug", "proxy", "simple", "utils"] diff --git a/gramps/gen/config.py b/gramps/gen/config.py index 39a4efdda..eb59e6bb0 100644 --- a/gramps/gen/config.py +++ b/gramps/gen/config.py @@ -31,7 +31,7 @@ This package implements access to GRAMPS configuration. # Gramps imports # #--------------------------------------------------------------- -import os, sys +import os import re import logging @@ -40,10 +40,10 @@ import logging # Gramps imports # #--------------------------------------------------------------- -from .const import GRAMPS_LOCALE as glocale -_ = glocale.translation.gettext from .const import HOME_DIR, USER_HOME, VERSION_DIR from .utils.configmanager import ConfigManager +from .const import GRAMPS_LOCALE as glocale +_ = glocale.translation.gettext #--------------------------------------------------------------- # @@ -134,7 +134,7 @@ register('behavior.check-for-update-types', ["new"]) register('behavior.last-check-for-updates', "1970/01/01") register('behavior.previously-seen-updates', []) register('behavior.do-not-show-previously-seen-updates', True) -register('behavior.database-path', os.path.join( HOME_DIR, 'grampsdb')) +register('behavior.database-path', os.path.join(HOME_DIR, 'grampsdb')) register('behavior.database-backend', 'bsddb') register('behavior.date-about-range', 50) register('behavior.date-after-range', 50) @@ -154,13 +154,13 @@ register('behavior.welcome', 100) register('behavior.web-search-url', 'http://google.com/#&q=%(text)s') register('behavior.addons-url', "https://raw.githubusercontent.com/gramps-project/addons/master/gramps50") -register('export.proxy-order', [ - ["privacy", 0], - ["living", 0], - ["person", 0], - ["note", 0], - ["reference", 0], - ]) +register('export.proxy-order', + [["privacy", 0], + ["living", 0], + ["person", 0], + ["note", 0], + ["reference", 0]] + ) register('geography.center-lon', 0.0) register('geography.lock', False) @@ -365,7 +365,7 @@ if not os.path.exists(CONFIGMAN.filename): # read it in old style: logging.warning("Importing old key file 'keys.ini'...") CONFIGMAN.load(os.path.join(HOME_DIR, "keys.ini"), - oldstyle=True) + oldstyle=True) logging.warning("Done importing old key file 'keys.ini'") # other version upgrades here... # check previous version of gramps: @@ -383,11 +383,14 @@ if not os.path.exists(CONFIGMAN.filename): # Perhaps addings specific list of versions to check # ----------------------------------------- digits = str(int(match.groups()[0]) - i) - previous_grampsini = os.path.join(fullpath, "gramps" + str(digits), filename) + previous_grampsini = os.path.join(fullpath, "gramps" + digits, + filename) if os.path.exists(previous_grampsini): - logging.warning("Importing old config file '%s'..." % previous_grampsini) + logging.warning("Importing old config file '%s'...", + previous_grampsini) CONFIGMAN.load(previous_grampsini) - logging.warning("Done importing old config file '%s'" % previous_grampsini) + logging.warning("Done importing old config file '%s'", + previous_grampsini) break #--------------------------------------------------------------- diff --git a/gramps/gen/const.py b/gramps/gen/const.py index db8186f8e..23e55b1f6 100644 --- a/gramps/gen/const.py +++ b/gramps/gen/const.py @@ -41,23 +41,27 @@ import uuid #------------------------------------------------------------------------- from .git_revision import get_git_revision from .constfunc import get_env_var -#------------------------------------------------------------------------- -# -# Gramps Version -# -#------------------------------------------------------------------------- -PROGRAM_NAME = "Gramps" from gramps.version import VERSION, VERSION_TUPLE, major_version +from gramps.gen.utils.resourcepath import ResourcePath +from gramps.gen.utils.grampslocale import GrampsLocale + #------------------------------------------------------------------------- # -# Standard GRAMPS Websites +# Gramps program name # #------------------------------------------------------------------------- -URL_HOMEPAGE = "http://gramps-project.org/" +PROGRAM_NAME = "Gramps" + +#------------------------------------------------------------------------- +# +# Standard Gramps Websites +# +#------------------------------------------------------------------------- +URL_HOMEPAGE = "http://gramps-project.org/" URL_MAILINGLIST = "http://sourceforge.net/mail/?group_id=25770" -URL_BUGHOME = "http://bugs.gramps-project.org" -URL_BUGTRACKER = "http://bugs.gramps-project.org/bug_report_page.php" -URL_WIKISTRING = "http://gramps-project.org/wiki/index.php?title=" +URL_BUGHOME = "http://bugs.gramps-project.org" +URL_BUGTRACKER = "http://bugs.gramps-project.org/bug_report_page.php" +URL_WIKISTRING = "http://gramps-project.org/wiki/index.php?title=" URL_MANUAL_PAGE = "Gramps_%s_Wiki_Manual" % major_version URL_MANUAL_DATA = '%s_-_Entering_and_editing_data:_detailed' % URL_MANUAL_PAGE URL_MANUAL_SECT1 = '%s_-_part_1' % URL_MANUAL_DATA @@ -65,7 +69,7 @@ URL_MANUAL_SECT2 = '%s_-_part_2' % URL_MANUAL_DATA URL_MANUAL_SECT3 = '%s_-_part_3' % URL_MANUAL_DATA WIKI_FAQ = "FAQ" WIKI_KEYBINDINGS = "Gramps_%s_Wiki_Manual_-_Keybindings" % major_version -WIKI_EXTRAPLUGINS= "%s_Addons" % major_version +WIKI_EXTRAPLUGINS = "%s_Addons" % major_version WIKI_EXTRAPLUGINS_RAWDATA = "Plugins%s&action=raw" % major_version #------------------------------------------------------------------------- @@ -73,13 +77,13 @@ WIKI_EXTRAPLUGINS_RAWDATA = "Plugins%s&action=raw" % major_version # Mime Types # #------------------------------------------------------------------------- -APP_FAMTREE = 'x-directory/normal' -APP_GRAMPS = "application/x-gramps" -APP_GRAMPS_XML = "application/x-gramps-xml" -APP_GEDCOM = "application/x-gedcom" -APP_GRAMPS_PKG = "application/x-gramps-package" -APP_GENEWEB = "application/x-geneweb" -APP_VCARD = ["text/x-vcard", "text/x-vcalendar"] +APP_FAMTREE = 'x-directory/normal' +APP_GRAMPS = "application/x-gramps" +APP_GRAMPS_XML = "application/x-gramps-xml" +APP_GEDCOM = "application/x-gedcom" +APP_GRAMPS_PKG = "application/x-gramps-package" +APP_GENEWEB = "application/x-geneweb" +APP_VCARD = ["text/x-vcard", "text/x-vcalendar"] #------------------------------------------------------------------------- # @@ -102,19 +106,19 @@ else: HOME_DIR = os.path.join(USER_HOME, '.gramps') -VERSION_DIR = os.path.join( +VERSION_DIR = os.path.join( HOME_DIR, "gramps%s%s" % (VERSION_TUPLE[0], VERSION_TUPLE[1])) CUSTOM_FILTERS = os.path.join(VERSION_DIR, "custom_filters.xml") REPORT_OPTIONS = os.path.join(HOME_DIR, "report_options.xml") -TOOL_OPTIONS = os.path.join(HOME_DIR, "tool_options.xml") +TOOL_OPTIONS = os.path.join(HOME_DIR, "tool_options.xml") -ENV_DIR = os.path.join(HOME_DIR, "env") -TEMP_DIR = os.path.join(HOME_DIR, "temp") -THUMB_DIR = os.path.join(HOME_DIR, "thumb") -THUMB_NORMAL = os.path.join(THUMB_DIR, "normal") -THUMB_LARGE = os.path.join(THUMB_DIR, "large") -USER_PLUGINS = os.path.join(VERSION_DIR, "plugins") +ENV_DIR = os.path.join(HOME_DIR, "env") +TEMP_DIR = os.path.join(HOME_DIR, "temp") +THUMB_DIR = os.path.join(HOME_DIR, "thumb") +THUMB_NORMAL = os.path.join(THUMB_DIR, "normal") +THUMB_LARGE = os.path.join(THUMB_DIR, "large") +USER_PLUGINS = os.path.join(VERSION_DIR, "plugins") # dirs checked/made for each Gramps session USER_DIRLIST = (USER_HOME, HOME_DIR, VERSION_DIR, ENV_DIR, TEMP_DIR, THUMB_DIR, THUMB_NORMAL, THUMB_LARGE, USER_PLUGINS) @@ -138,15 +142,15 @@ VERSION += git_revision # # Glade files # -GLADE_DIR = os.path.join(ROOT_DIR, "gui", "glade") -GLADE_FILE = os.path.join(GLADE_DIR, "gramps.glade") -PERSON_GLADE = os.path.join(GLADE_DIR, "edit_person.glade") -PLUGINS_GLADE = os.path.join(GLADE_DIR, "plugins.glade") -MERGE_GLADE = os.path.join(GLADE_DIR, "mergedata.glade") -RULE_GLADE = os.path.join(GLADE_DIR, "rule.glade") +GLADE_DIR = os.path.join(ROOT_DIR, "gui", "glade") +GLADE_FILE = os.path.join(GLADE_DIR, "gramps.glade") +PERSON_GLADE = os.path.join(GLADE_DIR, "edit_person.glade") +PLUGINS_GLADE = os.path.join(GLADE_DIR, "plugins.glade") +MERGE_GLADE = os.path.join(GLADE_DIR, "mergedata.glade") +RULE_GLADE = os.path.join(GLADE_DIR, "rule.glade") -PLUGINS_DIR = os.path.join(ROOT_DIR, "plugins") +PLUGINS_DIR = os.path.join(ROOT_DIR, "plugins") USE_TIPS = False @@ -160,7 +164,6 @@ else: # Paths to data files. # #------------------------------------------------------------------------- -from gramps.gen.utils.resourcepath import ResourcePath _resources = ResourcePath() DATA_DIR = _resources.data_dir IMAGE_DIR = _resources.image_dir @@ -203,7 +206,6 @@ ENV = { # Init Localization # #------------------------------------------------------------------------- -from gramps.gen.utils.grampslocale import GrampsLocale GRAMPS_LOCALE = GrampsLocale(localedir=_resources.locale_dir) _ = GRAMPS_LOCALE.translation.sgettext GTK_GETTEXT_DOMAIN = 'gtk30' @@ -213,12 +215,12 @@ GTK_GETTEXT_DOMAIN = 'gtk30' # About box information # #------------------------------------------------------------------------- -COPYRIGHT_MSG = "© 2001-2006 Donald N. Allingham\n" \ - "© 2007-2016 The Gramps Developers" -COMMENTS = _("Gramps\n (Genealogical Research and Analysis " - "Management Programming System)\n" - "is a personal genealogy program.") -AUTHORS = [ +COPYRIGHT_MSG = "© 2001-2006 Donald N. Allingham\n" \ + "© 2007-2016 The Gramps Developers" +COMMENTS = _("Gramps\n (Genealogical Research and Analysis " + "Management Programming System)\n" + "is a personal genealogy program.") +AUTHORS = [ "Alexander Roitman", "Benny Malengier", "Brian Matherly", @@ -233,7 +235,7 @@ AUTHORS = [ AUTHORS_FILE = os.path.join(DATA_DIR, "authors.xml") -DOCUMENTERS = [ +DOCUMENTERS = [ 'Alexander Roitman', ] @@ -242,12 +244,12 @@ DOCUMENTERS = [ # Constants # #------------------------------------------------------------------------- -THUMBSCALE = 96.0 +THUMBSCALE = 96.0 THUMBSCALE_LARGE = 180.0 -XMLFILE = "data.gramps" -NO_SURNAME = "(%s)" % _("surname|none") -NO_GIVEN = "(%s)" % _("given-name|none") -ARABIC_COMMA = "،" +XMLFILE = "data.gramps" +NO_SURNAME = "(%s)" % _("surname|none") +NO_GIVEN = "(%s)" % _("given-name|none") +ARABIC_COMMA = "،" ARABIC_SEMICOLON = "؛" #------------------------------------------------------------------------- @@ -259,18 +261,18 @@ ARABIC_SEMICOLON = "؛" # Note: Make sure to edit argparser.py _help string too! # (longName, shortName, type , default, flags, descrip , argDescrip) POPT_TABLE = [ - ("config", 'c', str, None, 0, "Set config setting(s) and start Gramps", ""), - ("open", 'O', str, None, 0, "Open family tree", "FAMILY_TREE"), - ("create", 'C', str, None, 0, "Create or Open family tree", "FAMILY_TREE"), - ("import", 'i', str, None, 0, "Import file", "FILENAME"), - ("export", 'e', str, None, 0, "Export file", "FILENAME"), - ("format", 'f', str, None, 0, 'Specify format', "FORMAT"), - ("action", 'a', str, None, 0, 'Specify action', "ACTION"), - ("options", 'p', str, None, 0, 'Specify options', "OPTIONS_STRING"), - ("debug", 'd', str, None, 0, 'Enable debug logs', "LOGGER_NAME"), - ("", 'l', None, None, 0, 'List Family Trees', ""), - ("", 'L', None, None, 0, 'List Family Tree Details', ""), - ("show", 's', None, None, 0, "Show config settings", ""), + ("config", 'c', str, None, 0, "Set config setting(s) and start Gramps", ""), + ("open", 'O', str, None, 0, "Open family tree", "FAMILY_TREE"), + ("create", 'C', str, None, 0, "Create or Open family tree", "FAMILY_TREE"), + ("import", 'i', str, None, 0, "Import file", "FILENAME"), + ("export", 'e', str, None, 0, "Export file", "FILENAME"), + ("format", 'f', str, None, 0, 'Specify format', "FORMAT"), + ("action", 'a', str, None, 0, 'Specify action', "ACTION"), + ("options", 'p', str, None, 0, 'Specify options', "OPTIONS_STRING"), + ("debug", 'd', str, None, 0, 'Enable debug logs', "LOGGER_NAME"), + ("", 'l', None, None, 0, 'List Family Trees', ""), + ("", 'L', None, None, 0, 'List Family Tree Details', ""), + ("show", 's', None, None, 0, "Show config settings", ""), ("force-unlock", 'u', None, None, 0, 'Force unlock of family tree', ""), ("version", 'v', None, None, 0, 'Show versions', ""), ] @@ -321,7 +323,7 @@ LONGOPTS = [ SHORTOPTS = "O:C:i:e:f:a:p:d:c:r:lLthuv?syq" -GRAMPS_UUID = uuid.UUID('516cd010-5a41-470f-99f8-eb22f1098ad6') +GRAMPS_UUID = uuid.UUID('516cd010-5a41-470f-99f8-eb22f1098ad6') #------------------------------------------------------------------------- # @@ -345,24 +347,24 @@ BACKGROUND_GRAD_AGE = 5 BACKGROUND_SINGLE_COLOR = 6 BACKGROUND_GRAD_PERIOD = 7 GENCOLOR = { - BACKGROUND_SCHEME1: ((255, 63, 0), - (255,175, 15), - (255,223, 87), - (255,255,111), - (159,255,159), - (111,215,255), - ( 79,151,255), - (231, 23,255), - (231, 23,121), - (210,170,124), - (189,153,112)), - BACKGROUND_SCHEME2: ((229,191,252), - (191,191,252), - (191,222,252), - (183,219,197), - (206,246,209)), - BACKGROUND_WHITE: ((255,255,255), - (255,255,255),), + BACKGROUND_SCHEME1: ((255, 63, 0), + (255, 175, 15), + (255, 223, 87), + (255, 255, 111), + (159, 255, 159), + (111, 215, 255), + (79, 151, 255), + (231, 23, 255), + (231, 23, 121), + (210, 170, 124), + (189, 153, 112)), + BACKGROUND_SCHEME2: ((229, 191, 252), + (191, 191, 252), + (191, 222, 252), + (183, 219, 197), + (206, 246, 209)), + BACKGROUND_WHITE: ((255, 255, 255), + (255, 255, 255),), } MAX_AGE = 100 diff --git a/gramps/gen/constfunc.py b/gramps/gen/constfunc.py index 902cd057e..5b7089fe0 100644 --- a/gramps/gen/constfunc.py +++ b/gramps/gen/constfunc.py @@ -32,7 +32,6 @@ perform a translation on import, eg Gtk. #------------------------------------------------------------------------ import platform import sys -import ctypes import os #------------------------------------------------------------------------- @@ -87,9 +86,12 @@ def is_quartz(): """ if mac(): try: + import gi + gi.require_version('Gtk', '3.0') + gi.require_version('Gdk', '3.0') from gi.repository import Gtk from gi.repository import Gdk - except: + except ImportError: return False return Gdk.Display.get_default().__class__.__name__.endswith("QuartzDisplay") return False @@ -102,9 +104,12 @@ def has_display(): # in argv, and we might have unicode. temp, sys.argv = sys.argv, sys.argv[:1] try: + import gi + gi.require_version('Gtk', '3.0') + gi.require_version('Gdk', '3.0') from gi.repository import Gtk from gi.repository import Gdk - except: + except ImportError: return False try: @@ -146,7 +151,7 @@ def get_env_var(name, default=None): environment variables. This routine does so using the native C wide-character function. ''' - if not name or not name in os.environ: + if not name or name not in os.environ: return default return os.environ[name] diff --git a/gramps/gen/dbstate.py b/gramps/gen/dbstate.py index ee57f231e..1d7097d2d 100644 --- a/gramps/gen/dbstate.py +++ b/gramps/gen/dbstate.py @@ -22,9 +22,21 @@ """ Provide the database state class """ + +#------------------------------------------------------------------------ +# +# Python modules +# +#------------------------------------------------------------------------ import sys import os +import logging +#------------------------------------------------------------------------ +# +# Gramps modules +# +#------------------------------------------------------------------------ from .db import DbReadBase from .proxy.proxybase import ProxyDbBase from .utils.callback import Callback @@ -35,7 +47,6 @@ from .config import config # set up logging # #------------------------------------------------------------------------- -import logging LOG = logging.getLogger(".dbstate") class DbState(Callback): @@ -54,8 +65,8 @@ class DbState(Callback): just a place holder until a real DB is assigned. """ Callback.__init__(self) - self.db = self.make_database("bsddb") - self.open = False + self.db = self.make_database("bsddb") + self.open = False self.stack = [] def change_database(self, database): @@ -82,7 +93,7 @@ class DbState(Callback): config.get('preferences.pprefix'), config.get('preferences.eprefix'), config.get('preferences.rprefix'), - config.get('preferences.nprefix') ) + config.get('preferences.nprefix')) self.open = True def signal_change(self): @@ -116,12 +127,15 @@ class DbState(Callback): >>> dbstate.apply_proxy(gramps.gen.proxy.LivingProxyDb, 0) >>> dbstate.apply_proxy(gramps.gen.proxy.PrivateProxyDb) - >>> from gramps.gen.filters.rules.person import IsDescendantOf, IsAncestorOf + >>> from gramps.gen.filters.rules.person import (IsDescendantOf, + IsAncestorOf) >>> from gramps.gen.filters import GenericFilter >>> filter = GenericFilter() >>> filter.set_logical_op("or") - >>> filter.add_rule(IsDescendantOf([db.get_default_person().gramps_id, True])) - >>> filter.add_rule(IsAncestorOf([db.get_default_person().gramps_id, True])) + >>> filter.add_rule(IsDescendantOf([db.get_default_person().gramps_id, + True])) + >>> filter.add_rule(IsAncestorOf([db.get_default_person().gramps_id, + True])) >>> dbstate.apply_proxy(gramps.gen.proxy.FilterProxyDb, filter) """ self.stack.append(self.db) @@ -140,7 +154,7 @@ class DbState(Callback): self.db = self.stack.pop() self.emit('database-changed', (self.db, )) - def make_database(self, id): + def make_database(self, plugin_id): """ Make a database, given a plugin id. """ @@ -148,14 +162,14 @@ class DbState(Callback): from .const import PLUGINS_DIR, USER_PLUGINS pmgr = BasePluginManager.get_instance() - pdata = pmgr.get_plugin(id) + pdata = pmgr.get_plugin(plugin_id) if not pdata: # This might happen if using gramps from outside, and # we haven't loaded plugins yet pmgr.reg_plugins(PLUGINS_DIR, self, None) pmgr.reg_plugins(USER_PLUGINS, self, None, load_on_reg=True) - pdata = pmgr.get_plugin(id) + pdata = pmgr.get_plugin(plugin_id) if pdata: if pdata.reset_system: @@ -225,7 +239,7 @@ class DbState(Callback): if user is None: user = User() (name, ext) = os.path.splitext(os.path.basename(filename)) - format = ext[1:].lower() + extension = ext[1:].lower() import_list = pmgr.get_reg_importers() if import_list == []: # This might happen if using gramps from outside, and @@ -234,12 +248,11 @@ class DbState(Callback): pmgr.reg_plugins(USER_PLUGINS, self, None, load_on_reg=True) import_list = pmgr.get_reg_importers() for pdata in import_list: - if format == pdata.extension: + if extension == pdata.extension: mod = pmgr.load_plugin(pdata) if not mod: for item in pmgr.get_fail_list(): name, error_tuple, pdata = item - # (filename, (exception-type, exception, traceback), pdata) etype, exception, traceback = error_tuple print("ERROR:", name, exception) return False @@ -261,7 +274,7 @@ class DbState(Callback): LOG.info("reset_modules!") # First, clear out old modules: for key in list(sys.modules.keys()): - del(sys.modules[key]) + del sys.modules[key] # Next, restore previous: for key in self._modules: sys.modules[key] = self._modules[key] diff --git a/gramps/gen/git_revision.py b/gramps/gen/git_revision.py index a8a4414c0..93cc10148 100644 --- a/gramps/gen/git_revision.py +++ b/gramps/gen/git_revision.py @@ -19,23 +19,30 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # +""" +Find the latest git revision. +""" + import subprocess def get_git_revision(path=""): + """ + Return the short commit hash of the latest commit. + """ stdout = "" command = "git log -1 --format=%h" try: - p = subprocess.Popen( - "{} \"{}\"".format(command, path), - shell=True, - stdout=subprocess.PIPE, stderr=subprocess.PIPE) - (stdout, stderr) = p.communicate() - except: + proc = subprocess.Popen( + "{} \"{}\"".format(command, path), + shell=True, + stdout=subprocess.PIPE, stderr=subprocess.PIPE) + (stdout, stderr) = proc.communicate() + except OSError: return "" # subprocess failed # subprocess worked if stdout and len(stdout) > 0: # has output try: - stdout = stdout.decode("utf-8", errors = 'replace') + stdout = stdout.decode("utf-8", errors='replace') except UnicodeDecodeError: pass return "-" + stdout if stdout else "" diff --git a/gramps/gen/recentfiles.py b/gramps/gen/recentfiles.py index fa10912d1..ddcde977b 100644 --- a/gramps/gen/recentfiles.py +++ b/gramps/gen/recentfiles.py @@ -28,15 +28,14 @@ #------------------------------------------------------------------------- import os import time -import io import logging -from xml.parsers.expat import ParserCreate +from xml.parsers.expat import ParserCreate, ExpatError try: import fcntl - use_lock = True -except: - use_lock = False + USE_LOCK = True +except ImportError: + USE_LOCK = False from gramps.gen.const import HOME_DIR, GRAMPS_LOCALE as glocale _ = glocale.translation.gettext @@ -46,7 +45,7 @@ _ = glocale.translation.gettext # Constants # #------------------------------------------------------------------------- -GRAMPS_FILENAME = os.path.join(HOME_DIR,"recent-files-gramps.xml") +GRAMPS_FILENAME = os.path.join(HOME_DIR, "recent-files-gramps.xml") MAX_GRAMPS_ITEMS = 10 #------------------------------------------------------------------------- @@ -59,27 +58,45 @@ class RecentItem(object): Interface to a single Gramps recent-items item """ - def __init__(self,p="", n="",t=0): + def __init__(self, p="", n="", t=0): self.path = p self.name = n self.time = t - def set_path(self,val): + def set_path(self, val): + """ + Set the file path. + """ self.path = val def get_path(self): + """ + Get the file path. + """ return self.path - def set_name(self,val): + def set_name(self, val): + """ + Set the file name. + """ self.name = val def get_name(self): + """ + Get the file name. + """ return self.name - def set_time(self,val): + def set_time(self, val): + """ + Set the file timestamp. + """ self.time = int(val) def get_time(self): + """ + Get the file timestamp. + """ return self.time def __eq__(self, other_item): @@ -100,10 +117,6 @@ class RecentItem(object): def __ge__(self, other_item): return self.time >= other_item.time -## Python 3, no __cmp__ -## def __cmp__(self, other_item): -## return cmp(self.time, other_item.time) - #------------------------------------------------------------------------- # # RecentFiles @@ -118,7 +131,10 @@ class RecentFiles(object): gramps_parser = RecentParser() self.gramps_recent_files = gramps_parser.get() - def add(self,item2add): + def add(self, item2add): + """ + Add a file to the recent files list. + """ # First we need to walk the existing items to see # if our item is already there for item in self.gramps_recent_files: @@ -129,11 +145,12 @@ class RecentFiles(object): return # At this point we walked the items and not found one, # so simply inserting a new item in the beginning - self.gramps_recent_files.insert(0,item2add) + self.gramps_recent_files.insert(0, item2add) - - - def rename_filename(self,filename, new_filename): + def rename_filename(self, filename, new_filename): + """ + Rename a file in the recent files list. + """ # First we need to walk the existing items to see # if our item is already there found = False @@ -145,7 +162,10 @@ class RecentFiles(object): if found: self.gramps_recent_files[index].set_name(new_filename) - def remove_filename(self,filename): + def remove_filename(self, filename): + """ + Remove a file from the recent files list. + """ # First we need to walk the existing items to see # if our item is already there found = False @@ -157,8 +177,10 @@ class RecentFiles(object): if found: self.gramps_recent_files.pop(index) - - def check_if_recent(self,filename): + def check_if_recent(self, filename): + """ + Check if a file is present in the recent files list. + """ # First we need to walk the existing items to see # if our item is already there found = False @@ -174,19 +196,21 @@ class RecentFiles(object): Attempt saving into XML. The trick is not to fail under any circumstances. """ + fname = os.path.expanduser(GRAMPS_FILENAME) try: - self.do_save() - except: - pass + self.do_save(fname) + except IOError as err: + logging.warning( + _("Unable to save list of recent DBs file {fname}: {error}" + ).format(fname=fname, error=err)) - def do_save(self): + def do_save(self, fname): """ Saves the current Gramps RecentFiles collection to the associated file. """ - with open(os.path.expanduser(GRAMPS_FILENAME), 'w', encoding='utf8') \ - as xml_file: - if use_lock: - fcntl.lockf(xml_file,fcntl.LOCK_EX) + with open(fname, 'w', encoding='utf8') as xml_file: + if USE_LOCK: + fcntl.lockf(xml_file, fcntl.LOCK_EX) xml_file.write("\n") xml_file.write('\n') index = 0 @@ -195,9 +219,12 @@ class RecentFiles(object): if index > MAX_GRAMPS_ITEMS: break xml_file.write(' \n') - xml_file.write(' \n' % item.get_path()) - xml_file.write(' \n' % item.get_name()) - xml_file.write(' %d\n' % item.get_time()) + xml_file.write(' \n' % + item.get_path()) + xml_file.write(' \n' % + item.get_name()) + xml_file.write(' %d\n' % + item.get_time()) xml_file.write(' \n') xml_file.write('\n') @@ -215,6 +242,8 @@ class RecentParser(object): def __init__(self): self.recent_files = [] + self.tlist = [] + self.item = None fname = os.path.expanduser(GRAMPS_FILENAME) if not os.path.exists(fname): @@ -222,37 +251,45 @@ class RecentParser(object): try: with open(fname, "rb") as xml_file: - if use_lock: - fcntl.lockf(xml_file,fcntl.LOCK_SH) + if USE_LOCK: + fcntl.lockf(xml_file, fcntl.LOCK_SH) - p = ParserCreate() - p.StartElementHandler = self.startElement - p.EndElementHandler = self.endElement - p.CharacterDataHandler = self.characters - p.ParseFile(xml_file) + parser = ParserCreate() + parser.StartElementHandler = self.start_element + parser.EndElementHandler = self.end_element + parser.CharacterDataHandler = self.characters + parser.ParseFile(xml_file) # all advisory locks on a file are released on close except IOError as err: logging.warning( - _("Unable to open list of recent DBs file {fname}: {error}" - ).format(fname=fname, error=err)) - except Exception as err: + _("Unable to open list of recent DBs file {fname}: {error}" + ).format(fname=fname, error=err)) + except ExpatError as err: logging.error( - _("Error parsing list of recent DBs from file {fname}: {error}.\n" - "This might indicate a damage to your files.\n" - "If you're sure there is no problem with other files, " - "delete it, and restart Gramps." - ).format(fname=fname, error=err)) + _("Error parsing list of recent DBs from file {fname}: " + "{error}.\nThis might indicate a damage to your files.\n" + "If you're sure there is no problem with other files, " + "delete it, and restart Gramps." + ).format(fname=fname, error=err)) def get(self): + """ + Return a list of recent files. + """ return self.recent_files - def startElement(self,tag,attrs): + def start_element(self, tag, attrs): + """ + Handler for XML start element. + """ self.tlist = [] if tag == "RecentItem": self.item = RecentItem() - def endElement(self,tag): - + def end_element(self, tag): + """ + Handler for XML end element. + """ text = ''.join(self.tlist) if tag == "RecentItem": @@ -266,6 +303,9 @@ class RecentParser(object): self.item.set_time(int(text)) def characters(self, data): + """ + Handler for XML character data. + """ self.tlist.append(data) #------------------------------------------------------------------------- @@ -275,9 +315,8 @@ class RecentParser(object): #------------------------------------------------------------------------- def recent_files(filename, name): """ - Add an entry to both GNOME and Gramps recent-items storages. + Add an entry to the Gramps recent items list. """ - the_time = int(time.time()) gramps_rf = RecentFiles() gramps_item = RecentItem( @@ -288,16 +327,24 @@ def recent_files(filename, name): gramps_rf.save() def remove_filename(filename): + """ + Remove an entry from the Gramps recent items list. + """ gramps_rf = RecentFiles() gramps_rf.remove_filename(filename) gramps_rf.save() def rename_filename(filename, new_filename): + """ + Rename an entry in the Gramps recent items list. + """ gramps_rf = RecentFiles() gramps_rf.rename_filename(filename, new_filename) gramps_rf.save() def check_if_recent(filename): + """ + Check if an entry is present in Gramps recent items list. + """ gramps_rf = RecentFiles() return gramps_rf.check_if_recent(filename) - diff --git a/gramps/gen/relationship.py b/gramps/gen/relationship.py index 6e2ce8c11..50ae852aa 100644 --- a/gramps/gen/relationship.py +++ b/gramps/gen/relationship.py @@ -20,56 +20,58 @@ """ Classes for relationships. """ + #------------------------------------------------------------------------- # -# python modules +# Python modules # #------------------------------------------------------------------------- -import os import logging -LOG = logging.getLogger("gen.relationship") -LOG.addHandler(logging.StreamHandler()) #------------------------------------------------------------------------- # # Gramps modules # #------------------------------------------------------------------------- - from .lib import Person, ChildRefType, EventType, FamilyRelType +from .plug import PluginRegister, BasePluginManager +from .const import GRAMPS_LOCALE as glocale +_ = glocale.translation.sgettext + MALE = Person.MALE FEMALE = Person.FEMALE UNKNOWN = Person.UNKNOWN -from .const import GRAMPS_LOCALE as glocale -_ = glocale.translation.sgettext -from .plug import PluginRegister, BasePluginManager + +LOG = logging.getLogger("gen.relationship") +LOG.addHandler(logging.StreamHandler()) + #------------------------------------------------------------------------- # # # #------------------------------------------------------------------------- -_level_name = [ "", "first", "second", "third", "fourth", "fifth", "sixth", - "seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth", - "thirteenth", "fourteenth", "fifteenth", "sixteenth", - "seventeenth", "eighteenth", "nineteenth", "twentieth" ] +_LEVEL_NAME = ["", "first", "second", "third", "fourth", "fifth", "sixth", + "seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth", + "thirteenth", "fourteenth", "fifteenth", "sixteenth", + "seventeenth", "eighteenth", "nineteenth", "twentieth"] -_removed_level = [ "", " once removed", " twice removed", - " three times removed", - " four times removed", " five times removed", - " six times removed", - " seven times removed", " eight times removed", - " nine times removed", - " ten times removed", " eleven times removed", - " twelve times removed", - " thirteen times removed", " fourteen times removed", - " fifteen times removed", - " sixteen times removed", " seventeen times removed", - " eighteen times removed", - " nineteen times removed", " twenty times removed" ] +_REMOVED_LEVEL = ["", " once removed", " twice removed", + " three times removed", + " four times removed", " five times removed", + " six times removed", + " seven times removed", " eight times removed", + " nine times removed", + " ten times removed", " eleven times removed", + " twelve times removed", + " thirteen times removed", " fourteen times removed", + " fifteen times removed", + " sixteen times removed", " seventeen times removed", + " eighteen times removed", + " nineteen times removed", " twenty times removed"] -_parents_level = [ "", "parents", "grandparents", "great grandparents", - "second great grandparents", +_PARENTS_LEVEL = ["", "parents", "grandparents", "great grandparents", + "second great grandparents", "third great grandparents", "fourth great grandparents", "fifth great grandparents", @@ -87,263 +89,286 @@ _parents_level = [ "", "parents", "grandparents", "great grandparents", "seventeenth great grandparents", "eighteenth great grandparents", "nineteenth great grandparents", - "twentieth great grandparents", ] + "twentieth great grandparents"] -_father_level = [ "", "%(step)sfather%(inlaw)s", "%(step)sgrandfather%(inlaw)s", +_FATHER_LEVEL = ["", "%(step)sfather%(inlaw)s", "%(step)sgrandfather%(inlaw)s", "great %(step)sgrandfather%(inlaw)s", - "second great %(step)sgrandfather%(inlaw)s", - "third great %(step)sgrandfather%(inlaw)s", - "fourth great %(step)sgrandfather%(inlaw)s", - "fifth great %(step)sgrandfather%(inlaw)s", - "sixth great %(step)sgrandfather%(inlaw)s", - "seventh great %(step)sgrandfather%(inlaw)s", - "eighth great %(step)sgrandfather%(inlaw)s", - "ninth great %(step)sgrandfather%(inlaw)s", - "tenth great %(step)sgrandfather%(inlaw)s", - "eleventh great %(step)sgrandfather%(inlaw)s", - "twelfth great %(step)sgrandfather%(inlaw)s", - "thirteenth great %(step)sgrandfather%(inlaw)s", - "fourteenth great %(step)sgrandfather%(inlaw)s", - "fifteenth great %(step)sgrandfather%(inlaw)s", - "sixteenth great %(step)sgrandfather%(inlaw)s", - "seventeenth great %(step)sgrandfather%(inlaw)s", - "eighteenth great %(step)sgrandfather%(inlaw)s", - "nineteenth great %(step)sgrandfather%(inlaw)s", - "twentieth great %(step)sgrandfather%(inlaw)s", ] + "second great %(step)sgrandfather%(inlaw)s", + "third great %(step)sgrandfather%(inlaw)s", + "fourth great %(step)sgrandfather%(inlaw)s", + "fifth great %(step)sgrandfather%(inlaw)s", + "sixth great %(step)sgrandfather%(inlaw)s", + "seventh great %(step)sgrandfather%(inlaw)s", + "eighth great %(step)sgrandfather%(inlaw)s", + "ninth great %(step)sgrandfather%(inlaw)s", + "tenth great %(step)sgrandfather%(inlaw)s", + "eleventh great %(step)sgrandfather%(inlaw)s", + "twelfth great %(step)sgrandfather%(inlaw)s", + "thirteenth great %(step)sgrandfather%(inlaw)s", + "fourteenth great %(step)sgrandfather%(inlaw)s", + "fifteenth great %(step)sgrandfather%(inlaw)s", + "sixteenth great %(step)sgrandfather%(inlaw)s", + "seventeenth great %(step)sgrandfather%(inlaw)s", + "eighteenth great %(step)sgrandfather%(inlaw)s", + "nineteenth great %(step)sgrandfather%(inlaw)s", + "twentieth great %(step)sgrandfather%(inlaw)s"] -_mother_level = [ "", "%(step)smother%(inlaw)s", - "%(step)sgrandmother%(inlaw)s", - "great %(step)sgrandmother%(inlaw)s", - "second great %(step)sgrandmother%(inlaw)s", - "third great %(step)sgrandmother%(inlaw)s", - "fourth great %(step)sgrandmother%(inlaw)s", - "fifth great %(step)sgrandmother%(inlaw)s", - "sixth great %(step)sgrandmother%(inlaw)s", - "seventh great %(step)sgrandmother%(inlaw)s", - "eighth great %(step)sgrandmother%(inlaw)s", - "ninth great %(step)sgrandmother%(inlaw)s", - "tenth great %(step)sgrandmother%(inlaw)s", - "eleventh great %(step)sgrandmother%(inlaw)s", - "twelfth great %(step)sgrandmother%(inlaw)s", - "thirteenth great %(step)sgrandmother%(inlaw)s", - "fourteenth great %(step)sgrandmother%(inlaw)s", - "fifteenth great %(step)sgrandmother%(inlaw)s", - "sixteenth great %(step)sgrandmother%(inlaw)s", - "seventeenth great %(step)sgrandmother%(inlaw)s", - "eighteenth great %(step)sgrandmother%(inlaw)s", - "nineteenth great %(step)sgrandmother%(inlaw)s", - "twentieth great %(step)sgrandmother%(inlaw)s", ] +_MOTHER_LEVEL = ["", "%(step)smother%(inlaw)s", + "%(step)sgrandmother%(inlaw)s", + "great %(step)sgrandmother%(inlaw)s", + "second great %(step)sgrandmother%(inlaw)s", + "third great %(step)sgrandmother%(inlaw)s", + "fourth great %(step)sgrandmother%(inlaw)s", + "fifth great %(step)sgrandmother%(inlaw)s", + "sixth great %(step)sgrandmother%(inlaw)s", + "seventh great %(step)sgrandmother%(inlaw)s", + "eighth great %(step)sgrandmother%(inlaw)s", + "ninth great %(step)sgrandmother%(inlaw)s", + "tenth great %(step)sgrandmother%(inlaw)s", + "eleventh great %(step)sgrandmother%(inlaw)s", + "twelfth great %(step)sgrandmother%(inlaw)s", + "thirteenth great %(step)sgrandmother%(inlaw)s", + "fourteenth great %(step)sgrandmother%(inlaw)s", + "fifteenth great %(step)sgrandmother%(inlaw)s", + "sixteenth great %(step)sgrandmother%(inlaw)s", + "seventeenth great %(step)sgrandmother%(inlaw)s", + "eighteenth great %(step)sgrandmother%(inlaw)s", + "nineteenth great %(step)sgrandmother%(inlaw)s", + "twentieth great %(step)sgrandmother%(inlaw)s"] -_son_level = [ "", "%(step)sson%(inlaw)s", "%(step)sgrandson%(inlaw)s", - "great %(step)sgrandson%(inlaw)s", - "second great %(step)sgrandson%(inlaw)s", - "third great %(step)sgrandson%(inlaw)s", - "fourth great %(step)sgrandson%(inlaw)s", - "fifth great %(step)sgrandson%(inlaw)s", - "sixth great %(step)sgrandson%(inlaw)s", - "seventh great %(step)sgrandson%(inlaw)s", - "eighth great %(step)sgrandson%(inlaw)s", - "ninth great %(step)sgrandson%(inlaw)s", - "tenth great %(step)sgrandson%(inlaw)s", - "eleventh great %(step)sgrandson%(inlaw)s", - "twelfth great %(step)sgrandson%(inlaw)s", - "thirteenth great %(step)sgrandson%(inlaw)s", - "fourteenth great %(step)sgrandson%(inlaw)s", - "fifteenth great %(step)sgrandson%(inlaw)s", - "sixteenth great %(step)sgrandson%(inlaw)s", - "seventeenth great %(step)sgrandson%(inlaw)s", - "eighteenth great %(step)sgrandson%(inlaw)s", - "nineteenth great %(step)sgrandson%(inlaw)s", - "twentieth great %(step)sgrandson%(inlaw)s", ] +_SON_LEVEL = ["", "%(step)sson%(inlaw)s", "%(step)sgrandson%(inlaw)s", + "great %(step)sgrandson%(inlaw)s", + "second great %(step)sgrandson%(inlaw)s", + "third great %(step)sgrandson%(inlaw)s", + "fourth great %(step)sgrandson%(inlaw)s", + "fifth great %(step)sgrandson%(inlaw)s", + "sixth great %(step)sgrandson%(inlaw)s", + "seventh great %(step)sgrandson%(inlaw)s", + "eighth great %(step)sgrandson%(inlaw)s", + "ninth great %(step)sgrandson%(inlaw)s", + "tenth great %(step)sgrandson%(inlaw)s", + "eleventh great %(step)sgrandson%(inlaw)s", + "twelfth great %(step)sgrandson%(inlaw)s", + "thirteenth great %(step)sgrandson%(inlaw)s", + "fourteenth great %(step)sgrandson%(inlaw)s", + "fifteenth great %(step)sgrandson%(inlaw)s", + "sixteenth great %(step)sgrandson%(inlaw)s", + "seventeenth great %(step)sgrandson%(inlaw)s", + "eighteenth great %(step)sgrandson%(inlaw)s", + "nineteenth great %(step)sgrandson%(inlaw)s", + "twentieth great %(step)sgrandson%(inlaw)s"] -_daughter_level = [ "", "%(step)sdaughter%(inlaw)s", "%(step)sgranddaughter%(inlaw)s", - "great %(step)sgranddaughter%(inlaw)s", - "second great %(step)sgranddaughter%(inlaw)s", - "third great %(step)sgranddaughter%(inlaw)s", - "fourth great %(step)sgranddaughter%(inlaw)s", - "fifth great %(step)sgranddaughter%(inlaw)s", - "sixth great %(step)sgranddaughter%(inlaw)s", - "seventh great %(step)sgranddaughter%(inlaw)s", - "eighth great %(step)sgranddaughter%(inlaw)s", - "ninth great %(step)sgranddaughter%(inlaw)s", - "tenth great %(step)sgranddaughter%(inlaw)s", - "eleventh great %(step)sgranddaughter%(inlaw)s", - "twelfth great %(step)sgranddaughter%(inlaw)s", - "thirteenth great %(step)sgranddaughter%(inlaw)s", - "fourteenth great %(step)sgranddaughter%(inlaw)s", - "fifteenth great %(step)sgranddaughter%(inlaw)s", - "sixteenth great %(step)sgranddaughter%(inlaw)s", - "seventeenth great %(step)sgranddaughter%(inlaw)s", - "eighteenth great %(step)sgranddaughter%(inlaw)s", - "nineteenth great %(step)sgranddaughter%(inlaw)s", - "twentieth great %(step)sgranddaughter%(inlaw)s", ] +_DAUGHTER_LEVEL = ["", "%(step)sdaughter%(inlaw)s", + "%(step)sgranddaughter%(inlaw)s", + "great %(step)sgranddaughter%(inlaw)s", + "second great %(step)sgranddaughter%(inlaw)s", + "third great %(step)sgranddaughter%(inlaw)s", + "fourth great %(step)sgranddaughter%(inlaw)s", + "fifth great %(step)sgranddaughter%(inlaw)s", + "sixth great %(step)sgranddaughter%(inlaw)s", + "seventh great %(step)sgranddaughter%(inlaw)s", + "eighth great %(step)sgranddaughter%(inlaw)s", + "ninth great %(step)sgranddaughter%(inlaw)s", + "tenth great %(step)sgranddaughter%(inlaw)s", + "eleventh great %(step)sgranddaughter%(inlaw)s", + "twelfth great %(step)sgranddaughter%(inlaw)s", + "thirteenth great %(step)sgranddaughter%(inlaw)s", + "fourteenth great %(step)sgranddaughter%(inlaw)s", + "fifteenth great %(step)sgranddaughter%(inlaw)s", + "sixteenth great %(step)sgranddaughter%(inlaw)s", + "seventeenth great %(step)sgranddaughter%(inlaw)s", + "eighteenth great %(step)sgranddaughter%(inlaw)s", + "nineteenth great %(step)sgranddaughter%(inlaw)s", + "twentieth great %(step)sgranddaughter%(inlaw)s"] -_sister_level = [ "", "%(step)ssister%(inlaw)s", "%(step)saunt%(inlaw)s", - "%(step)sgrandaunt%(inlaw)s", - "great %(step)sgrandaunt%(inlaw)s", - "second great %(step)sgrandaunt%(inlaw)s", - "third great %(step)sgrandaunt%(inlaw)s", - "fourth great %(step)sgrandaunt%(inlaw)s", - "fifth great %(step)sgrandaunt%(inlaw)s", - "sixth great %(step)sgrandaunt%(inlaw)s", - "seventh great %(step)sgrandaunt%(inlaw)s", - "eighth great %(step)sgrandaunt%(inlaw)s", - "ninth great %(step)sgrandaunt%(inlaw)s", - "tenth great %(step)sgrandaunt%(inlaw)s", - "eleventh great %(step)sgrandaunt%(inlaw)s", - "twelfth great %(step)sgrandaunt%(inlaw)s", - "thirteenth great %(step)sgrandaunt%(inlaw)s", - "fourteenth great %(step)sgrandaunt%(inlaw)s", - "fifteenth great %(step)sgrandaunt%(inlaw)s", - "sixteenth great %(step)sgrandaunt%(inlaw)s", - "seventeenth great %(step)sgrandaunt%(inlaw)s", - "eighteenth great %(step)sgrandaunt%(inlaw)s", - "nineteenth great %(step)sgrandaunt%(inlaw)s", - "twentieth great %(step)sgrandaunt%(inlaw)s", ] +_SISTER_LEVEL = ["", "%(step)ssister%(inlaw)s", "%(step)saunt%(inlaw)s", + "%(step)sgrandaunt%(inlaw)s", + "great %(step)sgrandaunt%(inlaw)s", + "second great %(step)sgrandaunt%(inlaw)s", + "third great %(step)sgrandaunt%(inlaw)s", + "fourth great %(step)sgrandaunt%(inlaw)s", + "fifth great %(step)sgrandaunt%(inlaw)s", + "sixth great %(step)sgrandaunt%(inlaw)s", + "seventh great %(step)sgrandaunt%(inlaw)s", + "eighth great %(step)sgrandaunt%(inlaw)s", + "ninth great %(step)sgrandaunt%(inlaw)s", + "tenth great %(step)sgrandaunt%(inlaw)s", + "eleventh great %(step)sgrandaunt%(inlaw)s", + "twelfth great %(step)sgrandaunt%(inlaw)s", + "thirteenth great %(step)sgrandaunt%(inlaw)s", + "fourteenth great %(step)sgrandaunt%(inlaw)s", + "fifteenth great %(step)sgrandaunt%(inlaw)s", + "sixteenth great %(step)sgrandaunt%(inlaw)s", + "seventeenth great %(step)sgrandaunt%(inlaw)s", + "eighteenth great %(step)sgrandaunt%(inlaw)s", + "nineteenth great %(step)sgrandaunt%(inlaw)s", + "twentieth great %(step)sgrandaunt%(inlaw)s"] -_brother_level = [ "", "%(step)sbrother%(inlaw)s", "%(step)suncle%(inlaw)s", - "%(step)sgranduncle%(inlaw)s", - "great %(step)sgranduncle%(inlaw)s", - "second great %(step)sgranduncle%(inlaw)s", - "third great %(step)sgranduncle%(inlaw)s", - "fourth great %(step)sgranduncle%(inlaw)s", - "fifth great %(step)sgranduncle%(inlaw)s", - "sixth great %(step)sgranduncle%(inlaw)s", - "seventh great %(step)sgranduncle%(inlaw)s", - "eighth great %(step)sgranduncle%(inlaw)s", - "ninth great %(step)sgranduncle%(inlaw)s", - "tenth great %(step)sgranduncle%(inlaw)s", - "eleventh great %(step)sgranduncle%(inlaw)s", - "twelfth great %(step)sgranduncle%(inlaw)s", - "thirteenth great %(step)sgranduncle%(inlaw)s", - "fourteenth great %(step)sgranduncle%(inlaw)s", - "fifteenth great %(step)sgranduncle%(inlaw)s", - "sixteenth great %(step)sgranduncle%(inlaw)s", - "seventeenth great %(step)sgranduncle%(inlaw)s", - "eighteenth great %(step)sgranduncle%(inlaw)s", - "nineteenth great %(step)sgranduncle%(inlaw)s", - "twentieth great %(step)sgranduncle%(inlaw)s", ] +_BROTHER_LEVEL = ["", "%(step)sbrother%(inlaw)s", "%(step)suncle%(inlaw)s", + "%(step)sgranduncle%(inlaw)s", + "great %(step)sgranduncle%(inlaw)s", + "second great %(step)sgranduncle%(inlaw)s", + "third great %(step)sgranduncle%(inlaw)s", + "fourth great %(step)sgranduncle%(inlaw)s", + "fifth great %(step)sgranduncle%(inlaw)s", + "sixth great %(step)sgranduncle%(inlaw)s", + "seventh great %(step)sgranduncle%(inlaw)s", + "eighth great %(step)sgranduncle%(inlaw)s", + "ninth great %(step)sgranduncle%(inlaw)s", + "tenth great %(step)sgranduncle%(inlaw)s", + "eleventh great %(step)sgranduncle%(inlaw)s", + "twelfth great %(step)sgranduncle%(inlaw)s", + "thirteenth great %(step)sgranduncle%(inlaw)s", + "fourteenth great %(step)sgranduncle%(inlaw)s", + "fifteenth great %(step)sgranduncle%(inlaw)s", + "sixteenth great %(step)sgranduncle%(inlaw)s", + "seventeenth great %(step)sgranduncle%(inlaw)s", + "eighteenth great %(step)sgranduncle%(inlaw)s", + "nineteenth great %(step)sgranduncle%(inlaw)s", + "twentieth great %(step)sgranduncle%(inlaw)s"] -_nephew_level = [ "", "%(step)snephew%(inlaw)s", "%(step)sgrandnephew%(inlaw)s", - "great %(step)sgrandnephew%(inlaw)s", - "second great %(step)sgrandnephew%(inlaw)s", - "third great %(step)sgrandnephew%(inlaw)s", - "fourth great %(step)sgrandnephew%(inlaw)s", - "fifth great %(step)sgrandnephew%(inlaw)s", - "sixth great %(step)sgrandnephew%(inlaw)s", - "seventh great %(step)sgrandnephew%(inlaw)s", - "eighth great %(step)sgrandnephew%(inlaw)s", - "ninth great %(step)sgrandnephew%(inlaw)s", - "tenth great %(step)sgrandnephew%(inlaw)s", - "eleventh great %(step)sgrandnephew%(inlaw)s", - "twelfth great %(step)sgrandnephew%(inlaw)s", - "thirteenth great %(step)sgrandnephew%(inlaw)s", - "fourteenth great %(step)sgrandnephew%(inlaw)s", - "fifteenth great %(step)sgrandnephew%(inlaw)s", - "sixteenth great %(step)sgrandnephew%(inlaw)s", - "seventeenth great %(step)sgrandnephew%(inlaw)s", - "eighteenth great %(step)sgrandnephew%(inlaw)s", - "nineteenth great %(step)sgrandnephew%(inlaw)s", - "twentieth great %(step)sgrandnephew%(inlaw)s", ] +_NEPHEW_LEVEL = ["", "%(step)snephew%(inlaw)s", "%(step)sgrandnephew%(inlaw)s", + "great %(step)sgrandnephew%(inlaw)s", + "second great %(step)sgrandnephew%(inlaw)s", + "third great %(step)sgrandnephew%(inlaw)s", + "fourth great %(step)sgrandnephew%(inlaw)s", + "fifth great %(step)sgrandnephew%(inlaw)s", + "sixth great %(step)sgrandnephew%(inlaw)s", + "seventh great %(step)sgrandnephew%(inlaw)s", + "eighth great %(step)sgrandnephew%(inlaw)s", + "ninth great %(step)sgrandnephew%(inlaw)s", + "tenth great %(step)sgrandnephew%(inlaw)s", + "eleventh great %(step)sgrandnephew%(inlaw)s", + "twelfth great %(step)sgrandnephew%(inlaw)s", + "thirteenth great %(step)sgrandnephew%(inlaw)s", + "fourteenth great %(step)sgrandnephew%(inlaw)s", + "fifteenth great %(step)sgrandnephew%(inlaw)s", + "sixteenth great %(step)sgrandnephew%(inlaw)s", + "seventeenth great %(step)sgrandnephew%(inlaw)s", + "eighteenth great %(step)sgrandnephew%(inlaw)s", + "nineteenth great %(step)sgrandnephew%(inlaw)s", + "twentieth great %(step)sgrandnephew%(inlaw)s"] -_niece_level = [ "", "%(step)sniece%(inlaw)s", "%(step)sgrandniece%(inlaw)s", - "great %(step)sgrandniece%(inlaw)s", - "second great %(step)sgrandniece%(inlaw)s", - "third great %(step)sgrandniece%(inlaw)s", - "fourth great %(step)sgrandniece%(inlaw)s", - "fifth great %(step)sgrandniece%(inlaw)s", - "sixth great %(step)sgrandniece%(inlaw)s", - "seventh great %(step)sgrandniece%(inlaw)s", - "eighth great %(step)sgrandniece%(inlaw)s", - "ninth great %(step)sgrandniece%(inlaw)s", - "tenth great %(step)sgrandniece%(inlaw)s", - "eleventh great %(step)sgrandniece%(inlaw)s", - "twelfth great %(step)sgrandniece%(inlaw)s", - "thirteenth great %(step)sgrandniece%(inlaw)s", - "fourteenth great %(step)sgrandniece%(inlaw)s", - "fifteenth great %(step)sgrandniece%(inlaw)s", - "sixteenth great %(step)sgrandniece%(inlaw)s", - "seventeenth great %(step)sgrandniece%(inlaw)s", - "eighteenth great %(step)sgrandniece%(inlaw)s", - "nineteenth great %(step)sgrandniece%(inlaw)s", - "twentieth great %(step)sgrandniece%(inlaw)s", ] +_NIECE_LEVEL = ["", "%(step)sniece%(inlaw)s", "%(step)sgrandniece%(inlaw)s", + "great %(step)sgrandniece%(inlaw)s", + "second great %(step)sgrandniece%(inlaw)s", + "third great %(step)sgrandniece%(inlaw)s", + "fourth great %(step)sgrandniece%(inlaw)s", + "fifth great %(step)sgrandniece%(inlaw)s", + "sixth great %(step)sgrandniece%(inlaw)s", + "seventh great %(step)sgrandniece%(inlaw)s", + "eighth great %(step)sgrandniece%(inlaw)s", + "ninth great %(step)sgrandniece%(inlaw)s", + "tenth great %(step)sgrandniece%(inlaw)s", + "eleventh great %(step)sgrandniece%(inlaw)s", + "twelfth great %(step)sgrandniece%(inlaw)s", + "thirteenth great %(step)sgrandniece%(inlaw)s", + "fourteenth great %(step)sgrandniece%(inlaw)s", + "fifteenth great %(step)sgrandniece%(inlaw)s", + "sixteenth great %(step)sgrandniece%(inlaw)s", + "seventeenth great %(step)sgrandniece%(inlaw)s", + "eighteenth great %(step)sgrandniece%(inlaw)s", + "nineteenth great %(step)sgrandniece%(inlaw)s", + "twentieth great %(step)sgrandniece%(inlaw)s"] -_children_level = [ "", - "children", "grandchildren", - "great grandchildren", "second great grandchildren", - "third great grandchildren", "fourth great grandchildren", - "fifth great grandchildren", "sixth great grandchildren", - "seventh great grandchildren", "eighth great grandchildren", - "ninth great grandchildren", "tenth great grandchildren", - "eleventh great grandchildren", "twelfth great grandchildren", - "thirteenth great grandchildren", "fourteenth great grandchildren", - "fifteenth great grandchildren", "sixteenth great grandchildren", - "seventeenth great grandchildren", "eighteenth great grandchildren", - "nineteenth great grandchildren", "twentieth great grandchildren", ] +_CHILDREN_LEVEL = ["", + "children", + "grandchildren", + "great grandchildren", + "second great grandchildren", + "third great grandchildren", + "fourth great grandchildren", + "fifth great grandchildren", + "sixth great grandchildren", + "seventh great grandchildren", + "eighth great grandchildren", + "ninth great grandchildren", + "tenth great grandchildren", + "eleventh great grandchildren", + "twelfth great grandchildren", + "thirteenth great grandchildren", + "fourteenth great grandchildren", + "fifteenth great grandchildren", + "sixteenth great grandchildren", + "seventeenth great grandchildren", + "eighteenth great grandchildren", + "nineteenth great grandchildren", + "twentieth great grandchildren"] -_siblings_level = [ "", - "siblings", "uncles/aunts", - "granduncles/aunts", "great granduncles/aunts", - "second great granduncles/aunts", "third great granduncles/aunts", - "fourth great granduncles/aunts", "fifth great granduncles/aunts", - "sixth great granduncles/aunts", "seventh great granduncles/aunts", - "eighth great granduncles/aunts", "ninth great granduncles/aunts", - "tenth great granduncles/aunts", "eleventh great granduncles/aunts", - "twelfth great granduncles/aunts", "thirteenth great granduncles/aunts", - "fourteenth great granduncles/aunts", "fifteenth great granduncles/aunts", - "sixteenth great granduncles/aunts", "seventeenth great granduncles/aunts", - "eighteenth great granduncles/aunts", "nineteenth great granduncles/aunts", - "twentieth great granduncles/aunts", ] +_SIBLINGS_LEVEL = ["", + "siblings", + "uncles/aunts", + "granduncles/aunts", + "great granduncles/aunts", + "second great granduncles/aunts", + "third great granduncles/aunts", + "fourth great granduncles/aunts", + "fifth great granduncles/aunts", + "sixth great granduncles/aunts", + "seventh great granduncles/aunts", + "eighth great granduncles/aunts", + "ninth great granduncles/aunts", + "tenth great granduncles/aunts", + "eleventh great granduncles/aunts", + "twelfth great granduncles/aunts", + "thirteenth great granduncles/aunts", + "fourteenth great granduncles/aunts", + "fifteenth great granduncles/aunts", + "sixteenth great granduncles/aunts", + "seventeenth great granduncles/aunts", + "eighteenth great granduncles/aunts", + "nineteenth great granduncles/aunts", + "twentieth great granduncles/aunts"] -_sibling_level = [ "", - "%(step)ssibling%(inlaw)s", - "%(step)suncle/aunt%(inlaw)s", - "%(step)sgranduncle/aunt%(inlaw)s", - "great %(step)sgranduncle/aunt%(inlaw)s", - "second great %(step)sgranduncle/aunt%(inlaw)s", - "third great %(step)sgranduncle/aunt%(inlaw)s", - "fourth great %(step)sgranduncle/aunt%(inlaw)s", - "fifth great %(step)sgranduncle/aunt%(inlaw)s", - "sixth great %(step)sgranduncle/aunt%(inlaw)s", - "seventh great %(step)sgranduncle/aunt%(inlaw)s", - "eighth great %(step)sgranduncle/aunt%(inlaw)s", - "ninth great %(step)sgranduncle/aunt%(inlaw)s", - "tenth great %(step)sgranduncle/aunt%(inlaw)s", - "eleventh great %(step)sgranduncle/aunt%(inlaw)s", - "twelfth great %(step)sgranduncle/aunt%(inlaw)s", - "thirteenth great %(step)sgranduncle/aunt%(inlaw)s", - "fourteenth great %(step)sgranduncle/aunt%(inlaw)s", - "fifteenth great %(step)sgranduncle/aunt%(inlaw)s", - "sixteenth great %(step)sgranduncle/aunt%(inlaw)s", - "seventeenth great %(step)sgranduncle/aunt%(inlaw)s", - "eighteenth great %(step)sgranduncle/aunt%(inlaw)s", - "nineteenth great %(step)sgranduncle/aunt%(inlaw)s", - "twentieth great %(step)sgranduncle/aunt%(inlaw)s", ] +_SIBLING_LEVEL = ["", + "%(step)ssibling%(inlaw)s", + "%(step)suncle/aunt%(inlaw)s", + "%(step)sgranduncle/aunt%(inlaw)s", + "great %(step)sgranduncle/aunt%(inlaw)s", + "second great %(step)sgranduncle/aunt%(inlaw)s", + "third great %(step)sgranduncle/aunt%(inlaw)s", + "fourth great %(step)sgranduncle/aunt%(inlaw)s", + "fifth great %(step)sgranduncle/aunt%(inlaw)s", + "sixth great %(step)sgranduncle/aunt%(inlaw)s", + "seventh great %(step)sgranduncle/aunt%(inlaw)s", + "eighth great %(step)sgranduncle/aunt%(inlaw)s", + "ninth great %(step)sgranduncle/aunt%(inlaw)s", + "tenth great %(step)sgranduncle/aunt%(inlaw)s", + "eleventh great %(step)sgranduncle/aunt%(inlaw)s", + "twelfth great %(step)sgranduncle/aunt%(inlaw)s", + "thirteenth great %(step)sgranduncle/aunt%(inlaw)s", + "fourteenth great %(step)sgranduncle/aunt%(inlaw)s", + "fifteenth great %(step)sgranduncle/aunt%(inlaw)s", + "sixteenth great %(step)sgranduncle/aunt%(inlaw)s", + "seventeenth great %(step)sgranduncle/aunt%(inlaw)s", + "eighteenth great %(step)sgranduncle/aunt%(inlaw)s", + "nineteenth great %(step)sgranduncle/aunt%(inlaw)s", + "twentieth great %(step)sgranduncle/aunt%(inlaw)s"] -_nephews_nieces_level = [ "", - "siblings", - "nephews/nieces", - "grandnephews/nieces", - "great grandnephews/nieces", - "second great grandnephews/nieces", - "third great grandnephews/nieces", - "fourth great grandnephews/nieces", - "fifth great grandnephews/nieces", - "sixth great grandnephews/nieces", - "seventh great grandnephews/nieces", - "eighth great grandnephews/nieces", - "ninth great grandnephews/nieces", - "tenth great grandnephews/nieces", - "eleventh great grandnephews/nieces", - "twelfth great grandnephews/nieces", - "thirteenth great grandnephews/nieces", - "fourteenth great grandnephews/nieces", - "fifteenth great grandnephews/nieces", - "sixteenth great grandnephews/nieces", - "seventeenth great grandnephews/nieces", - "eighteenth great grandnephews/nieces", - "nineteenth great grandnephews/nieces", - "twentieth great grandnephews/nieces", ] +_NEPHEWS_NIECES_LEVEL = ["", + "siblings", + "nephews/nieces", + "grandnephews/nieces", + "great grandnephews/nieces", + "second great grandnephews/nieces", + "third great grandnephews/nieces", + "fourth great grandnephews/nieces", + "fifth great grandnephews/nieces", + "sixth great grandnephews/nieces", + "seventh great grandnephews/nieces", + "eighth great grandnephews/nieces", + "ninth great grandnephews/nieces", + "tenth great grandnephews/nieces", + "eleventh great grandnephews/nieces", + "twelfth great grandnephews/nieces", + "thirteenth great grandnephews/nieces", + "fourteenth great grandnephews/nieces", + "fifteenth great grandnephews/nieces", + "sixteenth great grandnephews/nieces", + "seventeenth great grandnephews/nieces", + "eighteenth great grandnephews/nieces", + "nineteenth great grandnephews/nieces", + "twentieth great grandnephews/nieces"] #------------------------------------------------------------------------- @@ -356,24 +381,24 @@ class RelationshipCalculator(object): The relationship calculator helps to determine the relationship between two people. """ - REL_MOTHER = 'm' # going up to mother - REL_FATHER = 'f' # going up to father - REL_MOTHER_NOTBIRTH = 'M' # going up to mother, not birth relation - REL_FATHER_NOTBIRTH = 'F' # going up to father, not birth relation - REL_SIBLING = 's' # going sideways to sibling (no parents) - REL_FAM_BIRTH = 'a' # going up to family (mother and father) - REL_FAM_NONBIRTH = 'A' # going up to family, not birth relation - REL_FAM_BIRTH_MOTH_ONLY = 'b' # going up to fam, only birth rel to mother - REL_FAM_BIRTH_FATH_ONLY = 'c' # going up to fam, only birth rel to father + REL_MOTHER = 'm' # going up to mother + REL_FATHER = 'f' # going up to father + REL_MOTHER_NOTBIRTH = 'M' # going up to mother, not birth relation + REL_FATHER_NOTBIRTH = 'F' # going up to father, not birth relation + REL_SIBLING = 's' # going sideways to sibling (no parents) + REL_FAM_BIRTH = 'a' # going up to family (mother and father) + REL_FAM_NONBIRTH = 'A' # going up to family, not birth relation + REL_FAM_BIRTH_MOTH_ONLY = 'b' # going up to fam, only birth rel to mother + REL_FAM_BIRTH_FATH_ONLY = 'c' # going up to fam, only birth rel to father - REL_FAM_INLAW_PREFIX = 'L' # going to the partner. + REL_FAM_INLAW_PREFIX = 'L' # going to the partner. #sibling types - NORM_SIB = 0 # same birth parents - HALF_SIB_MOTHER = 1 # same mother, father known to be different - HALF_SIB_FATHER = 2 # same father, mother known to be different - STEP_SIB = 3 # birth parents known to be different - UNKNOWN_SIB = 4 # insufficient data to draw conclusion + NORM_SIB = 0 # same birth parents + HALF_SIB_MOTHER = 1 # same mother, father known to be different + HALF_SIB_FATHER = 2 # same father, mother known to be different + STEP_SIB = 3 # birth parents known to be different + UNKNOWN_SIB = 4 # insufficient data to draw conclusion #sibling strings STEP = 'step' @@ -382,12 +407,12 @@ class RelationshipCalculator(object): INLAW = '-in-law' #partner types - PARTNER_MARRIED = 1 - PARTNER_UNMARRIED = 2 - PARTNER_CIVIL_UNION = 3 - PARTNER_UNKNOWN_REL = 4 - PARTNER_EX_MARRIED = 5 - PARTNER_EX_UNMARRIED = 6 + PARTNER_MARRIED = 1 + PARTNER_UNMARRIED = 2 + PARTNER_CIVIL_UNION = 3 + PARTNER_UNKNOWN_REL = 4 + PARTNER_EX_MARRIED = 5 + PARTNER_EX_UNMARRIED = 6 PARTNER_EX_CIVIL_UNION = 7 PARTNER_EX_UNKNOWN_REL = 8 @@ -407,12 +432,22 @@ class RelationshipCalculator(object): except ImportError: pass + #data storage to communicate with recursive functions + self.__max_depth_reached = False + self.__loop_detected = False + self.__max_depth = 0 + self.__all_families = False + self.__all_dist = False + self.__only_birth = False + self.__crosslinks = False + self.__msg = [] + def set_depth(self, depth): """ Set how deep relationships must be searched. Input must be an integer > 0 """ - if not depth == self.depth: + if depth != self.depth: self.depth = depth self.dirtymap = True @@ -428,11 +463,11 @@ class RelationshipCalculator(object): """ Internal english method to create relation string """ - if level > len(_father_level) - 1: + if level > len(_FATHER_LEVEL) - 1: return self.DIST_FATHER % {'step': step, 'inlaw': inlaw, 'level': level} else: - return _father_level[level] % {'step': step, 'inlaw': inlaw} + return _FATHER_LEVEL[level] % {'step': step, 'inlaw': inlaw} DIST_SON = "distant %(step)sdescendant%(inlaw)s (%(level)d generations)" @@ -440,11 +475,11 @@ class RelationshipCalculator(object): """ Internal english method to create relation string """ - if level > len(_son_level) - 1: + if level > len(_SON_LEVEL) - 1: return self.DIST_SON % {'step': step, 'inlaw': inlaw, 'level': level} else: - return _son_level[level] % {'step': step, 'inlaw': inlaw} + return _SON_LEVEL[level] % {'step': step, 'inlaw': inlaw} DIST_MOTHER = "distant %(step)sancestor%(inlaw)s (%(level)d generations)" @@ -452,11 +487,11 @@ class RelationshipCalculator(object): """ Internal english method to create relation string """ - if level > len(_mother_level) - 1: + if level > len(_MOTHER_LEVEL) - 1: return self.DIST_MOTHER % {'step': step, 'inlaw': inlaw, 'level': level} else: - return _mother_level[level] % {'step': step, 'inlaw': inlaw} + return _MOTHER_LEVEL[level] % {'step': step, 'inlaw': inlaw} DIST_DAUGHTER = "distant %(step)sdescendant%(inlaw)s (%(level)d generations)" @@ -464,18 +499,18 @@ class RelationshipCalculator(object): """ Internal english method to create relation string """ - if level > len(_daughter_level) - 1: + if level > len(_DAUGHTER_LEVEL) - 1: return self.DIST_DAUGHTER % {'step': step, 'inlaw': inlaw, 'level': level} else: - return _daughter_level[level] % {'step': step, 'inlaw': inlaw} + return _DAUGHTER_LEVEL[level] % {'step': step, 'inlaw': inlaw} def _get_parent_unknown(self, level, step='', inlaw=''): """ Internal english method to create relation string """ - if level < len(_level_name): - return _level_name[level] + ' ' + '%sancestor%s' % (step, inlaw) + if level < len(_LEVEL_NAME): + return _LEVEL_NAME[level] + ' ' + '%sancestor%s' % (step, inlaw) else: return "distant %sancestor%s (%d generations)" % (step, inlaw, level) @@ -486,9 +521,9 @@ class RelationshipCalculator(object): """ Internal english method to create relation string """ - if level < len(_level_name): - return _level_name[level] + ' ' + '%(step)sdescendant%(inlaw)s' % { - 'step': step, 'inlaw': inlaw} + if level < len(_LEVEL_NAME): + return _LEVEL_NAME[level] + ' ' + '%(step)sdescendant%(inlaw)s' % { + 'step': step, 'inlaw': inlaw} else: return self.DIST_CHILD % {'step': step, 'level': level} @@ -498,10 +533,10 @@ class RelationshipCalculator(object): """ Internal english method to create relation string """ - if level > len(_sister_level) - 1: + if level > len(_SISTER_LEVEL) - 1: return self.DIST_AUNT % {'step': step, 'inlaw': inlaw} else: - return _sister_level[level] % {'step': step, 'inlaw': inlaw} + return _SISTER_LEVEL[level] % {'step': step, 'inlaw': inlaw} DIST_UNCLE = "distant %(step)suncle%(inlaw)s" @@ -509,10 +544,10 @@ class RelationshipCalculator(object): """ Internal english method to create relation string """ - if level > len(_brother_level) - 1: + if level > len(_BROTHER_LEVEL) - 1: return self.DIST_UNCLE % {'step': step, 'inlaw': inlaw} else: - return _brother_level[level] % {'step': step, 'inlaw': inlaw} + return _BROTHER_LEVEL[level] % {'step': step, 'inlaw': inlaw} DIST_NEPHEW = "distant %(step)snephew%(inlaw)s" @@ -520,10 +555,10 @@ class RelationshipCalculator(object): """ Internal english method to create relation string """ - if level > len(_nephew_level) - 1: + if level > len(_NEPHEW_LEVEL) - 1: return self.DIST_NEPHEW % {'step': step, 'inlaw': inlaw} else: - return _nephew_level[level] % {'step': step, 'inlaw': inlaw} + return _NEPHEW_LEVEL[level] % {'step': step, 'inlaw': inlaw} DIST_NIECE = "distant %(step)sniece%(inlaw)s" @@ -531,24 +566,23 @@ class RelationshipCalculator(object): """ Internal english method to create relation string """ - if level > len(_niece_level) - 1: + if level > len(_NIECE_LEVEL) - 1: return self.DIST_NIECE % {'step': step, 'inlaw': inlaw} else: - return _niece_level[level] % {'step': step, 'inlaw': inlaw} + return _NIECE_LEVEL[level] % {'step': step, 'inlaw': inlaw} def _get_cousin(self, level, removed, dir='', step='', inlaw=''): """ Internal english method to create relation string """ - if removed == 0 and level < len(_level_name): - return "%s %scousin%s" % (_level_name[level], - step, inlaw) - elif removed > len(_removed_level)-1 or level>len(_level_name)-1: + if removed == 0 and level < len(_LEVEL_NAME): + return "%s %scousin%s" % (_LEVEL_NAME[level], step, inlaw) + elif removed > len(_REMOVED_LEVEL)-1 or level > len(_LEVEL_NAME)-1: return "distant %srelative%s" % (step, inlaw) else: - return "%s %scousin%s%s%s" % (_level_name[level], - step, inlaw, - _removed_level[removed], dir) + return "%s %scousin%s%s%s" % (_LEVEL_NAME[level], + step, inlaw, + _REMOVED_LEVEL[removed], dir) DIST_SIB = "distant %(step)suncle/aunt%(inlaw)s" @@ -556,8 +590,8 @@ class RelationshipCalculator(object): """ Internal english method to create relation string """ - if level < len(_sibling_level): - return _sibling_level[level] % {'step': step, 'inlaw': inlaw} + if level < len(_SIBLING_LEVEL): + return _SIBLING_LEVEL[level] % {'step': step, 'inlaw': inlaw} else: return self.DIST_SIB % {'step': step, 'inlaw': inlaw} @@ -578,7 +612,7 @@ class RelationshipCalculator(object): return self.HALF_SIB_FATHER elif motherother == motherorig: return self.HALF_SIB_MOTHER - else : + else: return self.STEP_SIB else: # some birth parents are not known, hence we or cannot know if @@ -625,10 +659,9 @@ class RelationshipCalculator(object): family = db.get_family_from_handle(fam) if not family: continue - childrel = [(ref.get_mother_relation(), - ref.get_father_relation()) for ref in - family.get_child_ref_list() - if ref.ref == person.handle] + childrel = [(ref.get_mother_relation(), ref.get_father_relation()) + for ref in family.get_child_ref_list() + if ref.ref == person.handle] if not birthmother and childrel[0][0] == ChildRefType.BIRTH: birthmother = family.get_mother_handle() if not birthfather and childrel[0][1] == ChildRefType.BIRTH: @@ -649,20 +682,19 @@ class RelationshipCalculator(object): family = db.get_family_from_handle(fam) if not family: continue - childrel = [(ref.get_mother_relation(), - ref.get_father_relation()) for ref in - family.get_child_ref_list() - if ref.ref == person.handle] - if not childrel[0][0] == ChildRefType.BIRTH \ - and not childrel[0][0] == ChildRefType.UNKNOWN : + childrel = [(ref.get_mother_relation(), ref.get_father_relation()) + for ref in family.get_child_ref_list() + if ref.ref == person.handle] + if childrel[0][0] != ChildRefType.BIRTH \ + and childrel[0][0] != ChildRefType.UNKNOWN: nb_parents.append(family.get_mother_handle()) - if not childrel[0][1] == ChildRefType.BIRTH \ - and not childrel[0][1] == ChildRefType.UNKNOWN : + if childrel[0][1] != ChildRefType.BIRTH \ + and childrel[0][1] != ChildRefType.UNKNOWN: nb_parents.append(family.get_father_handle()) #make every person appear only once: return list(set(nb_parents)) - def _get_spouse_type(self, db, orig, other, all_rel = False): + def _get_spouse_type(self, db, orig, other, all_rel=False): """ Translation free determination if orig and other are partners. The procedure returns partner types, these can be passed to @@ -682,7 +714,7 @@ class RelationshipCalculator(object): for eventref in family.get_event_ref_list(): event = db.get_event_from_handle(eventref.ref) if event and (event.get_type() == EventType.DIVORCE - or event.get_type() == EventType.ANNULMENT): + or event.get_type() == EventType.ANNULMENT): ex = True break if family_rel == FamilyRelType.MARRIED: @@ -706,7 +738,7 @@ class RelationshipCalculator(object): else: val.append(self.PARTNER_UNKNOWN_REL) - if all_rel : + if all_rel: return val else: #last relation is normally the defenitive relation @@ -722,7 +754,8 @@ class RelationshipCalculator(object): spouse_type = self._get_spouse_type(db, orig, other, all_rel) if spouse_type: return self.get_partner_relationship_string(spouse_type, - orig.get_gender(), other.get_gender()) + orig.get_gender(), + other.get_gender()) else: return None @@ -786,125 +819,128 @@ class RelationshipCalculator(object): :type only_birth: bool """ #data storage to communicate with recursive functions - self.__maxDepthReached = False - self.__loopDetected = False + self.__max_depth_reached = False + self.__loop_detected = False self.__max_depth = self.get_depth() self.__all_families = all_families self.__all_dist = all_dist self.__only_birth = only_birth self.__crosslinks = False # no crosslinks - firstRel = -1 - secondRel = -1 + first_rel = -1 + second_rel = -1 self.__msg = [] common = [] - firstMap = {} - secondMap = {} + first_map = {} + second_map = {} rank = 9999999 try: if (self.storemap and self.stored_map is not None and self.map_handle == orig_person.handle and not self.dirtymap): - firstMap = self.stored_map - self.__maxDepthReached, self.__loopDetected, \ + first_map = self.stored_map + self.__max_depth_reached, self.__loop_detected, \ self.__all_families,\ self.__all_dist, self.__only_birth,\ self.__crosslinks, self.__msg = self.map_meta self.__msg = list(self.__msg) else: - self.__apply_filter(db, orig_person, '', [], firstMap) - self.map_meta = (self.__maxDepthReached, - self.__loopDetected, + self.__apply_filter(db, orig_person, '', [], first_map) + self.map_meta = (self.__max_depth_reached, + self.__loop_detected, self.__all_families, self.__all_dist, self.__only_birth, self.__crosslinks, list(self.__msg)) - self.__apply_filter(db, other_person, '', [], secondMap, - stoprecursemap = firstMap) + self.__apply_filter(db, other_person, '', [], second_map, + stoprecursemap=first_map) except RuntimeError: - return (-1, None, -1, [], -1, []) , \ + return (-1, None, -1, [], -1, []), \ [_("Relationship loop detected")] + self.__msg if self.storemap: - self.stored_map = firstMap + self.stored_map = first_map self.dirtymap = False self.map_handle = orig_person.handle - for person_handle in secondMap : - if person_handle in firstMap : + for person_handle in second_map: + if person_handle in first_map: com = [] #a common ancestor - for rel1, fam1 in zip(firstMap[person_handle][0], - firstMap[person_handle][1]): - l1 = len(rel1) - for rel2, fam2 in zip(secondMap[person_handle][0], - secondMap[person_handle][1]): - l2 = len(rel2) + for rel1, fam1 in zip(first_map[person_handle][0], + first_map[person_handle][1]): + len1 = len(rel1) + for rel2, fam2 in zip(second_map[person_handle][0], + second_map[person_handle][1]): + len2 = len(rel2) #collect paths to arrive at common ancestor - com.append((l1+l2, person_handle, rel1, fam1, - rel2, fam2)) + com.append((len1+len2, person_handle, rel1, fam1, + rel2, fam2)) #insert common ancestor in correct position, # if shorter links, check if not subset # if longer links, check if not superset pos = 0 - for ranknew, handlenew, rel1new, fam1new, rel2new, fam2new in com : + for (ranknew, handlenew, rel1new, fam1new, rel2new, + fam2new) in com: insert = True - for rank, handle, rel1, fam1, rel2, fam2 in common : - if ranknew < rank : + for rank, handle, rel1, fam1, rel2, fam2 in common: + if ranknew < rank: break - elif ranknew >= rank : + elif ranknew >= rank: #check subset if rel1 == rel1new[:len(rel1)] and \ - rel2 == rel2new[:len(rel2)] : + rel2 == rel2new[:len(rel2)]: #subset relation exists already insert = False break pos += 1 - if insert : - if common : - common.insert(pos, (ranknew, handlenew, - rel1new, fam1new, rel2new, fam2new)) + if insert: + if common: + common.insert(pos, (ranknew, handlenew, rel1new, + fam1new, rel2new, fam2new)) else: - common = [(ranknew, handlenew, - rel1new, fam1new, rel2new, fam2new)] + common = [(ranknew, handlenew, rel1new, fam1new, + rel2new, fam2new)] #now check if superset must be deleted from common deletelist = [] index = pos+1 - for rank, handle, rel1, fam1, rel2, fam2 in common[pos+1:]: + for (rank, handle, rel1, fam1, rel2, + fam2) in common[pos+1:]: if rel1new == rel1[:len(rel1new)] and \ - rel2new == rel2[:len(rel2new)] : + rel2new == rel2[:len(rel2new)]: deletelist.append(index) index += 1 deletelist.reverse() for index in deletelist: del common[index] #check for extra messages - if self.__maxDepthReached : + if self.__max_depth_reached: self.__msg += [_('Family Tree reaches back more than the maximum ' - '%d generations searched.\nIt is possible that ' - 'relationships have been missed') % (self.__max_depth)] + '%d generations searched.\nIt is possible that ' + 'relationships have been missed') % + (self.__max_depth)] - if common and not self.__all_dist : - rank = common[0][0] + if common and not self.__all_dist: + rank = common[0][0] person_handle = common[0][1] - firstRel = common[0][2] - firstFam = common[0][3] - secondRel = common[0][4] - secondFam = common[0][5] - return (rank, person_handle, firstRel, firstFam, secondRel, secondFam),\ - self.__msg - if common : + first_rel = common[0][2] + first_fam = common[0][3] + second_rel = common[0][4] + second_fam = common[0][5] + return (rank, person_handle, first_rel, first_fam, second_rel, + second_fam), self.__msg + if common: #list with tuples (rank, handle person,rel_str_orig,rel_fam_orig, # rel_str_other,rel_fam_str) and messages return common, self.__msg - if not self.__all_dist : + if not self.__all_dist: return (-1, None, '', [], '', []), self.__msg - else : + else: return [(-1, None, '', [], '', [])], self.__msg def __apply_filter(self, db, person, rel_str, rel_fam, pmap, - depth=1, stoprecursemap=None): + depth=1, stoprecursemap=None): """ Typically this method is called recursively in two ways: First method is stoprecursemap= None @@ -918,11 +954,11 @@ class RelationshipCalculator(object): will be looked up anyway an stored if common. At end the doubles are filtered out """ - if person is None or not person.handle : + if person is None or not person.handle: return if depth > self.__max_depth: - self.__maxDepthReached = True + self.__max_depth_reached = True #print('Maximum ancestor generations ('+str(depth)+') reached', \ # '(' + rel_str + ').',\ # 'Stopping relation algorithm.') @@ -949,15 +985,15 @@ class RelationshipCalculator(object): #check if there is no loop father son of his son, ... # loop means person is twice reached, same rel_str in begin for rel1 in pmap[person.handle][0]: - for rel2 in pmap[person.handle][0] : + for rel2 in pmap[person.handle][0]: if len(rel1) < len(rel2) and \ rel1 == rel2[:len(rel1)]: #loop, keep one message in storage! - self.__loopDetected = True + self.__loop_detected = True self.__msg += [_("Relationship loop detected:") + " " + _("Person %(person)s connects to himself via %(relation)s") % {'person' : person.get_primary_name().get_name(), - 'relation' : rel2[len(rel1):] }] + 'relation' : rel2[len(rel1):]}] return elif store: pmap[person.handle] = [[rel_str], [rel_fam]] @@ -965,71 +1001,70 @@ class RelationshipCalculator(object): #having added person to the pmap, we only look up recursively to # parents if this person is not common relative # if however the first map has crosslinks, we need to continue reduced - if commonancestor and not self.__crosslinks : + if commonancestor and not self.__crosslinks: #don't continue search, great speedup! return family_handles = [] main = person.get_main_parents_family_handle() - if main : + if main: family_handles = [main] - if self.__all_families : + if self.__all_families: family_handles = person.get_parent_family_handle_list() try: parentstodo = {} fam = 0 - for family_handle in family_handles : + for family_handle in family_handles: rel_fam_new = rel_fam + [fam] family = db.get_family_from_handle(family_handle) if not family: continue #obtain childref for this person childrel = [(ref.get_mother_relation(), - ref.get_father_relation()) for ref in - family.get_child_ref_list() - if ref.ref == person.handle] + ref.get_father_relation()) + for ref in family.get_child_ref_list() + if ref.ref == person.handle] fhandle = family.father_handle mhandle = family.mother_handle for data in [(fhandle, self.REL_FATHER, - self.REL_FATHER_NOTBIRTH, childrel[0][1]), + self.REL_FATHER_NOTBIRTH, childrel[0][1]), (mhandle, self.REL_MOTHER, - self.REL_MOTHER_NOTBIRTH, childrel[0][0])]: - if data[0] and data[0] not in parentstodo : + self.REL_MOTHER_NOTBIRTH, childrel[0][0])]: + if data[0] and data[0] not in parentstodo: persontodo = db.get_person_from_handle(data[0]) - if data[3] == ChildRefType.BIRTH : + if data[3] == ChildRefType.BIRTH: addstr = data[1] - elif not self.__only_birth : + elif not self.__only_birth: addstr = data[2] - else : + else: addstr = '' - if addstr : + if addstr: parentstodo[data[0]] = (persontodo, rel_str + addstr, rel_fam_new) - elif data [0] and data[0] in parentstodo: + elif data[0] and data[0] in parentstodo: #this person is already scheduled to research #update family list famlist = parentstodo[data[0]][2] if not isinstance(famlist[-1], list) and \ - not fam == famlist[-1]: + fam != famlist[-1]: famlist = famlist[:-1] + [[famlist[-1]]] if isinstance(famlist[-1], list) and \ - fam not in famlist[-1] : + fam not in famlist[-1]: famlist = famlist[:-1] + [famlist[-1] + [fam]] parentstodo[data[0]] = (parentstodo[data[0]][0], parentstodo[data[0]][1], - famlist - ) + famlist) if not fhandle and not mhandle and stoprecursemap is None: #family without parents, add brothers for orig person #other person has recusemap, and will stop when seeing #the brother. child_list = [ref.ref for ref in family.get_child_ref_list() - if ref.ref != person.handle] + if ref.ref != person.handle] addstr = self.REL_SIBLING - for chandle in child_list : - if chandle in pmap : + for chandle in child_list: + if chandle in pmap: pmap[chandle][0] += [rel_str + addstr] pmap[chandle][1] += [rel_fam_new] #person is already a grandparent in another branch @@ -1039,8 +1074,8 @@ class RelationshipCalculator(object): for handle, data in parentstodo.items(): self.__apply_filter(db, data[0], - data[1], data[2], - pmap, depth, stoprecursemap) + data[1], data[2], + pmap, depth, stoprecursemap) except: import traceback traceback.print_exc() @@ -1063,17 +1098,17 @@ class RelationshipCalculator(object): In the case of sibling, this is replaced by family with common ancestor handles empty list []! """ - if relations[0][0] == -1 : + if relations[0][0] == -1: return relations commonnew = [] existing_path = [] for relation in relations: relstrfirst = None commonhandle = [relation[1]] - if relation[2] : + if relation[2]: relstrfirst = relation[2][:-1] relstrsec = None - if relation[4] : + if relation[4]: relstrsec = relation[4][:-1] relfamfirst = relation[3][:] relfamsec = relation[5][:] @@ -1127,21 +1162,20 @@ class RelationshipCalculator(object): frstcomstr = rela2[-1] scndcomstr = tmp[2][-1] newcomstra = self._famrel_from_persrel(frstcomstr, - scndcomstr) + scndcomstr) frstcomstr = rela4[-1] scndcomstr = tmp[4][-1] newcomstrb = self._famrel_from_persrel(frstcomstr, - scndcomstr) + scndcomstr) commonnew[posfam] = (tmp[0], tmp[1]+commonhandle, - rela2[:-1]+newcomstra, - tmp[3], rela4[:-1]+newcomstrb, - tmp[5]) - else : + rela2[:-1]+newcomstra, + tmp[3], rela4[:-1]+newcomstrb, + tmp[5]) + else: existing_path.append(familypath) commonnew.append((relation[0], commonhandle, rela2, - familypath[2], rela4, familypath[3]) - ) + familypath[2], rela4, familypath[3])) #we now have multiple person handles, single families, now collapse # families again if all else equal collapsed = commonnew[:1] @@ -1183,14 +1217,18 @@ class RelationshipCalculator(object): if persrela == persrelb: #should not happen, procedure called in error, just return value return persrela - if (persrela == self.REL_MOTHER and persrelb == self.REL_FATHER) or \ - (persrelb == self.REL_MOTHER and persrela == self.REL_FATHER): + if ((persrela == self.REL_MOTHER and persrelb == self.REL_FATHER) or + (persrelb == self.REL_MOTHER and persrela == self.REL_FATHER)): return self.REL_FAM_BIRTH - if (persrela == self.REL_MOTHER and persrelb == self.REL_FATHER_NOTBIRTH) or \ - (persrelb == self.REL_MOTHER and persrela == self.REL_FATHER_NOTBIRTH): + if ((persrela == self.REL_MOTHER and + persrelb == self.REL_FATHER_NOTBIRTH) or + (persrelb == self.REL_MOTHER and + persrela == self.REL_FATHER_NOTBIRTH)): return self.REL_FAM_BIRTH_MOTH_ONLY - if (persrela == self.REL_FATHER and persrelb == self.REL_MOTHER_NOTBIRTH) or \ - (persrelb == self.REL_FATHER and persrela == self.REL_MOTHER_NOTBIRTH): + if ((persrela == self.REL_FATHER and + persrelb == self.REL_MOTHER_NOTBIRTH) or + (persrelb == self.REL_FATHER and + persrela == self.REL_MOTHER_NOTBIRTH)): return self.REL_FAM_BIRTH_FATH_ONLY #catch calling with family relations already, return val if (persrela == self.REL_FAM_BIRTH or @@ -1210,11 +1248,11 @@ class RelationshipCalculator(object): Given a path to common ancestor. Return True if only birth relations, False otherwise """ - only_birth = True - for str in path: - only_birth = only_birth and (str not in [self.REL_FAM_NONBIRTH, - self.REL_FATHER_NOTBIRTH, self.REL_MOTHER_NOTBIRTH]) - return only_birth + for value in path: + if value in [self.REL_FAM_NONBIRTH, self.REL_FATHER_NOTBIRTH, + self.REL_MOTHER_NOTBIRTH]: + return False + return True def get_one_relationship(self, db, orig_person, other_person, extra_info=False, olocale=glocale): @@ -1251,9 +1289,8 @@ class RelationshipCalculator(object): return rel_str data, msg = self.get_relationship_distance_new( - db, orig_person, other_person, - all_dist=True, - all_families=True, only_birth=False) + db, orig_person, other_person, all_dist=True, all_families=True, + only_birth=False) if data[0][0] == -1: if extra_info: return ('', -1, -1) @@ -1265,7 +1302,7 @@ class RelationshipCalculator(object): #most relevant relationship is a birth family relation of lowest rank databest = [data[0]] rankbest = data[0][0] - for rel in data : + for rel in data: #data is sorted on rank if rel[0] == rankbest: databest.append(rel) @@ -1276,19 +1313,15 @@ class RelationshipCalculator(object): birth = self.only_birth(rel[2]) and self.only_birth(rel[4]) if dist_orig == dist_other == 1: rel_str = self.get_sibling_relationship_string( - self.get_sibling_type( - db, orig_person, other_person), - orig_person.get_gender(), - other_person.get_gender()) + self.get_sibling_type(db, orig_person, other_person), + orig_person.get_gender(), + other_person.get_gender()) else: - rel_str = self.get_single_relationship_string(dist_orig, - dist_other, - orig_person.get_gender(), - other_person.get_gender(), - rel[2], rel[4], - only_birth=birth, - in_law_a=False, - in_law_b=False) + rel_str = self.get_single_relationship_string( + dist_orig, dist_other, + orig_person.get_gender(), other_person.get_gender(), + rel[2], rel[4], only_birth=birth, + in_law_a=False, in_law_b=False) else: order = [self.REL_FAM_BIRTH, self.REL_FAM_BIRTH_MOTH_ONLY, self.REL_FAM_BIRTH_FATH_ONLY, self.REL_MOTHER, @@ -1302,7 +1335,7 @@ class RelationshipCalculator(object): rel = relother break if not relbirth and self.only_birth(relother[2]) \ - and self.only_birth(relother[4]) : + and self.only_birth(relother[4]): #birth takes precedence rel = relother continue @@ -1329,20 +1362,16 @@ class RelationshipCalculator(object): dist_other = len(rel[4]) birth = self.only_birth(rel[2]) and self.only_birth(rel[4]) if dist_orig == dist_other == 1: - rel_str = self.get_sibling_relationship_string( - self.get_sibling_type( - db, orig_person, other_person), - orig_person.get_gender(), - other_person.get_gender()) + rel_str = self.get_sibling_relationship_string( + self.get_sibling_type(db, orig_person, other_person), + orig_person.get_gender(), + other_person.get_gender()) else: - rel_str = self.get_single_relationship_string(dist_orig, - dist_other, - orig_person.get_gender(), - other_person.get_gender(), - rel[2], rel[4], - only_birth=birth, - in_law_a=False, - in_law_b=False) + rel_str = self.get_single_relationship_string( + dist_orig, dist_other, + orig_person.get_gender(), other_person.get_gender(), + rel[2], rel[4], only_birth=birth, + in_law_a=False, in_law_b=False) if extra_info: return (rel_str, dist_orig, dist_other) else: @@ -1368,12 +1397,11 @@ class RelationshipCalculator(object): commons[is_spouse] = [] data, msg = self.get_relationship_distance_new( - db, orig_person, other_person, - all_dist=True, - all_families=True, only_birth=False) - if not data[0][0] == -1: + db, orig_person, other_person, all_dist=True, all_families=True, + only_birth=False) + if data[0][0] != -1: data = self.collapse_relations(data) - for rel in data : + for rel in data: rel2 = rel[2] rel4 = rel[4] rel1 = rel[1] @@ -1387,20 +1415,15 @@ class RelationshipCalculator(object): birth = self.only_birth(rel2) and self.only_birth(rel4) if dist_orig == dist_other == 1: rel_str = self.get_sibling_relationship_string( - self.get_sibling_type( - db, orig_person, other_person), - orig_person.get_gender(), - other_person.get_gender()) + self.get_sibling_type(db, orig_person, other_person), + orig_person.get_gender(), other_person.get_gender()) else: - rel_str = self.get_single_relationship_string(dist_orig, - dist_other, - orig_person.get_gender(), - other_person.get_gender(), - rel2, rel4, - only_birth=birth, - in_law_a=False, - in_law_b=False) - if not rel_str in relstrings: + rel_str = self.get_single_relationship_string( + dist_orig, dist_other, + orig_person.get_gender(), other_person.get_gender(), + rel2, rel4, only_birth=birth, + in_law_a=False, in_law_b=False) + if rel_str not in relstrings: relstrings.append(rel_str) if rel1: commons[rel_str] = rel1 @@ -1460,54 +1483,54 @@ class RelationshipCalculator(object): rel_str = "distant relatives" if Ga == 0: # These are descendants - if Gb < len(_children_level): - rel_str = _children_level[Gb] + if Gb < len(_CHILDREN_LEVEL): + rel_str = _CHILDREN_LEVEL[Gb] else: rel_str = "distant descendants" elif Gb == 0: # These are parents/grand parents - if Ga < len(_parents_level): - rel_str = _parents_level[Ga] + if Ga < len(_PARENTS_LEVEL): + rel_str = _PARENTS_LEVEL[Ga] else: rel_str = "distant ancestors" elif Gb == 1: # These are siblings/aunts/uncles - if Ga < len(_siblings_level): - rel_str = _siblings_level[Ga] + if Ga < len(_SIBLINGS_LEVEL): + rel_str = _SIBLINGS_LEVEL[Ga] else: rel_str = "distant uncles/aunts" elif Ga == 1: # These are nieces/nephews - if Gb < len(_nephews_nieces_level): - rel_str = _nephews_nieces_level[Gb] + if Gb < len(_NEPHEWS_NIECES_LEVEL): + rel_str = _NEPHEWS_NIECES_LEVEL[Gb] else: rel_str = "distant nephews/nieces" elif Ga > 1 and Ga == Gb: # These are cousins in the same generation - if Ga <= len(_level_name): - rel_str = "%s cousins" % _level_name[Ga-1] + if Ga <= len(_LEVEL_NAME): + rel_str = "%s cousins" % _LEVEL_NAME[Ga-1] else: rel_str = "distant cousins" elif Ga > 1 and Ga > Gb: # These are cousins in different generations with the second person # being in a higher generation from the common ancestor than the # first person. - if Gb <= len(_level_name) and (Ga-Gb) < len(_removed_level): - rel_str = "%s cousins%s (up)" % ( _level_name[Gb-1], - _removed_level[Ga-Gb] ) + if Gb <= len(_LEVEL_NAME) and (Ga-Gb) < len(_REMOVED_LEVEL): + rel_str = "%s cousins%s (up)" % (_LEVEL_NAME[Gb-1], + _REMOVED_LEVEL[Ga-Gb]) else: - rel_str = "distant cousins" + rel_str = "distant cousins" elif Gb > 1 and Gb > Ga: # These are cousins in different generations with the second person # being in a lower generation from the common ancestor than the # first person. - if Ga <= len(_level_name) and (Gb-Ga) < len(_removed_level): - rel_str = "%s cousins%s (down)" % ( _level_name[Ga-1], - _removed_level[Gb-Ga] ) + if Ga <= len(_LEVEL_NAME) and (Gb-Ga) < len(_REMOVED_LEVEL): + rel_str = "%s cousins%s (down)" % (_LEVEL_NAME[Ga-1], + _REMOVED_LEVEL[Gb-Ga]) else: - rel_str = "distant cousins" + rel_str = "distant cousins" - if in_law_b == True: + if in_law_b is True: rel_str = "spouses of %s" % rel_str return rel_str @@ -1611,7 +1634,7 @@ class RelationshipCalculator(object): else: step = self.STEP - if in_law_a or in_law_b : + if in_law_a or in_law_b: inlaw = self.INLAW else: inlaw = '' @@ -1620,7 +1643,7 @@ class RelationshipCalculator(object): if Ga == 0: # b is descendant of a - if Gb == 0 : + if Gb == 0: rel_str = 'same person' elif gender_b == MALE: rel_str = self._get_son(Gb, step, inlaw) @@ -1650,27 +1673,26 @@ class RelationshipCalculator(object): rel_str = self._get_nephew(Gb-1, step, inlaw) elif gender_b == FEMALE: rel_str = self._get_niece(Gb-1, step, inlaw) - elif Gb < len(_niece_level) and Gb < len(_nephew_level): + elif Gb < len(_NIECE_LEVEL) and Gb < len(_NEPHEW_LEVEL): rel_str = "%s or %s" % (self._get_nephew(Gb-1, step, inlaw), self._get_niece(Gb-1, step, inlaw)) else: rel_str = "distant %snephews/nieces%s" % (step, inlaw) elif Ga == Gb: # a and b cousins in the same generation - rel_str = self._get_cousin(Ga-1, 0, dir = '', step=step, - inlaw=inlaw) + rel_str = self._get_cousin(Ga-1, 0, dir='', step=step, inlaw=inlaw) elif Ga > Gb: # These are cousins in different generations with the second person # being in a higher generation from the common ancestor than the # first person. - rel_str = self._get_cousin(Gb-1, Ga-Gb, dir = ' (up)', - step=step, inlaw=inlaw) + rel_str = self._get_cousin(Gb-1, Ga-Gb, dir=' (up)', + step=step, inlaw=inlaw) elif Gb > Ga: # These are cousins in different generations with the second person # being in a lower generation from the common ancestor than the # first person. - rel_str = self._get_cousin(Ga-1, Gb-Ga, dir = ' (down)', - step=step, inlaw=inlaw) + rel_str = self._get_cousin(Ga-1, Gb-Ga, dir=' (down)', + step=step, inlaw=inlaw) return rel_str def get_sibling_relationship_string(self, sib_type, gender_a, gender_b, @@ -1695,7 +1717,7 @@ class RelationshipCalculator(object): elif sib_type == self.STEP_SIB: typestr = self.STEP - if in_law_a or in_law_b : + if in_law_a or in_law_b: inlaw = self.INLAW else: inlaw = '' @@ -1797,18 +1819,17 @@ class RelationshipCalculator(object): """ if self.__db_connected: return - assert(len(self.signal_keys)==0) + assert len(self.signal_keys) == 0 self.state_signal_key = dbstate.connect('database-changed', - self._dbchange_callback) + self._dbchange_callback) self.__connect_db_signals(dbstate.db) def __connect_db_signals(self, db): signals = ['person-add', 'person-update', 'person-delete', - 'person-rebuild', 'family-add', 'family-update', - 'family-delete', 'family-rebuild', 'database-changed'] + 'person-rebuild', 'family-add', 'family-update', + 'family-delete', 'family-rebuild', 'database-changed'] for name in signals: - self.signal_keys.append(db.connect(name, - self._datachange_callback)) + self.signal_keys.append(db.connect(name, self._datachange_callback)) self.storemap = True self.__db_connected = True @@ -1831,7 +1852,7 @@ class RelationshipCalculator(object): #signals are disconnected on close of old database, connect to new self.__connect_db_signals(db) - def _datachange_callback(self, list=[]): + def _datachange_callback(self, handle_list): """ When data in database changes, the map can no longer be used. As the map might be in use or might be generated at the moment, @@ -1881,7 +1902,8 @@ def get_relationship_calculator(reinit=False, clocale=glocale): break if not relation_translation_found and \ len(PluginRegister.get_instance().relcalc_plugins()): - LOG.warning(_("Family relationship translator not available for language '%s'. Using 'english' instead.") % lang) + LOG.warning(_("Family relationship translator not available for " + "language '%s'. Using 'english' instead."), lang) return __RELCALC_CLASS() #------------------------------------------------------------------------- @@ -1889,8 +1911,10 @@ def get_relationship_calculator(reinit=False, clocale=glocale): # Tests # #------------------------------------------------------------------------- +MAX = 30 +FMT = '%+50s' -def _test(rc, onlybirth, inlawa, inlawb, printrelstr, testNum = None): +def _test(rcalc, onlybirth, inlawa, inlawb, printrelstr, test_num=None): """ This is a generic test suite for the singular relationship TRANSLATORS: do NOT translate, use __main__ ! @@ -1899,24 +1923,21 @@ def _test(rc, onlybirth, inlawa, inlawb, printrelstr, testNum = None): import random random.seed() def _rand_f_m(): - if random.randint(0, 1) == 0 : + if random.randint(0, 1) == 0: return 'f' else: return 'm' - def _rand_relstr(len, endstr): - if len == 0: + def _rand_relstr(length, endstr): + if length == 0: return '' else: relstr = '' - for i in range(len-1): + for i in range(length-1): relstr += _rand_f_m() return relstr + endstr - FMT = '%+50s' - MAX = 30 - - if testNum == None: + if test_num is None: print(""" Select a test: 0 - all tests @@ -1940,344 +1961,286 @@ Select a test: Please enter a test number and press Enter for continue: """) - testNum = sys.stdin.readline().strip() - testNum = int(testNum) + test_num = sys.stdin.readline().strip() + test_num = int(test_num) - if testNum == 0 or testNum == 1: + if test_num == 0 or test_num == 1: print('\ntesting sons') #sys.stdin.readline() - for i in range(MAX) : - relstr = _rand_relstr(i,'f') - rel = FMT % rc.get_single_relationship_string(0, i, - MALE, - MALE, - '', relstr, - only_birth=onlybirth, - in_law_a=inlawa, - in_law_b=inlawb) - if printrelstr : + for i in range(MAX): + relstr = _rand_relstr(i, 'f') + rel = FMT % rcalc.get_single_relationship_string( + 0, i, MALE, MALE, '', relstr, only_birth=onlybirth, + in_law_a=inlawa, in_law_b=inlawb) + if printrelstr: print(rel + ' |info:', relstr) else: print(rel) - if testNum == 0 or testNum == 2: + if test_num == 0 or test_num == 2: print('\ntesting daughters\n') #sys.stdin.readline() - for i in range(MAX) : - relstr = _rand_relstr(i,'m') - rel = FMT % rc.get_single_relationship_string(0, i, - MALE, - FEMALE, - '', relstr, - only_birth=onlybirth, - in_law_a=inlawa, in_law_b=inlawb) - if printrelstr : + for i in range(MAX): + relstr = _rand_relstr(i, 'm') + rel = FMT % rcalc.get_single_relationship_string( + 0, i, MALE, FEMALE, '', relstr, only_birth=onlybirth, + in_law_a=inlawa, in_law_b=inlawb) + if printrelstr: print(rel + ' |info:', relstr) else: print(rel) - if testNum == 0 or testNum == 3: + if test_num == 0 or test_num == 3: print('\ntesting unknown children\n') #sys.stdin.readline() - for i in range(MAX) : - relstr = _rand_relstr(i,'f') - rel = FMT % rc.get_single_relationship_string(0, i, - MALE, - UNKNOWN, - '', relstr, - only_birth=onlybirth, - in_law_a=inlawa, in_law_b=inlawb) - if printrelstr : + for i in range(MAX): + relstr = _rand_relstr(i, 'f') + rel = FMT % rcalc.get_single_relationship_string( + 0, i, MALE, UNKNOWN, '', relstr, only_birth=onlybirth, + in_law_a=inlawa, in_law_b=inlawb) + if printrelstr: print(rel + ' |info:', relstr) else: print(rel) - if testNum == 0 or testNum == 4: + if test_num == 0 or test_num == 4: print('\ntesting grandfathers\n') #sys.stdin.readline() - for i in range(MAX) : - relstr = _rand_relstr(i,'f') - rel = FMT % rc.get_single_relationship_string(i, 0, - FEMALE, - MALE, - relstr, '', - only_birth=onlybirth, - in_law_a=inlawa, in_law_b=inlawb) - if printrelstr : - print(rel + ' |info:', relstr) + for i in range(MAX): + relstr = _rand_relstr(i, 'f') + rel = FMT % rcalc.get_single_relationship_string( + i, 0, FEMALE, MALE, relstr, '', only_birth=onlybirth, + in_law_a=inlawa, in_law_b=inlawb) + if printrelstr: + print(rel + ' |info:', relstr) else: print(rel) - if testNum == 0 or testNum == 5: + if test_num == 0 or test_num == 5: print('\ntesting grandmothers\n') #sys.stdin.readline() - for i in range(MAX) : - relstr = _rand_relstr(i,'m') - rel = FMT % rc.get_single_relationship_string(i, 0, - FEMALE, - FEMALE, - relstr, '', - only_birth=onlybirth, - in_law_a=inlawa, in_law_b=inlawb) - if printrelstr : - print(rel + ' |info:', relstr) + for i in range(MAX): + relstr = _rand_relstr(i, 'm') + rel = FMT % rcalc.get_single_relationship_string( + i, 0, FEMALE, FEMALE, relstr, '', only_birth=onlybirth, + in_law_a=inlawa, in_law_b=inlawb) + if printrelstr: + print(rel + ' |info:', relstr) else: print(rel) - if testNum == 0 or testNum == 6: + if test_num == 0 or test_num == 6: print('\ntesting unknown parents\n') #sys.stdin.readline() - for i in range(MAX) : - relstr = _rand_relstr(i,'f') - rel = FMT % rc.get_single_relationship_string(i, 0, - FEMALE, - UNKNOWN, - relstr, '', - only_birth=onlybirth, - in_law_a=inlawa, in_law_b=inlawb) - if printrelstr : - print(rel + ' |info:', relstr) + for i in range(MAX): + relstr = _rand_relstr(i, 'f') + rel = FMT % rcalc.get_single_relationship_string( + i, 0, FEMALE, UNKNOWN, relstr, '', only_birth=onlybirth, + in_law_a=inlawa, in_law_b=inlawb) + if printrelstr: + print(rel + ' |info:', relstr) else: print(rel) - if testNum == 0 or testNum == 7: + if test_num == 0 or test_num == 7: print('\ntesting nieces\n') #sys.stdin.readline() - for i in range(1, MAX) : - relstr = _rand_relstr(i,'m') - rel = FMT % rc.get_single_relationship_string(1, i, - FEMALE, - FEMALE, - 'm', relstr, - only_birth=onlybirth, - in_law_a=inlawa, in_law_b=inlawb) - if printrelstr : - print(rel + ' |info:', relstr) + for i in range(1, MAX): + relstr = _rand_relstr(i, 'm') + rel = FMT % rcalc.get_single_relationship_string( + 1, i, FEMALE, FEMALE, 'm', relstr, only_birth=onlybirth, + in_law_a=inlawa, in_law_b=inlawb) + if printrelstr: + print(rel + ' |info:', relstr) else: print(rel) - if testNum == 0 or testNum == 8: + if test_num == 0 or test_num == 8: print('\ntesting nephews\n') #sys.stdin.readline() - for i in range(1, MAX) : - relstr = _rand_relstr(i,'f') - rel = FMT % rc.get_single_relationship_string(1, i, - FEMALE, - MALE, - 'f', relstr, - only_birth=onlybirth, - in_law_a=inlawa, in_law_b=inlawb) - if printrelstr : - print(rel + ' |info:', relstr) + for i in range(1, MAX): + relstr = _rand_relstr(i, 'f') + rel = FMT % rcalc.get_single_relationship_string( + 1, i, FEMALE, MALE, 'f', relstr, only_birth=onlybirth, + in_law_a=inlawa, in_law_b=inlawb) + if printrelstr: + print(rel + ' |info:', relstr) else: print(rel) - if testNum == 0 or testNum == 9: + if test_num == 0 or test_num == 9: print('\ntesting unknown nephews/nieces\n') #sys.stdin.readline() - for i in range(1, MAX) : - relstr = _rand_relstr(i,'f') - rel = FMT % rc.get_single_relationship_string(1, i, - FEMALE, - UNKNOWN, - 'f', relstr, - only_birth=onlybirth, - in_law_a=inlawa, in_law_b=inlawb) - if printrelstr : - print(rel + ' |info:', relstr) + for i in range(1, MAX): + relstr = _rand_relstr(i, 'f') + rel = FMT % rcalc.get_single_relationship_string( + 1, i, FEMALE, UNKNOWN, 'f', relstr, only_birth=onlybirth, + in_law_a=inlawa, in_law_b=inlawb) + if printrelstr: + print(rel + ' |info:', relstr) else: print(rel) - if testNum == 0 or testNum == 10: + if test_num == 0 or test_num == 10: print('\ntesting uncles\n') #sys.stdin.readline() - for i in range(1, MAX) : - relstr = _rand_relstr(i,'f') - rel = FMT % rc.get_single_relationship_string(i, 1, - FEMALE, - MALE, - relstr, 'f', - only_birth=onlybirth, - in_law_a=inlawa, in_law_b=inlawb) - if printrelstr : - print(rel + ' |info:', relstr) + for i in range(1, MAX): + relstr = _rand_relstr(i, 'f') + rel = FMT % rcalc.get_single_relationship_string( + i, 1, FEMALE, MALE, relstr, 'f', only_birth=onlybirth, + in_law_a=inlawa, in_law_b=inlawb) + if printrelstr: + print(rel + ' |info:', relstr) else: print(rel) - if testNum == 0 or testNum == 11: + if test_num == 0 or test_num == 11: print('\ntesting aunts\n') #sys.stdin.readline() - for i in range(1, MAX) : - relstr = _rand_relstr(i,'f') - rel = FMT % rc.get_single_relationship_string(i, 1, - MALE, - FEMALE, - relstr, 'f', - only_birth=onlybirth, - in_law_a=inlawa, in_law_b=inlawb) - if printrelstr : - print(rel + ' |info:', relstr) + for i in range(1, MAX): + relstr = _rand_relstr(i, 'f') + rel = FMT % rcalc.get_single_relationship_string( + i, 1, MALE, FEMALE, relstr, 'f', only_birth=onlybirth, + in_law_a=inlawa, in_law_b=inlawb) + if printrelstr: + print(rel + ' |info:', relstr) else: print(rel) - if testNum == 0 or testNum == 12: + if test_num == 0 or test_num == 12: print('\ntesting unknown uncles/aunts\n') #sys.stdin.readline() - for i in range(1, MAX) : - relstr = _rand_relstr(i,'m') - rel = FMT % rc.get_single_relationship_string(i, 1, - MALE, - UNKNOWN, - relstr, 'm', - only_birth=onlybirth, - in_law_a=inlawa, in_law_b=inlawb) - if printrelstr : - print(rel + ' |info:', relstr) + for i in range(1, MAX): + relstr = _rand_relstr(i, 'm') + rel = FMT % rcalc.get_single_relationship_string( + i, 1, MALE, UNKNOWN, relstr, 'm', only_birth=onlybirth, + in_law_a=inlawa, in_law_b=inlawb) + if printrelstr: + print(rel + ' |info:', relstr) else: print(rel) - if testNum == 0 or testNum == 13: + if test_num == 0 or test_num == 13: print('\ntesting male cousins same generation\n') #sys.stdin.readline() - for i in range(1, MAX) : - relstra = _rand_relstr(i,'f') - relstrb = _rand_relstr(i,'f') - rel = FMT % rc.get_single_relationship_string(i, i, - MALE, - MALE, - relstra, - relstrb, - only_birth=onlybirth, - in_law_a=inlawa, in_law_b=inlawb) - if printrelstr : - print(rel + ' |info:', relstra, relstrb) + for i in range(1, MAX): + relstra = _rand_relstr(i, 'f') + relstrb = _rand_relstr(i, 'f') + rel = FMT % rcalc.get_single_relationship_string( + i, i, MALE, MALE, relstra, relstrb, only_birth=onlybirth, + in_law_a=inlawa, in_law_b=inlawb) + if printrelstr: + print(rel + ' |info:', relstra, relstrb) else: print(rel) - if testNum == 0 or testNum == 14: + if test_num == 0 or test_num == 14: print('\ntesting female cousins same generation\n') #sys.stdin.readline() - for i in range(1, MAX) : - relstra = _rand_relstr(i,'m') - relstrb = _rand_relstr(i,'m') - rel = FMT % rc.get_single_relationship_string(i, i, - MALE, - FEMALE, - relstra, - relstrb, - only_birth=onlybirth, - in_law_a=inlawa, in_law_b=inlawb) - if printrelstr : - print(rel + ' |info:', relstra, relstrb) + for i in range(1, MAX): + relstra = _rand_relstr(i, 'm') + relstrb = _rand_relstr(i, 'm') + rel = FMT % rcalc.get_single_relationship_string( + i, i, MALE, FEMALE, relstra, relstrb, only_birth=onlybirth, + in_law_a=inlawa, in_law_b=inlawb) + if printrelstr: + print(rel + ' |info:', relstra, relstrb) else: print(rel) - if testNum == 0 or testNum == 15: + if test_num == 0 or test_num == 15: print('\ntesting unknown cousins same generation\n') #sys.stdin.readline() - for i in range(1, MAX) : - relstra = _rand_relstr(i,'m') - relstrb = _rand_relstr(i,'m') - rel = FMT % rc.get_single_relationship_string(i, i, - MALE, - UNKNOWN, - relstra, - relstrb, - only_birth=onlybirth, - in_law_a=inlawa, in_law_b=inlawb) - if printrelstr : - print(rel + ' |info:', relstra, relstrb) + for i in range(1, MAX): + relstra = _rand_relstr(i, 'm') + relstrb = _rand_relstr(i, 'm') + rel = FMT % rcalc.get_single_relationship_string( + i, i, MALE, UNKNOWN, relstra, relstrb, only_birth=onlybirth, + in_law_a=inlawa, in_law_b=inlawb) + if printrelstr: + print(rel + ' |info:', relstra, relstrb) else: print(rel) - if testNum == 0 or testNum == 16: + if test_num == 0 or test_num == 16: print('\ntesting some cousins up\n') #sys.stdin.readline() - import random random.seed() - for i in range(1, MAX) : - for j in range (i, MAX) : + for i in range(1, MAX): + for j in range(i, MAX): rnd = random.randint(0, 100) - if rnd < 10 : - relstra = _rand_relstr(j,'f') - relstrb = _rand_relstr(i,'f') - if rnd < 5 : - rel = (FMT + ' |info: female, Ga=%2d, Gb=%2d') % ( - rc.get_single_relationship_string(j, i, - MALE, - FEMALE, - relstra, relstrb, - only_birth=onlybirth, - in_law_a=inlawa, in_law_b=inlawb), j, i ) - if printrelstr : - print(rel + ' |info:', relstra, relstrb) - else: - print(rel) - else: - rel = (FMT + ' |info: male, Ga=%2d, Gb=%2d') % ( - rc.get_single_relationship_string(j, i, - MALE, - MALE, - relstra, relstrb, - only_birth=onlybirth, - in_law_a=inlawa, in_law_b=inlawb), j, i ) - if printrelstr : - print(rel + ' |info:', relstra, relstrb) - else: - print(rel) - if testNum == 0 or testNum == 17: - print('\ntesting some cousins down\n') - #sys.stdin.readline() - for i in range(1, MAX) : - for j in range (i, MAX) : - rnd = random.randint(0, 100) - if rnd < 10 : - relstra = _rand_relstr(i,'f') - relstrb = _rand_relstr(j,'f') - if rnd < 5 : + if rnd < 10: + relstra = _rand_relstr(j, 'f') + relstrb = _rand_relstr(i, 'f') + if rnd < 5: rel = (FMT + ' |info: female, Ga=%2d, Gb=%2d') % ( - rc.get_single_relationship_string(i, j, - MALE, - FEMALE, - relstra, relstrb, - only_birth=onlybirth, - in_law_a=inlawa, in_law_b=inlawb), i, j) - if printrelstr : + rcalc.get_single_relationship_string( + j, i, MALE, FEMALE, relstra, relstrb, + only_birth=onlybirth, + in_law_a=inlawa, in_law_b=inlawb), j, i) + if printrelstr: print(rel + ' |info:', relstra, relstrb) else: print(rel) else: rel = (FMT + ' |info: male, Ga=%2d, Gb=%2d') % ( - rc.get_single_relationship_string(i, j, - MALE, - MALE, - relstra, relstrb, - only_birth=onlybirth, - in_law_a=inlawa, in_law_b=inlawb), i, j) - if printrelstr : + rcalc.get_single_relationship_string( + j, i, MALE, MALE, relstra, relstrb, + only_birth=onlybirth, + in_law_a=inlawa, in_law_b=inlawb), j, i) + if printrelstr: + print(rel + ' |info:', relstra, relstrb) + else: + print(rel) + if test_num == 0 or test_num == 17: + print('\ntesting some cousins down\n') + #sys.stdin.readline() + for i in range(1, MAX): + for j in range(i, MAX): + rnd = random.randint(0, 100) + if rnd < 10: + relstra = _rand_relstr(i, 'f') + relstrb = _rand_relstr(j, 'f') + if rnd < 5: + rel = (FMT + ' |info: female, Ga=%2d, Gb=%2d') % ( + rcalc.get_single_relationship_string( + i, j, MALE, FEMALE, relstra, relstrb, + only_birth=onlybirth, + in_law_a=inlawa, in_law_b=inlawb), i, j) + if printrelstr: + print(rel + ' |info:', relstra, relstrb) + else: + print(rel) + else: + rel = (FMT + ' |info: male, Ga=%2d, Gb=%2d') % ( + rcalc.get_single_relationship_string( + i, j, MALE, MALE, relstra, relstrb, + only_birth=onlybirth, + in_law_a=inlawa, in_law_b=inlawb), i, j) + if printrelstr: print(rel + ' |info:', relstra, relstrb) else: print(rel) -def _testsibling(rc): - vals = [(rc.NORM_SIB, 'sibling'), - (rc.HALF_SIB_MOTHER, 'half sib mother side'), - (rc.HALF_SIB_FATHER, 'half sib father side'), - (rc.STEP_SIB, 'step sib'), (rc.UNKNOWN_SIB, 'undetermined sib')] - FMT = '%+50s' +def _testsibling(rcalc): + vals = [(rcalc.NORM_SIB, 'sibling'), + (rcalc.HALF_SIB_MOTHER, 'half sib mother side'), + (rcalc.HALF_SIB_FATHER, 'half sib father side'), + (rcalc.STEP_SIB, 'step sib'), + (rcalc.UNKNOWN_SIB, 'undetermined sib')] for gendr, strgen in [(MALE, 'male'), - (FEMALE, 'female'), - (UNKNOWN, 'unknown')]: + (FEMALE, 'female'), + (UNKNOWN, 'unknown')]: for inlaw in [False, True]: - for sibt, str in vals: - print(FMT % rc.get_sibling_relationship_string( - sibt, MALE, gendr, - in_law_a = inlaw) + ' |info:', str, strgen) + for sibt, text in vals: + print(FMT % rcalc.get_sibling_relationship_string( + sibt, MALE, gendr, in_law_a=inlaw) + + ' |info:', text, strgen) -def _test_spouse(rc): - FMT = '%+50s' - vals = [(rc.PARTNER_MARRIED, 'married'), (rc.PARTNER_UNMARRIED, 'unmarried'), - (rc.PARTNER_CIVIL_UNION, 'civil union'), - (rc.PARTNER_UNKNOWN_REL, 'unknown rel'), - (rc.PARTNER_EX_MARRIED, 'ex-married'), - (rc.PARTNER_EX_UNMARRIED, 'ex-unmarried'), - (rc.PARTNER_EX_CIVIL_UNION, 'ex civil union'), - (rc.PARTNER_EX_UNKNOWN_REL, 'ex unknown rel')] +def _test_spouse(rcalc): + vals = [(rcalc.PARTNER_MARRIED, 'married'), + (rcalc.PARTNER_UNMARRIED, 'unmarried'), + (rcalc.PARTNER_CIVIL_UNION, 'civil union'), + (rcalc.PARTNER_UNKNOWN_REL, 'unknown rel'), + (rcalc.PARTNER_EX_MARRIED, 'ex-married'), + (rcalc.PARTNER_EX_UNMARRIED, 'ex-unmarried'), + (rcalc.PARTNER_EX_CIVIL_UNION, 'ex civil union'), + (rcalc.PARTNER_EX_UNKNOWN_REL, 'ex unknown rel')] for gender, strgen in [(MALE, 'male'), (FEMALE, 'female'), - (UNKNOWN, 'unknown')] : - for spouse_type, str in vals: - print(FMT % rc.get_partner_relationship_string( - spouse_type, MALE, gender) + \ - ' |info: gender='+strgen+', rel='+str) + (UNKNOWN, 'unknown')]: + for spouse_type, text in vals: + print(FMT % rcalc.get_partner_relationship_string( + spouse_type, MALE, gender) + + ' |info: gender='+strgen+', rel='+text) -def test(rc, printrelstr): +def test(rcalc, printrelstr): """ This is a generic test suite for the singular relationship TRANSLATORS: do NOT translate, call this from @@ -2287,13 +2250,13 @@ def test(rc, printrelstr): import argparse parser = argparse.ArgumentParser(description='Test the Relationship Calculator') - parser.add_argument('-r', type=int, help = 'type of the relations test') - parser.add_argument('-s', type=int, help = 'type of the singular relationship test') + parser.add_argument('-r', type=int, help='type of the relations test') + parser.add_argument('-s', type=int, help='type of the singular relationship test') args = parser.parse_args() - testNum = args.r + test_num = args.r - if testNum == None: + if test_num is None: print(""" Select a test: 0 - all tests @@ -2306,39 +2269,34 @@ Select a test: Please enter a test number and press Enter for continue: """) - testNum = sys.stdin.readline().strip() - testNum = int(testNum) + test_num = sys.stdin.readline().strip() + test_num = int(test_num) - if testNum == 0 or testNum == 1: + if test_num == 0 or test_num == 1: print('\n\n=== Test normal relations ===') - _test(rc, True, False, False, printrelstr, args.s) + _test(rcalc, True, False, False, printrelstr, args.s) - if testNum == 0 or testNum == 2: + if test_num == 0 or test_num == 2: print('\n\n=== Test step relations ===') - _test(rc, False, False, False, printrelstr, args.s) + _test(rcalc, False, False, False, printrelstr, args.s) - if testNum == 0 or testNum == 3: + if test_num == 0 or test_num == 3: print('\n\n=== Test in-law relations (first pers) ===') - _test(rc, True, True, False, printrelstr, args.s) + _test(rcalc, True, True, False, printrelstr, args.s) - if testNum == 0 or testNum == 4: + if test_num == 0 or test_num == 4: print('\n\n=== Test step and in-law relations ===') - _test(rc, False, True, False, printrelstr, args.s) + _test(rcalc, False, True, False, printrelstr, args.s) - if testNum == 0 or testNum == 5: + if test_num == 0 or test_num == 5: print('\n\n=== Test sibling types ===') - _testsibling(rc) + _testsibling(rcalc) - if testNum == 0 or testNum == 6: + if test_num == 0 or test_num == 6: print('\n\n=== Test partner types ===') - _test_spouse(rc) + _test_spouse(rcalc) if __name__ == "__main__": - # Test function. Call it as follows from the command line (so as to find - # imported modules): - # export PYTHONPATH=/path/to/gramps/src python src/plugins/rel_fr.py - # (Above not needed here) - """ TRANSLATORS, copy this if statement at the bottom of your rel_xx.py module, after adding: 'from Relationship import test' @@ -2348,5 +2306,5 @@ if __name__ == "__main__": See eg rel_fr.py at the bottom """ - rc = RelationshipCalculator() - test(rc, True) + REL_CALC = RelationshipCalculator() + test(REL_CALC, True) diff --git a/gramps/gen/sort.py b/gramps/gen/sort.py index 3cc217118..92fe758d0 100644 --- a/gramps/gen/sort.py +++ b/gramps/gen/sort.py @@ -114,8 +114,8 @@ class Sort(object): return glocale.sort_key(name1) ## def by_birthdate(self, first_id, second_id): -## """Sort routine for comparing two people by birth dates. If the birth dates -## are equal, sorts by name""" +## """Sort routine for comparing two people by birth dates. If the birth +## dates are equal, sorts by name""" ## first = self.database.get_person_from_handle(first_id) ## second = self.database.get_person_from_handle(second_id) ## @@ -218,7 +218,8 @@ class Sort(object): ## return 0 ## evt_a = self.database.get_event_from_handle(a_id) ## evt_b = self.database.get_event_from_handle(b_id) -## return glocale.strcoll(evt_a.get_description(), evt_b.get_description()) +## return glocale.strcoll(evt_a.get_description(), +## evt_b.get_description()) def by_event_description_key(self, a_id): """Sort routine for comparing two events by their descriptions. """ @@ -269,5 +270,5 @@ class Sort(object): """Sort routine for comparing two media objects by their title. """ if not a_id: return False - a = self.database.get_object_from_handle(a_id) - return glocale.sort_key(a.desc) + obj_a = self.database.get_object_from_handle(a_id) + return glocale.sort_key(obj_a.desc) diff --git a/gramps/gen/soundex.py b/gramps/gen/soundex.py index 44bdd4693..28035d6dd 100644 --- a/gramps/gen/soundex.py +++ b/gramps/gen/soundex.py @@ -35,8 +35,8 @@ import unicodedata # #------------------------------------------------------------------------- IGNORE = "HW~!@#$%^&*()_+=-`[]\|;:'/?.,<>\" \t\f\v" -TABLE = bytes.maketrans(b'ABCDEFGIJKLMNOPQRSTUVXYZ', - b'012301202245501262301202') +TABLE = bytes.maketrans(b'ABCDEFGIJKLMNOPQRSTUVXYZ', + b'012301202245501262301202') #------------------------------------------------------------------------- # @@ -46,8 +46,8 @@ TABLE = bytes.maketrans(b'ABCDEFGIJKLMNOPQRSTUVXYZ', def soundex(strval): "Return the soundex value to a string argument." - strval = unicodedata.normalize('NFKD', - str(strval.upper().strip())).encode('ASCII', 'ignore') + strval = unicodedata.normalize( + 'NFKD', str(strval.upper().strip())).encode('ASCII', 'ignore') if not strval: return "Z000" strval = strval.decode('ASCII', 'ignore') diff --git a/gramps/gen/test/user_test.py b/gramps/gen/test/user_test.py index 128a3c9a4..045f70394 100644 --- a/gramps/gen/test/user_test.py +++ b/gramps/gen/test/user_test.py @@ -34,9 +34,12 @@ class TestUser_prompt(unittest.TestCase): def setUp(self): self.user = user.User() - def test_returns_False(self): - assert not self.user.prompt( - TestUser.TITLE, TestUser.MSG, TestUser.ACCEPT, TestUser.REJECT) + def test_not_implemented(self): + self.assertRaises(NotImplementedError, self.user.prompt, + TestUser.TITLE, + TestUser.MSG, + TestUser.ACCEPT, + TestUser.REJECT) if __name__ == "__main__": unittest.main() diff --git a/gramps/gen/updatecallback.py b/gramps/gen/updatecallback.py index 3853b1862..71a27045f 100644 --- a/gramps/gen/updatecallback.py +++ b/gramps/gen/updatecallback.py @@ -35,6 +35,7 @@ import time import collections import logging _LOG = logging.getLogger(".gen") + #------------------------------------------------------------------------- # # Callback updater @@ -53,38 +54,54 @@ class UpdateCallback(object): :param interval: number of seconds at most between the updates :type interval: int """ - if isinstance(callback, collections.Callable): # callback is really callable + if isinstance(callback, collections.Callable): + # callback is really callable self.update = self.update_real self.callback = callback self.interval = interval - self.reset() else: self.update = self.update_empty + self.count = 0 + self.oldval = 0 + self.oldtime = 0 self.text = "" + self.total = 1 def reset(self, text=""): + """ + Reset the count to zero. + """ self.count = 0 self.oldval = 0 self.oldtime = 0 self.text = text def set_total(self, total): + """ + Set the total. + """ self.total = total if self.total == 0: _LOG.warning('UpdateCallback with total == 0 created') self.total = 1 def update_empty(self, count=None): + """ + Dummy update used when no callback is specified. + """ pass def update_real(self, count=None): + """ + Called when the count is updated. + """ self.count += 1 if not count: count = self.count newval = int(100 * count/self.total) newtime = time.time() - time_has_come = self.interval and (newtime-self.oldtime>self.interval) - value_changed = newval!=self.oldval + time_has_come = self.interval and (newtime-self.oldtime > self.interval) + value_changed = newval != self.oldval if value_changed or time_has_come: if self.text: self.callback(newval, text=self.text) diff --git a/gramps/gen/user.py b/gramps/gen/user.py index e1c8c74aa..3219fc9b6 100644 --- a/gramps/gen/user.py +++ b/gramps/gen/user.py @@ -18,12 +18,13 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # -import sys -from contextlib import contextmanager - """ The User class provides basic interaction with the user. """ + +import sys +from contextlib import contextmanager + class User(): """ This class provides a means to interact with the user in an abstract way. @@ -53,7 +54,7 @@ class User(): :type steps: int :returns: none """ - pass + raise NotImplementedError def step_progress(self): """ @@ -61,7 +62,7 @@ class User(): Don't use this method directly, use progress instead. """ - pass + raise NotImplementedError def callback(self, percentage, text=None): """ @@ -87,7 +88,7 @@ class User(): Don't use this method directly, use progress instead. """ - pass + raise NotImplementedError # Context-manager wrapper of the begin/step/end_progress above @contextmanager @@ -130,7 +131,7 @@ class User(): :returns: the user's answer to the question :rtype: bool """ - return False + raise NotImplementedError def warn(self, title, warning=""): """ @@ -142,7 +143,7 @@ class User(): :type warning: str :returns: none """ - pass + raise NotImplementedError def notify_error(self, title, error=""): """ @@ -154,7 +155,7 @@ class User(): :type error: str :returns: none """ - pass + raise NotImplementedError def notify_db_error(self, error): """ @@ -164,10 +165,10 @@ class User(): :type error: str :returns: none """ - pass + raise NotImplementedError def info(self, msg1, infotext, parent=None, monospaced=False): """ Displays information to the user """ - pass + raise NotImplementedError