~cjwatson/python-timeline/division

« back to all changes in this revision

Viewing changes to timeline/nestingtimedaction.py

  • Committer: Robert Collins
  • Date: 2011-08-09 22:47:47 UTC
  • Revision ID: robertc@robertcollins.net-20110809224747-nrhlmjjtefud218o
Extraction of the generic timeline model from lp.services.timeline.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2010, 2011, Canonical Ltd
 
2
#
 
3
# This program is free software: you can redistribute it and/or modify
 
4
# it under the terms of the GNU Affero General Public License as published by
 
5
# the Free Software Foundation, either version 3 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU Affero General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU Affero General Public License
 
14
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
# GNU Affero General Public License version 3 (see the file LICENSE).
 
16
 
 
17
"""Time an action which calls other timed actions."""
 
18
 
 
19
 
 
20
__all__ = ['NestingTimedAction']
 
21
 
 
22
__metaclass__ = type
 
23
 
 
24
 
 
25
import datetime
 
26
 
 
27
from timedaction import TimedAction
 
28
 
 
29
 
 
30
class NestingTimedAction(TimedAction):
 
31
    """A variation of TimedAction which creates a nested environment.
 
32
    
 
33
    This is done by recording two 0 length timed actions in the timeline:
 
34
    one at the start of the action and one at the end, with -start and
 
35
    -stop appended to their categories.
 
36
 
 
37
    See `TimedAction` for more information.
 
38
    """
 
39
 
 
40
    def _init(self):
 
41
        self.duration = datetime.timedelta()
 
42
        self._category = self.category
 
43
        self.category = self._category + '-start'
 
44
 
 
45
    def finish(self):
 
46
        """Mark the TimedAction as finished."""
 
47
        end = self.timeline.start(self._category + '-stop', self.detail)
 
48
        end.duration = datetime.timedelta()