~fitbit-accomplishment-maintainers/account-plugin-fitbit/trunk

« back to all changes in this revision

Viewing changes to tools/account-console

  • Committer: Ken VanDine
  • Date: 2012-12-03 03:26:57 UTC
  • Revision ID: ken.vandine@canonical.com-20121203032657-jnxwwjp42su4dx7i
don't conflict with account-plugin-tools

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#! /usr/bin/python3
2
 
 
3
 
import argparse
4
 
import sys
5
 
 
6
 
from gi.repository import GLib
7
 
from gi.repository import GObject
8
 
from gi.repository import Accounts
9
 
from gi.repository import Signon
10
 
 
11
 
class GStrv(list):
12
 
    __gtype__ = GObject.type_from_name('GStrv')
13
 
 
14
 
class AccountConsole:
15
 
    def __init__(self):
16
 
        self.manager = Accounts.Manager()
17
 
 
18
 
    def list_accounts(self, args):
19
 
        accounts = self.manager.list()
20
 
        if not accounts:
21
 
            print('No accounts')
22
 
            main_loop.quit()
23
 
            return
24
 
 
25
 
        for account_id in accounts:
26
 
            account = self.manager.get_account(account_id)
27
 
            enabledness = 'enabled' if account.get_enabled() else 'disabled'
28
 
            print('account: id %s, %s, provider: %s' % (account_id, enabledness, account.get_provider_name()))
29
 
        main_loop.quit()
30
 
 
31
 
 
32
 
    def show_account(self, args):
33
 
        self.args = args
34
 
        account = self.manager.get_account(args.account)
35
 
        if not account:
36
 
            print >> sys.stderr, 'Account "%s" not found' % args.account
37
 
            sys.exit(1)
38
 
 
39
 
        enabledness = 'enabled' if account.get_enabled() else 'disabled'
40
 
        print('account: id %s, %s, provider: %s' % (account.id, enabledness, account.get_provider_name()))
41
 
        print('  Global settings:')
42
 
        i = account.get_settings_iter(None)
43
 
        self.enumerate_settings(i)
44
 
 
45
 
        # enumerate services and their settings
46
 
        services = account.list_services()
47
 
        for s in services:
48
 
            service = Accounts.AccountService.new(account, s)
49
 
            print('  Settings for %s' % (s.get_name(),))
50
 
            i = service.get_settings_iter(None)
51
 
            self.enumerate_settings(i)
52
 
        main_loop.quit()
53
 
 
54
 
 
55
 
    def create_account(self, args):
56
 
        self.args = args
57
 
        account = self.manager.create_account(args.provider)
58
 
        account.set_enabled(not args.disabled)
59
 
        for setting in args.s:
60
 
            (name, value) = self.parse_setting(setting)
61
 
            account.set_value(name, value)
62
 
        account.store(self.on_account_stored, None)
63
 
 
64
 
    def edit_account(self, args):
65
 
        self.args = args
66
 
        account = self.manager.get_account(args.account)
67
 
        if not account:
68
 
            print >> sys.stderr, 'Account "%s" not found' % args.account
69
 
            sys.exit(1)
70
 
        self.account = account
71
 
 
72
 
        if args.service:
73
 
            service = self.manager.get_service(args.service)
74
 
            if not service:
75
 
                print >> sys.stderr, 'Service "%s" not found' % args.service
76
 
                sys.exit(1)
77
 
            account.select_service(service)
78
 
 
79
 
        if args.enable:
80
 
            account.set_enabled(True)
81
 
        elif args.disable:
82
 
            account.set_enabled(False)
83
 
 
84
 
        for setting in args.s:
85
 
            (name, value) = self.parse_setting(setting)
86
 
            account.set_value(name, value)
87
 
 
88
 
        for setting in args.u:
89
 
            account.set_value(setting, None)
90
 
 
91
 
        if args.username is not None or args.password is not None or args.caption:
92
 
            value = GObject.Value()
93
 
            value.init(GObject.TYPE_UINT)
94
 
            signon_id = account.get_value(args.signon_id_field, value)
95
 
            if signon_id != Accounts.SettingSource.NONE:
96
 
                self.identity = Signon.Identity.new_from_db(value.get_uint())
97
 
                self.identity.query_info(self.on_info_ready, None)
98
 
            else:
99
 
                self.identity = Signon.Identity.new()
100
 
                info = Signon.IdentityInfo.new()
101
 
                self.write_signon_info(info)
102
 
        else:
103
 
            account.store(self.on_account_stored, None)
104
 
 
105
 
 
106
 
    def on_info_ready(self, identity, info, error, userdata):
107
 
        if error:
108
 
            print >> sys.stderr, 'Couldn\'t get identity info'
109
 
            sys.exit(1)
110
 
 
111
 
        self.write_signon_info(info)
112
 
 
113
 
 
114
 
    def write_signon_info(self, info):
115
 
        if self.args.username is not None:
116
 
            info.set_username(self.args.username)
117
 
        if self.args.caption or self.args.username:
118
 
            info.set_caption(self.args.caption or self.args.username)
119
 
        if self.args.password is not None or info.get_id() == 0:
