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