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

Source Code for Module gen.lib.src

  1  # 
  2  # Gramps - a GTK+/GNOME based genealogy program 
  3  # 
  4  # Copyright (C) 2000-2007  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: src.py 10103 2008-02-24 13:55:55Z acraphae $ 
 22   
 23  """ 
 24  Source object 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.primaryobj import PrimaryObject 
 40  from gen.lib.mediabase import MediaBase 
 41  from gen.lib.notebase import NoteBase 
 42  from gen.lib.reporef import RepoRef 
 43  from gen.lib.markertype import MarkerType 
 44   
 45  #------------------------------------------------------------------------- 
 46  # 
 47  # Source class 
 48  # 
 49  #------------------------------------------------------------------------- 
50 -class Source(MediaBase, NoteBase, PrimaryObject):
51 """A record of a source of information.""" 52
53 - def __init__(self):
54 """Create a new Source instance.""" 55 PrimaryObject.__init__(self) 56 MediaBase.__init__(self) 57 NoteBase.__init__(self) 58 self.marker = MarkerType() 59 self.title = "" 60 self.author = "" 61 self.pubinfo = "" 62 self.datamap = {} 63 self.abbrev = "" 64 self.reporef_list = []
65
66 - def serialize(self):
67 """ 68 Convert the object to a serialized tuple of data. 69 """ 70 return (self.handle, self.gramps_id, unicode(self.title), 71 unicode(self.author), unicode(self.pubinfo), 72 NoteBase.serialize(self), 73 MediaBase.serialize(self), unicode(self.abbrev), 74 self.change, self.datamap, 75 [rr.serialize() for rr in self.reporef_list], 76 self.marker.serialize(), self.private)
77
78 - def unserialize(self, data):
79 """ 80 Convert the data held in a tuple created by the serialize method 81 back into the data in an Event structure. 82 """ 83 (self.handle, self.gramps_id, self.title, self.author, 84 self.pubinfo, note_list, media_list, 85 self.abbrev, self.change, self.datamap, reporef_list, 86 marker, self.private) = data 87 88 self.marker = InstanceType(MarkerType) 89 self.marker.unserialize(marker) 90 NoteBase.unserialize(self, note_list) 91 MediaBase.unserialize(self, media_list) 92 self.reporef_list = [InstanceType(RepoRef).unserialize(rr) 93 for rr in reporef_list]
94
95 - def _has_handle_reference(self, classname, handle):
96 """ 97 Return True if the object has reference to a given handle of given 98 primary object type. 99 100 @param classname: The name of the primary object class. 101 @type classname: str 102 @param handle: The handle to be checked. 103 @type handle: str 104 @return: Returns whether the object has reference to this handle of 105 this object type. 106 @rtype: bool 107 """ 108 if classname == 'Repository': 109 return handle in [ref.ref for ref in self.reporef_list] 110 return False
111
112 - def _remove_handle_references(self, classname, handle_list):
113 """ 114 Remove all references in this object to object handles in the list. 115 116 @param classname: The name of the primary object class. 117 @type classname: str 118 @param handle_list: The list of handles to be removed. 119 @type handle_list: str 120 """ 121 if classname == 'Repository': 122 new_list = [ ref for ref in self.reporef_list \ 123 if ref.ref not in handle_list ] 124 self.reporef_list = new_list
125
126 - def _replace_handle_reference(self, classname, old_handle, new_handle):
127 """ 128 Replace all references to old handle with those to the new handle. 129 130 @param classname: The name of the primary object class. 131 @type classname: str 132 @param old_handle: The handle to be replaced. 133 @type old_handle: str 134 @param new_handle: The handle to replace the old one with. 135 @type new_handle: str 136 """ 137 if classname == 'Repository': 138 handle_list = [ref.ref for ref in self.reporef_list] 139 while old_handle in handle_list: 140 ix = handle_list.index(old_handle) 141 self.reporef_list[ix].ref = new_handle 142 handle_list[ix] = ''
143
144 - def get_text_data_list(self):
145 """ 146 Return the list of all textual attributes of the object. 147 148 @return: Returns the list of all textual attributes of the object. 149 @rtype: list 150 """ 151 return [self.title, self.author, self.pubinfo, self.abbrev, 152 self.gramps_id] + self.datamap.keys() + self.datamap.values()
153
154 - def get_text_data_child_list(self):
155 """ 156 Return the list of child objects that may carry textual data. 157 158 @return: Returns the list of child objects that may carry textual data. 159 @rtype: list 160 """ 161 return self.media_list + self.reporef_list
162
163 - def get_sourcref_child_list(self):
164 """ 165 Return the list of child secondary objects that may refer sources. 166 167 @return: Returns the list of child secondary child objects that may 168 refer sources. 169 @rtype: list 170 """ 171 return self.media_list
172
173 - def get_note_child_list(self):
174 """ 175 Return the list of child secondary objects that may refer notes. 176 177 @return: Returns the list of child secondary child objects that may 178 refer notes. 179 @rtype: list 180 """ 181 return self.media_list + self.reporef_list
182
183 - def get_handle_referents(self):
184 """ 185 Return the list of child objects which may, directly or through 186 their children, reference primary objects. 187 188 @return: Returns the list of objects refereincing primary objects. 189 @rtype: list 190 """ 191 return self.media_list + self.reporef_list
192
193 - def get_referenced_handles(self):
194 """ 195 Return the list of (classname, handle) tuples for all directly 196 referenced primary objects. 197 198 @return: List of (classname, handle) tuples for referenced objects. 199 @rtype: list 200 """ 201 return self.get_referenced_note_handles()
202
203 - def has_source_reference(self, src_handle) :
204 """ 205 Return True if any of the child objects has reference to this source 206 handle. 207 208 @param src_handle: The source handle to be checked. 209 @type src_handle: str 210 @return: Returns whether any of it's child objects has reference to 211 this source handle. 212 @rtype: bool 213 """ 214 for item in self.get_sourcref_child_list(): 215 if item.has_source_reference(src_handle): 216 return True 217 218 return False
219
220 - def remove_source_references(self, src_handle_list):
221 """ 222 Remove references to all source handles in the list in all child 223 objects. 224 225 @param src_handle_list: The list of source handles to be removed. 226 @type src_handle_list: list 227 """ 228 for item in self.get_sourcref_child_list(): 229 item.remove_source_references(src_handle_list)
230
231 - def replace_source_references(self, old_handle, new_handle):
232 """ 233 Replace references to source handles in the list in this object and 234 all child objects. 235 236 @param old_handle: The source handle to be replaced. 237 @type old_handle: str 238 @param new_handle: The source handle to replace the old one with. 239 @type new_handle: str 240 """ 241 for item in self.get_sourcref_child_list(): 242 item.replace_source_references(old_handle, new_handle)
243
244 - def get_data_map(self):
245 """Return the data map of attributes for the source.""" 246 return self.datamap
247
248 - def set_data_map(self, datamap):
249 """Set the data map of attributes for the source.""" 250 self.datamap = datamap
251
252 - def set_data_item(self, key, value):
253 """Set the particular data item in the attribute data map.""" 254 self.datamap[key] = value
255
256 - def set_title(self, title):
257 """ 258 Set the descriptive title of the Source object. 259 260 @param title: descriptive title to assign to the Source 261 @type title: str 262 """ 263 self.title = title
264
265 - def get_title(self):
266 """ 267 Return the descriptive title of the Place object. 268 269 @returns: Returns the descriptive title of the Place 270 @rtype: str 271 """ 272 return self.title
273
274 - def set_author(self, author):
275 """Set the author of the Source.""" 276 self.author = author
277
278 - def get_author(self):
279 """Return the author of the Source.""" 280 return self.author
281
282 - def set_publication_info(self, text):
283 """Set the publication information of the Source.""" 284 self.pubinfo = text
285
286 - def get_publication_info(self):
287 """Return the publication information of the Source.""" 288 return self.pubinfo
289
290 - def set_abbreviation(self, abbrev):
291 """Set the title abbreviation of the Source.""" 292 self.abbrev = abbrev
293
294 - def get_abbreviation(self):
295 """Return the title abbreviation of the Source.""" 296 return self.abbrev
297
298 - def add_repo_reference(self, repo_ref):
299 """ 300 Add a L{RepoRef} instance to the Source's reporef list. 301 302 @param repo_ref: L{RepoRef} instance to be added to the object's 303 reporef list. 304 @type repo_ref: L{RepoRef} 305 """ 306 self.reporef_list.append(repo_ref)
307
308 - def get_reporef_list(self):
309 """ 310 Return the list of L{RepoRef} instances associated with the Source. 311 312 @Return: list of L{RepoRef} instances associated with the Source 313 @rtype: list 314 """ 315 return self.reporef_list
316
317 - def set_reporef_list(self, reporef_list):
318 """ 319 Set the list of L{RepoRef} instances associated with the Source. 320 It replaces the previous list. 321 322 @param reporef_list: list of L{RepoRef} instances to be assigned to 323 the Source. 324 @type reporef_list: list 325 """ 326 self.reporef_list = reporef_list
327
328 - def has_repo_reference(self, repo_handle):
329 """ 330 Return True if the Source has reference to this Repository handle. 331 332 @param repo_handle: The Repository handle to be checked. 333 @type repo_handle: str 334 @return: Returns whether the Source has reference to this Repository 335 handle. 336 @rtype: bool 337 """ 338 return repo_handle in [repo_ref.ref for repo_ref in self.reporef_list]
339
340 - def remove_repo_references(self, repo_handle_list):
341 """ 342 Remove references to all Repository handles in the list. 343 344 @param repo_handle_list: The list of Repository handles to be removed. 345 @type repo_handle_list: list 346 """ 347 new_reporef_list = [ repo_ref for repo_ref in self.reporef_list \ 348 if repo_ref.ref not in repo_handle_list ] 349 self.reporef_list = new_reporef_list
350
351 - def replace_repo_references(self, old_handle, new_handle):
352 """ 353 Replace all references to old Repository handle with the new handle. 354 355 @param old_handle: The Repository handle to be replaced. 356 @type old_handle: str 357 @param new_handle: The Repository handle to replace the old one with. 358 @type new_handle: str 359 """ 360 refs_list = [ repo_ref.ref for repo_ref in self.reporef_list ] 361 n_replace = refs_list.count(old_handle) 362 for ix_replace in xrange(n_replace): 363 ix = refs_list.index(old_handle) 364 self.reporef_list[ix].ref = new_handle 365 refs_list.pop(ix)
366