~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/cleanup.py

  • Committer: Alexander Belchenko
  • Date: 2010-06-17 08:53:15 UTC
  • mfrom: (5300 +trunk)
  • mto: (5303.2.1 integration)
  • mto: This revision was merged to the branch mainline in revision 5305.
  • Revision ID: bialix@ukr.net-20100617085315-hr8186zck57zn35s
merge bzr.dev; fix NEWS

Show diffs side-by-side

added added

removed removed

Lines of Context:
78
78
        _run_cleanup(func, *args, **kwargs)
79
79
 
80
80
 
81
 
class OperationWithCleanups(object):
 
81
class ObjectWithCleanups(object):
 
82
    """A mixin for objects that hold a cleanup list.
 
83
 
 
84
    Subclass or client code can call add_cleanup and then later `cleanup_now`.
 
85
    """
 
86
    def __init__(self):
 
87
        self.cleanups = deque()
 
88
 
 
89
    def add_cleanup(self, cleanup_func, *args, **kwargs):
 
90
        """Add a cleanup to run.
 
91
 
 
92
        Cleanups may be added at any time.  
 
93
        Cleanups will be executed in LIFO order.
 
94
        """
 
95
        self.cleanups.appendleft((cleanup_func, args, kwargs))
 
96
 
 
97
    def cleanup_now(self):
 
98
        _run_cleanups(self.cleanups)
 
99
        self.cleanups.clear()
 
100
 
 
101
 
 
102
class OperationWithCleanups(ObjectWithCleanups):
82
103
    """A way to run some code with a dynamic cleanup list.
83
104
 
84
105
    This provides a way to add cleanups while the function-with-cleanups is
102
123
    """
103
124
 
104
125
    def __init__(self, func):
 
126
        super(OperationWithCleanups, self).__init__()
105
127
        self.func = func
106
 
        self.cleanups = deque()
107
 
 
108
 
    def add_cleanup(self, cleanup_func, *args, **kwargs):
109
 
        """Add a cleanup to run.
110
 
 
111
 
        Cleanups may be added at any time before or during the execution of
112
 
        self.func.  Cleanups will be executed in LIFO order.
113
 
        """
114
 
        self.cleanups.appendleft((cleanup_func, args, kwargs))
115
128
 
116
129
    def run(self, *args, **kwargs):
117
130
        return _do_with_cleanups(
121
134
        return _do_with_cleanups(
122
135
            self.cleanups, self.func, *args, **kwargs)
123
136
 
124
 
    def cleanup_now(self):
125
 
        _run_cleanups(self.cleanups)
126
 
        self.cleanups.clear()
127
 
 
128
137
 
129
138
def _do_with_cleanups(cleanup_funcs, func, *args, **kwargs):
130
139
    """Run `func`, then call all the cleanup_funcs.