~didrocks/ubuntuone-client/dont-suffer-zg-crash

« back to all changes in this revision

Viewing changes to ubuntuone/syncdaemon/state.py

  • Committer: Bazaar Package Importer
  • Author(s): Rodney Dawes
  • Date: 2009-06-30 12:00:00 UTC
  • Revision ID: james.westby@ubuntu.com-20090630120000-by806ovmw3193qe8
Tags: upstream-0.90.3
ImportĀ upstreamĀ versionĀ 0.90.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# ubuntuone.syncdaemon.state - manages SyncDaemon state
 
2
#
 
3
# Author: John Lenton <john.lenton@canonical.com>
 
4
#
 
5
# Author: Rodney Dawes <rodney.dawes@canonical.com>
 
6
#
 
7
# Copyright 2009 Canonical Ltd.
 
8
#
 
9
# This program is free software: you can redistribute it and/or modify it
 
10
# under the terms of the GNU General Public License version 3, as published
 
11
# by the Free Software Foundation.
 
12
#
 
13
# This program is distributed in the hope that it will be useful, but
 
14
# WITHOUT ANY WARRANTY; without even the implied warranties of
 
15
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
16
# PURPOSE.  See the GNU General Public License for more details.
 
17
#
 
18
# You should have received a copy of the GNU General Public License along
 
19
# with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
"""
 
21
The state of the SyncDaemon is managed here.
 
22
"""
 
23
import logging
 
24
 
 
25
from ubuntuone.syncdaemon import states
 
26
 
 
27
class SyncDaemonStateManager(object):
 
28
    """
 
29
    Keep track of the state of the sync daemon.
 
30
    """
 
31
 
 
32
    def __init__(self, main):
 
33
        self.main = main
 
34
        self.state = states.INIT
 
35
        self.main.event_q.subscribe(self)
 
36
        all_states = (o for o in vars(states).itervalues()
 
37
                      if isinstance(o, states.SyncDaemonState))
 
38
        self.interesting_events = set(sum((i.transitions.keys()
 
39
                                           for i in all_states), []))
 
40
        self.logger = logging.getLogger('ubuntuone.SyncDaemon.State')
 
41
 
 
42
    def handle_default(self, event, *args, **kwargs):
 
43
        """
 
44
        Forward interesting events on to the current state object.
 
45
        """
 
46
        if event not in self.interesting_events:
 
47
            return
 
48
 
 
49
        # this try/except catches everything on purpose
 
50
        # pylint: disable-msg=W0702
 
51
        try:
 
52
            this_state = self.state
 
53
            next_state = self.state.next(event)
 
54
            if next_state != self.state and next_state.enter is not None:
 
55
                next_state.enter(self.main)
 
56
            if not next_state.is_connected:
 
57
                client = self.main.action_q.client
 
58
                if client is not None:
 
59
                    client.disconnect()
 
60
        except:
 
61
            next_state = states.UNKNOWN_ERROR
 
62
            self.logger.error('%s --[%s]--> ERROR!!!' % (self.state.name,
 
63
                                                         event),
 
64
                              exc_info=1)
 
65
        # pylint: enable-msg=W0702
 
66
 
 
67
        self.logger.debug('%s --[%s]--> %s' % (self.state.name,
 
68
                                               event,
 
69
                                               next_state.name))
 
70
        self.state = next_state
 
71
        self.main.event_q.push('SYS_STATE_CHANGED', state=next_state)
 
72
 
 
73
 
 
74
    @property
 
75
    def name(self):
 
76
        """
 
77
        Report our name as the name of the current state.
 
78
        """
 
79
        return self.state.name
 
80
 
 
81
    def __repr__(self):
 
82
        """
 
83
        Return a textual representation of the object. Used for str()
 
84
        and repr() calls (so a print will show this)
 
85
        """
 
86
        return '<SyncDaemonStateManager [current state: %s]>' % self.state.name
 
87
    def __cmp__(self, other):
 
88
        """
 
89
        Compare according to the current state.
 
90
        """
 
91
        return cmp(self.state, other)