refactored SimpleTable; QuickReports can return values; date diffs return Span

svn: r10578
This commit is contained in:
Doug Blank 2008-04-18 01:09:32 +00:00
parent e910a131a9
commit 2098a01f8d
13 changed files with 141 additions and 76 deletions

View File

@ -121,7 +121,7 @@ def run_quick_report_by_name(dbstate, uistate, report_name, handle, **kwargs):
report = item
break
if report:
run_report(dbstate, uistate, report[2], handle, report[0], **kwargs)
return run_report(dbstate, uistate, report[2], handle, report[0], **kwargs)
else:
raise AttributeError, ("No such quick report '%s'" % report_name)
@ -143,8 +143,9 @@ def run_quick_report_by_name_direct(report_name, database, document, handle):
d.dbstate = document.dbstate
d.uistate = document.uistate
d.open("")
report[0](database, d, handle)
retval = report[0](database, d, handle)
d.close()
return retval
else:
raise AttributeError, ("No such quick report '%s'" % report_name)
@ -177,6 +178,6 @@ def run_report(dbstate, uistate, category, handle, func, **kwargs):
obj = handle
if obj:
d.open("")
func(dbstate.db, d, obj, **kwargs)
retval = func(dbstate.db, d, obj, **kwargs)
d.close()
return retval

View File

@ -34,12 +34,11 @@ class SimpleTable:
Provide a simplified table creation interface.
"""
def __init__(self, access, doc, title=None):
def __init__(self, access, title=None):
"""
Initialize the class with a simpledb, and simpledoc
Initialize the class with a simpledb
"""
self.access = access
self.simpledoc = doc # simpledoc; simpledoc.doc = docgen object
self.title = title
self.__columns = []
self.__rows = []
@ -250,7 +249,8 @@ class SimpleTable:
else:
self.__rows.sort(lambda a, b: cmp(a[idx],b[idx]))
def write(self):
def write(self, document):
self.simpledoc = document # simpledoc; simpledoc.doc = docgen object
if self.simpledoc.doc.type == "standard":
doc = self.simpledoc.doc
doc.start_table('simple','Table')

View File

@ -1758,7 +1758,7 @@ class GrampsDBDir(GrampsDbBase, UpdateCallback):
birth_ref_index, # 6
event_ref_list, # 7
family_list, # 8
arent_family_list, # 9
parent_family_list, # 9
media_list, # 10
address_list, # 11
attribute_list, # 12
@ -1800,7 +1800,7 @@ class GrampsDBDir(GrampsDbBase, UpdateCallback):
birth_ref_index, # 6
event_ref_list, # 7
family_list, # 8
arent_family_list, # 9
parent_family_list, # 9
media_list, # 10
new_address_list, # 11
attribute_list, # 12

View File

@ -69,6 +69,26 @@ class DateError(Exception):
def __str__(self):
return self.value
class Span:
""" Class used in date differences """
def __init__(self, *diff_tuple):
self.diff_tuple = diff_tuple
def __getitem__(self, pos):
return self.diff_tuple[pos]
def __repr__(self):
if self.diff_tuple[1] != 0:
retval = ((_("%d years") % self.diff_tuple[0])+ ", " +
(_("%d months") % self.diff_tuple[1]))
else:
retval = (_("%d years") % self.diff_tuple[0])
return retval
def __int__(self):
return int(self.diff_tuple[0] * 12 + self.diff_tuple[1]) # months
#-------------------------------------------------------------------------
#
# Date class
@ -302,9 +322,9 @@ class Date:
d1, d2 = d2, d1
date1, date2 = date2, date1
# d1 - d2 (1998, 12, 32) - (1982, 12, 15)
if self.calendar != other.calendar:
if date1.calendar != date2.calendar:
diff = date1.sortval - date2.sortval
return (diff/365, (diff % 365)/30, (diff % 365) % 30)
return Span(diff/365, (diff % 365)/30, (diff % 365) % 30)
# days:
if d2[2] > d1[2]:
# months:
@ -329,24 +349,24 @@ class Date:
# estimate: (years, months, days)
# Check transitivity:
eDate = date1 - (years, months, days)
if eDate < date2: # too small
if eDate << date2: # too small, strictly less than
diff = 0
while eDate < date2 and diff < 60:
diff += 1
eDate = eDate + (0, 0, diff)
if diff == 60:
return None
return (years, months, days - diff)
elif eDate > date2:
return Span(-1, -1, -1)
return Span(years, months, days - diff)
elif eDate >> date2:
diff = 0
while eDate > date2 and diff > -60:
diff -= 1
eDate = eDate - (0, 0, abs(diff))
if diff == -60:
return None
return (years, months, days + diff)
return Span(-1, -1, -1)
return Span(years, months, days + diff)
else:
return (years, months, days)
return Span(years, months, days)
else:
raise AttributeError, "unknown date sub type: %s " % type(other)
@ -354,12 +374,30 @@ class Date:
#def __eq__(self, other):
# return self.sortval == other.sortval
def __contains__(self, string):
"""
For use with "x in Date" syntax.
"""
return (str(string) in self.text)
def __lshift__(self, other):
"""
Comparison for strictly less than.
"""
return self.match(other, comparison="<<")
def __lt__(self, other):
"""
Comparison for less than.
"""
return self.match(other, comparison="<")
def __rshift__(self, other):
"""
Comparison for strictly greater than.
"""
return self.match(other, comparison=">>")
def __gt__(self, other):
"""
Comparison for greater than.