120
 
            info.set_secret(self.args.password or '', True)
121
 
 
122
 
        self.identity.store_credentials_with_info(info,
123
 
                self.on_credentials_stored, self.account)
124
 
 
125
 
 
126
 
    def on_credentials_stored(self, identity, id, error, account):
127
 
        print('On credentials stored', id)
128
 
        account.set_value(self.args.signon_id_field, int(id))
129
 
        account.store(self.on_account_stored, None)
130
 
 
131
 
 
132
 
 
133
 
    def delete_account(self, args):
134
 
        self.args = args
135
 
        account = self.manager.get_account(args.account)
136
 
        if not account:
137
 
            print >> sys.stderr, 'Account "%s" not found' % args.account
138
 
            sys.exit(1)
139
 
 
140
 
        account.delete()
141
 
        account.store(self.on_account_stored_deleted, None)
142
 
 
143
 
 
144
 
    def login_identity(self, args):
145
 
        session_data = {}
146
 
        for parameter in args.p:
147
 
            (key, value) = self.parse_setting(parameter)
148
 
            session_data[key] = value
149
 
 
150
 
        self.login(args.identity, args.method, args.mechanism,session_data)
151
 
 
152
 
 
153
 
    def login_account(self, args):
154
 
        self.args = args
155
 
        account = self.manager.get_account(args.account)
156
 
        if not account:
157
 
            print >> sys.stderr, 'Account "%s" not found' % args.account
158
 
            sys.exit(1)
159
 
        self.account = account
160
 
 
161
 
        service = None
162
 
        if args.service:
163
 
            service = self.manager.get_service(args.service)
164
 
            if not service:
165
 
                print >> sys.stderr, 'Service "%s" not found' % args.service
166
 
                sys.exit(1)
167
 
 
168
 
        account_service = Accounts.AccountService.new(account, service)
169
 
        auth_data = account_service.get_auth_data()
170
 
        identity = auth_data.get_credentials_id()
171
 
        method = auth_data.get_method()
172
 
        mechanism = auth_data.get_mechanism()
173
 
        session_data = auth_data.get_parameters()
174
 
 
175
 
        # last, add session data from the command line
176
 
        for parameter in args.p:
177
 
            (key, value) = self.parse_setting(parameter)
178
 
            session_data[key] = value
179
 
 
180
 
        self.login(identity, method, mechanism, session_data)
181
 
 
182
 
 
183
 
    def login(self, identity, method, mechanism, session_data):
184
 
        if identity:
185
 
            self.session = Signon.AuthSession.new(identity, method)
186
 
        else:
187
 
            self.session = Signon.AuthSession.new(0, method)
188
 
 
189
 
        print(session_data)
190
 
        self.session.process(session_data, mechanism,
191
 
                self.login_process_cb, None)
192
 
 
193
 
 
194
 
    def load_auth_parameters(self, iterator, parameters):
195
 
        allsettings = {}
196
 
        (ok, key, value) = iterator.next()
197
 
        while ok:
198
 
            allsettings[key] = value
199
 
            (ok, key, value) = iterator.next()
200
 
 
201
 
        for (key, value) in allsettings.iteritems():
202
 
            # if the param key itself contains a '/', assume it's a list
203
 
            if '/' in key:
204
 
                (key, item_id) = key.split('/', 1)
205
 
                if not item_id.startswith('item'):
206
 
                    continue
207
 
                if not parameters.has_key(key):
208
 
                    parameters[key] = []
209
 
                parameters[key].append(value)
210
 
            else:
211
 
                parameters[key] = value
212
 
 
213
 
 
214
 
    def login_process_cb(self, session, reply, error, userdata):
215
 
        if error:
216
 
            print >> sys.stderr, 'Got authentication error:', error.message
217
 
            sys.exit(1)
218
 
 
219
 
        print('Got reply: ', reply)
220
 
        main_loop.quit()
221
 
 
222
 
 
223
 
    def on_account_stored_deleted(self, account, error, userdata):
224
 
        if not error:
225
 
            print('OK')
226
 
        else:
227
 
            print >> sys.stderr, 'Error occurred: ', error.message
228
 
        main_loop.quit()
229
 
 
230
 
    def on_account_stored(self, account, error, userdata):
231
 
        if not error:
232
 
            if 'print_id' in self.args and self.args.print_id:
233
 
                print('%s' % (account.id))
234
 
            else:
235
 
                print('OK %s' % (account.id))
236
 
        else:
237
 
            print >> sys.stderr, 'Error occurred: ', error.message
238
 
        main_loop.quit()
239
 
 
240
 
    def enumerate_settings(self, iterator):
241
 
        settings = []
242
 
        (ok, key, value) = iterator.next()
243
 
        while ok:
244
 
            settings.append((key, value))
245
 
            (ok, key, value) = iterator.next()
246
 
 
247
 
        settings.sort()
248
 
        for (key, value) in settings:
249
 
            print('    %s: %s (%s)' % (key, value, type(value)))
250
 
 
251
 
    def parse_setting(self, setting):
252
 
        (name, value_str) = setting.split('=')
253
 
        if ':' in name:
254
 
            (type, name) = name.split(':')
