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

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

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."
)

Two quick suggestions:

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

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:

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:

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 (free version)

Best wishes from Germany

This error is preventing the plugin from being registered.

Have a look in the 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 tool would provide a good example.

Note that the .gpr.py (Gramps Plugin Registration Python 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:

However, its help_url property 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 property should be EXPERIMENTAL or BETA rather than STABLE

You may want to select a different Tool category too.

1 Like

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