-
Notifications
You must be signed in to change notification settings - Fork 1
Create AutoDiscover class in infra/module.py #4
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import importlib | ||
|
|
||
|
|
||
| class AutoDiscover: | ||
| """ | ||
| This class search modules from path recursively. What it does | ||
| is actually search all the modules and run an `import_module` for each. | ||
|
|
||
| :param path: `pathlib.Path` object | ||
| :param pattern: Must be a string for search likes string.endswith(pattern) | ||
| :return: modules list | ||
|
|
||
| How to use: | ||
|
|
||
| autodiscover = AutoDiscover(path=...) | ||
| autodiscover() | ||
| """ | ||
|
|
||
| def __init__(self, path, pattern=None): | ||
| self.path = path | ||
| self.pattern = pattern | ||
| self.root = self.__get_dotted_full_path(path=path) | ||
|
|
||
| def __call__(self): | ||
| return self.__autodiscover(path=self.path, pattern=self.pattern) | ||
|
|
||
| def __autodiscover(self, path, pattern): | ||
| modules = [] | ||
|
|
||
| for obj in path.iterdir(): | ||
| if obj.name.startswith('_'): | ||
| continue | ||
|
|
||
| if ( | ||
| obj.is_file() | ||
| and obj.suffix == '.py' | ||
| and obj.match(pattern or '*') | ||
| ): | ||
| module_name = self.__normalize_module_name( | ||
| module_name='.'.join(obj.parts).replace('.py', '') | ||
| ) | ||
| modules.append(importlib.import_module(module_name)) | ||
|
|
||
| if obj.is_dir(): | ||
| modules.extend(self.__autodiscover(path=obj, pattern=pattern)) | ||
|
|
||
| return modules | ||
|
|
||
| def __normalize_module_name(self, module_name): | ||
| return module_name[module_name.find(self.root):] | ||
|
|
||
| def __get_root_parts(self, path): | ||
| def get_root_parts(path): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Depois me explica isso aqui? haha 🤔
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hahaha Esse método serve para buscar o O nome do módulo para funcionar deve ser: O que essa função faz é subir os niveis enquanto achar o Esse root serve de O normalize exclui tudo que vem antes do Saka? Isso foi feito para ser genérico, pois podemos chamar o |
||
| parts = [] | ||
| try: | ||
| next(path.glob('__init__.py')) | ||
| except StopIteration: | ||
| return parts | ||
| parts.append(path.name) | ||
| parts.extend(get_root_parts(path.parent)) | ||
| return parts | ||
| return reversed(get_root_parts(path=path)) | ||
|
|
||
| def __get_dotted_full_path(self, path): | ||
| return '.'.join(self.__get_root_parts(path=path)) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💅 Tem uns espaços a mais aqui... acho que da pra alinhar o
objcomand.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hahaha eu deixei para alinhar a condição... mas posso voltar também 🎉