-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from rfrezino/refactor-code
Fix code to work with nested dependencies
- Loading branch information
Showing
68 changed files
with
1,282 additions
and
419 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
27 changes: 27 additions & 0 deletions
27
src/hexagonal/domain/hexagonal_project/hexagonal_project.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from dataclasses import dataclass | ||
from typing import List, Optional | ||
|
||
from hexagonal.domain.hexagonal_project.hexagonal_project_layer import HexagonalProjectLayer | ||
from hexagonal.domain.python_file import PythonFile | ||
|
||
|
||
@dataclass | ||
class HexagonalProject: | ||
project_path: str | ||
layers: List[HexagonalProjectLayer] | ||
files_not_in_layers: List[PythonFile] | ||
|
||
def get_layer_for_file_path(self, file_full_path: str) -> Optional[HexagonalProjectLayer]: | ||
for layer in self.layers: | ||
for python_file in layer.python_files: | ||
if python_file.file_full_path == file_full_path: | ||
return layer | ||
return None | ||
|
||
def get_python_file(self, file_full_path: str) -> PythonFile: | ||
for layer in self.layers: | ||
for python_file in layer.python_files: | ||
if python_file.file_full_path == file_full_path: | ||
return python_file | ||
|
||
raise Exception(f'File not found {file_full_path}') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
from dataclasses import dataclass | ||
from typing import Optional, List | ||
|
||
from hexagonal.domain.python_module import PythonModule | ||
from typing import List | ||
|
||
|
||
@dataclass | ||
class PythonFile: | ||
full_path: str | ||
relative_path_from_source_module: str | ||
layer_name: str | ||
layer_index: Optional[int] | ||
imported_modules: List[PythonModule] | ||
file_full_path: str | ||
file_name: str | ||
file_folder_full_path: str | ||
# For instance if the project is located at: /usr/project/ | ||
# and the file is /usr/project/services/another/file.py | ||
# the project_relative_folder_path is "services/another" | ||
relative_folder_path_from_project_folder: str | ||
project_folder_full_path: str | ||
imported_modules: List[str] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from dataclasses import dataclass | ||
|
||
|
||
@dataclass | ||
class RawPythonFile: | ||
file_full_path: str | ||
file_name: str | ||
file_folder_full_path: str | ||
# For instance if the project is located at: /usr/project/ | ||
# and the file is /usr/project/services/another/file.py | ||
# the project_relative_folder_path is "services/another" | ||
relative_folder_path_from_project_folder: str | ||
project_folder_full_path: str | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,27 @@ | ||
from typing import List | ||
|
||
from hexagonal.domain.hexagonal_layer import HexagonalLayer | ||
from hexagonal.main import hexagonal_config | ||
from hexagonal.services.hexagonal_composition import HexagonalComposition | ||
|
||
|
||
class HexagonalConfig: | ||
excluded_dirs: List[str] | ||
_layers: HexagonalComposition | ||
|
||
def __init__(self): | ||
self._layers = HexagonalComposition() | ||
self.excluded_dirs = [] | ||
|
||
def add_inner_layer(self, layer: HexagonalLayer) -> 'HexagonalConfig': | ||
self._layers.append(layer) | ||
return self | ||
|
||
def clear_layers(self): | ||
self._layers.clear() | ||
|
||
@property | ||
def layers(self) -> HexagonalComposition: | ||
return self._layers | ||
|
||
infrastructure_layer = HexagonalLayer(name='infrastructure', directories=['infrastructure']) | ||
use_cases_layer = HexagonalLayer(name='use_cases', directories=['use_cases']) | ||
services_layer = HexagonalLayer(name='services', directories=['services']) | ||
domain_layer = HexagonalLayer(name='domain', directories=['domain']) | ||
|
||
hexagonal_config + infrastructure_layer >> use_cases_layer >> services_layer >> domain_layer | ||
hexagonal_config = HexagonalConfig() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from hexagonal.domain.hexagonal_layer import HexagonalLayer | ||
from hexagonal.hexagonal_config import hexagonal_config | ||
|
||
hexagonal_config.add_inner_layer(HexagonalLayer(name='infrastructure', directories=['/infrastructure'])) | ||
hexagonal_config.add_inner_layer(HexagonalLayer(name='use_cases', directories=['/use_cases'])) | ||
hexagonal_config.add_inner_layer(HexagonalLayer(name='services', directories=['/services'])) | ||
hexagonal_config.add_inner_layer(HexagonalLayer(name='domain', directories=['/domain'])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/hexagonal/services/hexagonal_dependency_flow_checker.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from dataclasses import dataclass | ||
from typing import List | ||
|
||
from hexagonal.domain.hexagonal_project.hexagonal_project import HexagonalProject | ||
from hexagonal.domain.hexagonal_project.hexagonal_project_layer import HexagonalProjectLayer | ||
from hexagonal.domain.python_file import PythonFile | ||
|
||
|
||
@dataclass | ||
class DependencyFlowError: | ||
source_file: PythonFile | ||
source_file_layer: HexagonalProjectLayer | ||
imported_module: PythonFile | ||
imported_module_layer: HexagonalProjectLayer | ||
|
||
|
||
@dataclass | ||
class DependencyFlowResponse: | ||
errors: List[DependencyFlowError] | ||
|
||
|
||
class HexagonalDependencyFlowChecker: | ||
_hexagonal_project: HexagonalProject | ||
|
||
def __init__(self, hexagonal_project: HexagonalProject): | ||
self._hexagonal_project = hexagonal_project | ||
|
||
def check(self) -> DependencyFlowResponse: | ||
errors = [] | ||
for layer in self._hexagonal_project.layers: | ||
errors.extend(self._check_layer(layer=layer)) | ||
return DependencyFlowResponse(errors=errors) | ||
|
||
def _check_layer(self, *, layer: HexagonalProjectLayer) -> List[DependencyFlowError]: | ||
result = [] | ||
for python_file in layer.python_files: | ||
result.extend(self._check_file(layer=layer, python_file=python_file)) | ||
return result | ||
|
||
def _check_file(self, *, layer: HexagonalProjectLayer, python_file: PythonFile) -> List[DependencyFlowError]: | ||
result = [] | ||
for imported_module in python_file.imported_modules: | ||
module_layer = self._hexagonal_project.get_layer_for_file_path(file_full_path=imported_module) | ||
if not module_layer: | ||
continue | ||
|
||
if layer.index < module_layer.index: | ||
error = DependencyFlowError( | ||
source_file=python_file, | ||
source_file_layer=layer, | ||
imported_module=self._hexagonal_project.get_python_file(file_full_path=imported_module), | ||
imported_module_layer=module_layer) | ||
result.append(error) | ||
|
||
return result |
Oops, something went wrong.