annotate libervia/pages/_browser/aio_bridge.py @ 1380:df79874947d7

pages: fix browser data re-creation on dev mode reload
author Goffi <goffi@goffi.org>
date Sat, 20 Feb 2021 13:51:11 +0100
parents 48e2a8b07c0b
children 72f9639594b2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1347
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
1 from browser import window, aio
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
2 import javascript
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
3
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
4
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
5 class BridgeException(Exception):
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
6 """An exception which has been raised from the backend and arrived to the frontend."""
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
7
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
8 def __init__(self, name, message="", condition=""):
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
9 """
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
10
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
11 @param name (str): full exception class name (with module)
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
12 @param message (str): error message
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
13 @param condition (str) : error condition
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
14 """
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
15 Exception.__init__(self)
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
16 self.fullname = str(name)
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
17 self.message = str(message)
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
18 self.condition = str(condition) if condition else ""
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
19 self.module, __, self.classname = str(self.fullname).rpartition(".")
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
20
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
21 def __str__(self):
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
22 message = (": %s" % self.message) if self.message else ""
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
23 return self.classname + message
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
24
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
25 def __eq__(self, other):
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
26 return self.classname == other
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
27
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
28
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
29 class Bridge:
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
30
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
31 def __getattr__(self, attr):
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
32 return lambda *args, **kwargs: self.call(attr, *args, **kwargs)
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
33
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
34 async def call(self, method_name, *args, **kwargs):
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
35 data = javascript.JSON.stringify({
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
36 "args": args,
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
37 "kwargs": kwargs,
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
38 })
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
39 url = f"/_bridge/{method_name}"
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
40 r = await aio.post(
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
41 url,
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
42 headers={
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
43 'X-Csrf-Token': window.csrf_token,
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
44 },
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
45 data=data,
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
46 )
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
47
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
48 if r.status == 200:
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
49 return javascript.JSON.parse(r.data)
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
50 elif r.status == 502:
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
51 ret = javascript.JSON.parse(r.data)
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
52 raise BridgeException(ret['fullname'], ret['message'], ret['condition'])
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
53 else:
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
54 print(f"bridge called failed: code: {r.status}, text: {r.statusText}")
48e2a8b07c0b browser: async version of bridge
Goffi <goffi@goffi.org>
parents:
diff changeset
55 raise BridgeException("InternalError", r.statusText)