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

Source Code for Module gen.lib.srcbase

  1  # 
  2  # Gramps - a GTK+/GNOME based genealogy program 
  3  # 
  4  # Copyright (C) 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: srcbase.py 10103 2008-02-24 13:55:55Z acraphae $ 
 22   
 23  """ 
 24  SourceBase class for GRAMPS. 
 25  """ 
 26   
 27  #------------------------------------------------------------------------- 
 28  # 
 29  # Python modules 
 30  # 
 31  #------------------------------------------------------------------------- 
 32  from types import InstanceType 
 33   
 34  #------------------------------------------------------------------------- 
 35  # 
 36  # GRAMPS modules 
 37  # 
 38  #------------------------------------------------------------------------- 
 39  from gen.lib.srcref import SourceRef 
 40   
 41  #------------------------------------------------------------------------- 
 42  # 
 43  # SourceBase classes 
 44  # 
 45  #------------------------------------------------------------------------- 
46 -class SourceBase:
47 """ 48 Base class for storing source references. 49 """ 50
51 - def __init__(self, source=None):
52 """ 53 Create a new SourceBase, copying from source if not None. 54 55 @param source: Object used to initialize the new object 56 @type source: SourceBase 57 """ 58 if source: 59 self.source_list = [SourceRef(sref) for sref in source.source_list] 60 else: 61 self.source_list = []
62
63 - def serialize(self):
64 """ 65 Convert the object to a serialized tuple of data. 66 """ 67 return [sref.serialize() for sref in self.source_list]
68
69 - def unserialize(self, data):
70 """ 71 Convert a serialized tuple of data to an object. 72 """ 73 self.source_list = [InstanceType(SourceRef).unserialize(item) 74 for item in data]
75
76 - def add_source_reference(self, src_ref) :
77 """ 78 Add a source reference to this object. 79 80 @param src_ref: The source reference to be added to the 81 SourceNote's list of source references. 82 @type src_ref: L{SourceRef} 83 """ 84 self.source_list.append(src_ref)
85
86 - def get_source_references(self) :
87 """ 88 Return the list of source references associated with the object. 89 90 @return: Returns the list of L{SourceRef} objects assocated with 91 the object. 92 @rtype: list 93 """ 94 return self.source_list
95
96 - def get_sourcref_child_list(self):
97 """ 98 Return the list of child secondary objects that may refer sources. 99 100 @return: Returns the list of child secondary child objects that may 101 refer sources. 102 @rtype: list 103 """ 104 return []
105
106 - def has_source_reference(self, src_handle) :
107 """ 108 Return True if the object or any of it's child objects has reference 109 to this source handle. 110 111 @param src_handle: The source handle to be checked. 112 @type src_handle: str 113 @return: Returns whether the object or any of it's child objects has 114 reference to this source handle. 115 @rtype: bool 116 """ 117 for src_ref in self.source_list: 118 # Using direct access here, not the getter method -- efficiency! 119 if src_ref.ref == src_handle: 120 return True 121 122 for item in self.get_sourcref_child_list(): 123 if item.has_source_reference(src_handle): 124 return True 125 126 return False
127
128 - def remove_source_references(self, src_handle_list):
129 """ 130 Remove references to all source handles in the list in this object 131 and all child objects. 132 133 @param src_handle_list: The list of source handles to be removed. 134 @type src_handle_list: list 135 """ 136 new_source_list = [ src_ref for src_ref in self.source_list \ 137 if src_ref.ref not in src_handle_list ] 138 self.source_list = new_source_list 139 140 for item in self.get_sourcref_child_list(): 141 item.remove_source_references(src_handle_list)
142
143 - def replace_source_references(self, old_handle, new_handle):
144 """ 145 Replace references to source handles in the list in this object and 146 all child objects. 147 148 @param old_handle: The source handle to be replaced. 149 @type old_handle: str 150 @param new_handle: The source handle to replace the old one with. 151 @type new_handle: str 152 """ 153 refs_list = [ src_ref.ref for src_ref in self.source_list ] 154 n_replace = refs_list.count(old_handle) 155 for ix_replace in xrange(n_replace): 156 ix = refs_list.index(old_handle) 157 self.source_list[ix].ref = new_handle 158 refs_list[ix] = new_handle 159 160 for item in self.get_sourcref_child_list(): 161 item.replace_source_references(old_handle, new_handle)
162
163 - def set_source_reference_list(self, src_ref_list) :
164 """ 165 Assign the passed list to the object's list of source references. 166 167 @param src_ref_list: List of source references to ba associated 168 with the object 169 @type src_ref_list: list of L{SourceRef} instances 170 """ 171 self.source_list = src_ref_list
172