|
| 1 | +"""ANTLR-generated files for parsing OpenPulse files. |
| 2 | +
|
| 3 | +This package sets up its import contents to be taken from the generated files |
| 4 | +whose ANTLR version matches the installed version of the ANTLR runtime. The |
| 5 | +generated files should be placed in directories called ``_<major>_<minor>``, |
| 6 | +where `major` is 4, and `minor` is the minor version of ANTLR (e.g. if ANTLR |
| 7 | +4.10 was used, those files should be in ``_4_10``). |
| 8 | +
|
| 9 | +The ANTLR files from more than one version of ANTLR can be present at once. This package will |
| 10 | +dynamically load the correct version based on the installed version of the runtime. |
| 11 | +""" |
| 12 | + |
| 13 | +from importlib.abc import MetaPathFinder as _MetaPathFinder, Loader as _Loader |
| 14 | +import pathlib |
| 15 | +import sys |
| 16 | + |
| 17 | +if sys.version_info < (3, 10): |
| 18 | + from importlib_metadata import version as _version |
| 19 | +else: |
| 20 | + from importlib.metadata import version as _version |
| 21 | + |
| 22 | +# The `antlr4` package is supplied by `antlr4_python3_runtime`. |
| 23 | +_parts = [int(x) for x in _version("antlr4_python3_runtime").split(".")] |
| 24 | +_resolved_dir = f"_{_parts[0]}_{_parts[1]}" |
| 25 | +_antlr_dir = pathlib.Path(__file__).parent |
| 26 | +if not (_antlr_dir / _resolved_dir).is_dir(): |
| 27 | + _available = [path.parent.name[1:] for path in _antlr_dir.rglob("openpulseParser.py")] |
| 28 | + if not _available: |
| 29 | + raise ImportError("No ANTLR-generated parsers found.") |
| 30 | + raise ImportError( |
| 31 | + f"Missing ANTLR-generated parser for version '{_parts[0]}.{_parts[1]}'." |
| 32 | + f" Available versions: {_available!r}" |
| 33 | + ) |
| 34 | + |
| 35 | + |
| 36 | +class ANTLRMetaPathFinder(_MetaPathFinder): |
| 37 | + """Redirect module/package lookups in `openpulse.antlr` to the concrete implementations |
| 38 | + pre-generated by the ANTLR version that matches the installed version of the runtime.""" |
| 39 | + |
| 40 | + def __init__(self, version_package: str): |
| 41 | + top_level = __package__.rsplit(".")[0] |
| 42 | + # Note the extra `.` in the domain because we don't want to handle ourselves. |
| 43 | + self._domain = f"{top_level}._antlr." |
| 44 | + self._versioned = f"{top_level}._antlr.{version_package}" |
| 45 | + |
| 46 | + def find_spec(self, fullname, path, target=None): |
| 47 | + from importlib.machinery import SourceFileLoader |
| 48 | + from importlib.util import spec_from_loader, find_spec |
| 49 | + |
| 50 | + if not fullname.startswith(self._domain) or fullname.startswith(self._versioned): |
| 51 | + return None |
| 52 | + newname = f"{self._versioned}.{fullname[len(self._domain):]}" |
| 53 | + # Get the spec and loader for the direct path to the versioned file, and rewrap them to have |
| 54 | + # the unversioned module name. The modules aren't loaded (or executed) by this, but the |
| 55 | + # loader is configured so that when they are, their scopes all carry the unversioned name. |
| 56 | + return spec_from_loader(fullname, SourceFileLoader(fullname, find_spec(newname).origin)) |
| 57 | + |
| 58 | + |
| 59 | +sys.meta_path = [ANTLRMetaPathFinder(_resolved_dir)] + sys.meta_path |
| 60 | + |
| 61 | +# ... and now the additional content of this module. |
| 62 | + |
| 63 | +RUNTIME_VERSION = tuple(int(x) for x in _parts) |
| 64 | +"""The runtime-detected version of the ANTLR runtime, as a tuple like ``sys.version_info``.""" |
| 65 | + |
| 66 | +# These imports are re-directed into concrete versioned ones. Doing them |
| 67 | +# manually here helps stop pylint complaining. |
| 68 | +from . import openpulseLexer, openpulseParser, openpulseParserVisitor, openpulseParserListener |
0 commit comments