1
# ubuntuone.syncdaemon.state - manages SyncDaemon state
3
# Author: John Lenton <john.lenton@canonical.com>
5
# Author: Rodney Dawes <rodney.dawes@canonical.com>
7
# Copyright 2009 Canonical Ltd.
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.
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.
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/>.
21
The state of the SyncDaemon is managed here.
25
from ubuntuone.syncdaemon import states
27
class SyncDaemonStateManager(object):
29
Keep track of the state of the sync daemon.
32
def __init__(self, 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')
42
def handle_default(self, event, *args, **kwargs):
44
Forward interesting events on to the current state object.
46
if event not in self.interesting_events:
49
# this try/except catches everything on purpose
50
# pylint: disable-msg=W0702
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:
61
next_state = states.UNKNOWN_ERROR
62
self.logger.error('%s --[%s]--> ERROR!!!' % (self.state.name,
65
# pylint: enable-msg=W0702
67
self.logger.debug('%s --[%s]--> %s' % (self.state.name,
70
self.state = next_state
71
self.main.event_q.push('SYS_STATE_CHANGED', state=next_state)
77
Report our name as the name of the current state.
79
return self.state.name
83
Return a textual representation of the object. Used for str()
84
and repr() calls (so a print will show this)
86
return '<SyncDaemonStateManager [current state: %s]>' % self.state.name
87
def __cmp__(self, other):
89
Compare according to the current state.
91
return cmp(self.state, other)