summaryrefslogtreecommitdiff
path: root/test/dbus-server-config.py
blob: 9efbf88152da3782dcd275dc87b29709cbf60469 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#! /usr/bin/python

'''Get config/reports and set config.
Usage: dbus-server-sync <server> [--getconfig | --config | --reports] [config options] 
<server> - configuration name
'''

import dbus
from dbus.mainloop.glib import DBusGMainLoop
import gobject
import sys

DBusGMainLoop(set_as_default=True)

bus = dbus.SessionBus()

object = dbus.Interface(bus.get_object('org.syncevolution',
                                       '/org/syncevolution/Server'),
                        'org.syncevolution.Server')

loop = gobject.MainLoop()

sessionpath = None
def SessionChanged(object, ready):
    print "SessionChanged:", object, ready
    if sessionpath == object:
        loop.quit()

bus.add_signal_receiver(SessionChanged,
                        'SessionChanged',
                        'org.syncevolution.Server',
                        'org.syncevolution',
                        None,
                        byte_arrays=True)

dummysessionpath = object.StartSession("")
sessionpath = object.StartSession(sys.argv[1])

# detach from dummy session so that real session can run
session = dbus.Interface(bus.get_object('org.syncevolution',
                                        dummysessionpath),
                         'org.syncevolution.Session')
session.Detach()
session = dbus.Interface(bus.get_object('org.syncevolution',
                                        sessionpath),
                         'org.syncevolution.Session')
if sys.argv[2] == "--getconfig":
    print 'Get Config:'
    dict = session.GetConfig(0)
    for name, value in dict.items():
        print 'name: [', name, ']'
        for config, confvalue in value.items():
            print '\t', config, ' = ', confvalue 
elif sys.argv[2] == "--config":
    print 'Set Config:'
    i = 3;
    length = len(sys.argv)
    dict = {}
    update = 1
    temporary = 0
    while i < length:
        if sys.argv[i] == "--clear":
            update = 0
            i = i + 1
            continue
        elif sys.argv[i] == "--temp":
            temporary = 1
            i = i + 1
            continue
        elif sys.argv[i].startswith('-z'):
            l1 = sys.argv[i].split('=')
            if len(l1) == 2:
                name = 'source/' + l1[1]
        elif sys.argv[i].startswith('-y'):
            name = ''

        l2 = sys.argv[i+1].split('=')
        idict = {}
        if dict.has_key(name):
            idict = dict[name]
            idict[l2[0]] = l2[1]
        else:
            idict[l2[0]] = l2[1]
            dict[name] = idict
        i = i+2

    print dict
    session.SetConfig(update, temporary, dict)
    session.Sync("", {})
elif sys.argv[2] == "--reports":
    reports = session.GetReports(0, 10)
    i = len(reports)
    j = 0
    while j < i:
        print 'Report ', j, ':'
        dict = reports.pop()
        for key, value in dict.items():
            print '\t[',key,'] = ' ,value
        j = j+1
    print ''
elif sys.argv[2] == "--checksource":
    i = 3;
    length = len(sys.argv)
    print 'CheckSource:'
    while i < length:
        try:
            session.CheckSource(sys.argv[i])
        except dbus.exceptions.DBusException, x:
            print '\t[', sys.argv[i], ']: failed, ', x
            i = i + 1
            continue
        print '\t[', sys.argv[i], ']: ok'
        i = i + 1
elif sys.argv[2] == "--getdatabases":
    i = 3;
    length = len(sys.argv)
    print 'GetDatabases:'
    while i < length:
        r = session.GetDatabases(sys.argv[i])
        print '\t[', sys.argv[i],']'
        for item in r:
            print '\t\t','name =', item[0], ', uri = ', item[1], ', default =', item[2]
        i = i + 1
    print ''
elif sys.argv[2] == "--getconfigs":
    i = 3
    length = len(sys.argv)
    r = {}
    if length == 3:
        r = session.GetConfigs(1)
        print ' Available configuration templates:'
    else:
        r = session.GetConfigs(0)
        print ' Configured servers:'
    for item in r:
        print '\t', item