View File

@ -38,7 +38,7 @@ def run(database, document, date):
# setup the simple access functions
sdb = SimpleAccess(database)
sdoc = SimpleDoc(document)
stab = SimpleTable(sdb, sdoc)
stab = SimpleTable(sdb)
if not date.get_valid():
sdoc.paragraph("Date is not a valid date.")
return
@ -67,16 +67,12 @@ def run(database, document, date):
if ((death_date != None) or
(death_date == None and
diff_tuple[0] <= Config.get(Config.MAX_AGE_PROB_ALIVE))):
if diff_tuple[1] != 0:
birth_str = ((_("%d years") % diff_tuple[0])+ ", " +
(_("%d months") % diff_tuple[1]))
else:
birth_str = (_("%d years") % diff_tuple[0])
birth_sort = int(diff_tuple[0] * 12 + diff_tuple[1]) # months
birth_str = str(diff_tuple)
birth_sort = int(diff_tuple)
if birth_str != "":
stab.row(person, birth_str)
stab.row_sort_val(1, birth_sort)
stab.write()
stab.write(sdoc)
sdoc.paragraph("")
def get_event_date_from_ref(database, ref):

View File

@ -712,6 +712,7 @@ class StatsGramplet(Gramplet):
class PythonGramplet(Gramplet):
def init(self):
self.prompt = ">"
self.tooltip = _("Enter Python expressions")
self.env = {"dbstate": self.gui.dbstate,
"uistate": self.gui.uistate,
@ -720,7 +721,7 @@ class PythonGramplet(Gramplet):
}
# GUI setup:
self.gui.textview.set_editable(True)
self.set_text("Python %s\n> " % sys.version)
self.set_text("Python %s\n%s " % (sys.version, self.prompt))
self.gui.textview.connect('key-press-event', self.on_key_press)
def format_exception(self, max_tb_level=10):
@ -729,6 +730,29 @@ class PythonGramplet(Gramplet):
retval += _("Error") + (" : %s %s" %(cla, exc))
return retval
def process_command(self, command):
# update states, in case of change:
self.env["dbstate"] = self.gui.dbstate
self.env["uistate"] = self.gui.uistate
_retval = None
if "_retval" in self.env:
del self.env["_retval"]
exp1 = """_retval = """ + command
exp2 = command.strip()
try:
_retval = eval(exp2, self.env)
except:
try:
exec exp1 in self.env
except:
try:
exec exp2 in self.env
except:
_retval = self.format_exception()
if "_retval" in self.env:
_retval = self.env["_retval"]
return _retval
def on_key_press(self, widget, event):
import gtk
if (event.keyval == gtk.keysyms.Home or
@ -764,39 +788,20 @@ class PythonGramplet(Gramplet):
end = buffer.get_iter_at_line_offset(line_cnt, line_len)
line = buffer.get_text(start, end)
self.append_text("\n")
if line.startswith(">"):
if line.startswith(self.prompt):
line = line[1:].strip()
else:
self.append_text("> ")
self.append_text("%s " % self.prompt)
return True
if echo:
self.append_text("> " + line)
self.append_text(("%s " % self.prompt) + line)
end = buffer.get_end_iter()
buffer.place_cursor(end)
return True
# update states, in case of change:
self.env["dbstate"] = self.gui.dbstate
self.env["uistate"] = self.gui.uistate
_retval = None
if "_retval" in self.env:
del self.env["_retval"]
exp1 = """_retval = """ + line
exp2 = line.strip()
try:
_retval = eval(exp2, self.env)
except:
try:
exec exp1 in self.env
except:
try:
exec exp2 in self.env
except:
_retval = self.format_exception()
if "_retval" in self.env:
_retval = self.env["_retval"]
_retval = self.process_command(line)
if _retval != None:
self.append_text("%s" % str(_retval))
self.append_text("\n> ")
self.append_text("\n%s " % self.prompt)
end = buffer.get_end_iter()
buffer.place_cursor(end)
return True
@ -985,6 +990,21 @@ class AgeOnDateGramplet(Gramplet):
'ageondate',
date)
class QueryGramplet(PythonGramplet):
def init(self):
self.prompt = "$"
self.tooltip = _("Enter SQL query")
# GUI setup:
self.gui.textview.set_editable(True)
self.set_text("Structured Query Language\n%s " % self.prompt)
self.gui.textview.connect('key-press-event', self.on_key_press)
def process_command(self, command):
retval = run_quick_report_by_name(self.gui.dbstate,
self.gui.uistate,
'query',
command)
return retval
register(type="gramplet",
name= "Top Surnames Gramplet",
@ -1100,3 +1120,13 @@ register(type="gramplet",
title=_("FAQ"),
)
register(type="gramplet",
name="Query Gramplet",
tname=_("Query Gramplet"),
height=300,
content = QueryGramplet,
title=_("Query"),
detached_width = 600,
detached_height = 400,
)

View File

@ -48,7 +48,7 @@ def run(database, document, filter_name, *args, **kwargs):
# setup the simple access functions
sdb = SimpleAccess(database)
sdoc = SimpleDoc(document)
stab = SimpleTable(sdb, sdoc)
stab = SimpleTable(sdb)
# display the title
sdoc.title(_("Filtering on %s") % _(filter_name)) # listed above
sdoc.paragraph("")
@ -219,7 +219,7 @@ def run(database, document, filter_name, *args, **kwargs):
sdoc.paragraph(_("Filter matched %d records.") % matches)
sdoc.paragraph("")
if matches > 0:
stab.write()
stab.write(sdoc)
#------------------------------------------------------------------------
#

View File

@ -63,11 +63,11 @@ def run(database, document, main_event):
# setup the simple access functions
sdb = SimpleAccess(database)
sdoc = SimpleDoc(document)
stab = SimpleTable(sdb, sdoc)
stab = SimpleTable(sdb)
stab.set_link_col(3)
yeartab = SimpleTable(sdb, sdoc)
yeartab = SimpleTable(sdb)
yeartab.set_link_col(3)
histab = SimpleTable(sdb, sdoc)
histab = SimpleTable(sdb)
histab.set_link_col(3)
# display the title
@ -108,7 +108,7 @@ def run(database, document, main_event):
if stab.get_row_count() > 0:
sdoc.paragraph(_("Events on this exact date"))
stab.write()
stab.write(sdoc)
else:
sdoc.paragraph(_("No events on this exact date"))
sdoc.paragraph("")
@ -116,7 +116,7 @@ def run(database, document, main_event):
if histab.get_row_count() > 0:
sdoc.paragraph(_("Other events on this month/day in history"))
histab.write()
histab.write(sdoc)
else:
sdoc.paragraph(_("No other events on this month/day in history"))
sdoc.paragraph("")
@ -125,7 +125,7 @@ def run(database, document, main_event):
if yeartab.get_row_count() > 0:
sdoc.paragraph(_("Other events in %(year)d") %
{"year":main_date.get_year()})
yeartab.write()
yeartab.write(sdoc)
else:
sdoc.paragraph(_("No other events in %(year)d") %
{"year":main_date.get_year()})

View File

@ -62,7 +62,7 @@ def run(database, document, object, item, trans):
# setup the simple access functions
sdb = SimpleAccess(database)
sdoc = SimpleDoc(document)
stab = SimpleTable(sdb, sdoc)
stab = SimpleTable(sdb)
# display the title
sdoc.title(_("References for this %s") % trans)
@ -74,7 +74,7 @@ def run(database, document, object, item, trans):
stab.row(_(objclass), ref) # translation are explicit (above)
if stab.get_row_count() > 0:
stab.write()
stab.write(sdoc)
else:
sdoc.paragraph(_("No references for this %s") % trans)
sdoc.paragraph("")

View File

@ -62,7 +62,7 @@ def run(database, document, person):
# setup the simple access functions
sdb = SimpleAccess(database)
sdoc = SimpleDoc(document)
stab = SimpleTable(sdb, sdoc)
stab = SimpleTable(sdb)
if type(person) == str:
surname = person
rsurname = person
@ -88,7 +88,7 @@ def run(database, document, person):
str(person.get_primary_name().get_type()))
matches += 1
sdoc.paragraph(_("There are %d people with a matching name, or alternate name.\n") % matches)
stab.write()
stab.write(sdoc)
#------------------------------------------------------------------------
#