255
 
        else:
256
 
            type = 's'
257
 
 
258
 
        if type == 'i':
259
 
            value = int(value_str)
260
 
        elif type == 'u':
261
 
            value = GObject.Value()
262
 
            value.init(GObject.TYPE_UINT)
263
 
            value.set_uint(int(value_str))
264
 
        elif type == 'b':
265
 
            value = bool(value_str)
266
 
        elif type == 's':
267
 
            value = value_str
268
 
        elif type == 'as':
269
 
            value = GStrv(eval(value_str))
270
 
        return (name, value)
271
 
 
272
 
app = AccountConsole()
273
 
 
274
 
parser = argparse.ArgumentParser(description='Command-line tool for account handling')
275
 
subparsers = parser.add_subparsers(title='Valid actions')
276
 
 
277
 
subparser = subparsers.add_parser('list',
278
 
        help='List existing accounts')
279
 
subparser.set_defaults(func=app.list_accounts)
280
 
 
281
 
subparser = subparsers.add_parser('show',
282
 
        help='Show an account\'s settings')
283
 
subparser.add_argument('account', type=int,
284
 
        help='Id of the account')
285
 
subparser.set_defaults(func=app.show_account)
286
 
 
287
 
subparser = subparsers.add_parser('create',
288
 
        help='Create a new account')
289
 
subparser.add_argument('provider',
290
 
        help='Provider name (see /usr/share/accounts/providers)')
291
 
subparser.add_argument('--disabled', action='store_true',
292
 
        help='Create the account in disabled state')
293
 
subparser.add_argument('--print-id', action='store_true',
294
 
        help='Print the account ID on stdout')
295
 
subparser.add_argument('-s', action='append', default=[],
296
 
        help='Add a service setting, in the form [<type>:]<name>=<value>')
297
 
subparser.set_defaults(func=app.create_account)
298
 
 
299
 
subparser = subparsers.add_parser('edit',
300
 
        help='Edit an existing account')
301
 
subparser.add_argument('account', type=int,
302
 
        help='Id of the account')
303
 
subparser.add_argument('--disable', action='store_true',
304
 
        help='Disable the account')
305
 
subparser.add_argument('--enable', action='store_true',
306
 
        help='Enable the account')
307
 
subparser.add_argument('--service', type=str,
308
 
        help='Operates on the given service')
309
 
subparser.add_argument('-s', action='append', default=[], metavar='SETTING',
310
 
        help='Add or changes a service setting, in the form [<type>:]<name>=<value>')
311
 
subparser.add_argument('-u', action='append', default=[],
312
 
        metavar='SETTING_NAME', help='Unset a setting')
313
 
subparser.add_argument('--signon-id-field', type=str, default='CredentialsId',
314
 
        help='Name of the key holding the SignOn ID')
315
 
subparser.add_argument('--caption', type=str,
316
 
        help='SignOn caption (username description)')
317
 
subparser.add_argument('--username', type=str,
318
 
        help='SignOn username')
319
 
subparser.add_argument('--password', type=str,
320
 
        help='SignOn password')
321
 
subparser.set_defaults(func=app.edit_account)
322
 
 
323
 
subparser = subparsers.add_parser('delete',
324
 
        help='Deletes an existing account')
325
 
subparser.add_argument('account', type=int,
326
 
        help='Id of the account')
327
 
subparser.set_defaults(func=app.delete_account)
328
 
 
329
 
subparser = subparsers.add_parser('signon_login',
330
 
        help='Authenticate the given identity')
331
 
subparser.add_argument('identity', type=int,
332
 
        help='Id of the SignOn identity')
333
 
subparser.add_argument('method', type=str,
334
 
        help='Authentication method')
335
 
subparser.add_argument('mechanism', type=str,
336
 
        help='Authentication mechanism')
337
 
subparser.add_argument('-p', action='append', default=[], metavar='PARAMETER',
338
 
        help='Session parameter, in the form [<type>:]<name>=<value>')
339
 
subparser.set_defaults(func=app.login_identity)
340
 
 
341
 
subparser = subparsers.add_parser('login',
342
 
        help='Authenticate the given identity')
343
 
subparser.add_argument('account', type=int,
344
 
        help='Id of the SignOn identity')
345
 
subparser.add_argument('--service', type=str,
346
 
        help='Account service')
347
 
subparser.add_argument('--signon-id-field', type=str, default='CredentialsId',
348
 
        help='Name of the key holding the SignOn ID')
349
 
mm = subparser.add_argument_group()
350
 
mm.add_argument('--method', type=str,
351
 
        help='Authentication method')
352
 
mm.add_argument('--mechanism', type=str,
353
 
        help='Authentication mechanism')
354
 
subparser.add_argument('-p', action='append', default=[], metavar='PARAMETER',
355
 
        help='Session parameter, in the form [<type>:]<name>=<value>')
356
 
subparser.set_defaults(func=app.login_account)
357
 
 
358
 
args = parser.parse_args()
359
 
 
360
 
main_loop = GLib.MainLoop()
361
 
GLib.idle_add(args.func, args)
362
 
 
363
 
main_loop.run()
364