Mercurial > libervia-backend
annotate src/bridge/bridge_constructor/constructors/pb/pb_frontend_template.py @ 2454:06ff33052354
core, template (filters): added pygments as a dependency + new highlight filter to use it.
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 30 Nov 2017 21:00:24 +0100 |
parents | 8b37a62336c3 |
children | 0046283a285d |
rev | line source |
---|---|
2092
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/env python2 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 #-*- coding: utf-8 -*- |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 # SAT communication bridge |
2414
8b37a62336c3
misc: date update (yes it's a bit late :p )
Goffi <goffi@goffi.org>
parents:
2092
diff
changeset
|
5 # Copyright (C) 2009-2017 Jérôme Poisson (goffi@goffi.org) |
2092
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 # (at your option) any later version. |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 # GNU Affero General Public License for more details. |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 from sat.core.log import getLogger |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 log = getLogger(__name__) |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 from sat.core import exceptions |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 from twisted.spread import pb |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 from twisted.internet import reactor |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 class SignalsHandler(pb.Referenceable): |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 def __getattr__(self, name): |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 if name.startswith("remote_"): |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 log.debug(u"calling an unregistered signal: {name}".format( |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 name = name[7:])) |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 return lambda *args, **kwargs: None |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 else: |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 raise AttributeError(name) |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
38 def register_signal(self, name, handler, iface="core"): |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 log.debug("registering signal {name}".format(name=name)) |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 method_name = "remote_" + name |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 try: |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 self.__getattribute__(self, method_name) |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 except AttributeError: |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 pass |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 else: |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 raise exceptions.InternalError(u"{name} signal handler has been registered twice".format( |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 name = method_name)) |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 setattr(self, method_name, handler) |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 class Bridge(object): |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 def __init__(self): |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 self.signals_handler = SignalsHandler() |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 def __getattr__(self, name): |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 return lambda *args, **kwargs: self.call(name, args, kwargs) |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 def remoteCallback(self, result, callback): |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 """call callback with argument or None |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 if result is not None not argument is used, |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 else result is used as argument |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 @param result: remote call result |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 @param callback(callable): method to call on result |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 """ |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 if result is None: |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 callback() |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 else: |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 callback(result) |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
71 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
72 def call(self, name, args, kwargs): |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
73 """call a remote method |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
75 @param name(str): name of the bridge method |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
76 @param args(list): arguments |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
77 may contain callback and errback as last 2 items |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
78 @param kwargs(dict): keyword arguments |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
79 may contain callback and errback |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
80 """ |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
81 callback = errback = None |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
82 if kwargs: |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
83 try: |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
84 callback = kwargs.pop('callback') |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
85 except KeyError: |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
86 pass |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
87 try: |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
88 errback = kwargs.pop('errback') |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
89 except KeyError: |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
90 pass |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
91 elif len(args) >= 2 and callable(args[-1]) and callable(args[-2]): |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
92 errback = args.pop() |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
93 callback = args.pop() |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
94 d = self.root.callRemote(name, *args, **kwargs) |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
95 if callback is not None: |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
96 d.addCallback(self.remoteCallback, callback) |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
97 if errback is not None: |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
98 d.addErrback(errback) |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
99 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
100 def _initBridgeEb(self, failure): |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
101 log.error(u"Can't init bridge: {msg}".format(msg=failure)) |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
102 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
103 def _set_root(self, root): |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
104 """set remote root object |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
105 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
106 bridge will then be initialised |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
107 """ |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
108 self.root = root |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
109 d = root.callRemote("initBridge", self.signals_handler) |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
110 d.addErrback(self._initBridgeEb) |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
111 return d |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
112 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
113 def _generic_errback(self, failure): |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
114 log.error(u"bridge failure: {}".format(failure)) |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
115 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
116 def bridgeConnect(self, callback, errback): |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
117 factory = pb.PBClientFactory() |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
118 reactor.connectTCP("localhost", 8789, factory) |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
119 d = factory.getRootObject() |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
120 d.addCallback(self._set_root) |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
121 d.addCallback(lambda dummy: callback()) |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
122 d.addErrback(errback) |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
123 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
124 def register_signal(self, functionName, handler, iface="core"): |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
125 self.signals_handler.register_signal(functionName, handler, iface) |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
126 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
127 ##METHODS_PART## |