Skip to content

Commit

Permalink
[Add] registry priority number
Browse files Browse the repository at this point in the history
  • Loading branch information
g5t committed Sep 27, 2024
1 parent 19df051 commit 139454b
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 7 deletions.
3 changes: 3 additions & 0 deletions src/mccode_antlr/assembler/assembler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/mccode_antlr/reader/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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, ]
Expand Down
14 changes: 7 additions & 7 deletions src/mccode_antlr/reader/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}'),
Expand All @@ -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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
2 changes: 2 additions & 0 deletions src/mccode_antlr/translators/c.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/mccode_antlr/translators/target.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 139454b

Please sign in to comment.