Skip to content

Commit

Permalink
fix(sonar): create new method to reduce complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinBelthle committed Jan 4, 2024
1 parent 4a62a9a commit 3ad8b51
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 75 deletions.
47 changes: 16 additions & 31 deletions antarest/study/storage/variantstudy/business/command_reverter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from antarest.study.storage.rawstudy.model.filesystem.config.model import transform_name_to_id
from antarest.study.storage.rawstudy.model.filesystem.factory import FileStudy
from antarest.study.storage.rawstudy.model.filesystem.folder_node import ChildNotFoundError
from antarest.study.storage.variantstudy.business.utils import strip_matrix_protocol
from antarest.study.storage.variantstudy.business.utils import get_matrix_id
from antarest.study.storage.variantstudy.model.command.common import CommandName
from antarest.study.storage.variantstudy.model.command.create_area import CreateArea
from antarest.study.storage.variantstudy.model.command.create_binding_constraint import CreateBindingConstraint
Expand Down Expand Up @@ -100,37 +100,22 @@ def _revert_update_binding_constraint(
if isinstance(command, UpdateBindingConstraint) and command.id == base_command.id:
return [command]
elif isinstance(command, CreateBindingConstraint) and transform_name_to_id(command.name) == base_command.id:
new_matrices = []
for matrix in [
command.values,
command.less_term_matrix,
command.equal_term_matrix,
command.greater_term_matrix,
]:
new_matrix = None
args = {
"id": base_command.id,
"enabled": command.enabled,
"time_step": command.time_step,
"operator": command.operator,
"coeffs": command.coeffs,
"filter_year_by_year": command.filter_year_by_year,
"filter_synthesis": command.filter_synthesis,
"comments": command.comments,
"command_context": command.command_context,
}
for matrix_name in ["values", "less_term_matrix", "equal_term_matrix", "greater_term_matrix"]:
matrix = command.__getattribute__(matrix_name)
if matrix is not None:
if isinstance(matrix, str):
new_matrix = strip_matrix_protocol(matrix)
elif isinstance(matrix, list):
new_matrix = command.command_context.matrix_service.create(matrix)
new_matrices.append(new_matrix)
return [
UpdateBindingConstraint(
id=base_command.id,
enabled=command.enabled,
time_step=command.time_step,
operator=command.operator,
coeffs=command.coeffs,
values=new_matrices[0],
less_term_matrix=new_matrices[1],
equal_term_matrix=new_matrices[2],
greater_term_matrix=new_matrices[3],
filter_year_by_year=command.filter_year_by_year,
filter_synthesis=command.filter_synthesis,
comments=command.comments,
command_context=command.command_context,
)
]
args[matrix_name] = get_matrix_id(matrix, command.command_context.matrix_service)
return [UpdateBindingConstraint(**args)]

return base_command.get_command_extractor().extract_binding_constraint(base, base_command.id)

Expand Down
16 changes: 16 additions & 0 deletions antarest/study/storage/variantstudy/business/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,22 @@ def strip_matrix_protocol(matrix_uri: Union[List[List[float]], str, None]) -> st
return matrix_uri


def get_matrix_id(matrix: Union[List[List[float]], str], matrix_service: ISimpleMatrixService) -> str:
"""
If the matrix is a str: Removes its prefix ("matrix://")
If it's an array: Stores it in the matrices repository and returns a reference to the stored array.
Raises:
TypeError: If the provided matrix is neither a matrix nor a link to a matrix.
"""
if isinstance(matrix, str):
return strip_matrix_protocol(matrix)
elif isinstance(matrix, list):
return matrix_service.create(matrix)
# pragma: no cover
raise TypeError(repr(matrix))


class AliasDecoder:
@staticmethod
def links_series(alias: str, study: FileStudy) -> str:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from antarest.study.storage.rawstudy.model.filesystem.config.model import FileStudyTreeConfig, transform_name_to_id
from antarest.study.storage.rawstudy.model.filesystem.factory import FileStudy
from antarest.study.storage.variantstudy.business.matrix_constants_generator import GeneratorMatrixConstants
from antarest.study.storage.variantstudy.business.utils import strip_matrix_protocol, validate_matrix
from antarest.study.storage.variantstudy.business.utils import get_matrix_id, validate_matrix
from antarest.study.storage.variantstudy.business.utils_binding_constraint import (
apply_binding_constraint,
parse_bindings_coeffs_and_save_into_config,
Expand Down Expand Up @@ -124,23 +124,20 @@ def to_dto(self) -> CommandDTO:
for matrix_name in ["values", "less_term_matrix", "greater_term_matrix", "equal_term_matrix"]:
matrix_attr = getattr(self, matrix_name, None)
if matrix_attr is not None:
if isinstance(matrix_attr, str):
args[matrix_name] = strip_matrix_protocol(matrix_attr)
elif isinstance(matrix_attr, list):
args[matrix_name] = self.command_context.matrix_service.create(matrix_attr)
args[matrix_name] = get_matrix_id(matrix_attr, self.command_context.matrix_service)
return CommandDTO(action=self.command_name.value, args=args, version=self.version)

def get_inner_matrices(self) -> List[str]:
matrices = []
for matrix in [self.values, self.less_term_matrix, self.greater_term_matrix, self.equal_term_matrix]:
if matrix is not None:
if isinstance(matrix, str):
matrices.append(strip_matrix_protocol(matrix))
elif isinstance(matrix, list):
matrices.append(self.command_context.matrix_service.create(matrix))
else:
raise TypeError(repr(matrix))
return matrices
return [
get_matrix_id(matrix, self.command_context.matrix_service)
for matrix in [
self.values,
self.less_term_matrix,
self.greater_term_matrix,
self.equal_term_matrix,
]
if matrix is not None
]

def get_corresponding_matrices(self, v: Optional[Union[MatrixType, str]], old: bool, create: bool) -> Optional[str]:
constants: GeneratorMatrixConstants
Expand Down Expand Up @@ -251,36 +248,26 @@ def match(self, other: ICommand, equal: bool = False) -> bool:
)

def _create_diff(self, other: "ICommand") -> List["ICommand"]:
other = cast(CreateBindingConstraint, other)
from antarest.study.storage.variantstudy.model.command.update_binding_constraint import UpdateBindingConstraint

other = cast(CreateBindingConstraint, other)
bd_id = transform_name_to_id(self.name)
new_matrices = []
for self_matrix, other_matrix in zip(
[self.values, self.less_term_matrix, self.equal_term_matrix, self.greater_term_matrix],
[other.values, other.less_term_matrix, other.equal_term_matrix, other.greater_term_matrix],
):
new_matrix = None

args = {
"id": bd_id,
"enabled": other.enabled,
"time_step": other.time_step,
"operator": other.operator,
"coeffs": other.coeffs,
"filter_year_by_year": other.filter_year_by_year,
"filter_synthesis": other.filter_synthesis,
"comments": other.comments,
"command_context": other.command_context,
}
for matrix_name in ["values", "less_term_matrix", "equal_term_matrix", "greater_term_matrix"]:
self_matrix = self.__getattribute__(matrix_name)
other_matrix = other.__getattribute__(matrix_name)
if self_matrix != other_matrix:
if isinstance(other_matrix, str):
new_matrix = strip_matrix_protocol(other_matrix)
elif isinstance(other_matrix, list):
new_matrix = self.command_context.matrix_service.create(other_matrix)
new_matrices.append(new_matrix)
return [
UpdateBindingConstraint(
id=bd_id,
enabled=other.enabled,
time_step=other.time_step,
operator=other.operator,
coeffs=other.coeffs,
values=new_matrices[0],
less_term_matrix=new_matrices[1],
equal_term_matrix=new_matrices[2],
greater_term_matrix=new_matrices[3],
filter_year_by_year=other.filter_year_by_year,
filter_synthesis=other.filter_synthesis,
comments=other.comments,
command_context=other.command_context,
)
]
args[matrix_name] = get_matrix_id(other_matrix, self.command_context.matrix_service)

return [UpdateBindingConstraint(**args)]

0 comments on commit 3ad8b51

Please sign in to comment.