~nataliabidart/ubuntuone-client/credentials-shutdown

« back to all changes in this revision

Viewing changes to ubuntuone/credentials/__init__.py

  • Committer: natalia.bidart at canonical
  • Date: 2011-01-11 19:13:52 UTC
  • Revision ID: natalia.bidart@canonical.com-20110111191352-zeucudsfuzlj6czc
The service should shutdown when unused (LP: #701606).

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
 
36
36
Q_ = lambda string: gettext.dgettext(GETTEXT_PACKAGE, string)
37
37
NO_OP = lambda *args, **kwargs: None
 
38
TIMEOUT_INTERVAL = 500
38
39
 
39
40
LOG_LEVEL = logging.DEBUG
40
41
path = os.path.join(LOGFOLDER, 'credentials.log')
68
69
class CredentialsManagement(dbus.service.Object):
69
70
    """DBus object that manages Ubuntu One credentials."""
70
71
 
71
 
    def __init__(self, *args, **kwargs):
 
72
    def __init__(self, timeout_func=lambda *a: None,
 
73
                 shutdown_func=lambda *a: None, *args, **kwargs):
72
74
        super(CredentialsManagement, self).__init__(*args, **kwargs)
 
75
        self._ref_count = 0
 
76
        self.timeout_func = timeout_func
 
77
        self.shutdown_func = shutdown_func
 
78
 
73
79
        self.sso_match = None
74
80
        self.sso_proxy = self._get_sso_proxy()
75
81
 
113
119
 
114
120
        return proxy
115
121
 
 
122
    def _get_ref_count(self):
 
123
        """Get value of ref_count."""
 
124
        logger.debug('ref_count is %r.', self._ref_count)
 
125
        return self._ref_count
 
126
 
 
127
    def _set_ref_count(self, new_value):
 
128
        """Set a new value to ref_count."""
 
129
        logger.debug('ref_count is %r, changing value to %r.',
 
130
                     self._ref_count, new_value)
 
131
        if new_value < 0:
 
132
            self._ref_count = 0
 
133
            msg = 'Attempting to decrease ref_count to a negative value (%r).'
 
134
            logger.warning(msg, new_value)
 
135
        else:
 
136
            self._ref_count = new_value
 
137
 
 
138
        if self._ref_count == 0:
 
139
            self.timeout_func(TIMEOUT_INTERVAL, self.shutdown_func)
 
140
 
 
141
    ref_count = property(fget=_get_ref_count, fset=_set_ref_count)
 
142
 
116
143
    # Operator not preceded by a space (fails with dbus decorators)
117
144
    # pylint: disable=C0322
118
145
 
119
146
    @dbus.service.signal(DBUS_CREDENTIALS_IFACE)
120
147
    def AuthorizationDenied(self):
121
148
        """Signal thrown when the user denies the authorization."""
 
149
        self.ref_count -= 1
122
150
        logger.info('%s: emitting AuthorizationDenied.',
123
151
                    self.__class__.__name__)
124
152
 
125
153
    @dbus.service.signal(DBUS_CREDENTIALS_IFACE, signature='a{ss}')
126
154
    def CredentialsFound(self, credentials):
127
155
        """Signal thrown when the credentials are found."""
 
156
        self.ref_count -= 1
128
157
        logger.info('%s: emitting CredentialsFound.',
129
158
                    self.__class__.__name__)
130
159
 
131
160
    @dbus.service.signal(DBUS_CREDENTIALS_IFACE)
132
161
    def CredentialsNotFound(self):
133
162
        """Signal thrown when the credentials are not found."""
 
163
        self.ref_count -= 1
134
164
        logger.info('%s: emitting CredentialsNotFound.',
135
165
                    self.__class__.__name__)
136
166
 
137
167
    @dbus.service.signal(DBUS_CREDENTIALS_IFACE)
138
168
    def CredentialsCleared(self):
139
169
        """Signal thrown when the credentials were cleared."""
 
170
        self.ref_count -= 1
140
171
        logger.info('%s: emitting CredentialsCleared.',
141
172
                    self.__class__.__name__)
142
173
 
143
174
    @dbus.service.signal(DBUS_CREDENTIALS_IFACE)
144
175
    def CredentialsStored(self):
145
176
        """Signal thrown when the credentials were cleared."""
 
177
        self.ref_count -= 1
146
178
        logger.info('%s: emitting CredentialsStored.',
147
179
                    self.__class__.__name__)
148
180
 
149
181
    @dbus.service.signal(DBUS_CREDENTIALS_IFACE, signature='a{ss}')
150
182
    def CredentialsError(self, error_dict):
151
183
        """Signal thrown when there is a problem getting the credentials."""
 
184
        self.ref_count -= 1
152
185
        logger.error('%s: emitting CredentialsError with error_dict %r.',
153
186
                     self.__class__.__name__, error_dict)
154
187
 
156
189
                         async_callbacks=("reply_handler", "error_handler"))
157
190
    def find_credentials(self, reply_handler=NO_OP, error_handler=NO_OP):
158
191
        """Ask the Ubuntu One credentials."""
 
192
        self.ref_count += 1
159
193
        self.sso_proxy.find_credentials(APP_NAME, {},
160
194
            reply_handler=reply_handler, error_handler=error_handler)
161
195
 
164
198
                         async_callbacks=("reply_handler", "error_handler"))
165
199
    def clear_credentials(self, reply_handler=NO_OP, error_handler=NO_OP):
166
200
        """Clear the Ubuntu One credentials."""
 
201
        self.ref_count += 1
167
202
        self.sso_proxy.clear_credentials(APP_NAME, {},
168
203
            reply_handler=reply_handler, error_handler=error_handler)
169
204
 
173
208
    def store_credentials(self, credentials,
174
209
                          reply_handler=NO_OP, error_handler=NO_OP):
175
210
        """Store the token for Ubuntu One application."""
 
211
        self.ref_count += 1
176
212
        self.sso_proxy.store_credentials(APP_NAME, credentials,
177
213
            reply_handler=reply_handler, error_handler=error_handler)
178
214
 
180
216
                         async_callbacks=("reply_handler", "error_handler"))
181
217
    def register(self, reply_handler=NO_OP, error_handler=NO_OP):
182
218
        """Get credentials if found else prompt to register to Ubuntu One."""
 
219
        self.ref_count += 1
183
220
        params = {HELP_TEXT_KEY: DESCRIPTION, TC_URL_KEY: TC_URL,
184
221
                  PING_URL_KEY: PING_URL, WINDOW_ID_KEY: '0'}
185
222
        self.sso_proxy.register(APP_NAME, params,