View File

@ -37,7 +37,7 @@ def run(database, document, person):
sdb = SimpleAccess(database)
sdoc = SimpleDoc(document)
stab = SimpleTable(sdb, sdoc)
stab = SimpleTable(sdb)
# get the personal events
event_list = sdb.events(person)
@ -61,7 +61,7 @@ def run(database, document, person):
stab.row(event,
sdb.event_date_obj(event),
sdb.event_place(event))
stab.write()
stab.write(sdoc)
def run_fam(database, document, family):
"""
@ -71,7 +71,7 @@ def run_fam(database, document, family):
sdb = SimpleAccess(database)
sdoc = SimpleDoc(document)
stab = SimpleTable(sdb, sdoc)
stab = SimpleTable(sdb)
# get the family events
event_list = [(_('Family'), x) for x in sdb.events(family)]
@ -106,9 +106,9 @@ def run_fam(database, document, family):
stab.row(person, sdb.event_type(event),
sdb.event_date_obj(event),
sdb.event_place(event))
stab.write()
stab.write(sdoc)
stab = SimpleTable(sdb, sdoc)
stab = SimpleTable(sdb)
sdoc.header1(_("Personal events of the children"))
stab.columns(_("Family Member"), _("Event Type"),
_("Event Date"), _("Event Place"))
@ -116,7 +116,7 @@ def run_fam(database, document, family):
stab.row(person, sdb.event_type(event),
sdb.event_date_obj(event),
sdb.event_place(event))
stab.write()
stab.write(sdoc)
def fam_sort(event1, event2):
"""

