Difference between revisions of "Addons development"

From Gramps
Jump to: navigation, search
(Localization: Update for new localization scheme)
(List and document your addon on the wiki)
 
(67 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{man warn|Warning:|This page documents the API, methods, and best practices for developing a 3rd-party addon for Gramps 3.2 / 3.3 / 3.4.}}
+
{{out of date}}
 +
{{man warn|Warning:|This page documents the API, methods, and best practices for developing a 3rd-party addon for Gramps 3.2 and later }}
  
Addons for Gramps can extend the program in many different ways. You can add any of the following types of addons:
+
Addons for Gramps can extend the program in many different ways. You can add any of the following [http://sourceforge.net/p/gramps/source/ci/master/tree/gramps/gen/plug/_pluginreg.py types] of addons:
  
# Doc creator
+
#Report
# Exporter
+
#Quickreport
# Gramplet
+
#Tool
# Importer
+
#Importer
# Map service
+
#Exporter
# Plugin lib
+
#Doc creator
# Quickreport
+
#Plugin lib
# Relationships
+
#Map service
# Report
+
#Gramps View
# Tool
+
#Relationships
# View
+
#Gramplet
 +
#Sidebar
  
 
Writing an addon is fairly straightforward if you have just a little bit of Python experience. And sharing your addon is the right thing to do. The general steps to writing an addon and sharing your own addons are:
 
Writing an addon is fairly straightforward if you have just a little bit of Python experience. And sharing your addon is the right thing to do. The general steps to writing an addon and sharing your own addons are:
Line 22: Line 24:
 
# Package your addon
 
# Package your addon
 
# List and document your addon
 
# List and document your addon
# Support it through issue tracker
+
# Support it through the issue tracker
 
# Maintain the code as Gramps continues to evolve
 
# Maintain the code as Gramps continues to evolve
  
Line 29: Line 31:
 
== Develop your addon ==
 
== Develop your addon ==
  
gramps-addons subversion repository has the following structure:
+
===Gramps 4.1 and earlier===
 +
 
 +
The [http://svn.code.sf.net/p/gramps-addons/code/ gramps-addons] subversion repository has the following structure:
  
 
* /gramps-addons
 
* /gramps-addons
Line 36: Line 40:
 
*** /download
 
*** /download
 
** /branches
 
** /branches
*** /gramps{{stable_branch}}
+
*** /gramps41
*** /gramps32
+
*** /gramps34
 
**** /contrib
 
**** /contrib
 
**** /download
 
**** /download
  
The contrib subdirectories hold the source code for the addons for a particular version. If you are working on a addon for gramps{{stable_branch}} then you should be working in gramps-addons/branches/gramps{{stable_branch}}/contrib. If you are working in gramps/trunk then you should use gramps-addons/trunk/contrib.
+
The contrib subdirectories hold the source code for the addons for a particular version. If you are working on a addon for <code>gramps41</code> then you should be working in <code>gramps-addons/branches/gramps41/contrib</code>.  
  
These steps show how to download and work with the addon development tools.
+
{{man warn|Note|that Gramps/trunk is no longer used on Sourceforge. To work on Gramp/master you will need to switch to Github and use [https://github.com/gramps-project/addons-source addons-source] See below}}
  
# Checkout the gramps-addons files from the [https://sourceforge.net/projects/gramps-addons/ gramps-addons] project:
+
===Gramps 4.2 and later===
## cd into gramps trunk, for example:
+
### cd ~/gramps/trunk
+
## Checkout gramps-addons:
+
### svn co https://svn.code.sf.net/p/gramps-addons/code gramps-addons
+
## Change to trunk or branches/gramps{{stable_branch}} directory:
+
### cd gramps-addons/branches/gramps{{stable_branch}}/contrib
+
# Make a new project directory in gramps-addon/branches/gramps{{stable_branch}}/contrib:
+
## mkdir NewProjectName
+
# Initialize the addon:
+
## ./make.py init NewProjectName
+
  
NOTE: to use make.py as shown throughout this document, you may have to use:
+
The [http://github.com/gramps-project/addons-source addons-source] git repository has the following structure:
  
  GRAMPSPATH=/path/to/gramps python make.py ...
+
* /addons-source
 +
** /''IndividualNameOfAddon1''
 +
** /''IndividualNameOfAddon2''
 +
** ...
  
if the default ("../../..") is not correct.
+
The [http://github.com/gramps-project/addons addons] git repository has the following structure:
  
 +
* /addons
 +
** /gramps42
 +
*** /download
 +
*** /listings
 +
** /gramps50
 +
*** /download
 +
*** /listings
 +
 +
addon-source holds the source code for the addons with branches holding the version for different gramps. If you are working on a addon for gramps for gramps42 or later then you should be working in addons-source.
 +
 +
==== Setup the addon development tools for Gramps 4.1 and earlier ====
 +
 +
These steps show how to download and work with the addon development tools.
 +
 +
{{man tip| 1=Tip |2=To use make.py as shown throughout this document, you may have to use:<br />
 +
<pre>GRAMPSPATH=/path/to/gramps python make.py ...</pre> <br /> if the default ("../../..") is not correct.}}
 +
 +
* Checkout the gramps-addons files from the [https://sourceforge.net/projects/gramps-addons/ gramps-addons] project:
 +
** cd into gramps trunk, for example:
 +
*** <pre>cd ~/gramps/trunk</pre>
 +
** Checkout gramps-addons:
 +
*** <pre>svn co https://svn.code.sf.net/p/gramps-addons/code gramps-addons</pre>
 +
** Change to trunk or branches/gramps41 directory:
 +
*** <pre>cd gramps-addons/branches/gramps41/contrib</pre>
 +
* Make a new project directory in gramps-addon/branches/gramps41/contrib:
 +
** <pre>mkdir NewProjectName</pre>
 +
* Initialize the addon:
 +
** <pre>./make.py init NewProjectName</pre>
 +
 +
===Follow the development API for your tool===
 
Follow the development API for your tool, [[Report-writing_tutorial|report]], view, or [[Gramplets]]. Place all of your associated .py, .glade, etc. files in this directory. For general information on Gramps development see [[Portal:Developers]] and [[Writing a Plugin]] specifically.
 
Follow the development API for your tool, [[Report-writing_tutorial|report]], view, or [[Gramplets]]. Place all of your associated .py, .glade, etc. files in this directory. For general information on Gramps development see [[Portal:Developers]] and [[Writing a Plugin]] specifically.
  
 +
=== Test your addon as you develop ===
 
To test your addon as you develop it it is suggested that you replace your Gramps user plugin directory with a link to your addon development directory, like so:
 
To test your addon as you develop it it is suggested that you replace your Gramps user plugin directory with a link to your addon development directory, like so:
  
  cd ~/.gramps/gramps{{stable_branch}}/
+
  cd ~/.gramps/gramps41/
  mv plugins/* /wherever/trunk/gramps-addons/branches/gramps{{stable_branch}}/contrib/
+
  mv plugins/* /wherever/trunk/gramps-addons/branches/gramps41/contrib/
 
  rm -rf plugins
 
  rm -rf plugins
  ln -s /wherever/trunk/gramps-addons/branches/gramps{{stable_branch}}/contrib plugins
+
  ln -s /wherever/trunk/gramps-addons/branches/gramps41/contrib plugins
  
 
Gramps will search this folder (and subdirectories) for .grp.py files, and add them to the plugin list.
 
Gramps will search this folder (and subdirectories) for .grp.py files, and add them to the plugin list.
Line 76: Line 104:
 
If you have code that you want to share between addons, you don't need to do anything special. Currently, Gramps adds each directory in which a .gpr.py is found onto the PYTHONPATH which is searched when you perform an import. Thus "import NewProjectName" will work from another addon. You should always make sure you name your addons with a name appropriate for Python imports.
 
If you have code that you want to share between addons, you don't need to do anything special. Currently, Gramps adds each directory in which a .gpr.py is found onto the PYTHONPATH which is searched when you perform an import. Thus "import NewProjectName" will work from another addon. You should always make sure you name your addons with a name appropriate for Python imports.
  
To commit your changes so that others can use your addon, follow these steps:
+
=== Commit your changes ===
 +
To commit your changes so that others can use your addon.
 +
====Gramps 4.1 and earlier====
 +
For Gramps 4.1 and earlier follow these steps:
  
 
# Get an http://sourceforge.net account if you don't already have one.
 
# Get an http://sourceforge.net account if you don't already have one.
# Request SVN write access for the gramps-addon project from https://sourceforge.net/project/memberlist.php?group_id=285429
+
# Request SVN write access for the gramps-addon project by emailing one of the admins of the project (listed under the gramps-addon title next to the group icon) from http://sourceforge.net/projects/gramps-addons/
 
# Remove the files that should not be added to SVN:
 
# Remove the files that should not be added to SVN:
## ./make.py clean NewProjectName
+
## <pre>./make.py clean NewProjectName</pre>
 
# Add the project to the repository:
 
# Add the project to the repository:
## svn add NewProjectName
+
## <pre>svn add NewProjectName</pre>
## svn commit -m "A message describing what this addon is"
+
## <pre>svn commit -m "A message describing what this addon is"</pre>
  
 
Before making additional edits to your addon, you should:
 
Before making additional edits to your addon, you should:
  
# svn update
+
# <pre>svn update</pre>
# svn status
+
# <pre>svn status</pre>
# svn commit -m "A message describing the changes"
+
# <pre>svn commit -m "A message describing the changes"</pre>
 +
 
 +
Also you may want to [[Addons_development#Package_your_addon |Package your addon]] so it can be downloaded via the plugin manager.
 +
 
 +
====Gramps 4.2 and later====
 +
For Gramps 4.2 and later follow these steps:
 +
# Get an https://github.com/join account if you don't already have one.
 +
# Request GIT write access for the https://github.com/gramps-project/addons-source project by emailing the [[Contact#Mailing_lists|gramps-devel mailing list]]
 +
See also [[Brief_introduction_to_Git|git introduction]].
 +
 
 +
git clone git@github.com:gramps-project/addons-source.git addons-source
 +
 
 +
or if you do not have a Github account:
 +
 
 +
git clone https://github.com/gramps-project/addons-source.git addons-source
 +
 
 +
To switch to a local copy of the gramps42 branch:
 +
 
 +
git checkout -b gramps42 origin/maintenance/gramps42
  
 
=== Config ===
 
=== Config ===
Line 96: Line 145:
 
Some addons may want to have persistent data (data settings that remain between sessions). You can handle this yourself, or you can use Gramps' built-in configure system.  
 
Some addons may want to have persistent data (data settings that remain between sessions). You can handle this yourself, or you can use Gramps' built-in configure system.  
  
In the file that defines the settings, you would do this:
+
At the top of the source file of your addon, you would do this:
  
  from config import config
+
  from config import config as configman
  cm = config.register_manager("view_placetreeview_0")
+
  config = configman.register_manager("grampletname")
  cm.register("section.variable1", value1)
+
  # register the values to save:
  cm.register("section.variable2", value2)
+
config.register("section.option-name1", value1)
 +
  config.register("section.option-name2", value2)
 
  ...
 
  ...
  cm.init()
+
  # load an existing file, if one:
 +
config.load()
 +
# save it, it case it didn't exist:
 +
config.save()
  
This will create the file "view_placetreeview_0.ini" and put in the same directory as the addon. In the addon, you can then:
+
This will create the file "grampletname.ini" and put in the same directory as the addon. If the config file already exists, it remains intact.
  
  x = cm.get("section.variable1")
+
In the addon, you can then:
  cm.set("section.variable1", 3)
+
 
 +
  x = config.get("section.option-name1")
 +
  config.set("section.option-name1", 3)
  
 
and when this code is exiting, you might want to save the config. In a Gramplet that would be:
 
and when this code is exiting, you might want to save the config. In a Gramplet that would be:
  
 
  def on_save(self):
 
  def on_save(self):
     cm.save()
+
     config.save()
  
 
If your code is a system-level file, then you might want to save the config in the Gramps system folder:
 
If your code is a system-level file, then you might want to save the config in the Gramps system folder:
  
  cm = config.register_manager("system", use_config_path=True)
+
  config = configman.register_manager("system", use_config_path=True)
  
 
This, however, would be rare; most .ini files would go into the plugins directory.
 
This, however, would be rare; most .ini files would go into the plugins directory.
Line 123: Line 178:
 
In other code that might use this config file, you would do this:
 
In other code that might use this config file, you would do this:
  
  from config import config
+
  from config import config as configman
  cm = config.get_manager("view_placetreeview_0")
+
  config = configman.get_manager("grampletname")
  x = cm.get("section.variable1")
+
  x = config.get("section.option-name1")
  
 
=== Localization ===
 
=== Localization ===
Line 162: Line 217:
  
 
'''sgettext''' is a Gramps extension that filters out clarifying comments for translators, such as
 
'''sgettext''' is a Gramps extension that filters out clarifying comments for translators, such as
  _(Remaining names | rest)
+
  _("Remaining names | rest")
Where "rest" is the English string that we want to present and "remaining names" is a hint for translators which let them know that we mean "what's left" rather than "take a nap".
+
Where "rest" is the English string that we want to present and "Remaining names" is a hint for translators.
  
 
== Create a Gramps Plugin Registration file ==
 
== Create a Gramps Plugin Registration file ==
Line 177: Line 232:
 
</pre>
 
</pre>
  
PTYPE is TOOL, GRAMPLET, REPORT, QUICKREPORT, IMPORT, EXPORT, DOCGEN, GENERAL, MAPSERVICE, VIEW, or RELCALC.
+
[http://sourceforge.net/p/gramps/source/ci/master/tree/gramps/gen/plug/_pluginreg.py#l78 PTYPE] is TOOL, GRAMPLET, REPORT, QUICKVIEW, IMPORT, EXPORT, DOCGEN, GENERAL, MAPSERVICE, VIEW, or RELCALC.
 +
 
  
 
ATTR depends on the PTYPE. But you must have '''gramps_target_version''' and '''version'''. gramps_target_version should be a string of the form "X.Y" version number matching Gramps X major, Y minor integer. version is a string of the form "X.Y.Z" representing the version of your addon. X, Y, and Z should all be integers.
 
ATTR depends on the PTYPE. But you must have '''gramps_target_version''' and '''version'''. gramps_target_version should be a string of the form "X.Y" version number matching Gramps X major, Y minor integer. version is a string of the form "X.Y.Z" representing the version of your addon. X, Y, and Z should all be integers.
Line 201: Line 257:
 
</pre>
 
</pre>
  
You can see examples of the kinds of addons [http://svn.code.sf.net/p/gramps/code/trunk/gramps/plugins/ here] (for example, see [http://svn.code.sf.net/p/gramps/code/trunk/gramps/plugins/drawreport/drawplugins.gpr.py trunk/gramps/plugins/drawreport/drawplugins.gpr.py]) and see the full documentation [http://svn.code.sf.net/p/gramps/code/trunk/gramps/gen/plug/_pluginreg.py here] in the comments and docstrings.
+
You can see examples of the kinds of addons [http://sourceforge.net/p/gramps/source/ci/master/tree/gramps/plugins/ here] (for example, see [http://sourceforge.net/p/gramps/source/ci/master/tree/gramps/plugins/drawreport/drawplugins.gpr.py gramps/source/ci/master/tree/gramps/plugins/drawreport/drawplugins.gpr.py]) and see the full documentation [http://sourceforge.net/p/gramps/source/ci/master/tree/gramps/gen/plug/_pluginreg.py here] in the comments and docstrings.
  
 
Note that this .gpr.py will automatically use translations if you have them (see below). That is, the function "_" is predefined to use your locale translations; you only need to mark the text with _("TEXT") and include a translation of "TEXT" in your translation file. For example, in the above example, _("Attach Source") is marked for translation. If you have developed and packaged your addon with translation support, then that phrase will be converted into the user's language.
 
Note that this .gpr.py will automatically use translations if you have them (see below). That is, the function "_" is predefined to use your locale translations; you only need to mark the text with _("TEXT") and include a translation of "TEXT" in your translation file. For example, in the above example, _("Attach Source") is marked for translation. If you have developed and packaged your addon with translation support, then that phrase will be converted into the user's language.
Line 220: Line 276:
  
 
Each report category has a set of standards and interface. The categories CATEGORY_TEXT and CATEGORY_DRAW use the Document interface of Gramps. See also [[Report API]] for a draft view on this.
 
Each report category has a set of standards and interface. The categories CATEGORY_TEXT and CATEGORY_DRAW use the Document interface of Gramps. See also [[Report API]] for a draft view on this.
The application programming interface or API for reports is treated at [[Report-writing_tutorial]]. For general information on GRAMPS development see [[Portal:Developers]] and [[Writing a plugin]] specifically.
+
The application programming interface or API for reports is treated at [[Report-writing_tutorial]]. For general information on Gramps development see [[Portal:Developers]] and [[Writing a plugin]] specifically.
  
 
=== General plugins ===
 
=== General plugins ===
Line 374: Line 430:
 
== Get translators to translate your addon into multiple languages ==
 
== Get translators to translate your addon into multiple languages ==
  
# Initialize and update the template.pot for your addon:
+
* Initialize and update the template.pot for your addon:
## ./make.py init NewProjectName
+
** <pre>./make.py init NewProjectName</pre>
# Initialize a language for your addon (say French, fr):
+
* Initialize a language for your addon (say French, fr):
## ./make.py init NewProjectName fr
+
** <pre>./make.py init NewProjectName fr</pre>
# Update it from gramps and other addons:
+
* Update it from gramps and other addons:
## ./make.py update NewProjectName fr
+
** <pre>./make.py update NewProjectName fr</pre>
# Edit contrib/NewProjectName/po/fr-local.po
+
* Edit <pre>contrib/NewProjectName/po/fr-local.po</pre>
# Compile the language:
+
* Compile the language:
## ./make.py compile NewProjectName
+
** <pre>./make.py compile NewProjectName</pre>
# Add or update your local language file, and commit changes:
+
* Add or update your local language file, and commit changes:
## svn add NewProjectName/po/fr-local.po
+
** <pre>svn add NewProjectName/po/fr-local.po</pre>
## svn commit NewProjectName/po/fr-local.po -m "Added fr po file"
+
** <pre>svn commit NewProjectName/po/fr-local.po -m "Added fr po file"</pre>
  
 
== Package your addon ==
 
== Package your addon ==
Line 393: Line 449:
 
  python make.py build NewProjectName
 
  python make.py build NewProjectName
  
That will build and copy your addon to ../download
+
This will automatically include the following files in your build:
  
NOTE: Running the '''make.py build''' will increment the third number in your dotted version number of all addons in the gpr.py file. Consider this number to be a "build number".
+
* *.py
 +
* *.glade
 +
* *.xml
 +
* *.txt
 +
* locale/*/LC_MESSAGES/*.mo
  
''New for Gramps 3.4'': You need to then make your addon available in listings of various languages. To do that:
+
Starting with Gramp 5.0, if you have additional files beyond those listed above, you should create a MANIFEST file in the root of your addon folder listing the files (or pattern) one per line, like this sample MANIFEST file:  
  
python make.py listing
+
<pre>
 +
README.md
 +
extra_dir/*
 +
help_files/docs/help.html
 +
</pre>
  
That will create a series of files in the ../listings/
+
{{man note|Note:|Running the command '''make.py build''' will increment the third number in your dotted version number of all addons in the gpr.py file. Consider this number to be a "build number".}}
  
 
Then add the package to SVN:
 
Then add the package to SVN:
  
  svn add ../download/NewProjectName.addon.tgz ../listings/*
+
  svn add ../download/NewProjectName.addon.tgz
 
  cd ..
 
  cd ..
 
  svn commit -m "Message describing changes"
 
  svn commit -m "Message describing changes"
 +
 +
== List your addon in the Gramps Plugin Manager==
 +
''New for Gramps 3.4'': You need to then make your addon available in listings of various languages.
 +
 +
{{man warn|Gramps needs to have been built|Make sure you have already built gramps34 or gramps41 or master}}
 +
 +
To create a listing:
 +
 +
cd gramps-addons/branches/gramps41/contrib                  # or wherever you have built your addon
 +
GRAMPSPATH=path/to/your/gramps/install python make.py listing NewProjectName
 +
 +
That will create a series of files in the <tt>../listings/</tt> directory.
 +
 +
Then add the updated listing to SVN:
 +
 +
svn add ../listings/*
 +
cd ..
 +
svn commit -m "Message describing changes"
 +
 +
== List and document your addon on the wiki==
 +
 +
Document the addon in the wiki using the name {{man menu|Addon:NewProjectName}}.
 +
 +
Edit [[Plugins3.4]] or [[4.2_Addons]] and describe your addon.
 +
 +
You can point to the addon.tgz in SVN or GIT as the downloadable file.
  
 
== Miscellaneous commands ==
 
== Miscellaneous commands ==
  
To build all projects:
+
To build and compile translations for all projects to their download/Addon.addon.tgz files:
  
 
  python make.py build all
 
  python make.py build all
  
To compile all projects to their download/Addon.addon.tgz files:
+
To compile translations for all projects :
  
 
  python make.py compile all
 
  python make.py compile all
  
== List and document your addon ==
+
== Support it through issue tracker ==
  
Edit [[Plugins3.4]] and describe your addon. You can point to the addon.tgz in SVN as the downloadable file. Document the addon in the wiki using the name "Addon:NewProjectName".
+
Visit https://gramps-project.org/bugs/view_all_bug_page.php and become a user. Suggest to check it regularly.
  
== Support it through issue tracker ==
+
== Maintain the code as Gramps continues to evolve ==
  
Visit http://www.gramps-project.org/bugs/view_all_bug_page.php and become a user. Suggest to check it regularly.
+
Remember that Gramps addons exist for many reasons and there are many
 +
Gramps developers that do support addons in various ways (translations,
 +
triage, keeping in sync with master, download infrastructure, etc).
  
== Maintain the code as GRAMPS continues to evolve ==
+
Some reasons why the addons exist; they provide:
 +
* A quick way for anyone to share their work; the Gramps-project has never denied adding a addon.
 +
* A method to continuously update and develop a stand-alone component, often before being officially accepted.
 +
* A place for controversial plugins that will never be accepted into core, but are loved by many users (eg, Data Entry Gramplet).
 +
* A place for experimental components to live.
  
 
= Resources =
 
= Resources =
 +
* [[Brief_introduction_to_Git|Git introduction]]
 +
 +
;Gramps Addons site for Gramps 4.2 and newer
 +
* https://github.com/gramps-project/addons-source  - Source code (Git)
 +
* https://github.com/gramps-project/addons - downloadable .tgz files
  
* http://gramps-addons.svn.sourceforge.net/viewvc/gramps-addons/ - SVN browse
+
;Gramps Addons site for Gramps 3.2 to Gramps 4.1.
 +
* https://sourceforge.net/p/gramps-addons/ - Source code (SVN) and downloadable .tgz files
  
 
[[Category:Developers/General]]
 
[[Category:Developers/General]]
Line 438: Line 540:
 
[[Category:Reports]]
 
[[Category:Reports]]
 
[[Category:Gramplets]]
 
[[Category:Gramplets]]
 +
[[Category:Addons]]

Latest revision as of 14:54, 5 August 2015

Gramps-notes.png
This page's factual accuracy may be compromised due to out-of-date information. Please help improve the Gramps Wiki as a useful resource by updating it.
Gnome-important.png
Warning:

This page documents the API, methods, and best practices for developing a 3rd-party addon for Gramps 3.2 and later

Addons for Gramps can extend the program in many different ways. You can add any of the following types of addons:

  1. Report
  2. Quickreport
  3. Tool
  4. Importer
  5. Exporter
  6. Doc creator
  7. Plugin lib
  8. Map service
  9. Gramps View
  10. Relationships
  11. Gramplet
  12. Sidebar

Writing an addon is fairly straightforward if you have just a little bit of Python experience. And sharing your addon is the right thing to do. The general steps to writing an addon and sharing your own addons are:

  1. Develop your addon
  2. Create a Gramps Plugin Registration file (.gpr.py)
  3. Get translators to translate your addon into multiple languages
  4. Package your addon
  5. List and document your addon
  6. Support it through the issue tracker
  7. Maintain the code as Gramps continues to evolve

We'll now look at each of these steps in detail.

Develop your addon

Gramps 4.1 and earlier

The gramps-addons subversion repository has the following structure:

  • /gramps-addons
    • /trunk
      • /contrib
      • /download
    • /branches
      • /gramps41
      • /gramps34
        • /contrib
        • /download

The contrib subdirectories hold the source code for the addons for a particular version. If you are working on a addon for gramps41 then you should be working in gramps-addons/branches/gramps41/contrib.

Gnome-important.png
Note

that Gramps/trunk is no longer used on Sourceforge. To work on Gramp/master you will need to switch to Github and use addons-source See below

Gramps 4.2 and later

The addons-source git repository has the following structure:

  • /addons-source
    • /IndividualNameOfAddon1
    • /IndividualNameOfAddon2
    • ...

The addons git repository has the following structure:

  • /addons
    • /gramps42
      • /download
      • /listings
    • /gramps50
      • /download
      • /listings

addon-source holds the source code for the addons with branches holding the version for different gramps. If you are working on a addon for gramps for gramps42 or later then you should be working in addons-source.

Setup the addon development tools for Gramps 4.1 and earlier

These steps show how to download and work with the addon development tools.

Gnome-important.png
Tip

To use make.py as shown throughout this document, you may have to use:

GRAMPSPATH=/path/to/gramps python make.py ...

if the default ("../../..") is not correct.


  • Checkout the gramps-addons files from the gramps-addons project:
    • cd into gramps trunk, for example:
      • cd ~/gramps/trunk
    • Checkout gramps-addons:
      • svn co https://svn.code.sf.net/p/gramps-addons/code gramps-addons
    • Change to trunk or branches/gramps41 directory:
      • cd gramps-addons/branches/gramps41/contrib
  • Make a new project directory in gramps-addon/branches/gramps41/contrib:
    • mkdir NewProjectName
  • Initialize the addon:
    • ./make.py init NewProjectName

Follow the development API for your tool

Follow the development API for your tool, report, view, or Gramplets. Place all of your associated .py, .glade, etc. files in this directory. For general information on Gramps development see Portal:Developers and Writing a Plugin specifically.

Test your addon as you develop

To test your addon as you develop it it is suggested that you replace your Gramps user plugin directory with a link to your addon development directory, like so:

cd ~/.gramps/gramps41/
mv plugins/* /wherever/trunk/gramps-addons/branches/gramps41/contrib/
rm -rf plugins
ln -s /wherever/trunk/gramps-addons/branches/gramps41/contrib plugins

Gramps will search this folder (and subdirectories) for .grp.py files, and add them to the plugin list.

If you have code that you want to share between addons, you don't need to do anything special. Currently, Gramps adds each directory in which a .gpr.py is found onto the PYTHONPATH which is searched when you perform an import. Thus "import NewProjectName" will work from another addon. You should always make sure you name your addons with a name appropriate for Python imports.

Commit your changes

To commit your changes so that others can use your addon.

Gramps 4.1 and earlier

For Gramps 4.1 and earlier follow these steps:

  1. Get an http://sourceforge.net account if you don't already have one.
  2. Request SVN write access for the gramps-addon project by emailing one of the admins of the project (listed under the gramps-addon title next to the group icon) from http://sourceforge.net/projects/gramps-addons/
  3. Remove the files that should not be added to SVN:
    1. ./make.py clean NewProjectName
  4. Add the project to the repository:
    1. svn add NewProjectName
    2. svn commit -m "A message describing what this addon is"

Before making additional edits to your addon, you should:

  1. svn update
  2. svn status
  3. svn commit -m "A message describing the changes"

Also you may want to Package your addon so it can be downloaded via the plugin manager.

Gramps 4.2 and later

For Gramps 4.2 and later follow these steps:

  1. Get an https://github.com/join account if you don't already have one.
  2. Request GIT write access for the https://github.com/gramps-project/addons-source project by emailing the gramps-devel mailing list

See also git introduction.

git clone git@github.com:gramps-project/addons-source.git addons-source

or if you do not have a Github account:

git clone https://github.com/gramps-project/addons-source.git addons-source

To switch to a local copy of the gramps42 branch:

git checkout -b gramps42 origin/maintenance/gramps42

Config

Some addons may want to have persistent data (data settings that remain between sessions). You can handle this yourself, or you can use Gramps' built-in configure system.

At the top of the source file of your addon, you would do this:

from config import config as configman
config = configman.register_manager("grampletname")
# register the values to save:
config.register("section.option-name1", value1)
config.register("section.option-name2", value2)
...
# load an existing file, if one:
config.load()
# save it, it case it didn't exist:
config.save()

This will create the file "grampletname.ini" and put in the same directory as the addon. If the config file already exists, it remains intact.

In the addon, you can then:

x = config.get("section.option-name1")
config.set("section.option-name1", 3)

and when this code is exiting, you might want to save the config. In a Gramplet that would be:

def on_save(self):
    config.save()

If your code is a system-level file, then you might want to save the config in the Gramps system folder:

config = configman.register_manager("system", use_config_path=True)

This, however, would be rare; most .ini files would go into the plugins directory.

In other code that might use this config file, you would do this:

from config import config as configman
config = configman.get_manager("grampletname")
x = config.get("section.option-name1")

Localization

For general help on translations in Gramps, see Coding for translation. However, that will only use translations that come with Gramps, or allows you to contribute translations to the Gramps core. To have your own managed translations that will be packaged with your addon, read the rest of this page.

For any addon which you have translations into other languages, you will need to add a way to retrieve the translation. You need to add this to the top of your NewProjectName.py file:

For Gramps 3:

from TransUtils import get_addon_translator
_ = get_addon_translator(__file__).gettext

For Gramps 4:

from gramps.gen.const import GRAMPS_LOCALE as glocale
_ = glocale.get_addon_translator(__file__).gettext


Then you can use the standard "_()" function to translate phrases in your addon.

You can use one of a few different types of translation functions:

  1. gettext
  2. lgettext
  3. ngettext
  4. lngettext
  5. sgettext

Gramps 3 also provides:

  1. ugettext
  2. ungettext

These have become obsolete in Gramps 4; gettext, ngettext, and sgettext always return translated strings in unicode for consistent portability between Python 2 and Python3.

See the python documentation for documentation of gettext and ngettext. The "l" versions return the string encoded according to the currently set locale; the "u" versions return unicode strings in Python2 and are not available in Python 3.

sgettext is a Gramps extension that filters out clarifying comments for translators, such as

_("Remaining names | rest")

Where "rest" is the English string that we want to present and "Remaining names" is a hint for translators.

Create a Gramps Plugin Registration file

First, create the NewProjectName.gpr.py file. The registration takes this general form:

register(PTYPE,
     gramps_target_version = "3.4",
     version = "1.0.0",
     ATTR = value,
)

PTYPE is TOOL, GRAMPLET, REPORT, QUICKVIEW, IMPORT, EXPORT, DOCGEN, GENERAL, MAPSERVICE, VIEW, or RELCALC.


ATTR depends on the PTYPE. But you must have gramps_target_version and version. gramps_target_version should be a string of the form "X.Y" version number matching Gramps X major, Y minor integer. version is a string of the form "X.Y.Z" representing the version of your addon. X, Y, and Z should all be integers.

Here is a sample Tool GPR file:

register(TOOL, 
         id    = 'AttachSource',
         name  = _("Attach Source"),
         description =  _("Attaches a shared source to multiple objects."),
         version = '1.0.0',
         gramps_target_version = '3.4',
         status = STABLE,
         fname = 'AttachSourceTool.py',
         authors = ["Douglas S. Blank"],
         authors_email = ["doug.blank@gmail.com"],
         category = TOOL_DBPROC,
         toolclass = 'AttachSourceWindow',
         optionclass = 'AttachSourceOptions',
         tool_modes = [TOOL_MODE_GUI]
         )

You can see examples of the kinds of addons here (for example, see gramps/source/ci/master/tree/gramps/plugins/drawreport/drawplugins.gpr.py) and see the full documentation here in the comments and docstrings.

Note that this .gpr.py will automatically use translations if you have them (see below). That is, the function "_" is predefined to use your locale translations; you only need to mark the text with _("TEXT") and include a translation of "TEXT" in your translation file. For example, in the above example, _("Attach Source") is marked for translation. If you have developed and packaged your addon with translation support, then that phrase will be converted into the user's language.

Report plugins

The possible report categories are (gen/plug/_pluginreg.py):

#possible report categories
CATEGORY_TEXT       = 0
CATEGORY_DRAW       = 1
CATEGORY_CODE       = 2
CATEGORY_WEB        = 3
CATEGORY_BOOK       = 4
CATEGORY_GRAPHVIZ   = 5
REPORT_CAT          = [ CATEGORY_TEXT, CATEGORY_DRAW, CATEGORY_CODE,
                        CATEGORY_WEB, CATEGORY_BOOK, CATEGORY_GRAPHVIZ]

Each report category has a set of standards and interface. The categories CATEGORY_TEXT and CATEGORY_DRAW use the Document interface of Gramps. See also Report API for a draft view on this. The application programming interface or API for reports is treated at Report-writing_tutorial. For general information on Gramps development see Portal:Developers and Writing a plugin specifically.

General plugins

The plugin framework also allows you to create generic plugins for use. This includes the ability to create libraries of functions, and plugins of your own design.

Example: A library of functions

In this example, a file name library.py will be imported at time of registration (when Gramps starts):

# file: library.gpr.py

register(GENERAL, 
   id    = 'My Library',
   name  = _("My Library"),
   description =  _("Provides a library for doing something."),
   version = '1.0',
   gramps_target_version = '3.4',
   status = STABLE,
   fname = 'library.py',
   load_on_reg = True,
  )

The code in the file library.py will be imported when Gramps begins. You can access the loaded module in other code by issuing an "import library" as Python keeps track of files already imported. However, the amount of useful code that you can run when the program is imported is limited. You might like to have the code do something that requires a dbstate or uistate object, and neither of these is available when just importing a file.

If "load_on_reg" was not True, then this code would be unavailable until manually loaded. There is no automatic mechanism in Gramps to load GENERAL plugins automatically.

In addition to importing a file at startup, one can also run a single function inside a GENERAL plugin, and it will be passed the dbstate, the uistate, and the plugin data. The function must be called "load_on_reg", and take those three parameters, like this:

# file: library.py

def load_on_reg(dbstate, uistate, plugin):
    """
    Runs when plugin is registered.
    """
    print "Hello World!"

Here, you could connect signals to the dbstate, open windows, etc.

Another example of what one can do with the plugin interface is to create a general purpose plugin framework for use by other plugins. Here is the basis for a plugin system that:

  • allows plugins to list data files
  • allows the plugin to process all of the data files

First, the gpr.py file:


register(GENERAL, 
  id    = "ID",
  category = "CATEGORY",
  load_on_reg = True,
  process = "FUNCTION_NAME",
  )

This example uses three new features:

  1. GENERAL plugins can have a category
  2. GENERAL plugins can have a load_on_reg function that returns data
  3. GENERAL plugins can have a function (called "process") which will process the data

If you (or someone else) create additional general plugins of this category, and they follow your load_on_reg data format API, then they could be used just like your original data. For example, here is an additional general plugin in the 'WEBSTUFF' category:

# anew.gpr.py

register(GENERAL, 
  id    = 'a new plugin',
  category = "WEBSTUFF",
  version = '1.0',
  gramps_target_version = '3.4',
  data = ["a", "b", "c"],
  )

This doesn't have load_on_reg = True, nor does it have a fname or process, but it does set the data directly in the .gpr.py file. Then we have the following results:

>>> from gui.pluginmanager import GuiPluginManager
>>> PLUGMAN = GuiPluginManager.get_instance()
>>> PLUGMAN.get_plugin_data('WEBSTUFF')
["a", "b", "c", "Stylesheet.css", "Another.css"]
>>> PLUGMAN.process_plugin_data('WEBSTUFF')
["A", "B", "C", "STYLESHEET.CSS", "ANOTHER.CSS"]

Registered GENERAL Categories

The following are the published secondary plugins API's (type GENERAL, with the following categories):

WEBSTUFF

A sample gpr.py file:

# stylesheet.gpr.py

register(GENERAL, 
  id    = 'system stylesheets',
  category = "WEBSTUFF",
  name  = _("CSS Stylesheets"),
  description =  _("Provides a collection of stylesheets for the web"),
  version = '1.0',
  gramps_target_version = '3.4',
  fname = "stylesheet.py",
  load_on_reg = True,
  process = "process_list",
  )

Here is the associated program:

# file: stylesheet.py

def load_on_reg(dbstate, uistate, plugin):
    """
    Runs when plugin is registered.
    """
    return ["Stylesheet.css", "Another.css"]

def process_list(files):
    return [file.upper() for file in files]

Filters

For example:

register(GENERAL,
   category="Filters",
   ...
   load_on_reg = True
)
def load_on_reg(dbstate, uistate, plugin):
    # returns a function that takes a namespace, 'Person', 'Family', etc.

    def filters(namespace):
        print "Ok...", plugin.category, namespace, uistate
        # return a Filter object here
    return filters

Get translators to translate your addon into multiple languages

  • Initialize and update the template.pot for your addon:
    • ./make.py init NewProjectName
  • Initialize a language for your addon (say French, fr):
    • ./make.py init NewProjectName fr
  • Update it from gramps and other addons:
    • ./make.py update NewProjectName fr
  • Edit
    contrib/NewProjectName/po/fr-local.po
  • Compile the language:
    • ./make.py compile NewProjectName
  • Add or update your local language file, and commit changes:
    • svn add NewProjectName/po/fr-local.po
    • svn commit NewProjectName/po/fr-local.po -m "Added fr po file"

Package your addon

To create a downloadable package:

python make.py build NewProjectName

This will automatically include the following files in your build:

  • *.py
  • *.glade
  • *.xml
  • *.txt
  • locale/*/LC_MESSAGES/*.mo

Starting with Gramp 5.0, if you have additional files beyond those listed above, you should create a MANIFEST file in the root of your addon folder listing the files (or pattern) one per line, like this sample MANIFEST file:

README.md
extra_dir/*
help_files/docs/help.html
Gramps-notes.png
Note:

Running the command make.py build will increment the third number in your dotted version number of all addons in the gpr.py file. Consider this number to be a "build number".

Then add the package to SVN:

svn add ../download/NewProjectName.addon.tgz
cd ..
svn commit -m "Message describing changes"

List your addon in the Gramps Plugin Manager

New for Gramps 3.4: You need to then make your addon available in listings of various languages.

Gnome-important.png
Gramps needs to have been built

Make sure you have already built gramps34 or gramps41 or master

To create a listing:

cd gramps-addons/branches/gramps41/contrib                  # or wherever you have built your addon
GRAMPSPATH=path/to/your/gramps/install python make.py listing NewProjectName

That will create a series of files in the ../listings/ directory.

Then add the updated listing to SVN:

svn add ../listings/*
cd ..
svn commit -m "Message describing changes"

List and document your addon on the wiki

Document the addon in the wiki using the name Addon:NewProjectName.

Edit Plugins3.4 or 4.2_Addons and describe your addon.

You can point to the addon.tgz in SVN or GIT as the downloadable file.

Miscellaneous commands

To build and compile translations for all projects to their download/Addon.addon.tgz files:

python make.py build all

To compile translations for all projects :

python make.py compile all

Support it through issue tracker

Visit https://gramps-project.org/bugs/view_all_bug_page.php and become a user. Suggest to check it regularly.

Maintain the code as Gramps continues to evolve

Remember that Gramps addons exist for many reasons and there are many Gramps developers that do support addons in various ways (translations, triage, keeping in sync with master, download infrastructure, etc).

Some reasons why the addons exist; they provide:

  • A quick way for anyone to share their work; the Gramps-project has never denied adding a addon.
  • A method to continuously update and develop a stand-alone component, often before being officially accepted.
  • A place for controversial plugins that will never be accepted into core, but are loved by many users (eg, Data Entry Gramplet).
  • A place for experimental components to live.

Resources

Gramps Addons site for Gramps 4.2 and newer
Gramps Addons site for Gramps 3.2 to Gramps 4.1.