From 3f23e4b899d8cc2ad1f86e1367c313a1dc243b42 Mon Sep 17 00:00:00 2001 From: Zsolt Foldvari Date: Tue, 25 Mar 2008 22:55:58 +0000 Subject: [PATCH] Introducing GrampsTypeMeta in gen.lib.grampstype.GrampsType. This way the subclassed type classes don't have to import and call init_map(). svn: r10397 --- src/gen/lib/attrtype.py | 7 +------ src/gen/lib/childreftype.py | 9 ++------- src/gen/lib/eventroletype.py | 9 ++------- src/gen/lib/eventtype.py | 9 ++------- src/gen/lib/familyreltype.py | 9 ++------- src/gen/lib/grampstype.py | 36 +++++++++++++++++++++++++++--------- src/gen/lib/markertype.py | 9 ++------- src/gen/lib/nametype.py | 9 ++------- src/gen/lib/notetype.py | 12 ++++-------- src/gen/lib/repotype.py | 9 ++------- src/gen/lib/srcmediatype.py | 9 ++------- src/gen/lib/urltype.py | 9 ++------- 12 files changed, 50 insertions(+), 86 deletions(-) diff --git a/src/gen/lib/attrtype.py b/src/gen/lib/attrtype.py index 2ec7c591a..2062d80a2 100644 --- a/src/gen/lib/attrtype.py +++ b/src/gen/lib/attrtype.py @@ -1,7 +1,7 @@ # # Gramps - a GTK+/GNOME based genealogy program # -# Copyright (C) 2000-2006 Donald N. Allingham +# Copyright (C) 2000-2007 Donald N. Allingham # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -76,10 +76,5 @@ class AttributeType(GrampsType): (WITNESS , _("Witness"), "Witness"), ] - _I2SMAP = init_map(_DATAMAP, 0, 1) - _S2IMAP = init_map(_DATAMAP, 1, 0) - _I2EMAP = init_map(_DATAMAP, 0, 2) - _E2IMAP = init_map(_DATAMAP, 2, 0) - def __init__(self, value=None): GrampsType.__init__(self, value) diff --git a/src/gen/lib/childreftype.py b/src/gen/lib/childreftype.py index 394777cc6..28e962325 100644 --- a/src/gen/lib/childreftype.py +++ b/src/gen/lib/childreftype.py @@ -1,7 +1,7 @@ # # Gramps - a GTK+/GNOME based genealogy program # -# Copyright (C) 2000-2006 Donald N. Allingham +# Copyright (C) 2000-2007 Donald N. Allingham # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -35,7 +35,7 @@ from gettext import gettext as _ # GRAMPS modules # #------------------------------------------------------------------------- -from gen.lib.grampstype import GrampsType, init_map +from gen.lib.grampstype import GrampsType class ChildRefType(GrampsType): """ @@ -65,11 +65,6 @@ class ChildRefType(GrampsType): (CUSTOM, _("Custom"), "Custom"), ] - _I2SMAP = init_map(_DATAMAP, 0, 1) - _S2IMAP = init_map(_DATAMAP, 1, 0) - _I2EMAP = init_map(_DATAMAP, 0, 2) - _E2IMAP = init_map(_DATAMAP, 2, 0) - def __init__(self, value=None): GrampsType.__init__(self, value) diff --git a/src/gen/lib/eventroletype.py b/src/gen/lib/eventroletype.py index 899f40a62..d0eda4031 100644 --- a/src/gen/lib/eventroletype.py +++ b/src/gen/lib/eventroletype.py @@ -1,7 +1,7 @@ # # Gramps - a GTK+/GNOME based genealogy program # -# Copyright (C) 2000-2006 Donald N. Allingham +# Copyright (C) 2000-2007 Donald N. Allingham # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -35,7 +35,7 @@ from gettext import gettext as _ # GRAMPS modules # #------------------------------------------------------------------------- -from gen.lib.grampstype import GrampsType, init_map +from gen.lib.grampstype import GrampsType class EventRoleType(GrampsType): @@ -66,10 +66,5 @@ class EventRoleType(GrampsType): (FAMILY, _("Family"), "Family"), ] - _I2SMAP = init_map(_DATAMAP, 0, 1) - _S2IMAP = init_map(_DATAMAP, 1, 0) - _I2EMAP = init_map(_DATAMAP, 0, 2) - _E2IMAP = init_map(_DATAMAP, 2, 0) - def __init__(self, value=None): GrampsType.__init__(self, value) diff --git a/src/gen/lib/eventtype.py b/src/gen/lib/eventtype.py index 91b580508..18d033c8f 100644 --- a/src/gen/lib/eventtype.py +++ b/src/gen/lib/eventtype.py @@ -1,7 +1,7 @@ # # Gramps - a GTK+/GNOME based genealogy program # -# Copyright (C) 2000-2006 Donald N. Allingham +# Copyright (C) 2000-2007 Donald N. Allingham # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -35,7 +35,7 @@ from gettext import gettext as _ # GRAMPS modules # #------------------------------------------------------------------------- -from gen.lib.grampstype import GrampsType, init_map +from gen.lib.grampstype import GrampsType class EventType(GrampsType): """ @@ -140,11 +140,6 @@ class EventType(GrampsType): (MARR_ALT , _("Alternate Marriage"), "Alternate Marriage"), ] - _I2SMAP = init_map(_DATAMAP, 0, 1) - _S2IMAP = init_map(_DATAMAP, 1, 0) - _I2EMAP = init_map(_DATAMAP, 0, 2) - _E2IMAP = init_map(_DATAMAP, 2, 0) - def __init__(self, value=None): GrampsType.__init__(self, value) diff --git a/src/gen/lib/familyreltype.py b/src/gen/lib/familyreltype.py index 9c6ba3b0f..d4e3f9866 100644 --- a/src/gen/lib/familyreltype.py +++ b/src/gen/lib/familyreltype.py @@ -1,7 +1,7 @@ # # Gramps - a GTK+/GNOME based genealogy program # -# Copyright (C) 2000-2006 Donald N. Allingham +# Copyright (C) 2000-2007 Donald N. Allingham # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -34,7 +34,7 @@ from gettext import gettext as _ # GRAMPS modules # #------------------------------------------------------------------------- -from gen.lib.grampstype import GrampsType, init_map +from gen.lib.grampstype import GrampsType class FamilyRelType(GrampsType): @@ -55,11 +55,6 @@ class FamilyRelType(GrampsType): (MARRIED, _("Married"), "Married"), ] - _I2SMAP = init_map(_DATAMAP, 0, 1) - _S2IMAP = init_map(_DATAMAP, 1, 0) - _I2EMAP = init_map(_DATAMAP, 0, 2) - _E2IMAP = init_map(_DATAMAP, 2, 0) - def __init__(self, value=None): if value == None: value = self.UNKNOWN diff --git a/src/gen/lib/grampstype.py b/src/gen/lib/grampstype.py index c48be9cb9..0450935d8 100644 --- a/src/gen/lib/grampstype.py +++ b/src/gen/lib/grampstype.py @@ -1,7 +1,7 @@ # # Gramps - a GTK+/GNOME based genealogy program # -# Copyright (C) 2000-2006 Donald N. Allingham +# Copyright (C) 2000-2007 Donald N. Allingham # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -31,7 +31,7 @@ Base type for all gramps types. #------------------------------------------------------------------------ from gettext import gettext as _ -def init_map(data, key_col, data_col): +def __init_map(data, key_col, data_col): """ Initialize the map, building a new map from the specified columns. """ @@ -40,18 +40,36 @@ def init_map(data, key_col, data_col): new_data[item[key_col]] = item[data_col] return new_data -class GrampsType: - +class GrampsTypeMeta(type): + """Metaclass for L{GrampsType}. + + The only thing this metaclass does is calling __class_init__ class method, + in order to create the class specific integer/string maps. + + """ + def __init__(mcs, name, bases, namespace): + type.__init__(mcs, name, bases, namespace) + mcs.__class_init__(namespace) + +class GrampsType(object): + """Base class for all Gramps object types.""" + (VALUE_POS, STRING_POS) = range(2) + _CUSTOM = 0 _DEFAULT = 0 _DATAMAP = [] - _I2SMAP = init_map(_DATAMAP, 0, 1) - _S2IMAP = init_map(_DATAMAP, 1, 0) - _I2EMAP = init_map(_DATAMAP, 0, 2) - _E2IMAP = init_map(_DATAMAP, 2, 0) - + __metaclass__ = GrampsTypeMeta + + def __class_init__(cls, namespace): + cls._I2SMAP = __init_map(cls._DATAMAP, 0, 1) + cls._S2IMAP = __init_map(cls._DATAMAP, 1, 0) + cls._I2EMAP = __init_map(cls._DATAMAP, 0, 2) + cls._E2IMAP = __init_map(cls._DATAMAP, 2, 0) + + __class_init__ = classmethod(__class_init__) + def __init__(self, value=None): """ Create a new type, initialize the value from one of several possible diff --git a/src/gen/lib/markertype.py b/src/gen/lib/markertype.py index e4dc3be4e..16113e453 100644 --- a/src/gen/lib/markertype.py +++ b/src/gen/lib/markertype.py @@ -1,7 +1,7 @@ # # Gramps - a GTK+/GNOME based genealogy program # -# Copyright (C) 2000-2006 Donald N. Allingham +# Copyright (C) 2000-2007 Donald N. Allingham # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -35,7 +35,7 @@ from gettext import gettext as _ # GRAMPS modules # #------------------------------------------------------------------------- -from gen.lib.grampstype import GrampsType, init_map +from gen.lib.grampstype import GrampsType class MarkerType(GrampsType): """ @@ -57,11 +57,6 @@ class MarkerType(GrampsType): (TODO_TYPE, _("ToDo"), "ToDo"), ] - _I2SMAP = init_map(_DATAMAP, 0, 1) - _S2IMAP = init_map(_DATAMAP, 1, 0) - _I2EMAP = init_map(_DATAMAP, 0, 2) - _E2IMAP = init_map(_DATAMAP, 2, 0) - def __init__(self, value=None): GrampsType.__init__(self, value) diff --git a/src/gen/lib/nametype.py b/src/gen/lib/nametype.py index e8b20b6b2..63677d5c5 100644 --- a/src/gen/lib/nametype.py +++ b/src/gen/lib/nametype.py @@ -1,7 +1,7 @@ # # Gramps - a GTK+/GNOME based genealogy program # -# Copyright (C) 2000-2006 Donald N. Allingham +# Copyright (C) 2000-2007 Donald N. Allingham # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -36,7 +36,7 @@ from gettext import gettext as _ # GRAMPS modules # #------------------------------------------------------------------------- -from gen.lib.grampstype import GrampsType, init_map +from gen.lib.grampstype import GrampsType class NameType(GrampsType): @@ -57,11 +57,6 @@ class NameType(GrampsType): (MARRIED, _("Married Name"), "Married Name"), ] - _I2SMAP = init_map(_DATAMAP, 0, 1) - _S2IMAP = init_map(_DATAMAP, 1, 0) - _I2EMAP = init_map(_DATAMAP, 0, 2) - _E2IMAP = init_map(_DATAMAP, 2, 0) - def __init__(self, value=None): GrampsType.__init__(self, value) diff --git a/src/gen/lib/notetype.py b/src/gen/lib/notetype.py index bd467c11f..970087318 100644 --- a/src/gen/lib/notetype.py +++ b/src/gen/lib/notetype.py @@ -36,7 +36,7 @@ from gettext import gettext as _ # GRAMPS modules # #------------------------------------------------------------------------- -from gen.lib.grampstype import GrampsType, init_map +from gen.lib.grampstype import GrampsType class NoteType(GrampsType): @@ -106,11 +106,6 @@ class NoteType(GrampsType): _DATAMAP = _DATAMAPREAL + _DATAMAPIGNORE - _I2SMAP = init_map(_DATAMAP, 0, 1) - _S2IMAP = init_map(_DATAMAP, 1, 0) - _I2EMAP = init_map(_DATAMAP, 0, 2) - _E2IMAP = init_map(_DATAMAP, 2, 0) - def __init__(self, value=None): GrampsType.__init__(self, value) @@ -126,12 +121,13 @@ class NoteType(GrampsType): @returns: list of integers corresponding with the types to ignore when showing a list of different NoteType's @rtype: list + """ ignlist = [x[0] for x in self._DATAMAPIGNORE] if exception: - for type in exception : + for type_ in exception: try: - del ignlist[ignlist.index(type)] + del ignlist[ignlist.index(type_)] except ValueError: pass return ignlist diff --git a/src/gen/lib/repotype.py b/src/gen/lib/repotype.py index 1e14b8e3c..332a098ad 100644 --- a/src/gen/lib/repotype.py +++ b/src/gen/lib/repotype.py @@ -1,7 +1,7 @@ # # Gramps - a GTK+/GNOME based genealogy program # -# Copyright (C) 2000-2006 Donald N. Allingham +# Copyright (C) 2000-2007 Donald N. Allingham # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -36,7 +36,7 @@ from gettext import gettext as _ # GRAMPS modules # #------------------------------------------------------------------------- -from gen.lib.grampstype import GrampsType, init_map +from gen.lib.grampstype import GrampsType class RepositoryType(GrampsType): @@ -69,11 +69,6 @@ class RepositoryType(GrampsType): (SAFE, _("Safe"), "Safe"), ] - _I2SMAP = init_map(_DATAMAP, 0, 1) - _S2IMAP = init_map(_DATAMAP, 1, 0) - _I2EMAP = init_map(_DATAMAP, 0, 2) - _E2IMAP = init_map(_DATAMAP, 2, 0) - def __init__(self, value=None): GrampsType.__init__(self, value) diff --git a/src/gen/lib/srcmediatype.py b/src/gen/lib/srcmediatype.py index dd31c9c66..3f85b8054 100644 --- a/src/gen/lib/srcmediatype.py +++ b/src/gen/lib/srcmediatype.py @@ -1,7 +1,7 @@ # # Gramps - a GTK+/GNOME based genealogy program # -# Copyright (C) 2000-2006 Donald N. Allingham +# Copyright (C) 2000-2007 Donald N. Allingham # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -35,7 +35,7 @@ from gettext import gettext as _ # GRAMPS modules # #------------------------------------------------------------------------- -from gen.lib.grampstype import GrampsType, init_map +from gen.lib.grampstype import GrampsType class SourceMediaType(GrampsType): @@ -76,10 +76,5 @@ class SourceMediaType(GrampsType): (VIDEO, _("Video"), "Video"), ] - _I2SMAP = init_map(_DATAMAP, 0, 1) - _S2IMAP = init_map(_DATAMAP, 1, 0) - _I2EMAP = init_map(_DATAMAP, 0, 2) - _E2IMAP = init_map(_DATAMAP, 2, 0) - def __init__(self, value=None): GrampsType.__init__(self, value) diff --git a/src/gen/lib/urltype.py b/src/gen/lib/urltype.py index 946b33b59..e3e753962 100644 --- a/src/gen/lib/urltype.py +++ b/src/gen/lib/urltype.py @@ -1,7 +1,7 @@ # # Gramps - a GTK+/GNOME based genealogy program # -# Copyright (C) 2000-2006 Donald N. Allingham +# Copyright (C) 2000-2007 Donald N. Allingham # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -36,7 +36,7 @@ from gettext import gettext as _ # GRAMPS modules # #------------------------------------------------------------------------- -from gen.lib.grampstype import GrampsType, init_map +from gen.lib.grampstype import GrampsType class UrlType(GrampsType): @@ -59,11 +59,6 @@ class UrlType(GrampsType): (WEB_FTP, _("FTP"), "FTP"), ] - _I2SMAP = init_map(_DATAMAP, 0, 1) - _S2IMAP = init_map(_DATAMAP, 1, 0) - _I2EMAP = init_map(_DATAMAP, 0, 2) - _E2IMAP = init_map(_DATAMAP, 2, 0) - def __init__(self, value=None): GrampsType.__init__(self, value)