View File

@ -57,10 +57,10 @@ def run_father(database, document, person):
" People in this lineage all share the same Y-chromosone."
))
sd.paragraph("")
stab = SimpleTable(sa, sd)
stab = SimpleTable(sa)
stab.columns(_("Name Father"), _("Birth Date"), _("Death Date"), _("Remark"))
make_details(gen.lib.Person.MALE, person, sa, sd, database, stab)
stab.write()
stab.write(sd)
sd.paragraph("")
if person.gender == gen.lib.Person.FEMALE :
@ -88,10 +88,10 @@ def run_mother(database, document, person):
))
sd.paragraph("")
stab = SimpleTable(sa, sd)
stab = SimpleTable(sa)
stab.columns(_("Name Mother"), _("Birth"), _("Death Date"), _("Remark"))
make_details(gen.lib.Person.FEMALE, person, sa, sd, database, stab)
stab.write()
stab.write(sd)
sd.paragraph("")
if person.gender == gen.lib.Person.MALE :

View File

@ -36,7 +36,7 @@ def run(database, document, person):
# setup the simple access functions
sdb = SimpleAccess(database)
sdoc = SimpleDoc(document)
stab = SimpleTable(sdb, sdoc)
stab = SimpleTable(sdb)
rel_class = relationship_class()
# display the title
sdoc.title(_("Siblings of %s") % sdb.name(person))
@ -60,7 +60,7 @@ def run(database, document, person):
sdb.gender(child),
sdb.birth_date_obj(child),
rel_str)
stab.write()
stab.write(sdoc)
#------------------------------------------------------------------------
#