Add sniff_function support to ImportPlugin for version-aware dispatch - #2252
Add sniff_function support to ImportPlugin for version-aware dispatch#2252dsblank wants to merge 1 commit into
Conversation
Previously, Gramps mapped each file extension to a single importer. This change adds an optional sniff_function attribute to ImportPlugin and PluginData so that multiple importers can register for the same file extension and be distinguished by file content rather than extension alone. When the importer for a file is selected, plugins that declare a sniff_function are tested first. The first plugin whose sniff function returns True for the file wins. Plugins without a sniff function continue to work as before, acting as a fallback. This makes it possible for an addon to register a GEDCOM 7 importer alongside the built-in GEDCOM 5.5 importer: the addon's sniff function checks for the VERS 7.x header and claims the file when present, leaving all other GEDCOM files to the existing importer. The same dispatch logic is applied in both the GUI (dbloader.py) and the CLI (arghandler.py).
|
It would be really nice if the Import code were converted to support GEDCOM extension addons as #0010550: Refactor Gramps GEDCOM import so as to support GEDCOM extension addons at the same time as this change, so that it is simpler to write alternative importers. |
|
I've had updated vCard importer and export plugins wish-coded that add support beyond the original v3.0 format for 5.2 and 6.0 Gramps. (It seems most smart phones still use version 2.1 .vcf files.) Do you want to use them to exercise the sniffer? I'd like to roll them into 6.1 but testing as addons might tell us something about integrating. The AI tried building automatic hiding of the superseded into the .gpr.py file. But that did not work. https://github.com/emyoulation/CuratedGrampsPlugins/tree/main/gramps60/download
|
Summary
sniff_functionattribute toPluginDataandImportPluginso multiple importers can share a file extension and be selected by file content.dbloader.py) and CLI (arghandler.py) dispatch to prefer a sniff-matched plugin over the first-match fallback.Motivation
The
.gedextension is currently mapped to a single GEDCOM importer.The GEDCOM 7 addon (PR #744) needs to register a second importer for
.gedfiles and have it chosen automatically when the file header identifies the file as GEDCOM 7. GEDCOM 7 requires a version string in the header, so detection is trivial — the only missing piece was a hook in core.How it works
A plugin declares a
sniff_functionby name in its.gpr.py:The function accepts a filename and returns
Truewhen the plugin should handle it:Plugins without a
sniff_functioncontinue to work exactly as before.Test plan
GRAMPS_RESOURCES=. python3 -m unittest discover -p "*_test.py" gramps/gen/plug/test— all 10 tests passblackrun on all changed files — no reformatting needed🤖 Generated with Claude Code