Skip to content

Commit

Permalink
fix: package scan and injection (tested on django)
Browse files Browse the repository at this point in the history
  • Loading branch information
sepgh committed Oct 23, 2023
1 parent 757077a commit a0485d5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
25 changes: 16 additions & 9 deletions rhazes/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,25 +62,32 @@ def decorator(obj_of_func):
# We are dealing with a class
@functools.wraps(obj_of_func, updated=())
class Proxy(obj_of_func):
def __int__(self, *args, **kwargs):
def __init__(self, *args, **kwargs):
inject_kwargs(
injections, configuration, obj_of_func.__init__, kwargs
)
super().__init__(*args, **kwargs)
super(obj_of_func, self).__init__(*args, **kwargs)

return Proxy

elif callable(obj_of_func):

def proxy(**kwargs):
inject_kwargs(injections, configuration, obj_of_func, kwargs)
return obj_of_func(**kwargs)
if (
obj_of_func.__name__ == "__init__" # constructor
):
def proxy(obj, *args, **kwargs):
inject_kwargs(injections, configuration, obj_of_func, kwargs)
return obj_of_func(obj, *args, **kwargs)

return proxy
return proxy
else:
def proxy(*args, **kwargs):
inject_kwargs(injections, configuration, obj_of_func, kwargs)
return obj_of_func(*args, **kwargs)

return proxy

else:
# Input is neither class or function
# Todo: raise error
pass
return obj_of_func

return decorator
13 changes: 8 additions & 5 deletions rhazes/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,12 @@ def scan(self) -> Set[str]:

def _scan(self, pkg) -> Set[str]:
packages = set()
mod = importlib.import_module(pkg)
for item in self._list_submodules(mod):
sub_m = f"{pkg}.{item}"
packages.add(sub_m)
packages.update(self._scan(sub_m))
try:
mod = importlib.import_module(pkg)
for item in self._list_submodules(mod):
sub_m = f"{pkg}.{item}"
packages.update(self._scan(sub_m))
packages.add(pkg)
except ModuleNotFoundError:
pass
return packages

0 comments on commit a0485d5

Please sign in to comment.