Introducing GrampsTypeMeta in gen.lib.grampstype.GrampsType.

This way the subclassed type classes don't have to import and call init_map().


svn: r10397
This commit is contained in:
Zsolt Foldvari 2008-03-25 22:55:58 +00:00
parent 29ed9a79fe
commit 3f23e4b899
12 changed files with 50 additions and 86 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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)

View File

@ -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

View File

@ -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)

View File

@ -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)

View File

@ -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)