Mercurial > libervia-backend
annotate src/bridge/bridge_constructor/dbus_core_template.py @ 273:3b5e856b3a32
Bridge: added mediawiki constructor in bridge constructor, usefull for documentation purpose
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 25 Jan 2011 01:37:02 +0100 |
parents | 1d2e0dfe7114 |
children | c25371424090 |
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)) | |
97 | |
98 def addSignal(self, name, int_suffix, signature): | |
99 """Dynamically add a signal to Dbus Bridge""" | |
100 #FIXME: Better way ??? | |
101 attributes = self.__attribute_string(signature) | |
102 | |
103 code = compile ('def '+name+' (self,'+attributes+'): debug ("'+name+' signal")', '<DBus bridge>','exec') | |
104 exec (code) | |
105 signal = locals()[name] | |
106 setattr(DbusObject, name, dbus.service.signal( | |
107 const_INT_PREFIX+int_suffix, signature=signature)(signal)) | |
108 | |
109 class DBusBridge(Bridge): | |
110 def __init__(self): | |
111 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) | |
112 Bridge.__init__(self) | |
113 info ("Init DBus...") | |
114 self.session_bus = dbus.SessionBus() | |
115 self.dbus_name = dbus.service.BusName(const_INT_PREFIX, self.session_bus) | |
116 self.dbus_bridge = DbusObject(self.session_bus, '/org/goffi/SAT/bridge') | |
117 | |
118 ##DIRECT_CALLS## | |
119 | |
120 def register(self, name, callback): | |
121 debug("registering DBus bridge method [%s]", name) | |
122 self.dbus_bridge.register(name, callback) | |
123 | |
124 def addMethod(self, name, int_suffix, in_sign, out_sign, method): | |
125 """Dynamically add a method to Dbus Bridge""" | |
126 print ("Adding method [%s] to DBus bridge" % name) | |
127 self.dbus_bridge.addMethod(name, int_suffix, in_sign, out_sign) | |
128 self.register(name, method) | |
129 | |
130 def addSignal(self, name, int_suffix, signature): | |
131 self.dbus_bridge.addSignal(name, int_suffix, signature) | |
132 setattr(DBusBridge, name, getattr(self.dbus_bridge, name)) | |
133 |