~dobey/ubuntu/oneiric/ubuntuone-control-panel/release-113

1.1.2 by natalia.bidart at canonical
Import upstream version 0.5.1
1
# -*- coding: utf-8 -*-
2
3
# Authors: Natalia B. Bidart <nataliabidart@canonical.com>
4
#
5
# Copyright 2010 Canonical Ltd.
6
#
7
# This program is free software: you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License version 3, as published
9
# by the Free Software Foundation.
10
#
11
# This program is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranties of
13
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
14
# PURPOSE.  See the GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License along
17
# with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19
"""Client to use replication services."""
20
21
from twisted.internet.defer import Deferred, inlineCallbacks, returnValue
22
23
from ubuntuone.controlpanel.logger import setup_logging
24
25
26
logger = setup_logging('replication_client')
27
28
BOOKMARKS = 'bookmarks'
29
CONTACTS = 'contacts'
30
# we should get this list from somewhere else
31
REPLICATIONS = set([BOOKMARKS, CONTACTS])
32
33
34
class ReplicationError(Exception):
35
    """A replication error."""
36
37
38
class NoPairingRecord(ReplicationError):
39
    """There is no pairing record."""
40
41
42
class InvalidIdError(ReplicationError):
43
    """The replication id is not valid."""
44
45
46
class NotExcludedError(ReplicationError):
47
    """The replication can not be replicated since is not excluded."""
48
49
50
class AlreadyExcludedError(ReplicationError):
51
    """The replication can not be excluded since is already excluded."""
52
53
54
def get_replication_proxy(replication_module=None):
55
    """Return a proxy to the replication client."""
56
    d = Deferred()
57
    if replication_module is None:
58
        # delay import in case DC is not installed at module import time
59
        # Unable to import 'desktopcouch.application.replication_services'
1.1.17 by Rodney Dawes
Import upstream version 1.1.1
60
        # pylint: disable=W0404,F0401
1.1.2 by natalia.bidart at canonical
Import upstream version 0.5.1
61
        from desktopcouch.application.replication_services \
62
            import ubuntuone as replication_module
63
    try:
64
        result = replication_module.ReplicationExclusion()
65
    except ValueError:
66
        d.errback(NoPairingRecord())
67
    else:
68
        d.callback(result)
69
70
    return d
71
72
73
@inlineCallbacks
74
def get_replications():
75
    """Retrieve the list of replications."""
76
    yield get_replication_proxy()
77
    returnValue(REPLICATIONS)
78
79
80
@inlineCallbacks
81
def get_exclusions():
82
    """Retrieve the list of exclusions."""
83
    proxy = yield get_replication_proxy()
84
    result = proxy.all_exclusions()
85
    returnValue(result)
86
87
88
@inlineCallbacks
89
def replicate(replication_id):
90
    """Remove replication_id from the exclusions list."""
91
    replications = yield get_replications()
92
    if replication_id not in replications:
93
        raise InvalidIdError(replication_id)
94
95
    exclusions = yield get_exclusions()
96
    if replication_id not in exclusions:
97
        raise NotExcludedError(replication_id)
98
99
    proxy = yield get_replication_proxy()
100
    yield proxy.replicate(replication_id)
101
102
103
@inlineCallbacks
104
def exclude(replication_id):
105
    """Add replication_id to the exclusions list."""
106
    replications = yield get_replications()
107
    if replication_id not in replications:
108
        raise InvalidIdError(replication_id)
109
110
    exclusions = yield get_exclusions()
111
    if replication_id in exclusions:
112
        raise AlreadyExcludedError(replication_id)
113
114
    proxy = yield get_replication_proxy()
115
    yield proxy.exclude(replication_id)