1. Implement iter_<object> methods in proxybase.
2. Change include_object to include_media_object to avoid confusion 3. Make _validated_id_prefix method in read.py a staticmethod 4. Use new iter_<objects> methods in Records.py, StatsgGramplet.py, GivenNameGramplet.py, ExtractCity.py and _IsSiblingOfFilterMatch.py as examples svn: r13151
This commit is contained in:
parent
c22e47eba1
commit
3564b04584
@ -52,8 +52,7 @@ class IsSiblingOfFilterMatch(MatchesFilter):
|
||||
self.map = {}
|
||||
filt = MatchesFilter(self.list)
|
||||
filt.prepare(db)
|
||||
for person_handle in db.iter_person_handles():
|
||||
person = db.get_person_from_handle( person_handle)
|
||||
for person in db.iter_people():
|
||||
if filt.apply (db, person):
|
||||
self.init_list (person)
|
||||
filt.reset()
|
||||
|
@ -850,7 +850,8 @@ class GrampsDbRead(GrampsDbBase, Callback):
|
||||
person = self.get_person_from_gramps_id(min(the_ids))
|
||||
return person
|
||||
|
||||
def _validated_id_prefix(self, val, default):
|
||||
@staticmethod
|
||||
def _validated_id_prefix(val, default):
|
||||
if isinstance(val, basestring) and val:
|
||||
try:
|
||||
str_ = val % 1
|
||||
@ -861,7 +862,7 @@ class GrampsDbRead(GrampsDbBase, Callback):
|
||||
else:
|
||||
prefix_var = default+"%04d" # not a string or empty string
|
||||
return prefix_var
|
||||
|
||||
|
||||
def set_person_id_prefix(self, val):
|
||||
"""
|
||||
Set the naming template for GRAMPS Person ID values.
|
||||
@ -871,7 +872,7 @@ class GrampsDbRead(GrampsDbBase, Callback):
|
||||
such as I%d or I%04d.
|
||||
"""
|
||||
self.person_prefix = self._validated_id_prefix(val, "I")
|
||||
|
||||
|
||||
def set_source_id_prefix(self, val):
|
||||
"""
|
||||
Set the naming template for GRAMPS Source ID values.
|
||||
@ -1558,3 +1559,8 @@ class GrampsDbRead(GrampsDbBase, Callback):
|
||||
"""
|
||||
return self._bm_changes > 0
|
||||
|
||||
if __name__ == '__main__':
|
||||
x = GrampsDbRead()
|
||||
x.person_prefix = 'foo %s bar'
|
||||
print x.person_prefix
|
||||
|
||||
|
@ -50,26 +50,18 @@ class FilterProxyDb(ProxyDbBase):
|
||||
self.person_filter = person_filter
|
||||
|
||||
if person_filter:
|
||||
#self.plist = set(person_filter.apply(
|
||||
# self.db, self.db.iter_person_handles()))
|
||||
self.plist = dict((h, 1) for h in person_filter.apply(
|
||||
self.db, self.db.iter_person_handles()))
|
||||
else:
|
||||
#self.plist = self.db.get_person_handles()
|
||||
self.plist = dict((h, 1) for h in self.db.iter_person_handles())
|
||||
|
||||
if event_filter:
|
||||
#self.elist = set(event_filter.apply(
|
||||
# self.db, self.db.iter_event_handles()))
|
||||
self.elist = dict((h, 1) for h in event_filter.apply(
|
||||
self.db, self.db.iter_event_handles()))
|
||||
else:
|
||||
#self.elist = self.db.get_event_handles()
|
||||
self.elist = dict((h, 1) for h in self.db.iter_event_handles())
|
||||
|
||||
if note_filter:
|
||||
#self.nlist = set(note_filter.apply(
|
||||
# self.db, self.db.iter_note_handles()))
|
||||
self.nlist = dict((h, 1) for h in note_filter.apply(
|
||||
self.db, self.db.iter_note_handles()))
|
||||
else:
|
||||
|
@ -250,7 +250,7 @@ class PrivateProxyDb(ProxyDbBase):
|
||||
obj = self.get_unfiltered_place(handle)
|
||||
return obj and not obj.get_privacy()
|
||||
|
||||
def include_object(self, handle):
|
||||
def include_media_object(self, handle):
|
||||
"""
|
||||
Predicate returning True if object is to be included, else False
|
||||
"""
|
||||
|
@ -93,7 +93,7 @@ class ProxyDbBase(GrampsDbBase):
|
||||
include_event = \
|
||||
include_source = \
|
||||
include_place = \
|
||||
include_object = \
|
||||
include_media_object = \
|
||||
include_repository = \
|
||||
include_note = \
|
||||
None
|
||||
@ -193,15 +193,6 @@ class ProxyDbBase(GrampsDbBase):
|
||||
"""
|
||||
return ifilter(self.include_person, self.db.iter_person_handles())
|
||||
|
||||
def iter_people(self):
|
||||
"""
|
||||
Return an iterator over handles and objects for Persons in the database
|
||||
"""
|
||||
for handle, person in self.db.iter_people():
|
||||
if (self.include_person is None or
|
||||
self.include_person(handle, person)):
|
||||
yield handle, person
|
||||
|
||||
def iter_family_handles(self):
|
||||
"""
|
||||
Return an iterator over database handles, one handle for each Family in
|
||||
@ -235,7 +226,7 @@ class ProxyDbBase(GrampsDbBase):
|
||||
Return an iterator over database handles, one handle for each Media
|
||||
Object in the database.
|
||||
"""
|
||||
return ifilter(self.include_object, self.db.iter_media_object_handles())
|
||||
return ifilter(self.include_media_object, self.db.iter_media_object_handles())
|
||||
|
||||
def iter_repository_handles(self):
|
||||
"""
|
||||
@ -250,6 +241,62 @@ class ProxyDbBase(GrampsDbBase):
|
||||
the database.
|
||||
"""
|
||||
return ifilter(self.include_note, self.db.iter_note_handles())
|
||||
|
||||
@staticmethod
|
||||
def __iter_object(filter, method):
|
||||
""" Helper function to return an iterator over an object class """
|
||||
return ifilter(lambda obj: (filter is None or filter(obj.handle)),
|
||||
method())
|
||||
|
||||
def iter_people(self):
|
||||
"""
|
||||
Return an iterator over Person objects in the database
|
||||
"""
|
||||
return self.__iter_object(self.include_person, self.db.iter_people)
|
||||
|
||||
def iter_families(self):
|
||||
"""
|
||||
Return an iterator over Family objects in the database
|
||||
"""
|
||||
return self.__iter_object(self.include_family, self.db.iter_families)
|
||||
|
||||
def iter_events(self):
|
||||
"""
|
||||
Return an iterator over Event objects in the database
|
||||
"""
|
||||
return self.__iter_object(self.include_event, self.db.iter_events)
|
||||
|
||||
def iter_places(self):
|
||||
"""
|
||||
Return an iterator over Place objects in the database
|
||||
"""
|
||||
return self.__iter_object(self.include_place, self.db.iter_places)
|
||||
|
||||
def iter_sources(self):
|
||||
"""
|
||||
Return an iterator over Source objects in the database
|
||||
"""
|
||||
return self.__iter_object(self.include_source, self.db.iter_sources)
|
||||
|
||||
def iter_media_objects(self):
|
||||
"""
|
||||
Return an iterator over Media objects in the database
|
||||
"""
|
||||
return self.__iter_object(self.include_media_object,
|
||||
self.db.iter_media_objects)
|
||||
|
||||
def iter_repositories(self):
|
||||
"""
|
||||
Return an iterator over Repositories objects in the database
|
||||
"""
|
||||
return self.__iter_object(self.include_repository,
|
||||
self.db.iter_repositories)
|
||||
|
||||
def iter_notes(self):
|
||||
"""
|
||||
Return an iterator over Note objects in the database
|
||||
"""
|
||||
return self.__iter_object(self.include_note, self.db.iter_notes)
|
||||
|
||||
@staticmethod
|
||||
def gfilter(predicate, obj):
|
||||
@ -274,6 +321,10 @@ class ProxyDbBase(GrampsDbBase):
|
||||
setattr(self, name, attr)
|
||||
return attr
|
||||
|
||||
# Default behaviour: lookup attribute in parent object
|
||||
|
||||
return getattr(self.db, name)
|
||||
|
||||
def get_person_from_handle(self, handle):
|
||||
"""
|
||||
Finds a Person in the database from the passed gramps handle.
|
||||
@ -319,7 +370,7 @@ class ProxyDbBase(GrampsDbBase):
|
||||
Finds an Object in the database from the passed gramps handle.
|
||||
If no such Object exists, None is returned.
|
||||
"""
|
||||
return self.gfilter(self.include_object,
|
||||
return self.gfilter(self.include_media_object,
|
||||
self.db.get_object_from_handle(handle))
|
||||
|
||||
def get_repository_from_handle(self, handle):
|
||||
@ -383,7 +434,7 @@ class ProxyDbBase(GrampsDbBase):
|
||||
Finds a MediaObject in the database from the passed gramps' ID.
|
||||
If no such MediaObject exists, None is returned.
|
||||
"""
|
||||
return self.gfilter(self.include_object,
|
||||
return self.gfilter(self.include_media_object,
|
||||
self.db.get_object_from_gramps_id(val))
|
||||
|
||||
def get_repository_from_gramps_id(self, val):
|
||||
@ -604,7 +655,7 @@ class ProxyDbBase(GrampsDbBase):
|
||||
"""
|
||||
returns True if the handle exists in the current MediaObjectdatabase.
|
||||
"""
|
||||
return self.gfilter(self.include_object,
|
||||
return self.gfilter(self.include_media_object,
|
||||
self.db.get_object_from_gramps_id(val)) is not None
|
||||
|
||||
def has_repository_handle(self, handle):
|
||||
@ -800,7 +851,7 @@ class ProxyDbBase(GrampsDbBase):
|
||||
return self.db.get_gramps_ids(obj_key)
|
||||
|
||||
def has_gramps_id(self, obj_key, gramps_id):
|
||||
return self.db.has_gramps_ids(obj_key, gramps_id)
|
||||
return self.db.has_gramps_id(obj_key, gramps_id)
|
||||
|
||||
def get_bookmarks(self):
|
||||
"""returns the list of Person handles in the bookmarks"""
|
||||
|
@ -59,7 +59,7 @@ class ReferencedProxyDb(ProxyDbBase):
|
||||
"""
|
||||
return handle not in self.unreferenced_places
|
||||
|
||||
def include_object(self, handle):
|
||||
def include_media_object(self, handle):
|
||||
"""
|
||||
Filter for media objects
|
||||
"""
|
||||
|
@ -196,8 +196,8 @@ def _find_records(db, filter, callname):
|
||||
family_shortest = []
|
||||
family_longest = []
|
||||
|
||||
for family_handle in db.iter_family_handles():
|
||||
family = db.get_family_from_handle(family_handle)
|
||||
for family in db.iter_families():
|
||||
#family = db.get_family_from_handle(family_handle)
|
||||
|
||||
father_handle = family.get_father_handle()
|
||||
if not father_handle:
|
||||
@ -220,7 +220,7 @@ def _find_records(db, filter, callname):
|
||||
|
||||
_record(None, family_mostchildren,
|
||||
len(family.get_child_ref_list()),
|
||||
name, 'Family', family_handle)
|
||||
name, 'Family', family.handle)
|
||||
|
||||
marriage_date = None
|
||||
divorce_date = None
|
||||
@ -243,7 +243,7 @@ def _find_records(db, filter, callname):
|
||||
if probably_alive(father, db) and probably_alive(mother, db):
|
||||
_record(family_youngestmarried, family_oldestmarried,
|
||||
today_date - marriage_date,
|
||||
name, 'Family', family_handle)
|
||||
name, 'Family', family.handle)
|
||||
elif (_good_date(divorce_date) or
|
||||
_good_date(father_death_date) or
|
||||
_good_date(mother_death_date)):
|
||||
@ -262,7 +262,7 @@ def _find_records(db, filter, callname):
|
||||
duration = end - marriage_date
|
||||
|
||||
_record(family_shortest, family_longest,
|
||||
duration, name, 'Family', family_handle)
|
||||
duration, name, 'Family', family.handle)
|
||||
|
||||
return [(text, varname, locals()[varname]) for (text, varname, default) in RECORDS]
|
||||
|
||||
|
@ -58,21 +58,18 @@ class GivenNameCloudGramplet(Gramplet):
|
||||
def main(self):
|
||||
self.set_text(_("Processing...") + "\n")
|
||||
yield True
|
||||
people = self.dbstate.db.iter_person_handles()
|
||||
givensubnames = {}
|
||||
representative_handle = {}
|
||||
|
||||
cnt = 0
|
||||
for person_handle in people:
|
||||
person = self.dbstate.db.get_person_from_handle(person_handle)
|
||||
if person:
|
||||
cnt += 1
|
||||
allnames = [person.get_primary_name()] + person.get_alternate_names()
|
||||
allnames = set([name.get_first_name().strip() for name in allnames])
|
||||
for givenname in allnames:
|
||||
for givensubname in givenname.split():
|
||||
givensubnames[givensubname] = givensubnames.get(givensubname, 0) + 1
|
||||
representative_handle[givensubname] = person_handle
|
||||
cnt = 0
|
||||
for person in self.dbstate.db.iter_people():
|
||||
allnames = [person.get_primary_name()] + person.get_alternate_names()
|
||||
allnames = set(name.get_first_name().strip() for name in allnames)
|
||||
for givenname in allnames:
|
||||
for givensubname in givenname.split():
|
||||
givensubnames[givensubname] = givensubnames.get(givensubname, 0) + 1
|
||||
representative_handle[givensubname] = person.handle
|
||||
cnt += 1
|
||||
if not cnt % _YIELD_INTERVAL:
|
||||
yield True
|
||||
|
||||
@ -83,22 +80,21 @@ class GivenNameCloudGramplet(Gramplet):
|
||||
for givensubname in givensubnames:
|
||||
givensubname_sort.append( (givensubnames[givensubname], givensubname) )
|
||||
total += givensubnames[givensubname]
|
||||
cnt += 1
|
||||
if not cnt % _YIELD_INTERVAL:
|
||||
yield True
|
||||
cnt += 1
|
||||
|
||||
total_givensubnames = cnt
|
||||
givensubname_sort.sort(reverse=True)
|
||||
cloud_names = []
|
||||
cloud_values = []
|
||||
|
||||
for cnt, (count, givensubname) in enumerate(givensubname_sort):
|
||||
for count, givensubname in givensubname_sort:
|
||||
cloud_names.append( (count, givensubname) )
|
||||
cloud_values.append( count )
|
||||
|
||||
cloud_names.sort(key=lambda k: k[1])
|
||||
counts = list(set(cloud_values))
|
||||
counts.sort(reverse=True)
|
||||
counts = sorted(set(cloud_values), reverse=True)
|
||||
line = 0
|
||||
### All done!
|
||||
# Now, find out how many we can display without going over top_size:
|
||||
|
@ -66,8 +66,7 @@ class StatsGramplet(Gramplet):
|
||||
def main(self):
|
||||
self.set_text(_("Processing..."))
|
||||
database = self.dbstate.db
|
||||
personList = database.iter_person_handles()
|
||||
familyList = database.iter_family_handles()
|
||||
personList = database.iter_people()
|
||||
|
||||
with_photos = 0
|
||||
total_photos = 0
|
||||
@ -90,38 +89,35 @@ class StatsGramplet(Gramplet):
|
||||
except:
|
||||
notfound.append(photo.get_path())
|
||||
|
||||
for cnt, person_handle in enumerate(personList):
|
||||
person = database.get_person_from_handle(person_handle)
|
||||
if not person:
|
||||
continue
|
||||
for cnt, person in enumerate(personList):
|
||||
length = len(person.get_media_list())
|
||||
if length > 0:
|
||||
with_photos = with_photos + 1
|
||||
total_photos = total_photos + length
|
||||
with_photos += 1
|
||||
total_photos += length
|
||||
|
||||
names = [person.get_primary_name()] + person.get_alternate_names()
|
||||
for name in names:
|
||||
if name.get_first_name() == "" or name.get_group_name() == "":
|
||||
incomp_names = incomp_names + 1
|
||||
incomp_names += 1
|
||||
if name.get_group_name() not in namelist:
|
||||
namelist.append(name.get_group_name())
|
||||
|
||||
if ((not person.get_main_parents_family_handle()) and
|
||||
(not len(person.get_family_handle_list()))):
|
||||
disconnected = disconnected + 1
|
||||
if (not person.get_main_parents_family_handle() and
|
||||
not len(person.get_family_handle_list())):
|
||||
disconnected += 1
|
||||
|
||||
birth_ref = person.get_birth_ref()
|
||||
if birth_ref:
|
||||
birth = database.get_event_from_handle(birth_ref.ref)
|
||||
if not DateHandler.get_date(birth):
|
||||
missing_bday = missing_bday + 1
|
||||
missing_bday += 1
|
||||
else:
|
||||
missing_bday = missing_bday + 1
|
||||
missing_bday += 1
|
||||
|
||||
if person.get_gender() == gen.lib.Person.FEMALE:
|
||||
females = females + 1
|
||||
females += 1
|
||||
elif person.get_gender() == gen.lib.Person.MALE:
|
||||
males = males + 1
|
||||
males += 1
|
||||
else:
|
||||
unknowns += 1
|
||||
if not cnt % _YIELD_INTERVAL:
|
||||
|
@ -434,8 +434,7 @@ class ExtractCity(Tool.BatchTool, ManagedWindow.ManagedWindow):
|
||||
|
||||
self.name_list = []
|
||||
|
||||
for handle in db.iter_place_handles():
|
||||
place = db.get_place_from_handle(handle)
|
||||
for place in db.iter_places():
|
||||
descr = place.get_title()
|
||||
loc = place.get_main_location()
|
||||
self.progress.step()
|
||||
@ -455,7 +454,7 @@ class ExtractCity(Tool.BatchTool, ManagedWindow.ManagedWindow):
|
||||
new_state = STATE_MAP.get(val.upper())
|
||||
if new_state:
|
||||
self.name_list.append(
|
||||
(handle, (city, new_state[0], postal,
|
||||
(place.handle, (city, new_state[0], postal,
|
||||
COUNTRY[new_state[1]])))
|
||||
continue
|
||||
|
||||
@ -471,7 +470,7 @@ class ExtractCity(Tool.BatchTool, ManagedWindow.ManagedWindow):
|
||||
new_state = STATE_MAP.get(val.upper())
|
||||
if new_state:
|
||||
self.name_list.append(
|
||||
(handle, (city, new_state[0], postal,
|
||||
(place.handle, (city, new_state[0], postal,
|
||||
COUNTRY[new_state[1]])))
|
||||
continue
|
||||
match = CITY_STATE.match(descr.strip())
|
||||
@ -490,7 +489,7 @@ class ExtractCity(Tool.BatchTool, ManagedWindow.ManagedWindow):
|
||||
new_state = STATE_MAP.get(val.upper())
|
||||
if new_state:
|
||||
self.name_list.append(
|
||||
(handle, (city, new_state[0], postal,
|
||||
(place.handle, (city, new_state[0], postal,
|
||||
COUNTRY[new_state[1]])))
|
||||
continue
|
||||
|
||||
@ -498,7 +497,7 @@ class ExtractCity(Tool.BatchTool, ManagedWindow.ManagedWindow):
|
||||
new_state = STATE_MAP.get(val)
|
||||
if new_state:
|
||||
self.name_list.append(
|
||||
(handle, (None, new_state[0], None,
|
||||
(place.handle, (None, new_state[0], None,
|
||||
COUNTRY[new_state[1]])))
|
||||
self.progress.close()
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user