forked from studio1247/gertrude
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistory.py
88 lines (72 loc) · 2.52 KB
/
history.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
# -*- coding: utf-8 -*-
## This file is part of Gertrude.
##
## Gertrude 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.
##
## Gertrude 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 Gertrude; if not, see <http://www.gnu.org/licenses/>.
import __builtin__
class Change:
def __init__(self, instance, member, value):
self.instance, self.member, self.value = instance, member, value
def Undo(self):
exec('self.instance.%s = self.value' % self.member)
class Delete:
def __init__(self, instance, index):
self.instance, self.index = instance, index
def Undo(self):
self.instance[self.index].delete()
del self.instance[self.index]
class Insert:
def __init__(self, instance, index, value):
self.instance, self.index, self.value = instance, index, value
def Undo(self):
if isinstance(self.instance, list):
self.instance.insert(self.index, self.value)
else:
self.instance[self.index] = self.value
self.value.create()
class Call:
def __init__(self, function, args=None):
self.function = function
self.args = args
def Undo(self):
if self.args is None:
self.function()
else:
self.function(self.args)
class History(list):
def __init__(self):
list.__init__(self)
def Undo(self, count=1):
result = 0
for i in range(count):
if len(self) > 0:
actions = self[-1]
if actions is None:
return result
self.pop(-1)
for action in actions:
action.Undo()
result += 1
return result
def Append(self, actions):
if actions is not None and not isinstance(actions, list):
actions = [actions]
self.append(actions)
def Last(self):
if len(self) > 0:
return self[-1]
else:
return None
def Clear(self):
del self[:]
__builtin__.history = History()