From 139454b28296172301c6ed85c3b5e78350c1b8de Mon Sep 17 00:00:00 2001 From: Gregory Tucker Date: Fri, 27 Sep 2024 23:29:33 +0200 Subject: [PATCH] [Add] registry priority number --- src/mccode_antlr/assembler/assembler.py | 3 +++ src/mccode_antlr/reader/reader.py | 4 ++++ src/mccode_antlr/reader/registry.py | 14 +++++++------- src/mccode_antlr/translators/c.py | 2 ++ src/mccode_antlr/translators/target.py | 1 + 5 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/mccode_antlr/assembler/assembler.py b/src/mccode_antlr/assembler/assembler.py index ff0bf2e..7d140f9 100644 --- a/src/mccode_antlr/assembler/assembler.py +++ b/src/mccode_antlr/assembler/assembler.py @@ -9,6 +9,9 @@ class Assembler: """Interactive instrument assembly""" def __init__(self, name: str, registries: list[Registry] = None): + from ..reader.registry import ordered_registries + if registries is not None: + registries = list(ordered_registries(registries)) self.instrument = Instr(name, source='interactive') self.reader = Reader(registries=registries) if registries is not None else Reader() self.instrument.registries = self.reader.registries diff --git a/src/mccode_antlr/reader/reader.py b/src/mccode_antlr/reader/reader.py index 4cc82c5..c0e54b9 100644 --- a/src/mccode_antlr/reader/reader.py +++ b/src/mccode_antlr/reader/reader.py @@ -3,6 +3,8 @@ from loguru import logger from dataclasses import dataclass, field from antlr4.error.ErrorListener import ErrorListener +from sympy import ordered + from .registry import Registry, MCSTAS_REGISTRY, registries_match, registry_from_specification from ..comp import Comp from ..common import Mode @@ -43,8 +45,10 @@ class Reader: c_flags: list[str] = field(default_factory=list) def __post_init__(self): + from .registry import ordered_registries if len(self.registries) == 0: self.registries = [MCSTAS_REGISTRY, ] + self.registries = list(ordered_registries(self.registries)) def prepend_registry(self, reg: Registry): self.registries[:0] = [reg, ] diff --git a/src/mccode_antlr/reader/registry.py b/src/mccode_antlr/reader/registry.py index 18a51ee..0fd0c05 100644 --- a/src/mccode_antlr/reader/registry.py +++ b/src/mccode_antlr/reader/registry.py @@ -41,7 +41,7 @@ class Registry: root = None pooch = None version = None - priority: int = -1 + priority: int = 0 def __str__(self): from mccode_antlr.common import TextWrapper @@ -113,7 +113,7 @@ def find_registry_file(name: str): class RemoteRegistry(Registry): - def __init__(self, name: str, url: str | None, version: str | None, filename: str | None, priority: int = -1): + def __init__(self, name: str, url: str | None, version: str | None, filename: str | None, priority: int = 0): self.name = name self.url = url self.version = version @@ -198,7 +198,7 @@ def __eq__(self, other): class ModuleRemoteRegistry(RemoteRegistry): - def __init__(self, name: str, url: str, filename=None, version=None, priority: int=-1): + def __init__(self, name: str, url: str, filename=None, version=None, priority: int = 0): super().__init__(name, url, version, filename, priority) self.pooch = pooch.create( path=pooch.os_cache(f'mccode_antlr-{self.name}'), @@ -218,7 +218,7 @@ def __init__(self, name: str, url: str, filename=None, version=None, priority: i class GitHubRegistry(RemoteRegistry): def __init__(self, name: str, url: str, version: str, filename: str | None = None, - registry: str | dict | None = None, priority: int = -1): + registry: str | dict | None = None, priority: int = 0): if filename is None: filename = f'{name}-registry.txt' super().__init__(name, url, version, filename, priority) @@ -260,7 +260,7 @@ def file_keys(cls) -> tuple[str, ...]: class LocalRegistry(Registry): - def __init__(self, name: str, root: str, priority: int = -1): + def __init__(self, name: str, root: str, priority: int = 10): self.name = name self.root = Path(root) self.version = mccode_antlr_version() @@ -333,7 +333,7 @@ def __eq__(self, other): class InMemoryRegistry(Registry): - def __init__(self, name, priority: int = -1, **components): + def __init__(self, name, priority: int = 100, **components): self.name = name self.root = '/proc/memory/' # Something pathlike is needed? self.version = mccode_antlr_version() @@ -463,7 +463,7 @@ def source_registry_tag(): src, reg, tag = source_registry_tag() - mc, mx, lib = [GitHubRegistry(name, src, tag, registry=reg) for name in ('mcstas', 'mcxtrace', 'libc')] + mc, mx, lib = [GitHubRegistry(name, src, tag, registry=reg, priority=-10) for name in ('mcstas', 'mcxtrace', 'libc')] return mc, mx, lib MCSTAS_REGISTRY, MCXTRACE_REGISTRY, LIBC_REGISTRY = _mccode_pooch_registries() diff --git a/src/mccode_antlr/translators/c.py b/src/mccode_antlr/translators/c.py index 04023b6..ec85a03 100644 --- a/src/mccode_antlr/translators/c.py +++ b/src/mccode_antlr/translators/c.py @@ -257,8 +257,10 @@ def __post_init__(self): languages. (A different target language would not include the same libraries in its raw blocks) """ # Make sure the registry list contains the C library registry, so that we can find and include files + from ..reader.registry import ordered_registries if not any(reg == LIBC_REGISTRY for reg in self.registries): self.source.registries += (LIBC_REGISTRY, ) + self.source.registries = tuple(ordered_registries(list(self.source.registries))) includes = [] inst = self.source diff --git a/src/mccode_antlr/translators/target.py b/src/mccode_antlr/translators/target.py index f3b9b15..0f27b64 100644 --- a/src/mccode_antlr/translators/target.py +++ b/src/mccode_antlr/translators/target.py @@ -50,6 +50,7 @@ def is_mcstas(self): @property def registries(self): + # TODO always ensure this is sorted by priority? return self.source.registries def known(self, name: str, which: str = None):