Changes

Jump to: navigation, search

Addons development

545 bytes added, 09:06, 8 October 2013
m
Config
''{{man warn|Warning:|This page documents the API, methods, and best practices for developing a 3rd-party addon for GRAMPS Gramps 3.2 / 3.3 / 3.4.''and later }}
= Addons development =for Gramps can extend the program in many different ways. You can add any of the following [http://svn.code.sf.net/p/gramps/code/trunk/gramps/gen/plug/_pluginreg.py types] of addons:
Addons for GRAMPS can extend the program in many different ways. You can add any of the following types of addons:#Report# Doc creatorQuickreport# ExporterTool# GrampletImporter# ImporterExporter# Map serviceDoc creator# Plugin lib# QuickreportMap service# RelationshipsGramps View# ReportRelationships# ToolGramplet# ViewSidebar
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:
# List and document your addon
# Support it through issue tracker
# Maintain the code as GRAMPS Gramps continues to evolve
We'll now look at each of these steps in detail.
== Develop your addon ==
The [http://svn.code.sf.net/p/gramps-addons /code/ gramps-addons] subversion repository has the following structure:
* /gramps-addons
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.
==== Setup the addon development tools====
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 ~/gramps/trunk
## Checkout gramps-addons:
### svn co https://gramps-addonssvn.svncode.sourceforgesf.net/svnrootp/gramps-addons /code gramps-addons
## Change to trunk or branches/gramps{{stable_branch}} directory:
### cd gramps-addons/branches/gramps{{stable_branch}}/contrib
## ./make.py init NewProjectName
NOTE: to use make===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 as shown throughout , .glade, etc. files in this document, you may have to usedirectory. For general information on Gramps development see [[Portal:Developers]] and [[Writing a Plugin]] specifically.
GRAMPSPATH=/path/to/gramps python make.py ... if the default ("../../..") is not correct. Follow the development API for == Test 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.addon as you develop ===To test your addon as you develop it it is suggested that you replace your GRAMPS Gramps user plugin directory with a link to your addon development directory, like so:
cd ~/.gramps/gramps{{stable_branch}}/
ln -s /wherever/trunk/gramps-addons/branches/gramps{{stable_branch}}/contrib plugins
GRAMPS 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 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, follow these steps:
# Get an http://sourceforge.net account if you don't already have one.
# 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 httpshttp://sourceforge.net/projectprojects/gramps-addons/memberlist.php?group_id=285429
# Remove the files that should not be added to SVN:
## ./make.py clean NewProjectName
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 At the top of the source file that defines the settingsof your addon, you would do this:
from config import configas configman cm config = configconfigman.register_manager("view_placetreeview_0grampletname") cm# register the values to save: config.register("section.variable1option-name1", value1) cmconfig.register("section.variable2option-name2", value2)
...
cm# load an existing file, if one: config.initload() # 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.
This will create the file "view_placetreeview_0.ini" and put in the same directory as the addon. In the addon, you can then:
x = cmconfig.get("section.variable1option-name1") cmconfig.set("section.variable1option-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):
cmconfig.save()
If your code is a system-level file, then you might want to save the config in the Gramps system folder:
cm config = configconfigman.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 configas configman cm config = configconfigman.get_manager("view_placetreeview_0grampletname") x = cmconfig.get("section.variable1option-name1")
=== Localization ===
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
<pre>
from TransUtils import get_addon_translator
_ = get_addon_translator().gettext
</pre>
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:
# gettext
# lgettext
# ngettext
# lngettext
# ngettextsgettext Gramps 3 also provides: 
# ugettext
# ungettext
NOTE: Currently we don't These have a method of using a context become obsolete in any of these functions. The translation functions that are supported are defined as followsGramps 4; gettext, ngettext, and sgettext always return translated strings in unicode for consistent portability between Python 2 and Python3.
'''See the [http://docs.python.org/3/library/gettext(message)'''.html#the-gnutranslations-class python documentation] for documentation of gettext and ngettext. The "l" versions return the string encoded according to the [http://docs.python.org/3/library/locale.html#locale.setlocale currently set locale]; the "u" versions return unicode strings in Python2 and are not available in Python 3.
If a fallback has been set, forward gettext() to the fallback. Otherwise, return the translated message. Overridden in derived classes. '''lgettext(message)sgettext''' If is a fallback has been setGramps extension that filters out clarifying comments for translators, forward lgettext() to the fallback. Otherwise, return the translated message. Overridden in derived classes.such as '''ugettext _(message"Remaining names | rest")''' If a fallback has been set, forward ugettext() to Where "rest" is the fallback. Otherwise, return the translated message as a Unicode English string. Overridden in derived classes. '''ngettext(singular, plural, n)''' If a fallback has been set, forward ngettext() to the fallback. Otherwise, return the translated message. Overridden in derived classes. '''lngettext(singular, plural, n)''' If a fallback has been set, forward ngettext() to the fallback. Otherwise, return the translated message. Overridden in derived classes. '''ungettext(singular, plural, n)''' If a fallback has been set, forward ungettext() that we want to the fallback. Otherwise, return the translated message as present and "Remaining names" is a Unicode string. Overridden in derived classeshint for translators.
== Create a Gramps Plugin Registration file ==
</pre>
PTYPE is TOOL, GRAMPLET, REPORT, QUICKREPORTQUICKVIEW, 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.
</pre>
You can see examples of the kinds of addons [http://grampssvn.svncode.sourceforgesf.net/viewvcp/gramps/code/trunk/srcgramps/plugins/ here] (for example, see [http://grampssvn.svncode.sourceforgesf.net/viewvcp/gramps/code/trunk/srcgramps/plugins/drawreport/drawplugins.gpr.py?view=markup trunk/srcgramps/plugins/drawreport/drawreportdrawplugins.gpr.py]) and see the full documentation [http://grampssvn.svncode.sourceforgesf.net/viewvcp/gramps/code/trunk/srcgramps/gen/plug/_pluginreg.py?view=markup 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.
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 Gramps development see [[Portal:Developers]] and [[Writing a plugin]] specifically.
=== General plugins ===
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".
== 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.  '''Make sure you have already built gramps34 or trunk''' To do thatcreate a listing:
python make.py listing
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 [[Plugins4.0]] and describe your addon. You can point to the addon.tgz in SVN as the downloadable file.
== Miscellaneous commands ==
To build and compile translations for all projectsto their download/Addon.addon.tgz files:
python make.py build all
To compile translations for all projects to their download/Addon.addon.tgz files:
python make.py compile all
 
== List and document your addon ==
 
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".
== Support it through issue tracker ==
Visit http://www.gramps-project.org/bugs/view_all_bug_page.php and become a user. Suggest to check it regularly.
== Maintain the code as GRAMPS Gramps continues to evolve ==
= Resources =
* httphttps://gramps-addons.svn.sourceforge.net/viewvcp/gramps-addons/ - SVN browseGramps Addons site
[[Category:Developers/General]]
38
edits

Navigation menu