Skip to content

Commit

Permalink
Enable access to resources through the module / tab-complete
Browse files Browse the repository at this point in the history
  • Loading branch information
austinschneider committed Sep 16, 2024
1 parent 0d41fb1 commit 87a76bc
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
1 change: 1 addition & 0 deletions python/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from . import _util
from . import Injector
from . import Weighter
from . import resources

# Intropspect package version
import sys
Expand Down
34 changes: 27 additions & 7 deletions python/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -724,14 +724,9 @@ def load_flux(model_name, *args, **kwargs):
return load_resource("flux", model_name, *args, **kwargs)


def load_detector(model_name, *args, **kwargs):
resource = load_resource("flux", model_name, *args, **kwargs)
if resource is not None:
return resource

resource_type = "detector"
def _detector_file_loader(model_name):
resource_name = model_name
folder = _resource_folder_by_name[resource_type]
folder = _resource_folder_by_name["detector"]

abs_dir = _get_model_path(resource_name, prefix=folder, is_file=False, must_exist=True, specific_file=None)

Expand All @@ -748,6 +743,13 @@ def load_detector(model_name, *args, **kwargs):
raise ValueError("Could not find detector loading script \"{script_fname}\" or densities and materials files \"{densities_fname}\", \"materials_fname\"")


def load_detector(model_name, *args, **kwargs):
resource = load_resource("flux", model_name, *args, **kwargs)
if resource is not None:
return resource
return _detector_file_loader


def load_processes(model_name, *args, **kwargs):
return load_resource("processes", model_name, *args, **kwargs)

Expand Down Expand Up @@ -853,3 +855,21 @@ def detector_help(detector_name):
def process_help(process_name):
doc = process_docs(process_name)
pydoc.pager(doc)

def _get_process_loader(process_name):
return get_resource_loader("processes", process_name)

def _get_flux_loader(flux_name):
return get_resource_loader("flux", flux_name)

def _get_detector_loader(detector_name):
loader = get_resource_loader("detector", detector_name)
if loader is not None:
return loader

def load_detector():
return _detector_file_loader(detector_name)

load_detector.__doc__ = detector_docs(detector_name)

return load_detector
27 changes: 27 additions & 0 deletions python/resources.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from . import _util

class ResourceList:
def __init__(self, resource_type, list_method, load_method):
self.__resource_type = resource_type
self.__list_method = list_method
self.__load_method = load_method

def __getattr__(self, name):
resources = self.__list_method()
if name in resources:
return self.__load_method(name)
else:
# Default behaviour
return object.__getattribute__(self, name)

def __dir__(self):
dirs = dir(self.__class__)
dirs += list(self.__dict__.keys())
dirs += self.__list_method()
return sorted(dirs)

fluxes = ResourceList('fluxes', _util.list_fluxes, _util._get_flux_loader)
detectors = ResourceList('detectors', _util.list_detectors, _util._get_detector_loader)
processes = ResourceList('processes', _util.list_processes, _util._get_process_loader)

del ResourceList

0 comments on commit 87a76bc

Please sign in to comment.