1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 from timelinelib.general.observer import Observable
23
24 - def __init__(self, initial_value, initial_name="", history_size=10):
25 Observable.__init__(self)
26 if history_size < 1:
27 raise ValueError("history_size is to small (must be at least 1)")
28 self._history_size = history_size
29 self._history = [(initial_name, initial_value)]
30 self._current_index = 0
31 self._current_transaction = None
32
33 @property
35 if self._current_transaction is not None:
36 return self._current_transaction.value
37 else:
38 return self._history[self._current_index][1]
39
40 @property
42 return (
43 self._current_index,
44 self._current_transaction is not None,
45 list(self._history)
46 )
47
49 self.ensure_not_in_transaction()
50 self._history = [self._history[self._current_index]]
51 self._current_index = 0
52 self._notify()
53
54 - def move(self, index):
55 self.ensure_not_in_transaction()
56 if index < 0 or index >= len(self._history):
57 raise ValueError("Index does not exist in history")
58 self._current_index = index
59 self._notify()
60
61 - def new(self, name):
62 self._current_transaction = Transaction(
63 self,
64 name,
65 self.value,
66 self._current_transaction
67 )
68 return self._current_transaction
69
70 - def commit(self, transaction):
81
85
94
96 if self._current_transaction is not None:
97 raise TransactionError(
98 "Operation is not allowed "
99 "because transaction {0!r} is active".format(
100 self._current_transaction
101 )
102 )
103
107
110
111 - def __init__(self, transactions, name, value, parent):
116
118 return "{0}(name={1!r}, ...)".format(
119 self.__class__.__name__,
120 self._name
121 )
122
123 @property
126
127 @property
130
131 @value.setter
135
136 @property
139
140 @property
143
145 self._transactions.commit(self)
146
149
152
153 - def __exit__(self, exc_type, exc_val, exc_tb):
154 if exc_type is None:
155 self.commit()
156 else:
157 self.rollback()
158
161
164
166 def updater(*args, **kwargs):
167 self._transaction.value = getattr(
168 self._transaction.value,
169 name
170 )(*args, **kwargs)
171 return updater
172