Commit b6b0a8a
Implement plugin/modular system for FMF metadata loaders (Phase 1)
Add a plugin architecture to support reading metadata from multiple
file formats beyond .fmf files. This enables future support for bash
scripts, Python/pytest tests, and other formats while maintaining
full backward compatibility.
## Architecture
### Core Components
**fmf/plugin.py** (148 lines):
- Abstract Plugin base class
- Methods: can_handle(), read(), write()
- Attributes: extensions, file_patterns, priority (0-200)
- Helper: _write_fmf_fallback() for plugins that can't write natively
**fmf/plugin_loader.py** (73 lines):
- PluginRegistry for managing built-in plugins
- Static registration (no dynamic loading - security)
- Priority-based plugin selection
- Config validation against known plugin names
**fmf/plugins/__init__.py** (23 lines):
- Static registration of all built-in plugins
- PLUGIN_NAMES dict for config validation
- Single source of truth for available plugins
**fmf/plugins/fmf.py** (110 lines):
- FmfPlugin - refactored .fmf YAML loading
- Uses ruamel.yaml for consistency
- Priority 100 (default format)
- Full write support via dict_to_yaml()
### Tree Integration
**fmf/base.py**:
- Import fmf.plugins to trigger registration
- Re-export MAIN, SUFFIX for backward compatibility
- _initialize(): Load plugins from config
- grow(): Use get_plugin_for_file() for each file
- __exit__(): Plugin write support in context manager
### Configuration
.fmf/config format:
## Features
✅ **Static registration** - All plugins in fmf/plugins/__init__.py
✅ **Priority system** - 0-200 scale, configurable per plugin
✅ **Priority override** - Adjust via config
✅ **Security** - Only built-in plugins allowed
✅ **can_handle()** - Direct filtering, supports regex patterns
✅ **Write fallback** - _write_fmf_fallback() helper
✅ **Backward compatible** - 100% existing test pass
✅ **Mixed formats** - .fmf and other types in same tree
## Testing
**tests/unit/test_plugin.py** (29 tests, 644 lines):
- TestPluginRegistry (7 tests) - Registration, validation
- TestFmfPlugin (5 tests) - Read, write, can_handle
- TestTreeWithPlugins (6 tests) - Tree integration
- TestPluginConfigurationOverride (3 tests) - Priority override
- TestRealWorldExamples (2 tests) - Existing examples
- TestMockPlugin (6 tests) - Multi-format with .txt files
**Test data**:
- tests/unit/data/plugin_basic/ - Config and .fmf files
**Coverage**: All 275 tests pass
## Simplifications Made
1. **plugin_loader.py**: 169 → 73 lines (57% reduction)
- Removed dynamic loading (importlib, inspect)
- Removed get_supported_file_patterns() (unused)
- Simplified to pure tracking + validation
2. **Static registration**: No environment variables, no file paths
- All plugins registered in fmf/plugins/__init__.py
- Config just validates plugin names
- Security-focused design
3. **Direct filtering**: Use can_handle() not pre-filtering
- Let plugins decide what they handle
- Supports regex patterns in file_patterns
- No verbose logging for non-matches
## Documentation
**docs/concept.rst**:
- New Plugins section
- Configuration examples
- Priority override documentation
- Security model explanation
**PLUGIN_FUTURE.md** (592 lines):
- Phase 2: Bash plugin design
- Phase 3: Python/pytest plugin design
- Phase 4: Write-back support
- Implementation steps (no code)
- Configuration system
- Testing strategy
- Security model
- Migration guide
## Backward Compatibility
✅ Trees without plugin config work (FmfPlugin auto-loaded)
✅ All existing .fmf files load correctly
✅ SUFFIX and MAIN constants still available
✅ All 267 existing tests pass unchanged
✅ No breaking changes to Tree API
## Security Model
🔒 **Only built-in plugins** from fmf/plugins/ directory
🔒 **No dynamic loading** from environment or arbitrary paths
🔒 **Static registration** in fmf/plugins/__init__.py
🔒 **Config validation** against PLUGIN_NAMES
🔒 **No code execution** (future Python plugin uses AST only)
## Future Phases
**Phase 2**: BashPlugin - Read from # fmf-key: value comments
**Phase 3**: PytestPlugin - Extract from marks and docstrings
**Phase 4**: Enhanced write-back support
## Files Changed
New files:
- fmf/plugin.py (148 lines)
- fmf/plugin_loader.py (73 lines)
- fmf/plugins/__init__.py (23 lines)
- fmf/plugins/fmf.py (110 lines)
- tests/unit/test_plugin.py (644 lines)
- tests/unit/data/plugin_basic/* (4 files)
- PLUGIN_FUTURE.md (592 lines)
Modified files:
- fmf/base.py (minimal changes for plugin integration)
- docs/concept.rst (added Plugins section)
Total: ~1590 lines added
## Contributors
Based on:
- Original issue: #103
- POC branch: py_plugin
- Design discussions and iterations
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>1 parent 50556c1 commit b6b0a8a
12 files changed
Lines changed: 1768 additions & 20 deletions
File tree
- docs
- fmf
- plugins
- tests/unit
- data/plugin_basic
- .fmf
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
123 | 123 | | |
124 | 124 | | |
125 | 125 | | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
126 | 211 | | |
127 | 212 | | |
128 | 213 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
15 | | - | |
16 | 15 | | |
17 | 16 | | |
18 | 17 | | |
| 18 | + | |
19 | 19 | | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
20 | 23 | | |
21 | 24 | | |
22 | 25 | | |
23 | 26 | | |
24 | 27 | | |
25 | 28 | | |
26 | | - | |
27 | | - | |
| 29 | + | |
28 | 30 | | |
29 | 31 | | |
30 | 32 | | |
| |||
210 | 212 | | |
211 | 213 | | |
212 | 214 | | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
213 | 227 | | |
214 | 228 | | |
215 | 229 | | |
| |||
716 | 730 | | |
717 | 731 | | |
718 | 732 | | |
719 | | - | |
720 | | - | |
721 | | - | |
722 | | - | |
723 | | - | |
724 | | - | |
725 | | - | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
| 736 | + | |
| 737 | + | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
726 | 742 | | |
727 | | - | |
| 743 | + | |
728 | 744 | | |
| 745 | + | |
729 | 746 | | |
730 | 747 | | |
| 748 | + | |
731 | 749 | | |
732 | | - | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
733 | 760 | | |
734 | | - | |
735 | | - | |
736 | | - | |
737 | | - | |
738 | | - | |
| 761 | + | |
| 762 | + | |
| 763 | + | |
739 | 764 | | |
740 | | - | |
| 765 | + | |
| 766 | + | |
741 | 767 | | |
742 | | - | |
| 768 | + | |
| 769 | + | |
743 | 770 | | |
744 | 771 | | |
745 | 772 | | |
746 | 773 | | |
747 | | - | |
| 774 | + | |
748 | 775 | | |
749 | 776 | | |
750 | 777 | | |
| |||
1075 | 1102 | | |
1076 | 1103 | | |
1077 | 1104 | | |
| 1105 | + | |
| 1106 | + | |
| 1107 | + | |
1078 | 1108 | | |
1079 | 1109 | | |
1080 | 1110 | | |
| 1111 | + | |
| 1112 | + | |
| 1113 | + | |
| 1114 | + | |
| 1115 | + | |
| 1116 | + | |
| 1117 | + | |
| 1118 | + | |
| 1119 | + | |
| 1120 | + | |
| 1121 | + | |
| 1122 | + | |
| 1123 | + | |
| 1124 | + | |
| 1125 | + | |
| 1126 | + | |
| 1127 | + | |
| 1128 | + | |
| 1129 | + | |
1081 | 1130 | | |
1082 | 1131 | | |
1083 | 1132 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
0 commit comments