Skip to content

Commit

Permalink
Add a priority number to each registry and sort to start each search …
Browse files Browse the repository at this point in the history
…with the highest priority (#76)

* [Add] registry priority int and sort function

* [Add] registry priority number
  • Loading branch information
g5t authored Sep 28, 2024
1 parent 47c029c commit 0a4a96f
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 9 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: 3 additions & 1 deletion src/mccode_antlr/reader/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from pathlib import Path
from loguru import logger
from dataclasses import dataclass, field
from antlr4.error.ErrorListener import ErrorListener

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 +43,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
25 changes: 17 additions & 8 deletions src/mccode_antlr/reader/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class Registry:
root = None
pooch = None
version = None
priority: int = 0

def __str__(self):
from mccode_antlr.common import TextWrapper
Expand Down Expand Up @@ -112,12 +113,13 @@ def find_registry_file(name: str):


class RemoteRegistry(Registry):
def __init__(self, name: str, url: str | None, version: str | None, filename: str | None):
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
self.filename = filename
self.pooch = None
self.priority = priority

@classmethod
def file_keys(cls) -> tuple[str, ...]:
Expand Down Expand Up @@ -196,8 +198,8 @@ def __eq__(self, other):


class ModuleRemoteRegistry(RemoteRegistry):
def __init__(self, name: str, url: str, filename=None, version=None):
super().__init__(name, url, version, filename)
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}'),
base_url=self.url,
Expand All @@ -216,10 +218,10 @@ def __init__(self, name: str, url: str, filename=None, version=None):

class GitHubRegistry(RemoteRegistry):
def __init__(self, name: str, url: str, version: str, filename: str | None = None,
registry: str | dict | None = None):
registry: str | dict | None = None, priority: int = 0):
if filename is None:
filename = f'{name}-registry.txt'
super().__init__(name, url, version, filename)
super().__init__(name, url, version, filename, priority)
import requests
base_url = f'{self.url}/raw/{self.version}/'
# If registry is a string url, we expect the registry file to be available from _that_ url
Expand Down Expand Up @@ -258,10 +260,11 @@ def file_keys(cls) -> tuple[str, ...]:


class LocalRegistry(Registry):
def __init__(self, name: str, root: str):
def __init__(self, name: str, root: str, priority: int = 10):
self.name = name
self.root = Path(root)
self.version = mccode_antlr_version()
self.priority = priority

def to_file(self, output, wrapper):
contents = '(' + ', '.join([
Expand Down Expand Up @@ -330,11 +333,12 @@ def __eq__(self, other):


class InMemoryRegistry(Registry):
def __init__(self, name, **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()
self.components = {k: v for k, v in components.items()}
self.priority = priority

def add(self, name: str, definition: str):
self.components[name] = definition
Expand Down Expand Up @@ -369,6 +373,11 @@ def contents(self, name: str, ext: str | None = None):
raise KeyError(f'InMemoryRegistry does not know of {name if ext is None else name + ext}')


def ordered_registries(registries: list[Registry]):
"""Sort the registries by their priority"""
return sorted(registries, key=lambda x: x.priority, reverse=True)


def registries_match(registry: Registry, spec):
if isinstance(spec, Registry):
return registry == spec
Expand Down Expand Up @@ -454,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 @@ -230,8 +230,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 0a4a96f

Please sign in to comment.