Skip to content

_base_nyx_command

_base_nyx_command

Attributes

LOGGER = logging.getLogger(__name__) module-attribute

Classes

NyxCommand(stage, text, parent=None)

Bases: QUndoCommand

Base undo command bound to a stage.

The stage is held weakly because it owns the undo stack that owns this command.

Commands change nodes of the stage: they list them in target_nodes, so the stage can refuse to change nodes that belong to a reference node.

Attributes:

Name Type Description
MODIFIES_STRUCTURE bool

the command adds, removes or redefines components or nodes, which the nodes may not allow.

Initialize NyxCommand.

Parameters:

Name Type Description Default
stage Stage

stage the command operates on.

required
text str

undo stack text.

required
parent QUndoCommand | None

parent command.

None
Source code in src/nyx/core/commands/_base_nyx_command.py
30
31
32
33
34
35
36
37
38
39
40
41
42
43
def __init__(
    self, stage: Stage, text: str, parent: QtGui.QUndoCommand | None = None
) -> None:
    """Initialize NyxCommand.

    Args:
        stage (Stage): stage the command operates on.
        text (str): undo stack text.
        parent (QtGui.QUndoCommand | None): parent command.
    """
    super().__init__(text, parent=parent)
    # Weak: the stage owns the undo stack that owns this command, so a strong
    # reference forms a cycle that cannot be torn down safely by the GC.
    self._stage_ref = weakref.ref(stage)
Attributes
MODIFIES_STRUCTURE = False class-attribute instance-attribute
stage property

Stage: stage the command operates on.

Functions
modifies_structure()

bool: the command adds, removes or redefines components or nodes.

Source code in src/nyx/core/commands/_base_nyx_command.py
57
58
59
def modifies_structure(self) -> bool:
    """bool: the command adds, removes or redefines components or nodes."""
    return self.MODIFIES_STRUCTURE
target_nodes()

Get the nodes the command changes.

Returns:

Type Description
Iterable[Node | None]

nodes changed by the command. Override in subclasses.

Source code in src/nyx/core/commands/_base_nyx_command.py
61
62
63
64
65
66
67
68
def target_nodes(self) -> typing.Iterable[Node | None]:
    """Get the nodes the command changes.

    Returns:
        Iterable[Node | None]: nodes changed by the command. Override in
            subclasses.
    """
    return ()
validate()

Check the command can be executed.

Raises:

Type Description
NyxCommandValidationError

if the command arguments are invalid.

Source code in src/nyx/core/commands/_base_nyx_command.py
50
51
52
53
54
55
def validate(self) -> None:
    """Check the command can be executed.

    Raises:
        NyxCommandValidationError: if the command arguments are invalid.
    """
validate_not_locked()

Check the nodes the command changes can be changed.

Raises:

Type Description
NyxCommandValidationError

if a node belongs to a reference node, or the command changes the structure of a node that doesn't allow it.

Source code in src/nyx/core/commands/_base_nyx_command.py
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
def validate_not_locked(self) -> None:
    """Check the nodes the command changes can be changed.

    Raises:
        NyxCommandValidationError: if a node belongs to a reference node, or the
            command changes the structure of a node that doesn't allow it.
    """
    for node in self.target_nodes():
        if node is None:
            continue
        if node.is_in_locked_scope:
            raise NyxCommandValidationError(
                f"{node.path} belongs to a reference and can't be changed"
            )
        if self.modifies_structure() and node.locks_structure:
            raise NyxCommandValidationError(
                f"The structure of {node.path} can't be changed"
            )