Skip to content

Commit

Permalink
Merge pull request #2 from 0nliner/feature/ignore_components
Browse files Browse the repository at this point in the history
add: добавил возможность игнорировать компоненты на уровне модуля
  • Loading branch information
0nliner authored Jul 1, 2023
2 parents 2f99064 + 0321907 commit e666adf
Show file tree
Hide file tree
Showing 25 changed files with 57 additions and 43 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ sdist:
bdist_wheel:
python setup.py bdist_wheel

upload_build:
python -m twine upload --repository pypi dist/* --verbose

.PHONY : sdist bdist_wheel
4 changes: 2 additions & 2 deletions injector.egg-info/PKG-INFO → archtool.egg-info/PKG-INFO
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Metadata-Version: 2.1
Name: injector
Name: archtool
Version: 0.1.0
Home-page: 'https://github.com/0nliner/injector'
Home-page: https://github.com/0nliner/injector
Author: Aleksandr Chudaikin
Author-email: aleksandrchudaikindev@gmail.com
License: MIT
Expand Down
10 changes: 5 additions & 5 deletions injector.egg-info/SOURCES.txt → archtool.egg-info/SOURCES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ README.md
pyproject.toml
setup.cfg
setup.py
archtool.egg-info/PKG-INFO
archtool.egg-info/SOURCES.txt
archtool.egg-info/dependency_links.txt
archtool.egg-info/not-zip-safe
archtool.egg-info/top_level.txt
injector/__init__.py
injector/dependecy_injector.py
injector/exceptions.py
injector/global_types.py
injector/utils.py
injector.egg-info/PKG-INFO
injector.egg-info/SOURCES.txt
injector.egg-info/dependency_links.txt
injector.egg-info/not-zip-safe
injector.egg-info/top_level.txt
injector/components/__init__.py
injector/components/default_component.py
injector/interfaces/__init__.py
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
45 changes: 29 additions & 16 deletions injector/dependecy_injector.py → archtool/dependecy_injector.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@
DependecyDoesNotRegistred
)

from .interfaces import (DependecyInjectorInterface,
LayerInterface
)
from .interfaces import DependecyInjectorInterface

from injector.layers.default_layers import default_layers
from injector.layers import Layer
from injector.global_types import (AppModules,
from archtool.layers.default_layers import default_layers
from archtool.layers.di_basic_layer import Layer
from archtool.components.default_component import ComponentPattern
from archtool.global_types import (AppModules,
ContainerT,
DEPENDENCY_KEY)

from injector.utils import (get_dependencies,
from archtool.utils import (get_dependencies,
serialize_dep_key,
get_all_interfaces_and_realizations)

Expand All @@ -26,26 +25,33 @@
class DependecyInjector(DependecyInjectorInterface):
def __init__(self,
modules_list: AppModules,
layers: List[LayerInterface] = None):
layers: List[Layer] = None):

self.modules_list = modules_list
self.layers = layers if layers else default_layers
# инициализируем классы слоёв
# TODO: позже нужно вынести инициализацию.
# Это должен делать пользователь
self.layers = [layer() for layer in self.layers]
self._dependencies: Dict[DEPENDENCY_KEY, object] = {}

self.layers = layers if layers else default_layers
self._layers = []
for layer in self.layers:
initialized_layer = layer()
self._layers.append(initialized_layer)
self.layers = self._layers

def inject(self):
for layer in self.layers:
if issubclass(type(layer), Layer):
# TODO: сделать аннотирование на components
# TODO: добавить __iter__ на Components класс
for component_group in layer.component_groups:
for component_pattern in layer.component_groups:
# достаём модули, которые не игнорируют слой
modules_to_interact = self._exclude_ignored_modules(
component_pattern=component_pattern)

component_group_deps = get_all_interfaces_and_realizations(
app_modules=self.modules_list,
name_pattern=component_group.module_name_regex,
superclass=component_group.superclass)
app_modules=modules_to_interact,
name_pattern=component_pattern.module_name_regex,
superclass=component_pattern.superclass)

# добавляем зависимости слоя в глобальные зависимости
for dep_key, dep_class in component_group_deps.items():
Expand All @@ -58,6 +64,13 @@ def inject(self):
for dep_interface, dep_instance in self._dependencies.items():
self._inject_dependencies(container=dep_instance)

def _exclude_ignored_modules(self, component_pattern: ComponentPattern):
filtered_modules = []
for module in self.modules_list:
if component_pattern not in module.ignore:
filtered_modules.append(module)
return filtered_modules

def _inject_dependencies(self, container: ContainerT) -> None:
"""
Внедряет зависимости
Expand Down
File renamed without changes.
7 changes: 1 addition & 6 deletions injector/global_types.py → archtool/global_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,7 @@ class AppModule:
"""
import_path: str
# TODO: перенести логику игнора слоёв
ignore_views: bool = field(default=False)
ignore_service_interfaces: bool = field(default=False)
ignore_repositories_interfaces: bool = field(default=False)
ignore_repositories: bool = field(default=False)
ignore_services: bool = field(default=False)
is_legacy: bool = field(default=True)
ignore: List[Any] = field(default_factory=list)

# TODO: написать отдельную функцию валидации модуля,
# вынести её отдельно
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from abc import ABC, abstractmethod
from injector.global_types import AppModules, DEPENDENCY_KEY
from archtool.global_types import AppModules, DEPENDENCY_KEY


class DependecyInjectorInterface(ABC):
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from injector.components.default_component import ComponentPattern
from injector.layers import Layer
from archtool.components.default_component import ComponentPattern
from archtool.layers import Layer

from injector.layers.default_layer_interfaces import (ABCView,
from archtool.layers.default_layer_interfaces import (ABCView,
ABCService,
ABCRepo,
ABCController)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import List, Generator
from inspect import isclass

from injector.components.default_component import ComponentPatternBase
from archtool.components.default_component import ComponentPatternBase


class ValidationError(Exception):
Expand Down Expand Up @@ -79,6 +79,7 @@ def __new__(cls, name, bases, attrs, **kwargs):

new_attrs = {"__module__": module,
"component_groups": components_instance,
"Components": components_class,
"depends_on": depends_on}

new_layer_class = super_new(cls, name, bases, new_attrs)
Expand Down
4 changes: 2 additions & 2 deletions injector/utils.py → archtool/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
from re import sub

from pathlib import Path
from injector.exceptions import (CheckFailedException,
from archtool.exceptions import (CheckFailedException,
MultipleRealizationsException,
RealizationNotFount)
from injector.global_types import (
from archtool.global_types import (
Dependecy,
ContainerT,
DependeciesT,
Expand Down
8 changes: 5 additions & 3 deletions examples/example_project/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@

sys.path.append(pathlib.Path.cwd().as_posix())

from injector.dependecy_injector import DependecyInjector
from injector.global_types import AppModule
from archtool.dependecy_injector import DependecyInjector
from archtool.layers.default_layers import DomainLayer
from archtool.global_types import AppModule

modules_list = [
AppModule("test_module_1"),
AppModule("test_module_1",
ignore=[DomainLayer.Components.services]),
AppModule("test_module_2")
]

Expand Down
2 changes: 1 addition & 1 deletion examples/example_project/test_module_1/interfaces.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from abc import abstractmethod, ABCMeta
from injector.layers.default_layer_interfaces import (ABCService,
from archtool.layers.default_layer_interfaces import (ABCService,
ABCRepo)


Expand Down
2 changes: 1 addition & 1 deletion examples/example_project/test_module_2/interfaces.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from abc import abstractmethod, ABCMeta
from injector.layers.default_layer_interfaces import (ABCService,
from archtool.layers.default_layer_interfaces import (ABCService,
ABCRepo)


Expand Down
4 changes: 2 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[metadata]
name = injector
name = archtool
version = 0.1.0
license = MIT
author = Aleksandr Chudaikin
Expand All @@ -8,7 +8,7 @@ author_email= aleksandrchudaikindev@gmail.com
description_file= file: README.md
# package_dir={'': 'injector'}
# py_modules=['injector'],
url='https://github.com/0nliner/injector'
url=https://github.com/0nliner/injector
keywords=''

install_requires=[]
Expand Down

0 comments on commit e666adf

Please sign in to comment.