Skip to content

Commit

Permalink
Try extending hardware.py
Browse files Browse the repository at this point in the history
Signed-off-by: Cristian Le <[email protected]>
  • Loading branch information
LecrisUT committed Apr 9, 2024
1 parent d7fe929 commit 00eb7ed
Showing 1 changed file with 14 additions and 40 deletions.
54 changes: 14 additions & 40 deletions tmt/hardware.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
[1] https://tmt.readthedocs.io/en/latest/spec/plans.html#hardware
"""

import dataclasses
import enum
import functools
import itertools
Expand All @@ -38,6 +37,7 @@
TYPE_CHECKING,
Any,
Callable,
ClassVar,
Generic,
NamedTuple,
Optional,
Expand All @@ -46,6 +46,7 @@
cast,
)

import attrs
import pint

import tmt.log
Expand Down Expand Up @@ -196,7 +197,7 @@ class ConstraintNameComponents(NamedTuple):
child_name: Optional[str]


@dataclasses.dataclass
@attrs.define
class ConstraintComponents:
"""
Components of a constraint.
Expand Down Expand Up @@ -327,7 +328,7 @@ def __init__(self, constraint_name: str, raw_value: str,
# Constraint classes
#

@dataclasses.dataclass(repr=False)
@attrs.define(repr=False)
class BaseConstraint(SpecBasedContainer[Spec, Spec]):
"""
Base class for all classes representing one or more constraints.
Expand Down Expand Up @@ -392,27 +393,16 @@ def variant(self) -> list['Constraint[Any]']:
return variants[0]


@dataclasses.dataclass(repr=False)
@attrs.define(repr=False)
class CompoundConstraint(BaseConstraint):
"""
Base class for all *compound* constraints.
"""

def __init__(
self,
reducer: ReducerType = any,
constraints: Optional[list[BaseConstraint]] = None
) -> None:
"""
Construct a compound constraint, constraint imposed to more than one dimension.
:param reducer: a callable reducing a list of results from child constraints into the final
answer.
:param constraints: child contraints.
"""

self.reducer = reducer
self.constraints = constraints or []
#: a callable reducing a list of results from child constraints into the final answer.
reducer: ClassVar[ReducerType] = any
#: child constraints.
constraints: list[BaseConstraint] = attrs.field(factory=list)

@property
def size(self) -> int:
Expand Down Expand Up @@ -465,7 +455,7 @@ def variants(
raise NotImplementedError


@dataclasses.dataclass(repr=False)
@attrs.define(repr=False)
class Constraint(BaseConstraint, Generic[ConstraintValueT]):
"""
A constraint imposing a particular limit to one of the guest properties.
Expand Down Expand Up @@ -742,20 +732,13 @@ def from_specification(
)


@dataclasses.dataclass(repr=False)
@attrs.define(repr=False)
class And(CompoundConstraint):
"""
Represents constraints that are grouped in ``and`` fashion.
"""

def __init__(self, constraints: Optional[list[BaseConstraint]] = None) -> None:
"""
Hold constraints that are grouped in ``and`` fashion.
:param constraints: list of constraints to group.
"""

super().__init__(all, constraints=constraints)
reducer = all

def variants(
self,
Expand Down Expand Up @@ -800,21 +783,12 @@ def variants(
+ simple_constraints


@dataclasses.dataclass(repr=False)
@attrs.define(repr=False)
class Or(CompoundConstraint):
"""
Represents constraints that are grouped in ``or`` fashion.
"""

def __init__(self, constraints: Optional[list[BaseConstraint]] = None) -> None:
"""
Hold constraints that are grouped in ``or`` fashion.
:param constraints: list of constraints to group.
"""

super().__init__(any, constraints=constraints)

def variants(
self,
members: Optional[list[Constraint[Any]]] = None
Expand Down Expand Up @@ -1355,7 +1329,7 @@ def parse_hw_requirements(spec: Spec) -> BaseConstraint:
return _parse_block(spec)


@dataclasses.dataclass
@attrs.define
class Hardware(SpecBasedContainer[Spec, Spec]):
constraint: Optional[BaseConstraint]
spec: Spec
Expand Down

0 comments on commit 00eb7ed

Please sign in to comment.