##// END OF EJS Templates
util: add base class for transactional context managers...
Martin von Zweigbergk -
r33790:bbbbd3c3 default
parent child Browse files
Show More
@@ -11,9 +11,10 b' from .i18n import _'
11
11
12 from . import (
12 from . import (
13 error,
13 error,
14 util,
14 )
15 )
15
16
16 class dirstateguard(object):
17 class dirstateguard(util.transactional):
17 '''Restore dirstate at unexpected failure.
18 '''Restore dirstate at unexpected failure.
18
19
19 At the construction, this class does:
20 At the construction, this class does:
@@ -43,16 +44,6 b' class dirstateguard(object):'
43 # ``release(tr, ....)``.
44 # ``release(tr, ....)``.
44 self._abort()
45 self._abort()
45
46
46 def __enter__(self):
47 return self
48
49 def __exit__(self, exc_type, exc_val, exc_tb):
50 try:
51 if exc_type is None:
52 self.close()
53 finally:
54 self.release()
55
56 def close(self):
47 def close(self):
57 if not self._active: # already inactivated
48 if not self._active: # already inactivated
58 msg = (_("can't close already inactivated backup: %s")
49 msg = (_("can't close already inactivated backup: %s")
@@ -1168,7 +1168,7 b' class pulloperation(object):'
1168 # deprecated; talk to trmanager directly
1168 # deprecated; talk to trmanager directly
1169 return self.trmanager.transaction()
1169 return self.trmanager.transaction()
1170
1170
1171 class transactionmanager(object):
1171 class transactionmanager(util.transactional):
1172 """An object to manage the life cycle of a transaction
1172 """An object to manage the life cycle of a transaction
1173
1173
1174 It creates the transaction on demand and calls the appropriate hooks when
1174 It creates the transaction on demand and calls the appropriate hooks when
@@ -101,7 +101,7 b' def _playback(journal, report, opener, v'
101 # only pure backup file remains, it is sage to ignore any error
101 # only pure backup file remains, it is sage to ignore any error
102 pass
102 pass
103
103
104 class transaction(object):
104 class transaction(util.transactional):
105 def __init__(self, report, opener, vfsmap, journalname, undoname=None,
105 def __init__(self, report, opener, vfsmap, journalname, undoname=None,
106 after=None, createmode=None, validator=None, releasefn=None,
106 after=None, createmode=None, validator=None, releasefn=None,
107 checkambigfiles=None):
107 checkambigfiles=None):
@@ -376,16 +376,6 b' class transaction(object):'
376 if self.count > 0 and self.usages == 0:
376 if self.count > 0 and self.usages == 0:
377 self._abort()
377 self._abort()
378
378
379 def __enter__(self):
380 return self
381
382 def __exit__(self, exc_type, exc_val, exc_tb):
383 try:
384 if exc_type is None:
385 self.close()
386 finally:
387 self.release()
388
389 def running(self):
379 def running(self):
390 return self.count > 0
380 return self.count > 0
391
381
@@ -15,6 +15,7 b' hide platform-specific details from the '
15
15
16 from __future__ import absolute_import
16 from __future__ import absolute_import
17
17
18 import abc
18 import bz2
19 import bz2
19 import calendar
20 import calendar
20 import codecs
21 import codecs
@@ -592,6 +593,31 b' class sortdict(collections.OrderedDict):'
592 for k, v in src:
593 for k, v in src:
593 self[k] = v
594 self[k] = v
594
595
596 class transactional(object):
597 """Base class for making a transactional type into a context manager."""
598 __metaclass__ = abc.ABCMeta
599
600 @abc.abstractmethod
601 def close(self):
602 """Successfully closes the transaction."""
603
604 @abc.abstractmethod
605 def release(self):
606 """Marks the end of the transaction.
607
608 If the transaction has not been closed, it will be aborted.
609 """
610
611 def __enter__(self):
612 return self
613
614 def __exit__(self, exc_type, exc_val, exc_tb):
615 try:
616 if exc_type is None:
617 self.close()
618 finally:
619 self.release()
620
595 @contextlib.contextmanager
621 @contextlib.contextmanager
596 def acceptintervention(tr=None):
622 def acceptintervention(tr=None):
597 """A context manager that closes the transaction on InterventionRequired
623 """A context manager that closes the transaction on InterventionRequired
General Comments 0
You need to be logged in to leave comments. Login now