Add sniff_function support to ImportPlugin for version-aware dispatch - #2251
Closed
dsblank wants to merge 1 commit into
Closed
Add sniff_function support to ImportPlugin for version-aware dispatch#2251dsblank wants to merge 1 commit into
dsblank 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).
Member
|
@dsblank Please don't create branches in origin. Use you own fork when creating pull requests. |
Member
Author
I'll add that to AGENTS.md |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 (except minor spacing in_import.py)🤖 Generated with Claude Code