Skip to content

Commit

Permalink
make error a warning if can't clone when installed as package, simpli…
Browse files Browse the repository at this point in the history
…fy error message, fix tests which are improperly accessing dunder methods.
  • Loading branch information
sneakers-the-rat committed Jul 16, 2024
1 parent a9b7878 commit 7d1d5f1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
27 changes: 15 additions & 12 deletions src/pynwb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ def load_namespaces(**kwargs):
namespace_path = getargs('namespace_path', kwargs)
return __TYPE_MAP.load_namespaces(namespace_path)

def available_namespaces():
"""Returns all namespaces registered in the namespace catalog"""
return __NS_CATALOG.namespaces


def _git_cmd(*args) -> subprocess.CompletedProcess:
"""
Expand All @@ -183,9 +187,7 @@ def _clone_submodules():
'Exception while initializing submodules, got:\n'
'stdout:\n' + ('-'*20) + res.stdout + "\nstderr:\n" + ('-'*20) + res.stderr)
else: # pragma: no cover
raise RuntimeError("'core' is not a registered namespace, and pynwb doesn't seem to be installed"
"from a cloned repository so the submodules can't be initialized. "
"Something has gone terribly wrong. maybe try reinstalling???")
raise RuntimeError("Package is not installed from a git repository, can't clone submodules")


def _load_core_namespace(final:bool=False):
Expand Down Expand Up @@ -234,19 +236,20 @@ def _load_core_namespace(final:bool=False):
else:
try:
_clone_submodules()
except (FileNotFoundError, OSError): # pragma: no cover
raise RuntimeError(
"'core' is not a registered namespace. If you installed PyNWB locally using a git clone, you need to "
"use the --recurse_submodules flag when cloning. See developer installation instructions here: "
"https://pynwb.readthedocs.io/en/stable/install_developers.html#install-from-git-repository"
)
except (FileNotFoundError, OSError, RuntimeError) as e: # pragma: no cover
if 'core' not in available_namespaces():
warnings.warn(
"'core' is not a registered namespace. If you installed PyNWB locally using a git clone, "
"you need to use the --recurse_submodules flag when cloning. "
"See developer installation instructions here: "
"https://pynwb.readthedocs.io/en/stable/install_developers.html#install-from-git-repository\n"
f"Got exception: \n{e}"
)
if not final:
_load_core_namespace(final=True)
_load_core_namespace()

def available_namespaces():
"""Returns all namespaces registered in the namespace catalog"""
return __NS_CATALOG.namespaces



# a function to register a container classes with the global map
Expand Down
9 changes: 2 additions & 7 deletions tests/unit/test_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
except ImportError:
from importlib_resources import files

import pynwb
from pynwb.testing import TestCase

class TestPyNWBSubmoduleClone(TestCase):
Expand All @@ -23,9 +24,7 @@ def setUp(self):

# remove cache files if they exist
self.typemap_cache = Path(files('pynwb') / 'core_typemap.pkl')
self.nscatalog_cache = Path(files('pynwb') / 'core_nscatalog.pkl')
self.typemap_cache.unlink(missing_ok=True)
self.nscatalog_cache.unlink(missing_ok=True)


def tearDown(self) -> None:
Expand All @@ -40,11 +39,9 @@ def test_clone_on_import(self):
we should try and import it
"""
assert not self.repo_dir.exists()
assert not self.nscatalog_cache.exists()
assert not self.typemap_cache.exists()

with self.assertWarns(Warning) as clone_warning:
import pynwb
importlib.reload(pynwb)

# there can be other warnings, but we should get at least one
Expand All @@ -53,7 +50,6 @@ def test_clone_on_import(self):
assert any(['initializing submodules' in msg for msg in messages])

assert self.repo_dir.exists()
assert self.nscatalog_cache.exists()
assert self.typemap_cache.exists()

# we should also get the namespaces correctly too
Expand All @@ -72,5 +68,4 @@ def test_typemap_ns_match(self):
The global __NS_CATALOG and the one contained within the global __TYPE_MAP
should be the same object after importing
"""
import pynwb
assert id(pynwb.__TYPE_MAP.namespace_catalog) == id(pynwb.__NS_CATALOG)
assert id(getattr(pynwb, '__TYPE_MAP').namespace_catalog) == id(getattr(pynwb, '__NS_CATALOG'))

0 comments on commit 7d1d5f1

Please sign in to comment.