annotate src/bridge/bridge_constructor/dbus_core_template.py @ 321:1a990a88bff2

wix: force updating of splitterWindow in ChatWindow (needed when Tarot game is launched)
author Goffi <goffi@goffi.org>
date Sun, 08 May 2011 17:09:03 +0200
parents e044d1dc37d1
children 4402ac630712
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
2 #-*- coding: utf-8 -*-
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
3
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 """
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
5 SAT: a jabber client
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
6 Copyright (C) 2009, 2010, 2011 Jérôme Poisson (goffi@goffi.org)
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
7
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 This program is free software: you can redistribute it and/or modify
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 it under the terms of the GNU General Public License as published by
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 the Free Software Foundation, either version 3 of the License, or
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
11 (at your option) any later version.
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
12
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 This program is distributed in the hope that it will be useful,
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
16 GNU General Public License for more details.
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
17
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 """
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
21
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
22
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from bridge import Bridge
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 import dbus
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 import dbus.service
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 import dbus.mainloop.glib
272
1d2e0dfe7114 bridge: core & frontend sides of bridge are now generated
Goffi <goffi@goffi.org>
parents: 267
diff changeset
27 from logging import debug, info
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
28
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 const_INT_PREFIX = "org.goffi.SAT" #Interface prefix
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 const_COMM_SUFFIX = ".communication"
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 const_REQ_SUFFIX = ".request"
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
32
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 class DbusObject(dbus.service.Object):
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
34
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 def __init__(self, bus, path):
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 dbus.service.Object.__init__(self, bus, path)
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 debug("Init DbusObject...")
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 self.cb={}
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
39
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 def register(self, name, cb):
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 self.cb[name]=cb
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
42
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 ### signals ###
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
44
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 ##SIGNALS_PART##
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
46
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 ### methods ###
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
48
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 ##METHODS_PART##
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
50
299
e044d1dc37d1 dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents: 298
diff changeset
51 def __attributes(self, in_sign):
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 """Return arguments to user given a in_sign
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 @param in_sign: in_sign in the short form (using s,a,i,b etc)
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 @return: list of arguments that correspond to a in_sign (e.g.: "sss" return "arg1, arg2, arg3")"""
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 i=0
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 idx=0
299
e044d1dc37d1 dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents: 298
diff changeset
57 attr=[]
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 while i<len(in_sign):
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 if in_sign[i] not in ['b','y','n','i','x','q','u','t','d','s','a']:
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 raise ParseError("Unmanaged attribute type [%c]" % in_sign[i])
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
61
299
e044d1dc37d1 dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents: 298
diff changeset
62 attr.append("arg_%i" % idx)
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 idx+=1
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
64
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 if in_sign[i] == 'a':
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 i+=1
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 if in_sign[i]!='{' and in_sign[i]!='(': #FIXME: must manage tuples out of arrays
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 i+=1
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 continue #we have a simple type for the array
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 opening_car = in_sign[i]
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 assert(opening_car in ['{','('])
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 closing_car = '}' if opening_car == '{' else ')'
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 opening_count = 1
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 while (True): #we have a dict or a list of tuples
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 i+=1
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 if i>=len(in_sign):
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 raise ParseError("missing }")
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 if in_sign[i] == opening_car:
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 opening_count+=1
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 if in_sign[i] == closing_car:
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 opening_count-=1
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 if opening_count == 0:
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 break
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 i+=1
299
e044d1dc37d1 dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents: 298
diff changeset
85 return attr
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
86
299
e044d1dc37d1 dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents: 298
diff changeset
87 def addMethod(self, name, int_suffix, in_sign, out_sign, async=False, doc={}):
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 """Dynamically add a method to Dbus Bridge"""
299
e044d1dc37d1 dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents: 298
diff changeset
89 _attributes = self.__attributes(in_sign)
e044d1dc37d1 dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents: 298
diff changeset
90
e044d1dc37d1 dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents: 298
diff changeset
91 if async:
e044d1dc37d1 dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents: 298
diff changeset
92 _attributes.extend(['callback','errback'])
e044d1dc37d1 dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents: 298
diff changeset
93
e044d1dc37d1 dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents: 298
diff changeset
94 attributes = ', '.join(_attributes)
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
95
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 code = compile ('def '+name+' (self,'+attributes+'): return self.cb["'+name+'"]('+attributes+')', '<DBus bridge>','exec')
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 exec (code)
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
98 method = locals()[name]
299
e044d1dc37d1 dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents: 298
diff changeset
99 async_callbacks = ('callback', 'errback') if async else None
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
100 setattr(DbusObject, name, dbus.service.method(
299
e044d1dc37d1 dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents: 298
diff changeset
101 const_INT_PREFIX+int_suffix, in_signature=in_sign, out_signature=out_sign,
e044d1dc37d1 dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents: 298
diff changeset
102 async_callbacks=async_callbacks)(method))
284
c25371424090 dbus bridge: fixed introspection for dynamically added methods and signals
Goffi <goffi@goffi.org>
parents: 272
diff changeset
103 function = getattr(self, name)
c25371424090 dbus bridge: fixed introspection for dynamically added methods and signals
Goffi <goffi@goffi.org>
parents: 272
diff changeset
104 func_table = self._dbus_class_table[self.__class__.__module__ + '.' + self.__class__.__name__][function._dbus_interface]
c25371424090 dbus bridge: fixed introspection for dynamically added methods and signals
Goffi <goffi@goffi.org>
parents: 272
diff changeset
105 func_table[function.__name__] = function #Needed for introspection
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
106
298
15c8916317d0 dbus bridge: added doc parameter, unmanaged yet
Goffi <goffi@goffi.org>
parents: 284
diff changeset
107 def addSignal(self, name, int_suffix, signature, doc={}):
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 """Dynamically add a signal to Dbus Bridge"""
299
e044d1dc37d1 dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents: 298
diff changeset
109 attributes = ', '.join(self.__attributes(signature))
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
110
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 code = compile ('def '+name+' (self,'+attributes+'): debug ("'+name+' signal")', '<DBus bridge>','exec')
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
112 exec (code)
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
113 signal = locals()[name]
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
114 setattr(DbusObject, name, dbus.service.signal(
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
115 const_INT_PREFIX+int_suffix, signature=signature)(signal))
284
c25371424090 dbus bridge: fixed introspection for dynamically added methods and signals
Goffi <goffi@goffi.org>
parents: 272
diff changeset
116 function = getattr(self, name)
c25371424090 dbus bridge: fixed introspection for dynamically added methods and signals
Goffi <goffi@goffi.org>
parents: 272
diff changeset
117 func_table = self._dbus_class_table[self.__class__.__module__ + '.' + self.__class__.__name__][function._dbus_interface]
c25371424090 dbus bridge: fixed introspection for dynamically added methods and signals
Goffi <goffi@goffi.org>
parents: 272
diff changeset
118 func_table[function.__name__] = function #Needed for introspection
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
119
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
120 class DBusBridge(Bridge):
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
121 def __init__(self):
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
122 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 Bridge.__init__(self)
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
124 info ("Init DBus...")
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
125 self.session_bus = dbus.SessionBus()
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
126 self.dbus_name = dbus.service.BusName(const_INT_PREFIX, self.session_bus)
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
127 self.dbus_bridge = DbusObject(self.session_bus, '/org/goffi/SAT/bridge')
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
128
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
129 ##DIRECT_CALLS##
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
130
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
131 def register(self, name, callback):
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 debug("registering DBus bridge method [%s]", name)
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
133 self.dbus_bridge.register(name, callback)
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
134
299
e044d1dc37d1 dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents: 298
diff changeset
135 def addMethod(self, name, int_suffix, in_sign, out_sign, method, async=False, doc={}):
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
136 """Dynamically add a method to Dbus Bridge"""
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
137 print ("Adding method [%s] to DBus bridge" % name)
299
e044d1dc37d1 dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents: 298
diff changeset
138 self.dbus_bridge.addMethod(name, int_suffix, in_sign, out_sign, async, doc)
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
139 self.register(name, method)
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
140
298
15c8916317d0 dbus bridge: added doc parameter, unmanaged yet
Goffi <goffi@goffi.org>
parents: 284
diff changeset
141 def addSignal(self, name, int_suffix, signature, doc={}):
299
e044d1dc37d1 dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents: 298
diff changeset
142 self.dbus_bridge.addSignal(name, int_suffix, signature, doc)
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
143 setattr(DBusBridge, name, getattr(self.dbus_bridge, name))
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
144