Skip to content

Commit

Permalink
Relaying ImportCatcher instance to FakeModule instance
Browse files Browse the repository at this point in the history
  • Loading branch information
Mandera committed May 20, 2023
1 parent f82ac62 commit d2f2e11
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
5 changes: 3 additions & 2 deletions generalimport/fake_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,10 @@ class FakeModule:
Unhandled use-cases: https://github.com/ManderaGeneral/generalimport/issues?q=is%3Aissue+is%3Aopen+label%3Aunhandled """
__path__ = []

def __init__(self, spec, trigger: Optional[str] = None):
def __init__(self, spec, trigger: Optional[str] = None, catcher=None):
self.name = spec.name
self.trigger = trigger
self.catcher = catcher

self.__name__ = spec.name
self.__loader__ = spec.loader
Expand Down Expand Up @@ -107,7 +108,7 @@ def _item_is_dunder(item):
return item in NON_CALLABLE_DUNDERS

def __getattr__(self, item):
fakemodule = FakeModule(spec=self.__spec__, trigger=item)
fakemodule = FakeModule(spec=self.__spec__, trigger=item, catcher=self.catcher)
if self._item_is_exception(item=item) or self._item_is_dunder(item=item):
fakemodule.error_func(item)
return fakemodule
Expand Down
9 changes: 5 additions & 4 deletions generalimport/general_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class GeneralImporter:

def __init__(self):
self.catchers = []
self.latest_catcher = None

self._singleton()
self._skip_fullname = None
Expand Down Expand Up @@ -45,7 +46,7 @@ def find_spec(self, fullname, path=None, target=None):
return self._handle_relay(fullname=fullname, spec=spec)

def create_module(self, spec):
return FakeModule(spec=spec)
return FakeModule(spec=spec, catcher=self.latest_catcher)

def exec_module(self, module):
pass
Expand Down Expand Up @@ -77,11 +78,11 @@ def _handle_ignore(self, fullname, reason):
return None

def _handle_handle(self, fullname, reason):
catcher = self.catch(fullname=fullname)
if not catcher:
self.latest_catcher = self.catch(fullname=fullname)
if not self.latest_catcher:
return self._handle_ignore(fullname=fullname, reason="Unhandled")

getLogger(__name__).info(f"{catcher} is handling '{fullname}' - {reason}")
getLogger(__name__).info(f"{self.latest_catcher} is handling '{fullname}' - {reason}")

sys.modules.pop(fullname, None) # Remove possible namespace

Expand Down

0 comments on commit d2f2e11

Please sign in to comment.