/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to mandos-ctl

  • Committer: Teddy Hogeborn
  • Date: 2016-06-28 18:46:40 UTC
  • Revision ID: teddy@recompile.se-20160628184640-22uh54op9nw0908b
mandos-ctl: Dump booleans as booleans in --dump-json output.

* mandos-ctl (main): Detect dbus.Boolean objects and recast them to
                     regular Python bool() objects to make them show
                     as boolean values in dump output.

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
import os
40
40
import collections
41
41
import doctest
 
42
import json
42
43
 
43
44
import dbus
44
45
 
72
73
server_path = "/"
73
74
server_interface = domain + ".Mandos"
74
75
client_interface = domain + ".Mandos.Client"
75
 
version = "1.7.7"
 
76
version = "1.7.10"
76
77
 
77
78
 
78
79
try:
280
281
                        help="Select all clients")
281
282
    parser.add_argument("-v", "--verbose", action="store_true",
282
283
                        help="Print all fields")
 
284
    parser.add_argument("-j", "--dump-json", action="store_true",
 
285
                        help="Dump client data in JSON format")
283
286
    parser.add_argument("-e", "--enable", action="store_true",
284
287
                        help="Enable client")
285
288
    parser.add_argument("-d", "--disable", action="store_true",
328
331
    if has_actions(options) and not (options.client or options.all):
329
332
        parser.error("Options require clients names or --all.")
330
333
    if options.verbose and has_actions(options):
331
 
        parser.error("--verbose can only be used alone or with"
332
 
                     " --all.")
 
334
        parser.error("--verbose can only be used alone.")
 
335
    if options.dump_json and (options.verbose or has_actions(options)):
 
336
        parser.error("--dump-json can only be used alone.")
333
337
    if options.all and not has_actions(options):
334
338
        parser.error("--all requires an action.")
335
 
 
 
339
    
336
340
    if options.check:
337
341
        fail_count, test_count = doctest.testmod()
338
342
        sys.exit(os.EX_OK if fail_count == 0 else 1)
389
393
                sys.exit(1)
390
394
    
391
395
    if not has_actions(options) and clients:
392
 
        if options.verbose:
 
396
        if options.verbose or options.dump_json:
393
397
            keywords = ("Name", "Enabled", "Timeout", "LastCheckedOK",
394
398
                        "Created", "Interval", "Host", "Fingerprint",
395
399
                        "CheckerRunning", "LastEnabled",
400
404
        else:
401
405
            keywords = defaultkeywords
402
406
        
403
 
        print_clients(clients.values(), keywords)
 
407
        if options.dump_json:
 
408
            json.dump({client["Name"]: {key:
 
409
                                        bool(client[key])
 
410
                                        if isinstance(client[key],
 
411
                                                      dbus.Boolean)
 
412
                                        else client[key]
 
413
                                        for key in keywords }
 
414
                       for client in clients.values() },
 
415
                      fp = sys.stdout, indent = 4,
 
416
                      separators = (',', ': '))
 
417
            print()
 
418
        else:
 
419
            print_clients(clients.values(), keywords)
404
420
    else:
405
421
        # Process each client in the list by all selected options
406
422
        for client in clients: