From 2098a01f8db02bb822cfd134081411c393ea0e65 Mon Sep 17 00:00:00 2001 From: Doug Blank Date: Fri, 18 Apr 2008 01:09:32 +0000 Subject: [PATCH] refactored SimpleTable; QuickReports can return values; date diffs return Span svn: r10578 --- src/QuickReports.py | 9 ++-- src/Simple/_SimpleTable.py | 8 ++-- src/gen/db/dbdir.py | 4 +- src/gen/lib/date.py | 56 +++++++++++++++++++---- src/plugins/AgeOnDate.py | 12 ++--- src/plugins/DefaultGramplets.py | 80 ++++++++++++++++++++++----------- src/plugins/FilterByName.py | 4 +- src/plugins/OnThisDay.py | 12 ++--- src/plugins/References.py | 4 +- src/plugins/SameSurnames.py | 4 +- src/plugins/all_events.py | 12 ++--- src/plugins/lineage.py | 8 ++-- src/plugins/siblings.py | 4 +- 13 files changed, 141 insertions(+), 76 deletions(-) diff --git a/src/QuickReports.py b/src/QuickReports.py index 98a745bf1..b54689355 100644 --- a/src/QuickReports.py +++ b/src/QuickReports.py @@ -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 diff --git a/src/Simple/_SimpleTable.py b/src/Simple/_SimpleTable.py index b38f5e2de..d5a7c5f9d 100644 --- a/src/Simple/_SimpleTable.py +++ b/src/Simple/_SimpleTable.py @@ -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') diff --git a/src/gen/db/dbdir.py b/src/gen/db/dbdir.py index 9cab6c757..a7dfa6439 100644 --- a/src/gen/db/dbdir.py +++ b/src/gen/db/dbdir.py @@ -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 diff --git a/src/gen/lib/date.py b/src/gen/lib/date.py index d86eaa061..0bbf0c902 100644 --- a/src/gen/lib/date.py +++ b/src/gen/lib/date.py @@ -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. diff --git a/src/plugins/AgeOnDate.py b/src/plugins/AgeOnDate.py index 833f34ffd..f6ee6083e 100644 --- a/src/plugins/AgeOnDate.py +++ b/src/plugins/AgeOnDate.py @@ -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): diff --git a/src/plugins/DefaultGramplets.py b/src/plugins/DefaultGramplets.py index 22359b697..3fe10c0c4 100644 --- a/src/plugins/DefaultGramplets.py +++ b/src/plugins/DefaultGramplets.py @@ -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, + ) + diff --git a/src/plugins/FilterByName.py b/src/plugins/FilterByName.py index 4de4e0ce9..5b4169a37 100644 --- a/src/plugins/FilterByName.py +++ b/src/plugins/FilterByName.py @@ -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) #------------------------------------------------------------------------ # diff --git a/src/plugins/OnThisDay.py b/src/plugins/OnThisDay.py index 21b74ff68..866bd412d 100644 --- a/src/plugins/OnThisDay.py +++ b/src/plugins/OnThisDay.py @@ -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()}) diff --git a/src/plugins/References.py b/src/plugins/References.py index e9c46cd6f..a899437b6 100644 --- a/src/plugins/References.py +++ b/src/plugins/References.py @@ -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("") diff --git a/src/plugins/SameSurnames.py b/src/plugins/SameSurnames.py index 7f9dc8aab..512403e5f 100644 --- a/src/plugins/SameSurnames.py +++ b/src/plugins/SameSurnames.py @@ -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) #------------------------------------------------------------------------ # diff --git a/src/plugins/all_events.py b/src/plugins/all_events.py index 0589060d7..fd43f4e1e 100644 --- a/src/plugins/all_events.py +++ b/src/plugins/all_events.py @@ -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): """ diff --git a/src/plugins/lineage.py b/src/plugins/lineage.py index 6d1aabb54..ed3bf6e30 100644 --- a/src/plugins/lineage.py +++ b/src/plugins/lineage.py @@ -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 : diff --git a/src/plugins/siblings.py b/src/plugins/siblings.py index 9d5616ec9..46c754f18 100644 --- a/src/plugins/siblings.py +++ b/src/plugins/siblings.py @@ -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) #------------------------------------------------------------------------ #