Skip to content

Commit

Permalink
UriScheme and CodeDependencyNode changes (#778)
Browse files Browse the repository at this point in the history
* _get_schemes returns type, get_scheme creates instance

* Module required if not shortform, add attr to retrieve var from module
  • Loading branch information
Helveg committed Nov 30, 2023
1 parent e367d19 commit 26e849b
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions bsb/storage/_files.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import typing
import warnings

import abc as _abc
import contextlib as _cl
Expand Down Expand Up @@ -331,20 +330,20 @@ def init_poolmanager(self, connections, maxsize, block=False, **kwargs):


@_ft.cache
def _get_schemes() -> _tp.Mapping[str, FileScheme]:
def _get_schemes() -> _tp.Mapping[str, typing.Type[FileScheme]]:
from ..plugins import discover

schemes = discover("storage.schemes")
schemes["file"] = FileScheme()
schemes["http"] = schemes["https"] = UrlScheme()
schemes["nm"] = NeuroMorphoScheme()
schemes["file"] = FileScheme
schemes["http"] = schemes["https"] = UrlScheme
schemes["nm"] = NeuroMorphoScheme
return schemes


def _get_scheme(scheme: str) -> FileScheme:
schemes = _get_schemes()
try:
return schemes[scheme]
return schemes[scheme]()
except KeyError:
raise KeyError(f"{scheme} is not a known file scheme.")

Expand Down Expand Up @@ -386,7 +385,8 @@ def get_stored_file(self):

@config.node
class CodeDependencyNode(FileDependencyNode):
module: str = config.attr(type=str)
module: str = config.attr(type=str, required=types.shortform())
attr: str = config.attr(type=str)

@config.property
def file(self):
Expand Down Expand Up @@ -414,6 +414,7 @@ def load_object(self):
module = importlib.util.module_from_spec(spec)
sys.modules[self.module] = module
spec.loader.exec_module(module)
return module if self.attr is None else module[self.attr]
finally:
tmp = list(reversed(sys.path))
tmp.remove(_os.getcwd())
Expand Down

0 comments on commit 26e849b

Please sign in to comment.