# \[My\] Addon ( Archivattribute ) won't work (Please Help me)

**URL:** https://gramps.discourse.group/t/my-addon-archivattribute-wont-work-please-help-me/7833
**Category:** Development
**Tags:** third-party-addon
**Created:** [June 13, 2025, 5:40pm UTC](https://gramps.discourse.group/t/my-addon-archivattribute-wont-work-please-help-me/7833 "2025-06-13T17:40:59Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![von\_Klinski](https://avatars.discourse-cdn.com/v4/letter/v/d07c76/32.png) [@von\_Klinski](https://gramps.discourse.group/u/von_Klinski)
#### Post date: [June 13, 2025, 5:40pm UTC](https://gramps.discourse.group/t/my-addon-archivattribute-wont-work-please-help-me/7833/1 "2025-06-13T17:40:59Z")

</div>

Hello, I’m new here and still desoriented. I’ve tried to build the follwing script, but it won’t work. Can anybody help me? It’s supposed to help me to archive my recources:

# Name: Archivattribute hinzufÃ¼gen

# Version: 1.0

# Beschreibung: FÃ¼gt Archivattribute zu allen Quellen hinzu

# Autoren: OpenAI / ChatGPT

# Gramps-Version: 6.0.1

```python
from gramps.gen.plug import Tool, ToolOptions
from gramps.gen.lib import Attribute
from gramps.gui.plug import tool

class AddArchiveAttributesTool(Tool):
    def __init__ (self, dbstate, user, options, callback=None):
        Tool. __init__ (self, dbstate, user, options, callback)
        self.db = self.dbstate.db
        self.trans = self.db.transaction
        self.run()

    def run(self):
        attribute_names = [
            "Archivsignatur", "Archivname", "Sammlung / Serie", "Bestandsnummer",
            "Signatur alt", "Original vorhanden", "Digitalisat vorhanden",
            "Digitalisierungsdatum", "Digitalisiert von", "Scankontrolle",
            "Aufbewahrungsort", "Standort", "Provenienz", "Vermerke",
            "Zugangsnummer", "Reproduktion erlaubt", "Rechtsstatus", "ErgÃ¤nzende Bemerkung"
        ]

        count = 0
        for handle in self.db.get_source_handles():
            source = self.db.get_source_from_handle(handle)
            existing = [attr.get_type() for attr in source.get_attribute_list()]
            changed = False
            for name in attribute_names:
                if name not in existing:
                    source.add_attribute(Attribute(type=name, value=""))
                    changed = True
            if changed:
                self.db.commit_source(source, self.trans)
                count += 1

        self.user.notify(f"{count} Quellen wurden aktualisiert.")

tool.register_tool(
    AddArchiveAttributesTool,
    ToolOptions,
    "Archivattribute hinzufÃ¼gen",
    "FÃ¼gt Archivattribute zu allen Quellen hinzu."
)

```

---

<div class="post-metadata">

### Author: ![Nick-Hall](https://yyz2.discourse-cdn.com/free1/user_avatar/gramps.discourse.group/nick-hall/32/95_2.png) [@Nick-Hall](https://gramps.discourse.group/u/Nick-Hall)
#### Post date: [June 13, 2025, 5:54pm UTC](https://gramps.discourse.group/t/my-addon-archivattribute-wont-work-please-help-me/7833/2 "2025-06-13T17:54:51Z")

</div>

Two quick suggestions:

- Register your tool in the `gpr` file.
- Use the `DbTxn` class for transactions.

---

<div class="post-metadata">

### Author: ![von\_Klinski](https://avatars.discourse-cdn.com/v4/letter/v/d07c76/32.png) [@von\_Klinski](https://gramps.discourse.group/u/von_Klinski)
#### Post date: [June 17, 2025, 1:23pm UTC](https://gramps.discourse.group/t/my-addon-archivattribute-wont-work-please-help-me/7833/3 "2025-06-17T13:23:07Z")

</div>

Thany for your reply. I’ve tried to get it working with your hints. Unfortunately it still doesn’t work.

Here is the code of the .gpr.py-file:

```python
from gramps.gen.plug import (ToolPlugin, TOOL_DBPROC, STABLE)
from gramps.gen.const import GRAMPS_LOCALE as glocale

_ = glocale.translation.gettext

class AddSourceArchiveAttributesToolPlugin(ToolPlugin):
    """
    Plugin-Registrierung für 'Archiv-Attribute für Quellen hinzufügen'
    """
    name = _("Archiv-Attribute für Quellen hinzufügen")
    description = _("Fügt allen Quellen Archiv-bezogene Attribute hinzu, falls sie fehlen.")
    version = "1.0.3"
    gramps_target_version = "6.0"
    status = STABLE
    fname = "add_source_archive_attributes.py"
    toolclass = "AddSourceArchiveAttributesTool"
    optionclass = "AddSourceArchiveAttributesOptions"
    category = TOOL_DBPROC
    authors = ["Dein Name"]
    authors_email = ["[email protected]"]

```

Here is the code of the .py-file:

```python
from gramps.gen.plug.menu import ToolOptions
from gramps.gen.plug.report import Tool
from gramps.gen.lib import Attribute
from gramps.gen.const import GRAMPS_LOCALE as glocale
_ = glocale.translation.gettext

ARCHIVE_ATTRIBUTES = [
    "Signatur",
    "Kurzbeschreibung des Inhalts",
    "Fundstelle/Bestand",
    "Datum der Quelle",
    "Dokumententyp",
    "Herkunft der Quelle/der Kopie",
    "Provenienz/Kontext",
    "Übertragungsstatus",
    "Hinweise zur Nutzung/Anmerkungen",
    "Bearbeiter/Erstellt am",
    "Digitale Datei/Verknüpfung",
    "Verweise auf verwandte Dokumente"
]

class AddSourceArchiveAttributesOptions(ToolOptions):
    def __init__ (self, name, person_id=None):
        ToolOptions. __init__ (self, name, person_id)

class AddSourceArchiveAttributesTool(Tool):
    def __init__ (self, dbstate, user, options_class, mode, callback=None):
        Tool. __init__ (self, dbstate, user, options_class, mode, callback)
        self.set_description(_("Archivattribute werden geprüft und ergänzt."))
        self.set_progress_total(self.db.get_number_of_sources())

    def apply(self):
        with self.transaction(_("Füge Archiv-Attribute zu Quellen hinzu")):
            for handle in self.db.iter_source_handles():
                source = self.db.get_source_from_handle(handle)
                existing_attrs = {attr.get_type() for attr in source.get_attribute_list()}
                changed = False

                for attr_name in ARCHIVE_ATTRIBUTES:
                    if attr_name not in existing_attrs:
                        new_attr = Attribute(attr_name, "")
                        source.add_attribute(new_attr)
                        changed = True

                if changed:
                    self.db.commit_source(source, _("Archiv-Attribute automatisch ergänzt"))

                self.step()

```

When starting GRAMPS with my file gramps\_debug.bat I get this:

Start Gramps in debug mode…

Set up debugging -L

ERROR: Failed to read additional module registration add\_source\_archive\_attributes.gpr.py

Traceback (most recent call last):

File “C:\Program Files\GrampsAIO64-6.0.1\gramps\gen\plug\_pluginreg.py”, line 1428, in scan\_dir

exec(

File “add\_source\_archive\_attributes.gpr. py”, line 1, in

ImportError: cannot import name ‘ToolPlugin’ from ‘gramps.gen.plug’ (C:\Program Files\GrampsAIO64-6.0.1\gramps\gen.plug\__init_\_.py)

WARNING: add-on module fixcoords has no translation for one of the configured languages and uses US-English instead

WARNING: add-on module HistContext has no translation for one of the configured languages and uses US-English instead

_Translated with [DeepL.com](https://www.deepl.com/?utm_campaign=product&utm_source=web_translator&utm_medium=web&utm_content=copy_free_translation) (free version)_

Best wishes from Germany

---

<div class="post-metadata">

### Author: ![Nick-Hall](https://yyz2.discourse-cdn.com/free1/user_avatar/gramps.discourse.group/nick-hall/32/95_2.png) [@Nick-Hall](https://gramps.discourse.group/u/Nick-Hall)
#### Post date: [June 17, 2025, 1:42pm UTC](https://gramps.discourse.group/t/my-addon-archivattribute-wont-work-please-help-me/7833/4 "2025-06-17T13:42:47Z")

</div>

> [@von\_Klinski](#):
>
> File “add\_source\_archive\_attributes.gpr. py”, line 1, in
> 
> ImportError: cannot import name ‘ToolPlugin’ from ‘gramps.gen.plug’ (C:\Program Files\GrampsAIO64-6.0.1\gramps\gen.plug\__init_\_.py)

This error is preventing the plugin from being registered.

Have a look in the [tools.gpr.py](https://github.com/gramps-project/gramps/blob/master/gramps/plugins/tool/tools.gpr.py) file for examples of plugin registrations.

I also suggest that you examine some tools in the `gramps/plugins/tool` directory to get a feel for how our core tools are coded. The [Sort Events](https://github.com/gramps-project/gramps/blob/master/gramps/plugins/tool/sortevents.py) tool would provide a good example.

---

<div class="post-metadata">

### Author: ![emyoulation](https://yyz2.discourse-cdn.com/free1/user_avatar/gramps.discourse.group/emyoulation/32/67_2.png) [@emyoulation](https://gramps.discourse.group/u/emyoulation)
#### Post date: [June 17, 2025, 1:59pm UTC](https://gramps.discourse.group/t/my-addon-archivattribute-wont-work-please-help-me/7833/5 "2025-06-17T13:59:08Z")

</div>

Note that the `.gpr.py` ( **G** ramps **P** lugin **R** egistration **Py** thon file) sample that @Nick-Hall referenced is a combined registration for ALL the built-in tools. So it is a bit more complex than you need to register a single add-on tool.

A good example of a Gramps Plugin Registration for a GUI-only Tool addon is:

- [https://github.com/gramps-project/addons-source/blob/maintenance/gramps60/AttachSourceTool/AttachSourceTool.gpr.py](https://github.com/gramps-project/addons-source/blob/maintenance/gramps60/AttachSourceTool/AttachSourceTool.gpr.py)

However, its `help_url` [property](https://gramps-project.org/docs/gen/gen_plug.html#gramps.gen.plug._pluginreg.PluginData) points to the wiki for an add-on having an established documentation page. During beta, it is recommended to have this be a URL pointing either to your GitHub repository (where it will show the README.md file) or to this Discourse thread, where you’ve announced your add-on.

Also, while in development, the [`status`](https://gramps-project.org/docs/gen/gen_plug.html#gramps.gen.plug._pluginreg.PluginData.status) property should be `EXPERIMENTAL` or `BETA` rather than `STABLE`

You may want to select a [different Tool `category`](https://github.com/gramps-project/gramps/blob/9aa872287dfbac66cf6e6bd38cd0e9607699172a/gramps/gui/plug/tool.py#L66-L73) too.

---

<div class="post-metadata">

### Author: ![system](https://global.discourse-cdn.com/free1/uploads/gramps/original/1X/2ac1e712b7adf612e31ca08a55419f4cf37c0158.png) [@system](https://gramps.discourse.group/u/system)
#### Post date: [July 17, 2025, 1:59pm UTC](https://gramps.discourse.group/t/my-addon-archivattribute-wont-work-please-help-me/7833/6 "2025-07-17T13:59:51Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
