Package gen :: Package lib :: Module baseobj
[frames] | no frames]

Source Code for Module gen.lib.baseobj

  1  # 
  2  # Gramps - a GTK+/GNOME based genealogy program 
  3  # 
  4  # Copyright (C) 2000-2006  Donald N. Allingham 
  5  # 
  6  # This program is free software; you can redistribute it and/or modify 
  7  # it under the terms of the GNU General Public License as published by 
  8  # the Free Software Foundation; either version 2 of the License, or 
  9  # (at your option) any later version. 
 10  # 
 11  # This program is distributed in the hope that it will be useful, 
 12  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 14  # GNU General Public License for more details. 
 15  # 
 16  # You should have received a copy of the GNU General Public License 
 17  # along with this program; if not, write to the Free Software 
 18  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 19  # 
 20   
 21  # $Id: baseobj.py 10103 2008-02-24 13:55:55Z acraphae $ 
 22   
 23  """ 
 24  Base Object class for GRAMPS 
 25  """ 
 26   
 27  #------------------------------------------------------------------------- 
 28  # 
 29  # standard python modules 
 30  # 
 31  #------------------------------------------------------------------------- 
 32  import re 
 33   
 34  #------------------------------------------------------------------------- 
 35  # 
 36  # Base Object 
 37  # 
 38  #------------------------------------------------------------------------- 
39 -class BaseObject:
40 """ 41 The BaseObject is the base class for all data objects in GRAMPS, 42 whether primary or not. 43 44 Its main goal is to provide common capabilites to all objects, such as 45 searching through all available information. 46 """ 47
48 - def serialize(self):
49 """ 50 Convert the object to a serialized tuple of data. 51 """ 52 assert False, "Needs to be overridden in the derived class"
53
54 - def unserialize(self, data):
55 """ 56 Convert a serialized tuple of data to an object. 57 """ 58 assert False, "Needs to be overridden in the derived class" 59 return self
60
61 - def matches_string(self, pattern, case_sensitive=False):
62 """ 63 Return True if any text data in the object or any of it's child 64 objects matches a given pattern. 65 66 @param pattern: The pattern to match. 67 @type pattern: str 68 @param case_sensitive: Whether the match is case-sensitive. 69 @type case_sensitive: bool 70 @return: Returns whether any text data in the object or any of it's child objects matches a given pattern. 71 @rtype: bool 72 """ 73 # Run through its own items 74 patern_upper = pattern.upper() 75 for item in self.get_text_data_list(): 76 if not item: 77 continue 78 if case_sensitive: 79 if item.find(pattern) != -1: 80 return True 81 else: 82 if item.upper().find(patern_upper) != -1: 83 return True 84 85 # Run through child objects 86 for obj in self.get_text_data_child_list(): 87 if obj.matches_string(pattern, case_sensitive): 88 return True 89 90 return False
91
92 - def matches_regexp(self, pattern, case_sensitive=False):
93 """ 94 Return True if any text data in the object or any of it's child 95 objects matches a given regular expression. 96 97 @param pattern: The pattern to match. 98 @type pattern: str 99 @return: Returns whether any text data in the object or any of it's child objects matches a given regexp. 100 @rtype: bool 101 """ 102 103 # Run through its own items 104 if case_sensitive: 105 pattern_obj = re.compile(pattern) 106 else: 107 pattern_obj = re.compile(pattern, re.IGNORECASE) 108 for item in self.get_text_data_list(): 109 if item and pattern_obj.match(item): 110 return True 111 112 # Run through child objects 113 for obj in self.get_text_data_child_list(): 114 if obj.matches_regexp(pattern, case_sensitive): 115 return True 116 117 return False
118
119 - def get_text_data_list(self):
120 """ 121 Return the list of all textual attributes of the object. 122 123 @return: Returns the list of all textual attributes of the object. 124 @rtype: list 125 """ 126 return []
127
128 - def get_text_data_child_list(self):
129 """ 130 Return the list of child objects that may carry textual data. 131 132 @return: Returns the list of child objects that may carry textual data. 133 @rtype: list 134 """ 135 return []
136
137 - def get_referenced_handles(self):
138 """ 139 Return the list of (classname, handle) tuples for all directly 140 referenced primary objects. 141 142 @return: Returns the list of (classname, handle) tuples for referenced objects. 143 @rtype: list 144 """ 145 return []
146
147 - def get_handle_referents(self):
148 """ 149 Return the list of child objects which may, directly or through 150 their children, reference primary objects. 151 152 @return: Returns the list of objects refereincing primary objects. 153 @rtype: list 154 """ 155 return []
156
158 """ 159 Return the list of (classname, handle) tuples for all referenced 160 primary objects, whether directly or through child objects. 161 162 @return: Returns the list of (classname, handle) tuples for referenced objects. 163 @rtype: list 164 """ 165 ret = self.get_referenced_handles() 166 167 # Run through child objects 168 for obj in self.get_handle_referents(): 169 ret += obj.get_referenced_handles_recursively() 170 return ret
171