diff --git a/tests/test_log_sys.py b/tests/test_log_sys.py index 779c9dbc..f93a0830 100644 --- a/tests/test_log_sys.py +++ b/tests/test_log_sys.py @@ -2,14 +2,12 @@ `tractor.log`-wrapping unit tests. ''' -import importlib from pathlib import Path import shutil -import sys -from types import ModuleType import pytest import tractor +from tractor import _code_load def test_root_pkg_not_duplicated_in_logger_name(): @@ -37,31 +35,6 @@ def test_root_pkg_not_duplicated_in_logger_name(): assert 'mod' not in sublog.name -# ?TODO, move this into internal libs? -# -[ ] we already use it in `modden.config._pymod` as well -def load_module_from_path( - path: Path, - module_name: str|None = None, -) -> ModuleType: - ''' - Taken from SO, - https://stackoverflow.com/a/67208147 - - which is based on stdlib docs, - https://docs.python.org/3/library/importlib.html#importing-a-source-file-directly - - ''' - module_name = module_name or path.stem - spec = importlib.util.spec_from_file_location( - module_name, - str(path), - ) - module = importlib.util.module_from_spec(spec) - sys.modules[module_name] = module - spec.loader.exec_module(module) - return module - - def test_implicit_mod_name_applied_for_child( testdir: pytest.Pytester, loglevel: str, @@ -109,7 +82,7 @@ def test_implicit_mod_name_applied_for_child( # XXX NOTE, once the "top level" pkg mod has been # imported, we can then use `import` syntax to # import it's sub-pkgs and modules. - pkgmod = load_module_from_path( + pkgmod = _code_load.load_module_from_path( Path(pkg / '__init__.py'), module_name=proj_name, ) diff --git a/tractor/_code_load.py b/tractor/_code_load.py new file mode 100644 index 00000000..65effaa1 --- /dev/null +++ b/tractor/_code_load.py @@ -0,0 +1,48 @@ +# tractor: structured concurrent "actors". +# Copyright 2018-eternity Tyler Goodlet. + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. + +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +''' +(Hot) coad (re-)load utils for python. + +''' +import importlib +from pathlib import Path +import sys +from types import ModuleType + +# ?TODO, move this into internal libs? +# -[ ] we already use it in `modden.config._pymod` as well +def load_module_from_path( + path: Path, + module_name: str|None = None, +) -> ModuleType: + ''' + Taken from SO, + https://stackoverflow.com/a/67208147 + + which is based on stdlib docs, + https://docs.python.org/3/library/importlib.html#importing-a-source-file-directly + + ''' + module_name = module_name or path.stem + spec = importlib.util.spec_from_file_location( + module_name, + str(path), + ) + module = importlib.util.module_from_spec(spec) + sys.modules[module_name] = module + spec.loader.exec_module(module) + return module