aeidon/revertable.py

Source code for module aeidon.revertable from file aeidon/revertable.py.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# -*- coding: utf-8 -*-

# Copyright (C) 2005 Osmo Salomaa
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

"""Actions that can be reverted, i.e. undone and redone."""

import aeidon

__all__ = ("RevertableAction", "RevertableActionGroup",)


class RevertableAction:

    """
    Action that can be reverted, i.e. undone and redone.

    :ivar description: Short one line description
    :ivar docs: Sequence of :attr:`aeidon.documents` items affected
    :ivar register: :attr:`aeidon.registers` item for action taken
    :ivar revert_args: Arguments passed to the revert method
    :ivar revert_function: Method called to revert this action
    :ivar revert_kwargs: Keyword arguments passed to the revert method
    """

    def __init__(self, **kwargs):
        """
        Initialize a :class:`RevertableAction` instance.

        `kwargs` can contain any of the names of public instance variables,
        of which :attr:`description`, :attr:`docs`, :attr:`register` and
        :attr:`revert_function` are required to be set eventually, either with
        `kwargs` or direct assignment later.
        """
        self.description = None
        self.docs = None
        self.register = None
        self.revert_args = ()
        self.revert_function = None
        self.revert_kwargs = {}
        for key, value in kwargs.items():
            setattr(self, key, value)

    def _get_reversion_register(self):
        """Return the :attr:`aeidon.registers` item for reversion."""
        if self.register.shift == 1:
            return aeidon.registers.UNDO
        if self.register.shift == -1:
            return aeidon.registers.REDO
        raise ValueError("Invalid register: {}"
                         .format(repr(self.register)))

    def revert(self):
        """Call the reversion function."""
        kwargs = self.revert_kwargs.copy()
        kwargs["register"] = self._get_reversion_register()
        return self.revert_function(*self.revert_args, **kwargs)


class RevertableActionGroup:

    """
    Group of :class:`RevertableAction`.

    :ivar actions: Sequence of :class:`RevertableAction` in group
    :ivar description: Short one line description
    """

    def __init__(self, **kwargs):
        """
        Initialize a :class:`RevertableAction` instance.

        `kwargs` can contain any of the names of public instance variables,
        of which :attr:`actions` and :attr:`description` are required to be
        set eventually, either with `kwargs` or direct assignment later.
        """
        self.actions = None
        self.description = None
        for key, value in kwargs.items():
            setattr(self, key, value)