* src/RelLib/__init__.py: More breaking up in RelLib;
* src/RelLib/Makefile.am: Ship new files; * src/RelLib/ various files: add more files. svn: r5593
This commit is contained in:
200
src/RelLib/_BaseObject.py
Normal file
200
src/RelLib/_BaseObject.py
Normal file
@@ -0,0 +1,200 @@
|
||||
#
|
||||
# Gramps - a GTK+/GNOME based genealogy program
|
||||
#
|
||||
# Copyright (C) 2000-2005 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
|
||||
# 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
|
||||
#
|
||||
#-------------------------------------------------------------------------
|
||||
class BaseObject:
|
||||
"""
|
||||
The BaseObject is the base class for all data objects in GRAMPS,
|
||||
whether primary or not. Its main goal is to provide common capabilites
|
||||
to all objects, such as searching through all available information.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
"""
|
||||
Initialize a BaseObject.
|
||||
"""
|
||||
pass
|
||||
|
||||
def matches_string(self,pattern,case_sensitive=False):
|
||||
"""
|
||||
Returns True if any text data in the object or any of it's child
|
||||
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():
|
||||
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():
|
||||
if obj.matches_string(pattern,case_sensitive):
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
def matches_regexp(self,pattern,case_sensitive=False):
|
||||
"""
|
||||
Returns True if any text data in the object or any of it's child
|
||||
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:
|
||||
pattern_obj = re.compile(pattern,re.IGNORECASE)
|
||||
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():
|
||||
if obj.matches_regexp(pattern,case_sensitive):
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
def get_text_data_list(self):
|
||||
"""
|
||||
Returns the list of all textual attributes of the object.
|
||||
|
||||
@return: Returns the list of all textual attributes of the object.
|
||||
@rtype: list
|
||||
"""
|
||||
return []
|
||||
|
||||
def get_text_data_child_list(self):
|
||||
"""
|
||||
Returns the list of child objects that may carry textual data.
|
||||
|
||||
@return: Returns the list of child objects that may carry textual data.
|
||||
@rtype: list
|
||||
"""
|
||||
return []
|
||||
|
||||
def get_referenced_handles(self):
|
||||
"""
|
||||
Returns the list of (classname,handle) tuples for all directly
|
||||
referenced primary objects.
|
||||
|
||||
@return: Returns the list of (classname,handle) tuples for referenced objects.
|
||||
@rtype: list
|
||||
"""
|
||||
return []
|
||||
|
||||
def get_handle_referents(self):
|
||||
"""
|
||||
Returns the list of child objects which may, directly or through
|
||||
their children, reference primary objects.
|
||||
|
||||
@return: Returns the list of objects refereincing primary objects.
|
||||
@rtype: list
|
||||
"""
|
||||
return []
|
||||
|
||||
def get_referenced_handles_recursively(self):
|
||||
"""
|
||||
Returns the list of (classname,handle) tuples for all referenced
|
||||
primary objects, whether directly or through child objects.
|
||||
|
||||
@return: Returns the list of (classname,handle) tuples for referenced objects.
|
||||
@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
|
||||
|
||||
class PrivacyBase:
|
||||
"""
|
||||
Base class for privacy-aware objects.
|
||||
"""
|
||||
|
||||
def __init__(self,source=None):
|
||||
"""
|
||||
Initialize a PrivacyBase. If the source is not None, then object
|
||||
is initialized from values of the source object.
|
||||
|
||||
@param source: Object used to initialize the new object
|
||||
@type source: PrivacyBase
|
||||
"""
|
||||
|
||||
if source:
|
||||
self.private = source.private
|
||||
else:
|
||||
self.private = False
|
||||
|
||||
def set_privacy(self,val):
|
||||
"""
|
||||
Sets or clears the privacy flag of the data
|
||||
|
||||
@param val: value to assign to the privacy flag. True indicates that the
|
||||
record is private, False indicates that it is public.
|
||||
@type val: bool
|
||||
"""
|
||||
self.private = val
|
||||
|
||||
def get_privacy(self):
|
||||
"""
|
||||
Returns the privacy level of the data.
|
||||
|
||||
@returns: True indicates that the record is private
|
||||
@rtype: bool
|
||||
"""
|
||||
return self.private
|
||||
Reference in New Issue
Block a user