|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Harness for plugin transform types. Two modes: |
| 4 | +
|
| 5 | + Discover: |
| 6 | + python _plugin_harness.py --discover <module_path> |
| 7 | +
|
| 8 | + Scans the module for transformer classes and writes a JSON array to stdout: |
| 9 | + [{"types": [...], "default_inputs": [...], "default_outputs": [...]}, ...] |
| 10 | +
|
| 11 | + Transform: |
| 12 | + python _plugin_harness.py <metadata_json> |
| 13 | +
|
| 14 | + Reads input data from stdin, runs the transform, and writes a JSON object |
| 15 | + to stdout: |
| 16 | + {"success": true, "output": "<str>", "binary": false, "stderr": null} |
| 17 | + {"success": true, "output": "<base64>", "binary": true, "stderr": null} |
| 18 | + {"success": false, "output": null, "binary": false, "stderr": "<str>"} |
| 19 | +
|
| 20 | + The metadata object passed to transform() has these attributes: |
| 21 | + type (str) transform type identifier |
| 22 | + transform_content (str) code/script declared in transforms.yaml |
| 23 | + input_data (str) example snippet text |
| 24 | + source_mime_type (str) |
| 25 | + target_mime_type (str) |
| 26 | + metadata (dict) extra metadata (keys starting with _ excluded) |
| 27 | + sandbox_dir None always None in subprocess context |
| 28 | +""" |
| 29 | +import importlib |
| 30 | +import inspect |
| 31 | +import json |
| 32 | +import sys |
| 33 | +from base64 import b64encode |
| 34 | + |
| 35 | + |
| 36 | +# --------------------------------------------------------------------------- |
| 37 | +# Helpers |
| 38 | +# --------------------------------------------------------------------------- |
| 39 | + |
| 40 | +def _transformer_classes(module): |
| 41 | + """Yield (cls,) for every transformer class defined in module.""" |
| 42 | + module_name = module.__name__ |
| 43 | + for _, cls in inspect.getmembers(module, inspect.isclass): |
| 44 | + if cls.__module__ != module_name: |
| 45 | + continue |
| 46 | + types = getattr(cls, 'transform_types', None) |
| 47 | + if types and isinstance(types, list) and all(isinstance(t, str) for t in types): |
| 48 | + yield cls |
| 49 | + |
| 50 | + |
| 51 | +class _Meta: |
| 52 | + """Minimal TransformMetadata-compatible namespace passed to plugin transform().""" |
| 53 | + __slots__ = ('type', 'transform_content', 'input_data', |
| 54 | + 'source_mime_type', 'target_mime_type', 'metadata', 'sandbox_dir') |
| 55 | + |
| 56 | + |
| 57 | +# --------------------------------------------------------------------------- |
| 58 | +# Discover mode |
| 59 | +# --------------------------------------------------------------------------- |
| 60 | + |
| 61 | +def _discover(module_path: str) -> None: |
| 62 | + module = importlib.import_module(module_path) |
| 63 | + result = [ |
| 64 | + { |
| 65 | + 'class': cls.__name__, |
| 66 | + 'types': list(cls.transform_types), |
| 67 | + 'default_inputs': list(getattr(cls, 'default_inputs', None) or []), |
| 68 | + 'default_outputs': list(getattr(cls, 'default_outputs', None) or []), |
| 69 | + } |
| 70 | + for cls in _transformer_classes(module) |
| 71 | + ] |
| 72 | + print(json.dumps(result)) |
| 73 | + |
| 74 | + |
| 75 | +# --------------------------------------------------------------------------- |
| 76 | +# Transform mode |
| 77 | +# --------------------------------------------------------------------------- |
| 78 | + |
| 79 | +def _transform(meta_json: str) -> None: |
| 80 | + meta_dict = json.loads(meta_json) |
| 81 | + |
| 82 | + m = _Meta() |
| 83 | + m.type = meta_dict['type'] |
| 84 | + m.transform_content = meta_dict['transform_content'] |
| 85 | + m.source_mime_type = meta_dict['source_mime_type'] |
| 86 | + m.target_mime_type = meta_dict['target_mime_type'] |
| 87 | + m.metadata = meta_dict.get('metadata', {}) |
| 88 | + m.input_data = sys.stdin.buffer.read().decode('utf-8') |
| 89 | + m.sandbox_dir = None |
| 90 | + |
| 91 | + module_path = meta_dict['module'] |
| 92 | + transform_type = meta_dict['type'] |
| 93 | + module = importlib.import_module(module_path) |
| 94 | + |
| 95 | + transformer = next( |
| 96 | + (cls() for cls in _transformer_classes(module) |
| 97 | + if transform_type in cls.transform_types), |
| 98 | + None, |
| 99 | + ) |
| 100 | + |
| 101 | + if transformer is None: |
| 102 | + print(json.dumps({ |
| 103 | + 'success': False, 'output': None, 'binary': False, |
| 104 | + 'stderr': f"No transformer found for type '{transform_type}' in '{module_path}'", |
| 105 | + })) |
| 106 | + return |
| 107 | + |
| 108 | + try: |
| 109 | + result = transformer.transform(m) |
| 110 | + except Exception as e: |
| 111 | + print(json.dumps({ |
| 112 | + 'success': False, 'output': None, 'binary': False, 'stderr': str(e), |
| 113 | + })) |
| 114 | + return |
| 115 | + |
| 116 | + if result is None: |
| 117 | + print(json.dumps({'success': True, 'output': None, 'binary': False, 'stderr': None})) |
| 118 | + return |
| 119 | + |
| 120 | + if isinstance(result, bytes): |
| 121 | + print(json.dumps({ |
| 122 | + 'success': True, |
| 123 | + 'output': b64encode(result).decode('ascii'), |
| 124 | + 'binary': True, |
| 125 | + 'stderr': None, |
| 126 | + })) |
| 127 | + else: |
| 128 | + print(json.dumps({'success': True, 'output': result, 'binary': False, 'stderr': None})) |
| 129 | + |
| 130 | + |
| 131 | +# --------------------------------------------------------------------------- |
| 132 | +# Entry point |
| 133 | +# --------------------------------------------------------------------------- |
| 134 | + |
| 135 | +if __name__ == '__main__': |
| 136 | + if len(sys.argv) < 2: |
| 137 | + print('Usage: _plugin_harness.py --discover <module> | <metadata_json>', |
| 138 | + file=sys.stderr) |
| 139 | + sys.exit(1) |
| 140 | + |
| 141 | + if sys.argv[1] == '--discover': |
| 142 | + if len(sys.argv) < 3: |
| 143 | + print('Usage: _plugin_harness.py --discover <module>', file=sys.stderr) |
| 144 | + sys.exit(1) |
| 145 | + _discover(sys.argv[2]) |
| 146 | + else: |
| 147 | + _transform(sys.argv[1]) |
0 commit comments