2005-12-21 02:18:18 +05:30
|
|
|
#
|
|
|
|
# Gramps - a GTK+/GNOME based genealogy program
|
|
|
|
#
|
2006-02-04 03:33:53 +05:30
|
|
|
# Copyright (C) 2000-2006 Donald N. Allingham
|
2005-12-21 02:18:18 +05:30
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
#
|
|
|
|
|
|
|
|
# $Id$
|
|
|
|
|
|
|
|
"""
|
|
|
|
Base Object class for GRAMPS
|
|
|
|
"""
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# standard python modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
import re
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Base Object
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2009-05-21 22:49:50 +05:30
|
|
|
class BaseObject(object):
|
2005-12-21 02:18:18 +05:30
|
|
|
"""
|
|
|
|
The BaseObject is the base class for all data objects in GRAMPS,
|
2008-02-24 19:25:55 +05:30
|
|
|
whether primary or not.
|
|
|
|
|
|
|
|
Its main goal is to provide common capabilites to all objects, such as
|
|
|
|
searching through all available information.
|
2005-12-21 02:18:18 +05:30
|
|
|
"""
|
|
|
|
|
2006-02-04 03:33:53 +05:30
|
|
|
def serialize(self):
|
2007-01-08 07:19:33 +05:30
|
|
|
"""
|
2008-02-24 19:25:55 +05:30
|
|
|
Convert the object to a serialized tuple of data.
|
2007-01-08 07:19:33 +05:30
|
|
|
"""
|
2006-02-04 03:33:53 +05:30
|
|
|
assert False, "Needs to be overridden in the derived class"
|
|
|
|
|
2007-01-08 07:19:33 +05:30
|
|
|
def unserialize(self, data):
|
|
|
|
"""
|
2008-02-24 19:25:55 +05:30
|
|
|
Convert a serialized tuple of data to an object.
|
2007-01-08 07:19:33 +05:30
|
|
|
"""
|
2006-02-04 03:33:53 +05:30
|
|
|
assert False, "Needs to be overridden in the derived class"
|
|
|
|
return self
|
2005-12-21 02:18:18 +05:30
|
|
|
|
2007-01-08 07:19:33 +05:30
|
|
|
def matches_string(self, pattern, case_sensitive=False):
|
2005-12-21 02:18:18 +05:30
|
|
|
"""
|
2008-02-24 19:25:55 +05:30
|
|
|
Return True if any text data in the object or any of it's child
|
2005-12-21 02:18:18 +05:30
|
|
|
objects matches a given pattern.
|
|
|
|
|
|
|
|
@param pattern: The pattern to match.
|
|
|
|
@type pattern: str
|
|
|
|
@param case_sensitive: Whether the match is case-sensitive.
|
|
|
|
@type case_sensitive: bool
|
|
|
|
@return: Returns whether any text data in the object or any of it's child objects matches a given pattern.
|
|
|
|
@rtype: bool
|
|
|
|
"""
|
|
|
|
# Run through its own items
|
|
|
|
patern_upper = pattern.upper()
|
|
|
|
for item in self.get_text_data_list():
|
2008-11-22 21:05:14 +05:30
|
|
|
# Some items are strings, which will fail in item.upper(), and some items are unicode.
|
|
|
|
# Convert all items to unicode and the item.upper().find(patern_upper) will work OK.
|
|
|
|
item = unicode(item)
|
2005-12-21 02:18:18 +05:30
|
|
|
if not item:
|
|
|
|
continue
|
|
|
|
if case_sensitive:
|
|
|
|
if item.find(pattern) != -1:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
if item.upper().find(patern_upper) != -1:
|
|
|
|
return True
|
|
|
|
|
|
|
|
# Run through child objects
|
|
|
|
for obj in self.get_text_data_child_list():
|
2007-01-08 07:19:33 +05:30
|
|
|
if obj.matches_string(pattern, case_sensitive):
|
2005-12-21 02:18:18 +05:30
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
2007-01-08 07:19:33 +05:30
|
|
|
def matches_regexp(self, pattern, case_sensitive=False):
|
2005-12-21 02:18:18 +05:30
|
|
|
"""
|
2008-02-24 19:25:55 +05:30
|
|
|
Return True if any text data in the object or any of it's child
|
2005-12-21 02:18:18 +05:30
|
|
|
objects matches a given regular expression.
|
|
|
|
|
|
|
|
@param pattern: The pattern to match.
|
|
|
|
@type pattern: str
|
|
|
|
@return: Returns whether any text data in the object or any of it's child objects matches a given regexp.
|
|
|
|
@rtype: bool
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Run through its own items
|
|
|
|
if case_sensitive:
|
|
|
|
pattern_obj = re.compile(pattern)
|
|
|
|
else:
|
2007-01-08 07:19:33 +05:30
|
|
|
pattern_obj = re.compile(pattern, re.IGNORECASE)
|
2005-12-21 02:18:18 +05:30
|
|
|
for item in self.get_text_data_list():
|
|
|
|
if item and pattern_obj.match(item):
|
|
|
|
return True
|
|
|
|
|
|
|
|
# Run through child objects
|
|
|
|
for obj in self.get_text_data_child_list():
|
2007-01-08 07:19:33 +05:30
|
|
|
if obj.matches_regexp(pattern, case_sensitive):
|
2005-12-21 02:18:18 +05:30
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
def get_text_data_list(self):
|
|
|
|
"""
|
2008-02-24 19:25:55 +05:30
|
|
|
Return the list of all textual attributes of the object.
|
2005-12-21 02:18:18 +05:30
|
|
|
|
|
|
|
@return: Returns the list of all textual attributes of the object.
|
|
|
|
@rtype: list
|
|
|
|
"""
|
|
|
|
return []
|
|
|
|
|
|
|
|
def get_text_data_child_list(self):
|
|
|
|
"""
|
2008-02-24 19:25:55 +05:30
|
|
|
Return the list of child objects that may carry textual data.
|
2005-12-21 02:18:18 +05:30
|
|
|
|
|
|
|
@return: Returns the list of child objects that may carry textual data.
|
|
|
|
@rtype: list
|
|
|
|
"""
|
|
|
|
return []
|
|
|
|
|
|
|
|
def get_referenced_handles(self):
|
|
|
|
"""
|
2008-02-24 19:25:55 +05:30
|
|
|
Return the list of (classname, handle) tuples for all directly
|
2005-12-21 02:18:18 +05:30
|
|
|
referenced primary objects.
|
|
|
|
|
2008-02-24 19:25:55 +05:30
|
|
|
@return: Returns the list of (classname, handle) tuples for referenced objects.
|
2005-12-21 02:18:18 +05:30
|
|
|
@rtype: list
|
|
|
|
"""
|
|
|
|
return []
|
|
|
|
|
|
|
|
def get_handle_referents(self):
|
|
|
|
"""
|
2008-02-24 19:25:55 +05:30
|
|
|
Return the list of child objects which may, directly or through
|
2005-12-21 02:18:18 +05:30
|
|
|
their children, reference primary objects.
|
|
|
|
|
|
|
|
@return: Returns the list of objects refereincing primary objects.
|
|
|
|
@rtype: list
|
|
|
|
"""
|
|
|
|
return []
|
|
|
|
|
|
|
|
def get_referenced_handles_recursively(self):
|
|
|
|
"""
|
2008-02-24 19:25:55 +05:30
|
|
|
Return the list of (classname, handle) tuples for all referenced
|
2005-12-21 02:18:18 +05:30
|
|
|
primary objects, whether directly or through child objects.
|
|
|
|
|
2008-02-24 19:25:55 +05:30
|
|
|
@return: Returns the list of (classname, handle) tuples for referenced objects.
|
2005-12-21 02:18:18 +05:30
|
|
|
@rtype: list
|
|
|
|
"""
|
|
|
|
ret = self.get_referenced_handles()
|
|
|
|
|
|
|
|
# Run through child objects
|
|
|
|
for obj in self.get_handle_referents():
|
|
|
|
ret += obj.get_referenced_handles_recursively()
|
|
|
|
return ret
|