Skip to content

Commit 8ad235b

Browse files
authored
Make setting the inst_pkgs path more convenient (#627)
2 parents 1eb2b12 + 22b3b42 commit 8ad235b

5 files changed

Lines changed: 61 additions & 10 deletions

File tree

scopesim/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161

6262
# import specific classes from the modules to included in the global namespace
6363

64-
from .utils import bug_report
64+
from .utils import bug_report, set_inst_pkgs_path, link_irdb
6565
from .optics.optical_train import OpticalTrain
6666
from .commands.user_commands import UserCommands
6767
from .commands.scopesimple import Simulation

scopesim/commands/user_commands.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,10 @@ def patch_fake_symlinks(path: Path):
497497
# The path does not exist.
498498
parent = path.parent
499499
pathup = patch_fake_symlinks(parent)
500-
assert pathup != parent, ValueError("Cannot find path")
500+
501+
if pathup == parent:
502+
raise FileNotFoundError(f"Cannot find path '{path.name}'")
503+
501504
return patch_fake_symlinks(pathup / path.name)
502505

503506

@@ -515,13 +518,21 @@ def add_packages_to_rc_search(local_path, package_list):
515518
A list of the package names to add
516519
517520
"""
518-
plocal_path = patch_fake_symlinks(Path(local_path))
521+
try:
522+
plocal_path = patch_fake_symlinks(Path(local_path))
523+
except FileNotFoundError:
524+
# retry with mocks
525+
plocal_path = patch_fake_symlinks(Path("./scopesim/tests/mocks"))
526+
519527
for pkg in package_list:
520528
pkg_dir = plocal_path / pkg
521529
if not pkg_dir.exists():
522-
# todo: keep here, but add test for this by downloading test_package
523-
# raise ValueError("Package could not be found: {}".format(pkg_dir))
524-
logger.warning("Package could not be found: %s", pkg_dir)
530+
# For sub-packages (e.g. ELT, Armazones) it might be ok to not find
531+
# them, but if we're at the top level, this should not go silent.
532+
if len(package_list) == 1:
533+
raise ValueError(f"Package could not be found: {pkg_dir}")
534+
else:
535+
logger.warning("Package could not be found: %s", pkg_dir)
525536

526537
rc.__search_path__.append_first(pkg_dir)
527538

scopesim/tests/test_utils_functions.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,6 @@ def test_seq_includes_last_when_it_should_not(self, start, stop, step):
255255
assert arr[-1] != stop
256256
assert stop not in arr
257257

258-
259258
@pytest.mark.parametrize("start,stop,step",
260259
[(0, 10, 1),
261260
(10, 0, -1),
@@ -264,3 +263,9 @@ def test_seq_includes_last_when_it_should_not(self, start, stop, step):
264263
def test_seq_has_correct_step_size(self, start, stop, step):
265264
arr = utils.seq(start, stop, step)
266265
assert arr[1:] - arr[:-1] == approx(step)
266+
267+
268+
@pytest.mark.usefixtures("protect_currsys")
269+
def test_setting_instpkgspath():
270+
utils.link_irdb("bogus")
271+
assert rc.__config__["!SIM.file.local_packages_path"] == "bogus"

scopesim/tests/tests_commands/test_UserCommands.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,16 @@ def test_see_if_theres_an_entry_on_the_server_log_file(self):
113113
_ = UserCommands(use_instrument="test_package")
114114

115115

116+
@pytest.mark.usefixtures("protect_currsys")
117+
class TestIffyPkgPaths:
118+
def test_finds_basic_instrument(self):
119+
UserCommands(use_instrument="basic_instrument")
120+
121+
def test_throws_for_bogus_inst(self):
122+
with pytest.raises(ValueError):
123+
UserCommands(use_instrument="bogus_instrument")
124+
125+
116126
def test_patch_fake_symlinks(tmp_path):
117127
"""Setup a temporary directory with files and links."""
118128
# tmp_path is a fixture

scopesim/utils.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ def find_file(filename, path=None, silent=False):
273273

274274
# TODO: Not sure what to do here
275275
if from_currsys("!SIM.file.error_on_missing_file"):
276-
raise ValueError(msg)
276+
raise ValueError(msg)
277277

278278
return None
279279

@@ -699,14 +699,20 @@ def wrapper(*args, **kwargs):
699699

700700

701701
def update_logging(capture_warnings=True):
702-
"""Reload logging configuration from ``rc.__logging_config__``."""
702+
"""Reload logging configuration from ``rc.__logging_config__``.
703+
704+
.. versionadded:: 0.8.0
705+
"""
703706
# Need to access NestedMapping's internal dict here...
704707
dictConfig(rc.__logging_config__)
705708
logging.captureWarnings(capture_warnings)
706709

707710

708711
def log_to_file(enable=True):
709-
"""Enable or disable logging to file (convenience function)."""
712+
"""Enable or disable logging to file (convenience function).
713+
714+
.. versionadded:: 0.8.0
715+
"""
710716
if enable:
711717
handlers = ["console", "file"]
712718
else:
@@ -721,11 +727,30 @@ def set_console_log_level(level="INFO"):
721727
722728
This controls what is actually printed to the console by ScopeSim.
723729
Accepted values are: DEBUG, INFO (default), WARNING, ERROR and CRITICAL.
730+
731+
.. versionadded:: 0.8.0
724732
"""
725733
rc.__logging_config__["handlers"]["console"]["level"] = level
726734
update_logging()
727735

728736

737+
def set_inst_pkgs_path(pkg_path: Path | str) -> None:
738+
"""Set the local path for !SIM.file.local_packages_path (shortcut).
739+
740+
.. versionadded:: PLACEHOLDER_NEXT_RELEASE_VERSION
741+
"""
742+
rc.__config__["!SIM.file.local_packages_path"] = str(pkg_path)
743+
744+
745+
def link_irdb(irdb_path: Path | str | None = None) -> None:
746+
"""Set ``inst_pkgs`` to local clone of IRDB (convenience shortcut).
747+
748+
.. versionadded:: PLACEHOLDER_NEXT_RELEASE_VERSION
749+
"""
750+
irdb_path = irdb_path or rc.__pkg_dir__.parent.parent / "irdb"
751+
set_inst_pkgs_path(irdb_path)
752+
753+
729754
def seq(start, stop, step=1):
730755
"""Replacement for numpy.arange modelled after R's seq function.
731756

0 commit comments

Comments
 (0)