Jump to: navigation, search

Report-writing tutorial

923 bytes added, 12:08, 12 September 2021
Typo in code variable name : "fist" instead of "first"
{{Out of date}}{{languages|Report-writing tutorial}} This tutorial covers the basics of writing a simple report using the GRAMPS Gramps report infrastructure. It covers the process of handling options, building a document and creating the report.
The goals of this report are to create a database summary report. It will include the following information in the report:
* The most common surname
As of {{man tip|From Gramps version 3.2, there is also a |A [[Simple Access API|simple access]] database [http API] available, with accompanying [[Quick ReportsViews]], [[Gramplets]] and [[Addons_DevelopmentAddons development|Addons]].}}
Before going into details, it is useful to note that the report should have three two basic parts. As This is explained on the [[Addons_Development|Addonsdevelopment]] page, these go that the source code goes into two different files: # the source code Gramps Plugin Registration (''*'') file (e.g.: <code> </code># and a Gramps Plugin Registration the main source code file (''*.py'') e.g.: <code></code> ;Registration statement : This initializes the report by a single call to the <tt>[ register()]</tt> function. It is trivial, but without it your report will not become available to Gramps, even if it is otherwise perfectly writtenA report can potentially be generated as a standalone report, as a Gramps Book item, and as a command line report. The registration determines which modes are enabled for a given report. The report class does not have to know anything about the mode. The options class is there to provide options interface for all available modes. * [[Report-writing_tutorial#Registering_the_report]]
;Report class : This is the code that takes data from the GRAMPS Gramps database and organizes it into the document structure. This structure can later be printed, viewed, or written into a file in a variety of formats. This class uses the [http docgen] interface to abstract away the output format details.
;Options class : This is the code that provides means to obtain options necessary for the report using a variety of available mechanisms.
==Document interface=report= The [[Report Generation]] article provides an overview of the 'docgen' interfaces that are available for outputting
;Registration statement : This initializes The [[Report API]] article provides more details about the report by a single call to the <tt>register()</tt> function. It is trivial, but without it your report will not become available to GRAMPS, even if it is otherwise perfectly writteninterfaces.
A report can potentially be generated as a standalone report, as a GRAMPS Book item, and as a command line reportThe developer [ The registration determines which modes are enabled for _docgenplugin docgen] api documentation provides a given report. The report class does not have to know anything about detailed specification of the mode. The options class is there to provide options interface for all available modesinterfaces.
==Document interface==GRAMPS Gramps attempts to abstract the output document format away from the report. By coding to the [http docgen] class, the report can generate its output in the format desired by the end user. The document passed to the report (<tt>self.doc</tt>) could represent an HTML, OpenDocument, PDF or any of the other formats supported by the user. The report does not have to concern itself with the output format details, since all details are handled by the document object.
A document is composed of paragraphs, tables, and graphics objects. Tables and graphics objects will not be covered in this tutorial.
The report defines a set of paragraph and font styles, along with their default implementation. The user can override the definition of each style, allowing the user to [[Gramps_{{man version}}_Wiki_Manual_-_Settings#Customize_report_output_formats|customize the report]]. Each paragraph style must be named uniquely, to prevent collisions when printed in a book format. It is recommended to prefix each paragraph style with a three letter code unique to the report.
Paragraph and font styles are defined in the <tt>make_default_style()</tt> function of the options class. The paragraphs are grouped into a <tt>StyleSheet</tt>, which the <tt>make_default_style()</tt> function defines. For the example report (<tt>DbSummary</tt>), the paragraph styles are defined as below:
{{stub}}<!-- need to check code is correct for Gramps 5.1.x -->
compare to <code>gramps\plugins\textreport\</code> starting at [ line 297]
def make_default_style(self, default_style):
# Define the title paragraph, named 'DBS-Title', which uses a # 18 point, bold Sans Serif font with a paragraph that is centered
font = docgen.FontStyle() font.set_size(18) font.set_type_face(docgen.FONT_SANS_SERIF) font.set_bold(True)
para = docgen.ParagraphStyle() para.set_header_level(1) para.set_alignment(docgen.PARA_ALIGN_CENTER) para.set_font(font) para.set_description(_('The style used for the title of the page.'))
# Define the normal paragraph, named 'DBS-Normal', which uses a # 12 point, Serif font.
font = docgen.FontStyle() font.set_size(12) font.set_type_face(docgen.FONT_SERIF)
para = docgen.ParagraphStyle() para.set_font(font) para.set_description(_('The style used for normal text'))
===Report class===
The user's report class should inherit from the Report class contained within the Report module. The constructor should take three arguments (besides class instance itself, usually denoted by 'self' name):
* GRAMPS Gramps database instance
* options class instance
* user class instance
The first is the database to work with. The second is the instance of the options class defined in the same report, see next section. The third is an instance of the User class, used for interaction with the user. Here's an example of a report class definition:
from ReportBase import Report, ReportUtils, ReportOptions
class ReportClassName(Report): def __init__(self, database, options_class, user): Report.__init__(self, database, options_class, user)
The Report class's constructor will initialize several variables for the user based off the passed values. They are:
;self.doc : The opened document instance ready for output. This is of the type [http docgen], and is '''not''' a normal file object.
;self.database : The [ GrampsDbBase] database object.
;self.options_class : The [ _docgenplugin ReportOptions] class passed to the report.
You'll probably need a start-person for which to write the report. This person should be obtained from the <tt>options_class</tt> object through the PersonOption class which will default to the active person in the database. Anything else the report class needs in order to produce the report should be obtained from the <tt>options_class</tt> object. For example, you may need to include the additional code in the report class constructor to obtain any options you defined for the report.
Report class '''must''' provide a <tt>write_report()</tt> method. This method should dump the report's contents into the already opened document instance.
def write_report(self): self.doc.start_paragraph("ABC-Title") self.doc.write_text(_("Some text")) self.doc.end_paragraph()
The rest of the report class is pretty much up to the report writer. Depending on the goals and the scope of the report, there can be any amount of code involved. When the user generates the report in any mode, the class constructor will be run, and then the <tt>write_report()</tt> method will be called. So if you wrote that beautiful method listing something really important, make sure it is eventually called from within the <tt>write_report()</tt>. Otherwise nobody will see it unless looking at the code.
====Using ReportOptions====
class OptionsClassName(ReportOptions): def __init__(self,name,person_id=None): ReportOptions.__init__(self,name,person_id)
* It should set new options that are specific for this report, by overriding the <tt>set_new_options()</tt> method which defines <tt>options_dict</tt> and <tt>options_help</tt> dictionaries:
def set_new_options(self): # Options specific for this report self.options_dict = { 'my_fist_optionmy_first_option' : 0, 'my_second_option' : '', } self.options_help = { 'my_fist_optionmy_first_option' : ("=num","Number of something", [ "First value", "Second value" ], True), 'my_second_option' : ("=str","Some necessary string for the report", "Whatever String You Wish"), }
* It should also enable the "semi-common" options that are used in this report, by overriding the <tt>enable_options</tt> method which defines <tt>enable_dict</tt> dictionary. The semi-commons are the options which GRAMPS Gramps knows about, but which are not necessarily present in all reports:
def enable_options(self): # Semi-common options that should be enabled for this report self.enable_dict = { 'filter' : 0, }
All the common options are already taken care of by the core of GRAMPSGramps.
* For any new options set up in the options class, there must be defined UI widgets to provide means of changing these options through the dialogs. Also, there must be defined methods to extract values of these options from the widgets and to set them into the class-variable dictionary:
def add_user_options(self,dialog): option_menu = gtk.OptionMenu() self.the_menu = gtk.Menu()
for item_index in range(10): item = _("Item numer %d") % item_index menuitem = gtk.MenuItem(item) self.the_menu.append(menuitem)
option_menu.set_menu(self.the_menu) option_menu.set_history(self.options_dict['my_first_option'])
dialog.add_option(_('My first option'),option_menu)
self.the_string_entry = gtk.Entry() if self.options_dict['my_second_option']: self.the_string_entry.set_text(self.options_dict['my_second_option']) else: self.the_string_entry.set_text(_("Empty string")) dialog.add_option(_('My second option'),self.the_string_entry)
def parse_user_options(self,dialog): self.options_dict['my_second_option'] = unicode(self.the_string_entry.get_text()) self.options_dict['my_first_option'] = self.the_menu.get_history()
* Finally, the default definitions for the user-adjustable paragraph styles must be defined here, to form a 'default' stylesheet:
def make_default_style(self, default_style): f = docgen.FontStyle() f.set_size(10) f.set_type_face(docgen.FONT_SANS_SERIF) p = docgen.ParagraphStyle() p.set_font(f) p.set_description(_("The style used for the person's name.")) default_style.add_style("ABC-Name",p)
def add_menu_options(self, menu): """ Add options to the menu for this report. """ category_name = _("Report Options") what_types = BooleanListOption(_('Select From:')) what_types.add_button(_('People'), True) what_types.add_button(_('Families'), False) what_types.add_button(_('Places'), False) what_types.add_button(_('Events'), False) menu.add_option(category_name, "what_types", what_types)
===Defining the Report Options class===
In this example, no special options are required. This makes the options class very simple. All that is necessary is to define the default styles.
** translated name (the one to display in menus)
** modes that should be enabled for the report (standalone, book item, command line)
* If the report requires an active person to run, then <code>require_active </code> should be set to <code>True</code>.
* Finally, both report class and options class should be passed to registration.
Here's the [[Quick_Views#Analysis:_registering_the_report|example registration ]] statement:
description = _("Produces a ..."),
version = '1.0',
gramps_target_version = '35.21',
status = STABLE,
fname = '',
There are arguments for the report class and options class. The <tt>report_modes</tt> argument is set to a list of zero or more of the following modes:
* REPORT_MODE_GUI (available for GRAMPS Gramps running in a window, graphical user interface)
* REPORT_MODE_BKI (available as a book report item)
* REPORT_MODE_CLI (available for the command line interface)
See an existing book report for more details.
== Manually installing your report ==
Install in the plugins directory of your Gramps user directory.
== Example output ==
Start Gramps and your tutorial report will be available from the reports menu as ....
Run the report and this is what the output looks like ....
==See also==
* [[Report_API|Report API]]
* [[Report_Generation|Report Generation]] (describing output format options)* [[Gramps_Data_Model|Gramps Data Model]]
[[Category: Plugins]]

Navigation menu